[BUG] `npm install` strips `#<commit>` from git `resolved` URLs in `yarn.lock`, installing HEAD instead of the locked commit

Đang mở
#10,347 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 2 ngày

@kaygaaf đang làm issue này rồi.

Từ ngày 25/11/2026.

  • #10348 của @kaygaaf — đang mở

Đánh giá

Độ khó
3/5
Thời gian dự kiến
Nửa ngày
Mức phù hợp với người mới
18/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
git, javascript, nodejs
Lĩnh vực
cli, tooling

Hướng nghiên cứu

The stripping happens in YarnLock#parse in workspaces/arborist/lib/yarn-lock.js, where metadata[1].replace(/#.*/, '') removes the #<commit> fragment from git resolved URLs. Read YarnLock.fromTree alongside it, since that path writes the fragment back. Done means a git resolved keeps its commit hash through parse, and npm install --allow-git=root against the sample yarn.lock installs the locked commit with npm ls exiting 0. Linked PR #10348 is already open against this, so check it before starting.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Triage Requested
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 package-lock.json is absent and a Yarn v1 yarn.lock is present, npm install uses yarn.lock to seed the tree (Shrinkwrap#checkYarnLock), but strips #<commit> from git+... resolved entries.

For example, with "abbrev": "git+https://github.com/isaacs/abbrev-js.git#semver:^1.1.0" in package.json and a yarn.lock generated by [email protected] locking [email protected] at commit a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb:

  1. npm install --allow-git=root fails with EALLOWGIT because npm fetches default-branch HEAD ([email protected]), which makes the root edge invalid (edge.valid === false) and causes #extractOrLink to treat the root dependency as a non-root git dependency:
npm error code EALLOWGIT
npm error Fetching non-root packages of type "git" have been disabled
npm error Refusing to fetch "abbrev@git+https://github.com/isaacs/abbrev-js.git"
  1. npm install --allow-git=all (and default npm install on npm 11) ignores both the commit in yarn.lock and #semver:^1.1.0 in package.json, installs [email protected] from HEAD, writes "resolved": "git+https://github.com/isaacs/abbrev-js.git" without a commit hash to package-lock.json and yarn.lock, and leaves npm ls failing with ELSPROBLEMS:
npm warn skipping integrity check for git dependency https://github.com/isaacs/abbrev-js.git

added 1 package in 992ms
[email protected] /tmp/repro
└── [email protected] invalid: "git+https://github.com/isaacs/abbrev-js.git#semver:^1.1.0" from the root project (git+https://github.com/isaacs/abbrev-js.git)

npm error code ELSPROBLEMS
npm error invalid: [email protected] /tmp/repro/node_modules/abbrev

Removing yarn.lock and running npm install --allow-git=root installs [email protected] (git+https://github.com/isaacs/abbrev-js.git#a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb) cleanly. The same stripping also occurs when package.json pins an exact commit hash (git+https://github.com/isaacs/abbrev-js.git#a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb) alongside yarn.lock.

Root cause: in workspaces/arborist/lib/yarn-lock.js, YarnLock#parse runs metadata[1] = metadata[1].replace(/#.*/, '') on every resolved value to remove legacy tarball #<sha1> shasums, which also strips the #<commit> fragment from git+... URLs (even though YarnLock.fromTree emits resolved "git+...#<commit>").

Expected Behavior

YarnLock#parse preserves #<commit> fragments on git resolved URLs so npm install --allow-git=root installs the locked commit ([email protected] at a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb), records the commit hash in package-lock.json, and npm ls exits 0.

Steps To Reproduce
WORK=$(mktemp -d)
cd "$WORK"

cat > package.json << 'EOF'
{
  "name": "repro",
  "version": "1.0.0",
  "dependencies": {
    "abbrev": "git+https://github.com/isaacs/abbrev-js.git#semver:^1.1.0"
  }
}
EOF

# Generated by [email protected] for the package.json above
cat > yarn.lock << 'EOF'
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"abbrev@git+https://github.com/isaacs/abbrev-js.git#semver:^1.1.0":
  version "1.1.1"
  resolved "git+https://github.com/isaacs/abbrev-js.git#a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb"
EOF

# 1. Fails with EALLOWGIT on npm 12 under --allow-git=root
npm install --allow-git=root --no-audit --no-fund

# 2. Installs HEAD ([email protected]) instead of locked 1.1.1 under --allow-git=all
npm install --allow-git=all --no-audit --no-fund
npm ls
Environment
  • npm: 12.2.3 (bundles @npmcli/arborist 10.0.5; also reproduces on npm/cli latest at 787f1520fa and npm 11.21.3)
  • Node.js: v24.22.2
  • OS Name: Debian GNU/Linux 12 (bookworm), Linux 6.1 x86_64
  • npm config:
; defaults (empty .npmrc)

Credit: https://ucsbplse.github.io/

Ngôn ngữ chính
JavaScript
Star
10.3k
Fork
5k
Merge trung bình
4 ngày 11 giờ
Pull request đã merge (30 ngày)
26

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của npm/cli

Tất cả issue của npm/cli

Issue tương tự

Thêm issue về JavaScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.