fix(js): anchor rescued external imports to the package, not the path leaf (#3084)

(cherry picked from commit 794e0c391f)
This commit is contained in:
rohit-jsfreaky authored and safishamsi committed 2026-10-12 23:02:50 +01:00
1 parent ba8b4c863c
commit e90bd328d9
2 files changed
+59 -4

No files matched your search

+11 -4
View File
@@ -2450,12 +2450,14 @@ def _resolve_rescued_specifier(
resolved_unmapped = _resolve_js_module_path(raw, path.parent)
if resolved_unmapped is not None and resolved_unmapped.is_file():
return _make_id(str(resolved_unmapped)), str(resolved_unmapped), resolved_unmapped, False
# Bare/scoped import (node_modules) - use last segment;
# build_from_json drops as external if no matching node exists.
module_name = raw.split("/")[-1]
# Bare/scoped import (node_modules) - anchor to the PACKAGE, not the path
# leaf: `lodash/fp/map` is a dependency on `lodash`, and `@a/utils` and
# `@b/utils` are different packages that share a leaf.
parts = raw.split("/")
module_name = "/".join(parts[:2]) if raw.startswith("@") and len(parts) >= 2 else parts[0]
if not module_name:
return None
return _make_id(module_name), raw, None, True
return _make_id(module_name), module_name, None, True
def _emit_rescued_import(
@@ -2520,6 +2522,11 @@ def _emit_rescued_import(
# left on the dead id, so a declared dependency silently loses all of its
# inbound edges while an undeclared one keeps them (#3084).
stub["type"] = "module"
# For an external, `stub_source_file` is the package name (see
# _resolve_rescued_specifier) — and the node IS that package, so name it
# that way. `lodash/fp/map` and `lodash/debounce` are one `lodash` node,
# and neither subpath should end up titling it.
stub["label"] = stub_source_file
result.setdefault("nodes", []).append(stub)
result.setdefault("edges", []).append(edge)
existing_ids.add(node_id)
+48
View File
@@ -351,3 +351,51 @@ def test_unresolved_relative_import_is_not_marked_a_module(tmp_path, monkeypatch
assert node.get("type") != "module", (
"an unresolved relative import must not be exempted from disambiguation"
)
def test_scoped_packages_sharing_a_leaf_stay_distinct(tmp_path, monkeypatch):
"""`@a/utils` and `@b/utils` are different packages that share a path leaf.
The external stub is anchored to the package name, not the last segment.
Anchoring to the leaf gave both the id `utils`, and because a module anchor
is exempt from id-disambiguation they then merged into one node — so an
import of `@scope-a/utils` resolved to a node labelled `@scope-b/utils`.
"""
_write(tmp_path / "a.mts", "const x = (await import('@scope-a/utils')).default;\nexport const A = 1;\n")
_write(tmp_path / "b.mts", "const y = (await import('@scope-b/utils')).default;\nexport const B = 2;\n")
monkeypatch.chdir(tmp_path)
result = extract([Path("a.mts"), Path("b.mts")], cache_root=tmp_path / ".cache")
stubs = {n["id"]: n for n in result["nodes"] if n.get("type") == "module"}
assert len(stubs) == 2, f"the two packages must not share a node: {sorted(stubs)}"
assert {n["label"] for n in stubs.values()} == {"@scope-a/utils", "@scope-b/utils"}
node_ids = {n["id"] for n in result["nodes"]}
targets = {e["target"] for e in result["edges"] if e["relation"] == "dynamic_import"}
assert len(targets) == 2, f"each import must reach its own package: {targets}"
for t in targets:
assert t in node_ids
def test_subpath_imports_collapse_onto_their_package(tmp_path, monkeypatch):
"""`lodash/fp/map` is a dependency on `lodash`, not on `map`.
Every subpath of one package is one package node, and it is named for the
package — not for whichever specifier happened to be extracted first.
"""
_write(tmp_path / "a.mts", "const x = (await import('lodash/fp/map')).default;\nexport const A = 1;\n")
_write(tmp_path / "b.mts", "const y = (await import('lodash/debounce')).default;\nexport const B = 2;\n")
monkeypatch.chdir(tmp_path)
result = extract([Path("a.mts"), Path("b.mts")], cache_root=tmp_path / ".cache")
# One stub dict is emitted per importing file; they carry the same id and so
# collapse to a single node at build. What matters is that the id and the
# name are the package's, whichever subpath minted them.
stubs = [n for n in result["nodes"] if n.get("type") == "module"]
assert {n["id"] for n in stubs} == {"lodash"}, "both subpaths are the same package"
assert {n["label"] for n in stubs} == {"lodash"}, "the node is the package, not one of its subpaths"
targets = {e["target"] for e in result["edges"] if e["relation"] == "dynamic_import"}
assert targets == {"lodash"}