Skip to content

feat(npm): resolve a lockfile's paths from its own directory with lockfile_relative_paths - #420

Closed
ignaciofernandez-l5e wants to merge 1 commit into
mainfrom
ignaciofernandez-l5e/link-relative-to-lockfile
Closed

ignaciofernandez-l5e wants to merge 1 commit into
mainfrom
ignaciofernandez-l5e/link-relative-to-lockfile

Conversation

@ignaciofernandez-l5e

@ignaciofernandez-l5e ignaciofernandez-l5e commented Dec 2, 2026 •

Copy link
Copy Markdown

Why: We want Bazel to build the mobile app, so we can remove an old GitHub Actions workflow.

The mobile app has its own list of dependencies, kept in the mobile/ folder. One of those dependencies is a package from another folder in our repo.

pnpm writes down where that package lives starting from the mobile/ folder. rules_typescript reads that location starting from the top of the repo, so it looks in the wrong place. It can't find the package, skips it, and doesn't warn anyone. Without that package, the mobile app can't build in Bazel.

This PR adds an option that tells rules_typescript to read the location starting from the lockfile's folder, the same way pnpm wrote it. The option is off by default, so nothing changes for anyone who doesn't turn it on.

What changes:

pnpm writes importer keys and link: targets against the lockfile's
directory. Read against the workspace root, a link out of a nested
lockfile climbed out of the root and was dropped with no error.

The default is unchanged, so tests/npm's root-relative fixture reads as
before.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22ccf-aaba-7197-af23-2ede7ec39d69
Co-authored-by: Amp [email protected]

…kfile_relative_paths

pnpm writes importer keys and link: targets against the lockfile's
directory. Read against the workspace root, a link out of a nested
lockfile climbed out of the root and was dropped with no error.

The default is unchanged, so tests/npm's root-relative fixture reads as
before.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22ccf-aaba-7197-af23-2ede7ec39d69
Co-authored-by: Amp <[email protected]>

@claude claude Bot 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.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Comment on lines +651 to +652
state["link_base"] = _resolve_link_path(lock_dir, importer)
state["current_importer"] = importer if importer == "." else state["link_base"]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 (optional) An importer key that resolves to the workspace root or climbs out of it is silently renamed to "". Its links then resolve against the root, and nothing reports the error. _resolve_link_path returns "" for both of those cases, and line 652 uses that "" as the importer key and link_base. Fix: treat a "" result for any importer other than "." as an error, using fail() with an actionable message. Do the same for a link: target that escapes in _record_importer_dep (:579-583), rather than dropping it. Same pattern at 2 sites (npm_translate_lock.bzl:651-652, npm_translate_lock.bzl:579-583).

Why this was flagged

Trigger: with lockfile_relative_paths = True and lock_dir "apps/mobile", pnpm writes the importer key "../.." for a workspace whose repo-root package.json is a member, or "../../../ext" for a package outside the repo. _resolve_link_path (npm_translate_lock.bzl:526-546) returns "" both for the root and for an escape, so line 652 creates importer key "" and every escaping importer merges into it. Its links then resolve from the workspace root: link:lib inside "../../../ext" becomes "lib", which names the wrong package. In lazy.bzl:838-840, key "" becomes a member candidate at path "". _member_manifest (lazy.bzl:254) then reads the root package.json and declares the root as a member. Default mode is affected too: a raw key like "../x" used to stay "../x" with its links dropped, and now also collapses to "". No fail() fires, which goes against AGENTS.md's "silence is worse than an error" rule.

Verification: _resolve_link_path (npm_translate_lock.bzl:536-546) returns "" when a ".." escapes and when the path lands exactly on the root. Line 652 sets state["current_importer"] = importer if importer == "." else state["link_base"]. So any escaping importer gets the key "". No error or warning is raised anywhere on this path.

Finding ID: f-1b9e7d42

Comment on lines +463 to +492
def _lockfile_relative_paths_resolve_from_the_lockfile_test(ctx):
env = unittest.begin(ctx)

# pnpm writes a lockfile in apps/mobile against apps/mobile: read as
# workspace-root paths, ../../packages/shared climbs out and is dropped.
lockfile = """lockfileVersion: '9.0'

importers:

.:
dependencies:
shared:
specifier: link:../../packages/shared
version: link:../../packages/shared

tools:
dependencies:
sibling:
specifier: link:../../../packages/sibling
version: link:../../../packages/sibling

packages: {}
"""
importers = parse_importers(lockfile, "apps/mobile")
asserts.equals(env, {"shared": "packages/shared", "sibling": "packages/sibling"}, importers["links"])
asserts.equals(env, [".", "apps/mobile/tools"], sorted(importers["importers"].keys()))
asserts.equals(env, {"sibling": "packages/sibling"}, importers["importers"]["apps/mobile/tools"]["links"])
asserts.equals(env, {}, parse_importers(lockfile)["links"])

return unittest.end(env)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 (optional) No test covers the new lockfile_relative_paths attribute through the module extension, so a broken wiring would ship unnoticed. The only test calls parse_importers directly. Nothing tests the conditional at npm/lazy.bzl:618 or the member lookup from workspace_root. AGENTS.md says not to skip integration tests when adding features. Fix: add a fixture with a nested lockfile (for example under tests/npm) that sets lockfile_relative_paths = True and asserts the hub resolves //packages/shared:shared.

Why this was flagged

The feature works only if three pieces agree: the extensions.bzl attribute (line 66), the pnpm_lock.package if lockfile_relative_paths else "" argument (npm/lazy.bzl:618), and _member_manifest(module_ctx, workspace_root, path) (lazy.bzl:846) reading the rewritten keys. The test added in this PR (parser_tests.bzl:463-492) checks only the parser's output dict. No translate_lock in MODULE.bazel or tests/integration sets the attribute, so the extension path never runs in CI. Suppose the attribute is dropped from the call at extensions.bzl:66, or the package is passed in the wrong form. Users would get the old behaviour of silently dropped links, and the suite would stay green. AGENTS.md (Testing; Anti-Patterns: "Don't skip the integration tests when adding new features") calls for an end-to-end test of new features.

Verification: nit. The test gap is real. The diff's only test (tests/npm/parser_tests.bzl:463-494) calls parse_importers(lockfile, "apps/mobile") directly. No MODULE.bazel translate_lock and no tests/integration workspace sets lockfile_relative_paths, so the extension path through lazy.bzl:618 never runs in CI. Graded nit because the wiring looks correct today.

Finding ID: f-36708319

state["current_importer"] = importer.strip().strip("'\"")
importer = importer.strip().strip("'\"")
state["link_base"] = _resolve_link_path(lock_dir, importer)
state["current_importer"] = importer if importer == "." else state["link_base"]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 (optional) With lockfile_relative_paths = True, a member that links the lockfile's own root project gets a store entry with none of that project's dependencies. The root importer keeps the key "." here. A link: to it now resolves to its real path (e.g. "apps/mobile"), and npm/lazy.bzl:539 looks deps up by that path. The lookup misses and falls back to {"deps": {}, "links": {}}. Fix: when lock_dir is set, give "." one identity. Either map a member path equal to lock_dir onto the "." entry wherever importers are read by path, or reject such a link with an actionable fail(). Code that matches on "." (lazy.bzl:766, hoist.bzl:146, tsgo_lock.bzl:123) must keep working.

Why this was flagged

Trigger: the lockfile sits in apps/mobile with lockfile_relative_paths = True, and importer tools declares the root project as version: link:... At npm_translate_lock.bzl:651-652, link_base for tools is "apps/mobile/tools", so _record_importer_dep resolves link:.. to "apps/mobile". The root importer's own entry is still stored under ".". In npm/lazy.bzl:825-840, "apps/mobile" becomes a member candidate. _store_graph at lazy.bzl:539 then calls importers["importers"].get("apps/mobile", {"deps": {}, "links": {}}), which returns the empty default. The npm_store_member for apps/mobile is declared with no deps or links, and nothing reports the problem. Consumers in tools get a linked package whose node_modules is missing every dependency it declared. On the base branch a link can never resolve to the same directory as ".": _resolve_link_path returns "" for the root, so this key split cannot happen. No check compares member paths against lock_dir.

Verification: npm_translate_lock.bzl:651-652 keeps the root importer's entry under "." while every other importer is keyed by its workspace-root path. For tools, link_base is "apps/mobile/tools". lazy.bzl:539 importers["importers"].get(path, {"deps": {}, "links": {}}) looks up "apps/mobile", which does not exist because the entry is under ".".

Finding ID: f-fa41a165

Comment on lines +579 to 582
path = _resolve_link_path(link_base, version[len("link:"):])
if path:
result["links"][dep_name] = path
entry["links"][dep_name] = path

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟣 pre-existing, not blocking: A link: that climbs out of the workspace is still dropped with no error, so a nested lockfile without the new attribute keeps losing its links. When _resolve_link_path returns "", the if path: branch at line 580 just skips the link. This is pre-existing, but it is the exact failure this PR describes, and the opt-in default leaves it silent. Fix: fail() when a link: target escapes the workspace root, naming the importer and target. When lock_dir is "", add a "Did you mean lockfile_relative_paths = True?" hint. A target that resolves to the root itself can still be skipped.
A small fix can ride a push you are already making; otherwise a short reply is enough.

Why this was flagged

Take a user with npm.translate_lock(pnpm_lock = "//apps/mobile:pnpm-lock.yaml") whose lockfile pnpm generated in apps/mobile, and who does not know about the new attribute (it defaults to False, extensions.bzl:106). lazy.bzl:618 passes "" as lock_dir. link:../../packages/shared from importer "." then resolves against "" and climbs out, so _resolve_link_path returns "". _record_importer_dep at npm_translate_lock.bzl:579-582 records nothing and raises no error. The member view for shared is missing from the hub, and the only clue is a missing target at build time. The same silent drop also hits a lockfile_relative_paths lockfile whose link really escapes the workspace. AGENTS.md:422 says "silence is worse than both", and every fail() needs an actionable "Did you mean...?" message. The PR fixes only the case where the user already knows to opt in.

Verification: _resolve_link_path (npm_translate_lock.bzl:533-542) returns "" when a link escapes the root. _record_importer_dep (lines 578-583) then reaches if path:, records nothing and just returns, with no fail() or warning. In default mode, lazy.bzl:618 passes "" as lock_dir, so the link is dropped silently, exactly as on the base branch.

Finding ID: f-31ce0787

@ignaciofernandez-l5e

Copy link
Copy Markdown
Author

Closing: lovable is moving mobile into its root pnpm workspace, where agent-sdk is a workspace:* link the fork already reads correctly. A separate small PR can make an unresolvable link: fail loudly instead of being dropped.

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.

1 participant