Skip to content

Report the real package name for aliased package-lock.json entries - #184

Open
abhinavgautam01 wants to merge 1 commit into
git-pkgs:mainfrom
abhinavgautam01:npm-lock-alias-names
Open

abhinavgautam01 wants to merge 1 commit into
git-pkgs:mainfrom
abhinavgautam01:npm-lock-alias-names

Conversation

@abhinavgautam01

Copy link
Copy Markdown
Contributor

Fixes #177.

Summary

An npm alias such as "my-lodash": "npm:lodash@^4.17.21" installs lodash at node_modules/my-lodash. The lockfile parser named every entry after its install path, so the alias came out as my-lodash with pkg:npm/my-lodash@4.17.21, a different package on the registry.

v2 / v3 lockfiles

npm records the real package in the entry's name field whenever it differs from the path. packageLockEntry now reads name, in both the line scanner and the JSON decoder, and uses it for Name when present:

before: name="my-lodash" version="4.17.21" purl="pkg:npm/my-lodash@4.17.21"
after:  name="lodash"    version="4.17.21" purl="pkg:npm/lodash@4.17.21"

Root dependencies are keyed by the alias, so Direct is still decided by the install name. Without that, aliased dependencies would turn indirect.

v1 lockfiles

v1 keeps the alias in the version string ("version": "npm:lodash@4.17.21"), which was reported as a version of my-lodash. It now goes through the existing parseNpmAlias, giving lodash at 4.17.21, the same as package.json parsing.

Line scanner

The scanner starts a fresh entry at every : { line, so a dependency literally named name inside an entry's nested dependencies cannot rename the entry. The new fixture covers this.

Tests

  • New fixture testdata/npm/npm-lockfile-alias/package-lock.json in npm's formatting: a plain alias, a scoped dev alias, a nested alias, a nested dependency named name and an unaliased package. It is checked for name, version, scope and directness, and added to TestPackageLockPathsAgree so the line scanner and the decoder must agree on it.
  • TestPackageLockAliasCompactJSON uses the exact lockfile from the issue.
  • TestPackageLockV1Alias covers plain and scoped v1 aliases.
  • npm_lock_alias_test.go checks pkg:npm/lodash@4.17.21 through Parse for package-lock.json and npm-shrinkwrap.json.
  • TestNpmPackageLockV3 expected the install name alias-package-name even though its fixture records "name": "@some-scope/actual-package". It now expects the real name.

go build ./..., go vet ./..., go test ./... and golangci-lint run ./... all pass.

Notes

  • yarn.lock reports the same alias under its install name (TestYarnLock expects alias-package-name). That is a separate parser, so I left it for a follow-up.
  • Preserve npm workspace links as path sources #150 also edits toDependency, so whichever lands second will need a small rebase.

@coderabbitai

coderabbitai Bot commented Dec 4, 2026

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b421935b-7556-44dd-b42a-6e66bb501ead
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

package-lock.json alias entries are reported under the alias name

1 participant