webpro-nl/knip

๐Ÿ› Impossible to Ignore Env Var False Positives in Yarn Scripts

joealden opened this issue ยท 2 comments

Prerequisites

Reproduction url

https://stackblitz.com/edit/github-1l9dwy?file=knip.json

Reproduction access

  • I've made sure the reproduction is publicly accessible

Description of the issue

I know this is a slightly odd way of using scripts, but to be able to easily determine what script to run in our CI pipeline, we have generic scripts that use an environment variable to pick the specialised script to run - for example:

{
  "scripts": {
    "run-script:test": "ls",
    "run-script:prod": "ls",
    "run-script": "yarn run-script:$TEST_ENV_VAR"
  }
}

I understand that knip won't be able to pick up on this easily, but I think Knip should either:

  1. Automatically ignore "binaries" that contain an environment variable ($...)
  2. Allow me to ignore the "binary" via ignoreBinaries.

Knip output:

Unlisted binaries (1)
run-script:$TEST_ENV_VAR  package.json
Configuration issues (1)
Unused item in ignoreBinaries: /run-script:$TEST_ENV_VAR/

I thought I might be able to work around this by using yarn run instead of just yarn (to make it clear that it's a script not a binary - like how npm run doesn't trigger this), but it looks like this doesn't work. So maybe another partial fix for this would be to mirror the npm run logic to yarn run, and maybe yarn (although the environment variable solution would ensure real missing binaries aren't ignored)?

The $ should be escaped, like so:

{
  "ignoreBinaries": ["run-script:\\$TEST_ENV_VAR"]
}

or

export default {
  ignoreBinaries: [/run-script:\$TEST_ENV_VAR/],
};

I looked into ignoring script keys that match /\$[A-Z]/ but it's not great because $ is a valid character.

Ah makes sense - thanks!