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

Knip doesn't parse arguments of yarn run --top-level correctly #739

Open
Jym77 opened this issue Jul 23, 2024 · 2 comments
Open

Knip doesn't parse arguments of yarn run --top-level correctly #739

Jym77 opened this issue Jul 23, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@Jym77
Copy link

Jym77 commented Jul 23, 2024

Reproduction url

Sandbox

Description of the issue

In a monorepo setting, a workspace script invoking a top-level script which takes positional parameters gets these parameters incorrectly interpreted as scripts (and reported) by knip


Top-level package.json:

"scripts": {
    "echo": "echo"
  },

Here, the echo script just calls the echo shell command, which takes extra parameters on the command line.

Workspace (packages/shared) package.json:

"scripts": {
    "foo": "yarn run --top-level echo hello"
  }

This uses the --top-level flag which seems to not exist in yarn 1. It simply calls the top-level script, providing a parameter to it.

When running (from the toplevel) yarn workspace @monorepo/shared run foo, or (from the workspace directory) yarn foo, I get the correct behaviour (display hello in the terminal).

When running yarn knip, I get:

Unlisted binaries (2)
...
hello  packages/shared/package.json

So, it seems that knip is interpreting the command as running echo and hello scripts.
Adding a hello script inside the package does remove the reporting. Adding one at toplevel doesn't.

This may be related to #735 but I believe it is different.


I was not able to reproduce this in a basic (single workspace) setting; Without the --top-level option (and using an echo script in the workspace), hello is not reported, so the option may have something to do with it.

Somehow, removing the echo script from the top-level is not reported. Maybe knip interpretes yarn run --top-level echo hello as being an option --top-level echo and calling script hello (instead of option --top-level and calling script echo with parameter hello).

@Jym77 Jym77 added the bug Something isn't working label Jul 23, 2024
@Jym77 Jym77 changed the title Knip interprets positional parameters as script names when yarn run --top-level is used Knip doesn't parse arguments of yarn run --top-level correctly Jul 23, 2024
@webpro
Copy link
Collaborator

webpro commented Jul 23, 2024

Knip does not understand the --top-level argument. Unknown arguments are treated as string arguments, so e.g. --top-level echo is basically ignored. Knip does not actually use Yarn for argument interpretation. So indeed your thinking seems correct to me.

This may be related to #735 but I believe it is different.

Why do you think it's different?

@Jym77
Copy link
Author

Jym77 commented Jul 23, 2024

This may be related to #735 but I believe it is different.

Why do you think it's different?

#735 (and the linked #433) seemed related to Github action on first read.
Looking more, #735 may have something to do with parsing CLI arguments, but there knip correctly detects the script name, and incorrectly doesn't find it (as it looks in the wrong directory). Here, knip incorrectly detects the script name, and ("correctly") reports it as missing (that script indeed does not exist).

But indeed, they may both boil down to the fact that --foo bar is effectively discarded while in both cases (--cwd, --top-level) it actually has a meaning that should impact knip behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants