Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI Argument Parsing is Broken with --single-thread #3107

Closed
6 tasks done
alexlafroscia opened this issue Mar 30, 2023 · 10 comments · Fixed by #3111
Closed
6 tasks done

CLI Argument Parsing is Broken with --single-thread #3107

alexlafroscia opened this issue Mar 30, 2023 · 10 comments · Fixed by #3111
Labels
help wanted Extra attention is needed

Comments

@alexlafroscia
Copy link

Describe the bug

When providing the --single-thread argument to the Vitest CLI, the presence of other arguments can mess with the way that the command line arguments are parsed.

This works fine

vitest --inspect-brk --single-thread

but this gives me a complaint that --single-thread is necessary to use --inspect-brk, even though it is present already

vitest --inspect-brk --single-thread SomeTestModuleNameToFilterOn

CleanShot 2023-03-30 at 11 40 58@2x

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-brxbu1?file=package.json&initialPath=__vitest__

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 7.17.0 - /usr/local/bin/npm
  npmPackages:
    @vitest/ui: latest => 0.29.8 
    vite: latest => 4.2.1 
    vitest: latest => 0.29.8

Used Package Manager

npm

Validations

@alexlafroscia
Copy link
Author

alexlafroscia commented Mar 30, 2023

Interestingly, this doesn't seem to have the same error

vitest --single-thread --inspect-brk SomeTestModuleNameToFilterOn

It allows the tests to run with the inspector enabled, but the file-path filtering is totally ignored; rather than filtering on that file name, it ignores the filters and just starts running all of the tests.

@sheremet-va
Copy link
Member

I don't think it's possible to pass it down like that. cac reads it as a value for the --argument.

@AriPerkkio
Copy link
Member

The difference of these two comes from here:

if (resolved.inspect || resolved.inspectBrk) {
if (resolved.threads !== false && resolved.singleThread !== true) {
const inspectOption = `--inspect${resolved.inspectBrk ? '-brk' : ''}`
throw new Error(`You cannot use ${inspectOption} without "threads: false" or "singleThread: true"`)
}
}

We are explicitly looking for resolved.singleThread !== true, when singleThread is actually a filename. When comparing resolved.inspect it is coerced to Boolean.

As the described workflow of this issue is expected to be common I think we should explicitly convert these flags to Boolean in resolved configuration. It does not require much code changes.

@sheremet-va
Copy link
Member

As the described workflow of this issue is expected to be common I think we should explicitly convert these flags to Boolean in resolved configuration. It does not require much code changes.

Yes, but it will not work as described in the issue. The issue wants the same as:

vitest SomeTestModuleNameToFilterOn --single-thread --inspect

But if we just coerce it will be

vitest --single-thread --inspect

If I understand it correctly.

@AriPerkkio
Copy link
Member

I think this issue about these two commands:

  • vitest --single-thread --inspect-brk <file>
  • vitest --inspect-brk --single-thread <file>

The second one is not working currently. #3111 fixes it.

@alexlafroscia
Copy link
Author

alexlafroscia commented Apr 11, 2023

I'm not sure that #3111 addressed this issue in its entirety. Or rather, while it resolved the original issue around parsing the arguments, other issues around using these flags remain.

It seems that now, additional filters after the arguments to launch Vitest with the debugger enabled are just ignored entirely

If I run the following, just my one tests/App.test.ts file is executed

yarn vitest App.test

If I do the same, but with the debugging commands provided, it does launch in debugging mode, but starts running all of my tests

yarn vitest --inspect-brk --single-thread App.test

I would expect that the behavior here is that it launches just tests/App.test.ts but with the ability to attach a debugger.

@AriPerkkio
Copy link
Member

AriPerkkio commented Apr 12, 2023

I'm able to reproduce this. It seems that kebab-case vs camelCase produces different results. I wonder if this is a bug in cac.

# Runs only 'math.test.ts' with single thread
$ vitest run --singleThread tests/math.test.ts

# Runs all tests in single thread
$ vitest run --single-thread tests/math.test.ts 

@AriPerkkio AriPerkkio reopened this Apr 12, 2023
@AriPerkkio AriPerkkio added bug help wanted Extra attention is needed labels Apr 12, 2023
@AriPerkkio
Copy link
Member

import { cac } from "cac";

const cli = cac("repro");
cli.option("--single-thread", "Testing");

console.log(JSON.stringify(cli.parse(), null, 2));
$ node cac-repro.mjs --single-thread tests/file.test.ts
{
  "args": [],
  "options": {
    "--": [],
    "singleThread": "tests/file.test.ts"
  }
}

$ node cac-repro.mjs --singleThread tests/file.test.ts
{
  "args": [
    "tests/file.test.ts"
  ],
  "options": {
    "--": [],
    "singleThread": true
  }
}

@vitkarpov
Copy link

vitkarpov commented Jun 18, 2023

Maybe add "1" or "y" after "single-thread"? Otherwise, cac thinks the path is the option value rather than the argument.

$ node cac-repro.mjs --single-thread 1 tests/file.test.ts
{
  "args": [
    "tests/file.test.ts"
  ],
  "options": {
    "--": [],
    "singleThread": 1
  }
}

UPD Switching option and argument would also work, i.e. node cac-repro.mjs tests/file.test.ts --single-thread

@sheremet-va
Copy link
Member

This issue should be fixed in 1.3.0.

@sheremet-va sheremet-va removed the bug label Feb 16, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Mar 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants