Skip to content

fix(javascript): resolve ESM package self references - #1884

Closed
xu91102 wants to merge 1 commit into
morluto:mainfrom
xu91102:fix/esm-package-self-references
Closed

xu91102 wants to merge 1 commit into
morluto:mainfrom
xu91102:fix/esm-package-self-references

Conversation

@xu91102

@xu91102 xu91102 commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

ESM package self-references were marked external even when the package's exact exported files were inventoried. Resolve them through the nearest package's name and exports before dependency lookup, using the import loader's conditions. Fixes #1881.

Problem and expected behavior

import { answer } from "rea-self-fixture/feature" loads successfully in Node, but previously produced resolved_path: null and resolution_status: "external". It now resolves to the exported internal file and retains its binding relationship and source provenance.

Change and scope

Reuse the existing self-reference scope walk and exports selector for both named loaders. Rename the former CommonJS-only helper to describe its shared responsibility. Keep the existing nearest-package, container-identity, blocked-export, and exact-target rules.

Contract and boundary impact

  • Semantic owner and earliest changed stage: application-layer artifact module resolution.
  • CLI and MCP/tool-catalog contract: unchanged schemas; both surfaces now report the correct local import target.
  • Provider, bridge, target-format, or platform compatibility: static JavaScript package analysis; no engine or bridge changes.
  • Evidence, artifact, provenance, or reconstruction contract: retain caller specifiers, source ranges, source digests, and container identity.
  • Process execution, authorization, cleanup, or containment impact: none in production; resolution operates on the existing inventory.
  • Generated metadata, package, or installation impact: none.

Evidence and regression coverage

  • New compiled CLI and stdio MCP acceptance coverage checks package-root/subpath self imports, different import/require targets, and original source digest/location.

  • A real Node subprocess independently loads the same source-owned fixture and prints 11 22.

  • On base fb94d9dde0ccda83ae520ac6fb22b1d283562cf3, both new surface tests failed because the expected internal imports were external. Both pass after the fix.

  • Existing CommonJS self-reference, nearest-package-scope, foreign-container, blocked/missing exports, and package-subpath tests pass.

  • Review: self-review of the fixed base, complete patch, and new acceptance test; no actionable findings.

  • Remaining proof gaps: full deterministic coverage is left to upstream CI; real native engines and browser runtime are outside this static resolution change.

  • Observed, derived, and inferred claims remain distinguishable.

  • Artifact identity, source provenance, and failed attempts remain preserved.

  • Unsupported, incomplete, unavailable, or uncertain outcomes remain visible.

Validation performed

All local checks used Node 24.18.0 / npm 11.16.0 on macOS 27.0.1 arm64.

  • npm run test:focused -- tests/acceptance/applications/packageSelfReferencesCli.test.ts before the production fix — 2 expected regression failures.
  • npm run test:focused -- tests/acceptance/applications/packageSelfReferencesCli.test.ts tests/acceptance/applications/packageSubpathsCli.test.ts tests/boundary/javascript/packageConditionalExports.test.ts src/application/javascript/JavaScriptArtifactPathResolution.test.ts — 152 tests passed across 4 files, including freshly built CLI/MCP workflows.
  • npm run check — passed typecheck, lint, formatting, knip, metadata, module boundaries, and architecture guards; 758 lint warnings, zero errors.
  • npm run verify:test-discovery — all 843 test files discovered exactly once.
  • git diff --check — passed.
  • Upstream CI — awaiting maintainer approval for this fork contribution (action_required); no CI jobs have executed. This is not counted as a passing check.

Compatibility, safety, and release

  • Breaking changes or migration steps: none.
  • Real Hopper/Ghidra, browser, or OS coverage: actual Node resolver and REA CLI/MCP on macOS; no native-provider or browser-runtime claim.
  • Package or release metadata impact: none.
  • Security, privacy, process, or containment review: same-container checks and artifact-root confinement remain enforced; no new process authority or external fetches.

Review checklist

  • This PR addresses a concrete reproduced problem and has one focused outcome.
  • The title follows Conventional Commits and the related issue is linked.
  • Tests cover observable behavior and existing refusal/scope paths.
  • No owning docs, schemas, or generated metadata require changes.
  • Representative CLI/MCP output and actual validation are recorded above.
  • The final diff contains no secrets, unrelated cleanup, or unsupported provider claims.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-11T15:51:49.140695Z 14a1f22 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@xu91102
xu91102 force-pushed the fix/esm-package-self-references branch from 14a1f22 to ef91773 Compare October 11, 2026 15:51

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 14a1f22f34

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

"Percent signs are not admitted in ES module package names.",
]);
const self = commonJsSelfCandidate(input, declared);
const self = packageSelfCandidate(input, declared);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve suffixes while matching ESM self-export keys

When an ESM self import contains a query or fragment (for example, import "pkg/feature?raw"), declared has already been passed through stripQueryAndFragment in contextualCandidate before this call. Node includes that suffix in the package subpath, so an exports map such as { "./feature": "./plain.js", "./feature?raw": "./raw.js" } loads raw.js; this path instead resolves plain.js (or reports the import unmatched if only the suffixed key exists), producing a wrong graph edge. Keep the original specifier for self-export key selection and strip only the selected target URL afterward.

AGENTS.md reference: AGENTS.md:L11-L11

Useful? React with 👍 / 👎.

declared: string,
): CandidateResolution | null => {
if (input.moduleKind !== "require" || !input.files.has(input.sourcePath))
if (input.moduleKind === undefined || !input.files.has(input.sourcePath))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Require an exact parsed package-name match for ESM self references

When the nearest package has a slash-bearing noncanonical name that merely prefixes the import (for example, metadata name: "@scope" and an import of @scope/pkg), admitting import here reuses the CommonJS prefix test below and incorrectly selects local exports["./pkg"]. The ESM resolver parses the requested package name as @scope/pkg and only self-resolves on exact equality, so with an inventoried node_modules/@scope/pkg dependency REA now records the wrong local target. Preserve the prefix behavior for require, but compare metadata.name with the parsed barePackageName(declared) for imports.

AGENTS.md reference: AGENTS.md:L21-L21

Useful? React with 👍 / 👎.

@morluto

morluto commented Oct 11, 2026

Copy link
Copy Markdown
Owner

Superseded by #1939, which retains your implementation commit, integrates it with #1888 and #1887 on current main, and adds the missing suffixed ESM self-reference interaction checked against real Node. The combined CLI/MCP and boundary checks passed; #1881 remains linked for closure when the integration merges.

@morluto morluto closed this Oct 11, 2026
@morluto morluto added bug Something isn't working area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior labels Oct 14, 2026
@morluto

morluto commented Oct 15, 2026

Copy link
Copy Markdown
Owner

Thanks @xu91102 for surfacing ESM package self-references that should resolve through the package’s own exports map. The current resolver handles that package scope and keeps the selected export conditions in the lookup (main).

@morluto morluto added the area: build Build, CI, packaging, and publishing label Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: build Build, CI, packaging, and publishing area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] ESM package self-references are reported as external dependencies

2 participants