Skip to content

fix(arborist): preserve symlink root coordinates - #10294

Open
jeffwidman wants to merge 1 commit into
npm:latestfrom
jeffwidman:jeffwidman-fix-symlink-prefix-coordinates
Open

jeffwidman wants to merge 1 commit into
npm:latestfrom
jeffwidman:jeffwidman-fix-symlink-prefix-coordinates

Conversation

@jeffwidman

@jeffwidman jeffwidman commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Summary

Fix Arborist lockfile handling when the project root is a filesystem symlink.

For example:

/shortcuts/project -> /repo/project

Arborist keeps /shortcuts/project as the configured path, while the project files and installed dependencies use /repo/project. Shrinkwrap previously mixed these paths, so the same node could be saved or looked up under different lockfile locations.

Why add an abstraction?

The first fixes translated paths at individual call sites. Further testing showed that serialization, virtual loading, validation, hidden lockfiles, and Yarn integration were all making the same path decision independently.

Continuing to patch each case would be fragile. The underlying rule is simple:

Lockfiles use portable paths relative to the configured project root. Filesystem operations use canonical paths.

This PR puts that rule in one immutable PathCoordinates object shared by every consumer. It converts between lockfile locations, filesystem paths, and local file specifications.

Shrinkwrap owns the coordinate context and updates it when the live root target changes. Non-symlink projects use the same object with identical configured and canonical roots, so their behavior does not change.

Bugs fixed

These issues are present on the base branch.

  1. Non-portable package-lock locations

    • Committing a tree loaded through a symlinked root could write canonical directory traversal into package-lock.json.
    • Instead of portable entries such as node_modules/foo, the lockfile could contain machine-specific locations such as ../actual/node_modules/foo.
    • Package entries now remain relative to the configured project root.
  2. Incorrect root metadata

    • Shrinkwrap could track the root Link instead of its live target.
    • Root metadata could therefore come from the link rather than the project package.
    • When the canonical directory name differed from the package name, legacy lockfile generation could incorrectly write the root version as an npm: alias.
    • Shrinkwrap now tracks the live target while preserving logical lockfile locations.
  3. Unstable local tarball resolutions

    • A portable dependency such as file:tarballs/dep.tgz could be serialized using a path derived from the canonical project location.
    • This made the lockfile depend on the machine’s symlink layout.
    • Local tarball resolutions now remain stable and relative to the configured project root.
  4. Incorrect linked-project locations

    • Links and their targets could be written relative to the canonical root.
    • This produced incorrect locations for entries such as node_modules/workspace and packages/workspace.
    • Both the link and its target now use portable lockfile coordinates.
  5. Inconsistent metadata operations

    • The same node could map to different metadata depending on whether a caller used a relative lockfile location, an absolute configured path, or an absolute canonical path.
    • This affected get(), add(), and delete().
    • Metadata added using a canonical path might not be returned by a relative lookup, and deleting by canonical path might leave the lockfile entry intact.
    • All supported path forms now resolve to the same metadata location.
  6. Stale root targets

    • During tree transplantation, metadata can be attached while the root Link temporarily has no target.
    • When the target was restored or replaced, Shrinkwrap could continue tracking the detached or stale node.
    • Shrinkwrap now updates its metadata tree and canonical root whenever the live target changes.

Scope

This PR does not change how Arborist reconciles ideal and virtual inventory identities.

A dependency-free npm ci invocation through a symlinked prefix still exposes a separate mismatch between ideal and virtual synthetic-root inventory keys. That behavior reproduces on the base commit and occurs after lockfile coordinate hydration.

Testing

Regression coverage verifies:

  • portable package-lock keys
  • root metadata from the live target
  • stable local tarball resolutions
  • correct link and target locations
  • equivalent relative, configured-absolute, and canonical-absolute lookups
  • consistent get(), add(), and delete() behavior
  • targetless root restoration and target replacement
  • virtual loading and save/reload round trips
  • external dependencies
  • hidden lockfiles
  • Yarn metadata
  • local directory and tarball validation

Validation:

  • npm test --ignore-scripts --workspace @npmcli/arborist
  • npm run lint --ignore-scripts --workspace @npmcli/arborist
  • Arborist CI, CodeQL, and pull-request checks

@jeffwidman
jeffwidman requested a review from a team as a code owner November 13, 2026 06:29
@jeffwidman
jeffwidman force-pushed the jeffwidman-fix-symlink-prefix-coordinates branch 4 times, most recently from d634284 to 810ec4b Compare November 13, 2026 07:32

@ychampion ychampion left a comment

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.

Verified 810ec4b on Linux/Node 24.22.3: the complete 59-file Arborist suite passes with 100% coverage, plus ESLint and the template check. Actual CLI installs through a sibling symlink now write node_modules/foo rather than ../../real/app/node_modules/foo; registry, alias, local-tarball and internal/external file: dependencies remain loadable after installation.

One remaining round-trip case may be useful for a follow-up test. Even a dependency-free project installed through the symlink still fails npm ci through that same path:

CLI=/path/to/this/checkout/bin/npm-cli.js
R=$(mktemp -d)
mkdir "$R/real"
printf '%s\n' '{"name":"consumer","version":"1.0.0","private":true}' > "$R/real/package.json"
ln -s "$R/real" "$R/app"
node "$CLI" install --prefix "$R/app" --offline --ignore-scripts --no-audit --no-fund
node "$CLI" ci --prefix "$R/app" --offline --ignore-scripts --no-audit --no-fund
# EUSAGE: Missing: app@1.0.0 from lock file

I checked this against both the parent d538936 and this head: both fail the symlinked ci case, while both direct-path install/ci controls pass. The head correctly saves the root's name/version, so this is a remaining end-to-end limitation rather than a regression introduced by this change. The serialization correction itself is reproduced and verified; I have not tested the independent fixtures on Windows.

@jeffwidman

jeffwidman commented Nov 13, 2026 •

Copy link
Copy Markdown
Member Author

Yeah, that’s an interesting finding, but I think it’s outside the scope of this PR because addressing it requires a broader decision about how Arborist compares or normalizes the ideal and virtual tree inventories. This PR is a localized fix to how Shrinkwrap serializes and looks up lockfile paths for symlinked roots. The npm ci failure also reproduces on the base commit and happens later, when the ideal and virtual trees represent the synthetic root link under different location keys.

@ychampion ychampion left a comment

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.

Agreed on scope. The dependency-free ci case fails on both d538936 and 810ec4b, so it does not block this serialization fix. The full Arborist suite and the independent install cases I reported pass at this unchanged head.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Symlink-root path handling leaves linked dependencies missing and tarball edges invalid after reload, while target replacement can cause stale metadata to be serialized.

3 open issues
What changed in this PR

This PR updates Arborist’s Shrinkwrap handling for symlinked project roots, keeping lockfile locations relative to the configured logical path while using the canonical target for metadata.

Changes:

  • Centralizes path translation across metadata lookups, mutations, and serialization.
  • Adjusts root metadata, linked targets, local tarball resolutions, and legacy version handling.
  • Adds symlink-root regression coverage and updates lockfile snapshots.
File Description
workspaces/​arborist/​test/​shrinkwrap.js Adds filesystem-symlink and targetless-root metadata regressions.
workspaces/​arborist/​tap-snapshots/​test/​shrinkwrap.js.test.cjs Updates expected package keys and legacy root versions.
workspaces/​arborist/​lib/​shrinkwrap.js Adds coordinate translation and canonical-target metadata handling.

Review effort: Max

Comment thread workspaces/arborist/lib/shrinkwrap.js Resolved
Comment thread workspaces/arborist/lib/shrinkwrap.js Resolved
Comment thread workspaces/arborist/lib/shrinkwrap.js Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved symlink-root path inconsistencies can corrupt hydrated dependency graphs, invalidate local dependencies, and produce incorrect or unusable lockfiles.

4 open issues
2 previously missed issues

In code that hasn't changed since last review

Medium severity Resolve root backlinks to the canonical target

workspaces/​arborist/​lib/​arborist/​load-virtual.js:188

For a symlinked root with a file:. dependency, nodes.get('') is the root Link, not its canonical target. Passing that Link into #loadLink() gives the dependency the logical root path and corrupts the hydrated graph. Pass target.target (ordinary nodes return themselves) and cover this root-backlink reload case.

Medium severity Translate symlink targets when validating hidden lockfiles

workspaces/​arborist/​lib/​shrinkwrap.js:1022

For a symlinked root with an absolute dependency-link target, the new hidden-lockfile key is packages/workspace, but assertNoNewer() checks the target as ../actual/packages/workspace. A fresh .package-lock.json is therefore rejected and loadActual() falls back to a full filesystem walk. Translate symlink targets during validation and add a hidden-lockfile save/reload regression covering relative and absolute targets.

3 resolved since last review

Review effort: Max

Comment thread workspaces/arborist/lib/shrinkwrap.js Resolved
Comment thread workspaces/arborist/lib/shrinkwrap.js Resolved
Comment thread workspaces/arborist/lib/shrinkwrap.js Resolved
@jeffwidman

Copy link
Copy Markdown
Member Author

a3cfe1f also resolves root backlinks against the canonical target and translates symlink targets during hidden-lockfile validation, with regressions for both relative and absolute link targets.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

File and link paths still resolve against logical rather than canonical roots in symlinked projects, selecting incorrect tarballs and link targets and causing dependency validation failures and rejection of freshly saved hidden lockfiles.

5 open issues
1 previously missed issue

In code that hasn't changed since last review

Medium severity Translate directory request paths before link validation

workspaces/​arborist/​lib/​arborist/​load-virtual.js:268

With /shortcuts/project -> /repo/project, an absolute directory request such as file:/shortcuts/project/packages/ws keeps its logical fetchSpec, while this loader attaches a link targeting /repo/project/packages/ws. linkValid() compares those paths directly, so the reloaded dependency is invalid. Normalize directory requests with the root-coordinate translator in dep-valid.js:154, as tarball validation now does.

4 resolved since last review

Review effort: Max

Comment thread workspaces/arborist/lib/dep-valid.js Resolved
Comment thread workspaces/arborist/lib/shrinkwrap.js Resolved
Comment thread workspaces/arborist/lib/shrinkwrap.js Resolved
@jeffwidman

Copy link
Copy Markdown
Member Author

The summary-only directory validation finding is also fixed in 65758f0: absolute logical directory requests and canonical link targets now normalize through the same coordinate context before comparison. 36f9fbc restores the suite’s required 100% coverage, and CI is green.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment thread workspaces/arborist/lib/dep-valid.js Resolved
Comment thread workspaces/arborist/lib/shrinkwrap.js Resolved
Comment thread workspaces/arborist/lib/shrinkwrap.js Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Critical defects remain: symlink-root migration can produce invalid tarball paths, while incomplete external-node adoption can lose dependency edges and drop lockfile entries.

2 open issues
3 resolved since last review

Review effort: Max

Comment on lines +815 to +818
meta.resolved = this.#coordinates.toLockfileSpec(
spec.fetchSpec,
this.#coordinates.canonicalRoot
)
Keep lockfile metadata in configured-root coordinates while resolving filesystem operations from the canonical root. Preserve virtual tree shape and external dependency topology across symlink roots.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 9ec52121e7f84203af4415cf8082eae1
@jeffwidman
jeffwidman force-pushed the jeffwidman-fix-symlink-prefix-coordinates branch from 9e871b5 to e586636 Compare November 25, 2026 10:47
@jeffwidman
jeffwidman requested a max review from Copilot November 25, 2026 18:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved path-canonicalization and pending-update defects can invalidate valid links, prevent lockfile integrity restoration, and resurrect removed dependencies.

6 open issues
1 resolved since last review

Review effort: Max

return treeCheck(this.virtualTree)
}

const rootRealpath = await realpath(this.path, this[realpathCache], this[statCache])
const requestedPath = root
? coordinates.toFilesystem(requested.fetchSpec)
: requested.fetchSpec
return isLink && relative(child.realpath, requestedPath) === ''
Comment on lines +682 to 684
setRoot (root) {
this.#coordinates = new PathCoordinates(this.path, root.realpath)
}
Comment on lines +893 to +894
const resolvedOk = !resolved || !metadataNode.resolved ||
metadataNode.resolved === pathFixed
})
const actual = resolve(path, 'repo/project')
const logical = resolve(path, 'shortcuts/project')
fs.symlinkSync(actual, logical)
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.

3 participants