Repository navigation
fix(nodeenv): read npm's parseable listing in freeze - #414
Merged
Merged
Conversation
freeze scraped the drawn `npm ls` tree, and every filter in that pipeline
lost packages. `grep -E '^.{4}\w{1}'` dropped each scoped package, because
`@scope/name` puts an `@` in the column the filter wants a word character
in, and the version regexp had no `/` to keep the scope with either.
`grep -v npm` was meant to skip npm itself and also ate npmlog,
npm-check-updates and every other name containing "npm". What it did keep
was corepack, which node.js installs on its own: feeding such a file back
runs `npm install -g [email protected]`, which exits 0 and silently downgrades
the bundled copy.
Read `npm ls --depth=0 --parseable --long` instead. It prints
`path:name@version[:flags]`, one package per line, so the scope survives and
the tree root is the only line without `/node_modules/` in its path, in
global and in local mode alike. npm and corepack are excluded by exact name
now.
The fish version printed nothing at all under npm >= 1: its `set -l
NPM_LIST` sits inside the `else` block, which fish scopes it to, and it
greped `$re` where the variable is `$RE`. Its npm 0.x branch sent the list
to /dev/null and passed `^/dev/null` to npm as a literal argument, fish 3.0
having turned on stderr-nocaret, and `echo` joined the packages with spaces
where a requirements file needs newlines.
tests/test_activate_shells.py gets a stub npm and checks freeze, `freeze -l`
and `freeze FILE` in every shell it runs.
Closes #287
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #287.
Problem
freezescraped the drawnnpm lstree:Every filter in that pipeline loses packages. On node 24.21.0 / npm 11.19.0:
npm ls -gfreeze@sindresorhus/[email protected][email protected][email protected][email protected][email protected][email protected][email protected]├── @scope/[email protected]has an@in the columngrep -E '^.{4}\w{1}'wants a word character in, so the line never reaches the second grep. The version regexp has no/either, so even a surviving line would yield[email protected]without the scope.npmvanishes.grep -v npmwas meant to skip npm itself and also eatsnpmlog,npm-check-updates,npm-run-all.corepackis kept. node.js installs it on its own. Feeding the frozen file back runsnpm install -g [email protected], which exits 0 and silently downgrades the bundled copy:corepack --versionthen reports0.9.0on a node 24 environment.The result is a requirements file that describes a different environment than the one it was taken from, with no warning either way.
The fish
freezeprints nothing at all under npm >= 1:set -l NPM_LISTis declared inside theelseblock and fish scopes it there, so theechoafterendsees an empty variable. The same function greps"$re"where the variable is$RE, its npm 0.x branch runsnpm list installed active >/dev/null ^/dev/null- the list goes to/dev/nulland, since fish 3.0 turned onstderr-nocaret,^/dev/nullreaches npm as a literal argument - andecho $NPM_LISTjoins a fish list with spaces where a requirements file needs newlines.Fix
Read
npm ls --depth=0 --parseable --longinstead. It printspath:name@version[:flags], one package per line:The scope survives, and the tree root is the only line without
/node_modules/in its path, in global and in local mode alike, so onesedboth drops it and extracts the name.npmandcorepackare excluded by exact name,^(npm|corepack)@, which anchors whatgrep -v npmonly approximated.:EXTRANEOUSand the other trailing flags are stripped; the package itself is installed and stays.The npm 0.x branch of the posix script is untouched -
npm list installed activepredates--parseable --long. In the fish script that branch is corrected to2>/dev/null, since as written the function cannot work at all.npm ls --jsonwould be more robust still, but parsing it means spawning node from inside a shell function.Verification
tests/test_activate_shells.pyalready sources the generated scripts in sh, dash, bash, zsh and fish. It gets a stubnpmnext to the existing stubnode- it answers-vand returns a fixed--parseable --longlisting - and three tests forfreeze,freeze -landfreeze FILE, parametrised over every shell. No download and no network. They fail in all five shells before the change; zsh staysxfailon the knownsourceguard (#398).End to end on a real environment, the same reproduction as above:
freeze->nodeenv -p -r node-requirements.txt->freezenow returns the identical list, and corepack stays at the version node.js shipped. The fish script produces the same output.pytest testsgreen (352 passed, 20 xfailed),flake8 --extend-ignore=E127 nodeenv.py tests setup.pyclean.Documentation
README gains a note that packages bundled with node.js are left out, and the answer to the question the issue actually asks -
freezesaves the packages,.node-versionpins the runtime.[email protected]is dropped from the sample requirements file, sincefreezewill not write such a line any more.