Skip to content

buildNpmPackage: add support for npm-shrinkwrap.json#443832

Merged
doronbehar merged 1 commit intoNixOS:stagingfrom
aduh95:npm-shrinkwrap.json
Sep 26, 2025
Merged

buildNpmPackage: add support for npm-shrinkwrap.json#443832
doronbehar merged 1 commit intoNixOS:stagingfrom
aduh95:npm-shrinkwrap.json

Conversation

@aduh95
Copy link
Copy Markdown
Contributor

@aduh95 aduh95 commented Sep 17, 2025

According to https://docs.npmjs.com/cli/v11/configuring-npm/npm-shrinkwrap-json, it's that file that is used in priority over package-lock.json.

Things done

  • Built on platform:
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • Tested, as applicable:
  • Ran nixpkgs-review on this PR. See nixpkgs-review usage.
  • Tested basic functionality of all binary files, usually in ./result/bin/.
  • Nixpkgs Release Notes
    • Package update: when the change is major or breaking.
  • NixOS Release Notes
    • Module addition: when adding a new NixOS module.
    • Module update: when the change is significant.
  • Fits CONTRIBUTING.md, pkgs/README.md, maintainers/README.md and other READMEs.

Add a 👍 reaction to pull requests you find important.

@nixpkgs-ci nixpkgs-ci Bot added 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 5001+ This PR causes many rebuilds on Darwin and must target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. 6.topic: nodejs Node.js is a free, open-source, cross-platform JavaScript runtime environment labels Sep 17, 2025
Copy link
Copy Markdown
Contributor

@sarahec sarahec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aduh95 aduh95 force-pushed the npm-shrinkwrap.json branch from e322274 to 8463f6b Compare September 17, 2025 23:34
@aduh95 aduh95 changed the base branch from master to staging September 17, 2025 23:34
@nixpkgs-ci nixpkgs-ci Bot closed this Sep 17, 2025
@nixpkgs-ci nixpkgs-ci Bot reopened this Sep 17, 2025
@sarahec sarahec self-requested a review September 18, 2025 01:55
Copy link
Copy Markdown
Member

@raboof raboof left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel qualified to judge whether this change makes sense globally for npm-config-hook.sh, but I can confirm that it seems to work for x2t.

@sarahec
Copy link
Copy Markdown
Contributor

sarahec commented Sep 18, 2025

@winterqt I'll defer to your judgement.

@aduh95
Copy link
Copy Markdown
Contributor Author

aduh95 commented Sep 22, 2025

In case someone needs convincing, you can confirm using the following script that npm does not read the package-lock.json when a npm-shrinkwrap.json is available:

rm -rf repro
(
    set -e
    mkdir repro
    cd repro
    echo '{}' > package.json
    echo 'invalid json' > package-lock.json
    ! npm ci
    echo '{}' > npm-shrinkwrap.json
    npm ci

    echo
    echo "=========="
    echo
    for file in *; do
        echo "=== Contents of $file ==="
        cat "$file"
        echo
    done
)
rm -rf repro

There is also this test case in npm repo: https://github.com/npm/cli/blob/d3896147c61b06d6d39a55bbb609f878548e0107/workspaces/arborist/test/shrinkwrap.js#L1612-L1620

Copy link
Copy Markdown
Contributor

@doronbehar doronbehar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach would feel a bit safer to reviewers, and I won't mind merge it and be responsible for it.

Comment thread pkgs/build-support/node/build-npm-package/hooks/npm-config-hook.sh Outdated
Comment thread pkgs/build-support/node/build-npm-package/hooks/npm-config-hook.sh Outdated
@aduh95 aduh95 force-pushed the npm-shrinkwrap.json branch from ce6fe1a to 3ef36b4 Compare September 22, 2025 15:50
@doronbehar doronbehar merged commit 8367c12 into NixOS:staging Sep 26, 2025
29 of 31 checks passed
@aduh95 aduh95 deleted the npm-shrinkwrap.json branch September 26, 2025 14:15
@thunze
Copy link
Copy Markdown
Member

thunze commented Nov 9, 2025

I'm not sure this made sense as a global change. From what I can see, this broke most packages it touched because fetchNpmDeps also fails if package-lock.json doesn't exist. (When testing, make sure to also rebuild <package>.npmDeps as it's still cached in most cases.)

Hydra already caught one instance of this issue: https://hydra.nixos.org/build/312774851/nixlog/5

And I'm also not sure it's great that we now have to sync the (fairly rare) npm-shrinkwrap.json edge case across all npm tooling in nixpkgs.

I see at least two possible courses of action here to fix the breakages:

  1. Revert this change and maybe document how to manually deal with npm-shrinkwrap.json.
  2. Keep it, handle the edge case wherever it needs to be handled (maybe other fetchers, package managers, etc. should handle this, too?) and probably also document where necessary that npm-shrinkwrap.json is handled implicity.

I don't know enough about the npm ecosystem in nixpkgs to take care of all of 2., so I'd like to hear your opinion on this first. :)

Relevant to ZHF: #457852

@aduh95
Copy link
Copy Markdown
Contributor Author

aduh95 commented Nov 9, 2025

I've opened #460084 to address this.

@doronbehar
Copy link
Copy Markdown
Contributor

Damn I'm surprised we missed it, and that more then a month later we got notified this is buggy.


postPatch = ''
ln -s npm-shrinkwrap.json package-lock.json
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this PR was completely buggy, I wonder how come balena-cli hasn't became broken. @thunze Do you have an idea?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nix-build -A balena-cli.npmDeps --check fails for me.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see. We see this failure only now when this PR has reached master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: nodejs Node.js is a free, open-source, cross-platform JavaScript runtime environment 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 5001+ This PR causes many rebuilds on Darwin and must target the staging branches. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants