Settings

Theme

Major Node.js Pitfall: Globstar paths not matched by shell

gist.github.com

1 points by khalidx 2 years ago · 3 comments

Reader

khalidxOP 2 years ago

I'm reporting this here for awareness. Please check your CI pipelines for any issues.

TLDR:

Fixing the issue with globstar paths (`**/*`) not working when running an npm script:

`npm` uses `sh` by default on Linux when running a script with `npm run`. The thing is, if you are using globstar paths in your script, like `node --test src/**/*.test.js`, paths that are more than one-directory-level deep won't be expanded!

This is not an issue with Node.js or npm per say, but with how `sh` works by default. The thing is, this isn't the behavior that most people _expecting_ when setting up their npm scripts.

The link in this submission includes a description of this "pitfall", a resolution for Linux, and some other notes.

I strongly recommend checking your important repositories and CI pipelines for any issues, like tests getting skipped or assets not getting bundled (since their globstar paths are not being picked up by `sh`). Basically just check for any `package.json` that has `**/*` in it. You may also want to check that all your other scripts are being run with bash (with the `shopt globstar` setting set to `on` -- or just use `zsh`).

Sorry for the drama -- but I just _KNOW_ that this is being overlooked all over the place.

  • RadixDLT 2 years ago

    just use yarn

    • khalidxOP 2 years ago

      TLDR: Issue with yarn also.

      I just tested with yarn, and the same issue exists unless the yarn or npm config option for `script-shell` is set to zsh (like the solution described in the link).

      With `script-shell` set to zsh: globstar paths work with subdirectories. Without `script-shell` set (the default on linux): only top-level paths work.

      The command I ran was `yarn test` with this test script in the scripts section of package.json: `node --test src/**/*.test.ts`.

      I don't want to turn this into a GitHub issue thread, but also didn't want to be dismissive. I hope that helps.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection