Repository navigation
Description
Is there an existing issue for this?
- I have searched the existing issues
This issue exists in the latest npm version
- I am using the latest npm
This is not just a request to bump a dependency for a CVE
- This is not solely a request to bump a dependency for a CVE
Current Behavior
When two git dependencies on the same repository pin different full commit SHAs, npm installs one copy and uses it for both, even though the pinned commits contain different versions. npm install succeeds and npm ls does not mark the tree invalid.
In the reproduction, the root depends on core at commit A (core@1.0.0) and b depends on core at commit B (core@2.0.0). b gets the root's core@1.0.0:
app@ /private/tmp/work/app
├─┬ b@1.0.0 (git+file:///tmp/work/b#90820d10931f087d620a8807646b33b8dbfd838b)
│ └── core@1.0.0 deduped
└── core@1.0.0 (git+file:///tmp/work/core#32abc94467ac5f33574461b2288b24d8136b408c)
This only happens for repositories that are not recognized as hosted. In dep-valid.js, the git case compares the committish only on the hosted branch (reqHost?.ssh(nc) !== resHost.ssh(nc)). For other repositories it compares resRepo.fetchSpec !== requested.fetchSpec, and fetchSpec excludes the committish, so any commit of the same URL satisfies the edge. The comment above it says "if it specifies a definite commit, then it must have the same commit to be considered the same repo".
Calling depValid directly with a child resolved at commit A and a request pinned to commit B:
git+https://github.com/o/r.git#Avsgithub:o/r#B:false(correct)git://127.0.0.1:1/repo.git#Avsgit://127.0.0.1:1/repo.git#B:truegit+https://git.example.com/o/r.git#Avsgit+https://git.example.com/o/r.git#B:true
This affects self-hosted git servers (for example a self-managed GitLab on a custom domain) and git+file: URLs.
Expected Behavior
b gets its own core@2.0.0 (commit B), as it does for hosted repositories, or the tree is reported invalid.
Steps To Reproduce
set -e
rm -rf work && mkdir work && cd work
ROOT=$PWD
# core@1.0.0 at commit A, core@2.0.0 at commit B
git init -q core && cd core
git config user.email a@b && git config user.name a
echo '{"name":"core","version":"1.0.0"}' > package.json
git add . && git commit -qm v1 && A=$(git rev-parse HEAD)
echo '{"name":"core","version":"2.0.0"}' > package.json
git commit -qam v2 && B=$(git rev-parse HEAD)
cd "$ROOT"
# b pins commit B; the root pins commit A
git init -q b && cd b
git config user.email a@b && git config user.name a
echo "{\"name\":\"b\",\"version\":\"1.0.0\",\"dependencies\":{\"core\":\"git+file://$ROOT/core#$B\"}}" > package.json
git add . && git commit -qm b && BC=$(git rev-parse HEAD)
cd "$ROOT"
mkdir app && cd app
echo "{\"name\":\"app\",\"private\":true,\"dependencies\":{\"core\":\"git+file://$ROOT/core#$A\",\"b\":\"git+file://$ROOT/b#$BC\"}}" > package.json
npm install --allow-git=all --no-audit --no-fund
npm ls --all
node -p 'require("module").createRequire(require.resolve("b/package.json"))("core/package.json").version'The last command prints 1.0.0: core as resolved from b is the root's copy.
Environment
- npm: 12.2.2 (also reproduced with 11.21.2)
- Node.js: 26.13.0
- OS Name: macOS
- System Model Name: Mac
- npm config:
; default config
Activity
docs: record monorepo publishing decisions in engineering notes (#82)