[PR #3844] [MERGED] fix(reference): select a package main before its directory index #5941

Closed
opened 2026-10-31 01:10:37 +00:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/morluto/rea/pull/3844
Author: @kaoru0822-kitauji
Created: 10/15/2026
Status: ✅ Merged
Merged: 10/15/2026
Merged by: @kaoru0822-kitauji

Base: main ← Head: fix/reference-package-main


📝 Commits (2)

  • d77f035 fix(reference): select a package main before its directory index
  • 22ee27a fix(reference): resolve package main the way path.resolve does

📊 Changes

2 files changed (+131 additions, -13 deletions)

View changed files

📝 src/application/ReferenceSourceImportEntries.ts (+103 -12)
📝 tests/boundary/cli/referenceSourceDirectoryImports.test.ts (+28 -1)

📄 Description

Summary

Fixes #3383. import-reference-source resolved require("./dep") to dep/index.js even when dep/package.json declares "main": "actual.js". Node loads dep/actual.js. The historical graph reported the wrong file as an internal, parsed dependency, with no parse failure or limitation.

Now a directory import selects the package's main before its index, as Node does. A manifest whose selection can't be established (unreadable or invalid JSON) leaves the relationship unresolved instead of claiming the index.

Problem and expected behavior

resolveInternalSpecifier resolved specifiers from inventory path candidates only, and no manifest was consulted. Node's LOAD_AS_DIRECTORY reads X/package.json. When main is a non-empty string, Node loads it as a file, then as a directory index, and falls back to X/index.* only when neither exists (the DEP0128 legacy path).

Change and scope

  • The reference-source entry owner, src/application/ReferenceSourceImportEntries.ts, changes as follows:
    • parseReferenceSourceEntries parses each inventoried package.json once from bytes already read (readPackageMains). Each manifest is classified as declared, none (no main, an empty main, or a non-string main) or unknown (unreadable, invalid UTF-8, or invalid JSON).
    • selectPackageMain runs only when the existing resolver chose a directory index (<normalized>/index.*). It loads the declared main with the same suffixes as other candidates, then as a directory index. When the main's target is absent, it keeps the index, matching Node's fallback.
    • A main that escapes the imported root becomes unresolved.
  • File-before-directory precedence, the extensionless-index rule from #3789, and explicit ./dep/index.js imports are unchanged. The lookup and manifests travel together in one SpecifierContext instead of separate parameters.
  • Not included: package exports, imports and types. The issue concerns the legacy main, and historical-source import does not model export maps.

Contract and boundary impact

  • Semantic owner and earliest changed stage: historical-source internal specifier resolution.
  • Contribution scope fit and linked maintainer decision: correctness fix within existing read-only import; not applicable.
  • CLI and MCP/tool-catalog contract: schemas unchanged. Some relationships now name the declared main, or become unresolved when the manifest is invalid.
  • Provider, bridge, target-format, or platform compatibility: unchanged.
  • Evidence, artifact, provenance, or reconstruction contract: wrong established dependencies become correct or explicitly unresolved.
  • Target selection, authority, upstream behavior, and cleanup details: no new reads; the manifests are already inventoried bytes.
  • Generated metadata, package, or installation impact: none.

Expected effects:

  • Read existing files or captured data.

Evidence and regression coverage

  • Tests updated: tests/boundary/cli/referenceSourceDirectoryImports.test.ts, the built-CLI test that uses Node's createRequire().resolve as the oracle, adds three cases:

    • a declared main (./pkg → pkg/actual.js)
    • a missing main falling back to the index (./stale → stale/index.js)
    • an invalid manifest, where Node throws and rea reports broken as unresolved

    All existing directory cases still pass.

  • Base reproduction: against the unfixed module, the test fails on package-main.cjs and invalid-manifest.cjs.

  • User-visible output on the issue's fixture, after:

    node=dep/actual.js
    rea: [{"from_path":"main.cjs","to":"dep/actual.js","kind":"requires","resolution":"internal","parse_state":"parsed"}]
    

    Before: "to":"dep/index.js".

  • More fixtures compared with Node's resolver, all matching:

    • an extensionless main (lib/actual → dep/lib/actual.js)
    • a directory main (lib → dep/lib/index.js)
    • a missing main (→ dep/index.js, with Node's DEP0128 warning)
    • no manifest (→ dep/index.js)
    • an empty main (→ dep/index.js)
    • an explicit ESM ./dep/index.js (unchanged)
  • Remaining proof gaps: export maps (out of scope).

For evidence-bearing changes:

  • 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

  • npm run test:focused -- tests/boundary/cli/referenceSourceDirectoryImports.test.ts: passed
  • npm run test:focused -- on 9 reference-source suites (filesystem import, recovery, status, legacy facts, summary, UTF-8, exclusions, entries, types): 47 passed, 2 skipped
  • npm run check:fast, npm run knip: passed
  • Real data, comparing base and fixed builds: I imported a copy of the real zod package (747 entries, 1,717 relationships, with nested v3/v4/mini package manifests).
    • The sorted relationships are byte-identical between builds: no regressions.
    • Wall time is within noise: base 1.48 s and 1.52 s; fixed 1.51 s, 1.59 s and 1.77 s. The manifest pre-pass parses only package.json bytes already in memory.

Compatibility, safety, and release

  • Breaking changes or migration steps: none. Relationships that named the wrong index now name Node's selection.
  • Real Hopper/Ghidra, browser, or OS coverage: not applicable. The test skips Windows, as before (reference-source reads need no-follow opens).
  • Package or release metadata impact: none.
  • Security, privacy, process, or containment review: no target execution; manifests are parsed as inert JSON.

Author readiness

Human owner:

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • I declare this PR ready for initial maintainer review.

Before merge

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/morluto/rea/pull/3844 **Author:** [@kaoru0822-kitauji](https://github.com/kaoru0822-kitauji) **Created:** 10/15/2026 **Status:** ✅ Merged **Merged:** 10/15/2026 **Merged by:** [@kaoru0822-kitauji](https://github.com/kaoru0822-kitauji) **Base:** `main` ← **Head:** `fix/reference-package-main` --- ### 📝 Commits (2) - [`d77f035`](https://github.com/morluto/rea/commit/d77f0356fe06cd9b8493ce296ba342341cbd565e) fix(reference): select a package main before its directory index - [`22ee27a`](https://github.com/morluto/rea/commit/22ee27a3c8d0fa6aa7198b754d720a21a9a2e2c6) fix(reference): resolve package main the way path.resolve does ### 📊 Changes **2 files changed** (+131 additions, -13 deletions) <details> <summary>View changed files</summary> 📝 `src/application/ReferenceSourceImportEntries.ts` (+103 -12) 📝 `tests/boundary/cli/referenceSourceDirectoryImports.test.ts` (+28 -1) </details> ### 📄 Description ## Summary Fixes #3383. `import-reference-source` resolved `require("./dep")` to `dep/index.js` even when `dep/package.json` declares `"main": "actual.js"`. Node loads `dep/actual.js`. The historical graph reported the wrong file as an internal, parsed dependency, with no parse failure or limitation. Now a directory import selects the package's `main` before its index, as Node does. A manifest whose selection can't be established (unreadable or invalid JSON) leaves the relationship `unresolved` instead of claiming the index. ## Problem and expected behavior `resolveInternalSpecifier` resolved specifiers from inventory path candidates only, and no manifest was consulted. Node's `LOAD_AS_DIRECTORY` reads `X/package.json`. When `main` is a non-empty string, Node loads it as a file, then as a directory index, and falls back to `X/index.*` only when neither exists (the `DEP0128` legacy path). ## Change and scope - The reference-source entry owner, `src/application/ReferenceSourceImportEntries.ts`, changes as follows: - `parseReferenceSourceEntries` parses each inventoried `package.json` once from bytes already read (`readPackageMains`). Each manifest is classified as `declared`, `none` (no main, an empty main, or a non-string main) or `unknown` (unreadable, invalid UTF-8, or invalid JSON). - `selectPackageMain` runs only when the existing resolver chose a directory index (`<normalized>/index.*`). It loads the declared main with the same suffixes as other candidates, then as a directory index. When the main's target is absent, it keeps the index, matching Node's fallback. - A main that escapes the imported root becomes `unresolved`. - File-before-directory precedence, the extensionless-index rule from #3789, and explicit `./dep/index.js` imports are unchanged. The lookup and manifests travel together in one `SpecifierContext` instead of separate parameters. - Not included: package `exports`, `imports` and `types`. The issue concerns the legacy `main`, and historical-source import does not model export maps. ## Contract and boundary impact - Semantic owner and earliest changed stage: historical-source internal specifier resolution. - Contribution scope fit and linked maintainer decision: correctness fix within existing read-only import; not applicable. - CLI and MCP/tool-catalog contract: schemas unchanged. Some relationships now name the declared main, or become `unresolved` when the manifest is invalid. - Provider, bridge, target-format, or platform compatibility: unchanged. - Evidence, artifact, provenance, or reconstruction contract: wrong established dependencies become correct or explicitly unresolved. - Target selection, authority, upstream behavior, and cleanup details: no new reads; the manifests are already inventoried bytes. - Generated metadata, package, or installation impact: none. Expected effects: - [x] Read existing files or captured data. ## Evidence and regression coverage - Tests updated: `tests/boundary/cli/referenceSourceDirectoryImports.test.ts`, the built-CLI test that uses Node's `createRequire().resolve` as the oracle, adds three cases: - a declared main (`./pkg` → `pkg/actual.js`) - a missing main falling back to the index (`./stale` → `stale/index.js`) - an invalid manifest, where Node throws and rea reports `broken` as unresolved All existing directory cases still pass. - Base reproduction: against the unfixed module, the test fails on `package-main.cjs` and `invalid-manifest.cjs`. - User-visible output on the issue's fixture, after: ``` node=dep/actual.js rea: [{"from_path":"main.cjs","to":"dep/actual.js","kind":"requires","resolution":"internal","parse_state":"parsed"}] ``` Before: `"to":"dep/index.js"`. - More fixtures compared with Node's resolver, all matching: - an extensionless main (`lib/actual` → `dep/lib/actual.js`) - a directory main (`lib` → `dep/lib/index.js`) - a missing main (→ `dep/index.js`, with Node's `DEP0128` warning) - no manifest (→ `dep/index.js`) - an empty main (→ `dep/index.js`) - an explicit ESM `./dep/index.js` (unchanged) - Remaining proof gaps: export maps (out of scope). For evidence-bearing changes: - [x] Observed, derived, and inferred claims remain distinguishable. - [x] Artifact identity, source provenance, and failed attempts remain preserved. - [x] Unsupported, incomplete, unavailable, or uncertain outcomes remain visible. ## Validation performed - `npm run test:focused -- tests/boundary/cli/referenceSourceDirectoryImports.test.ts`: passed - `npm run test:focused --` on 9 reference-source suites (filesystem import, recovery, status, legacy facts, summary, UTF-8, exclusions, entries, types): 47 passed, 2 skipped - `npm run check:fast`, `npm run knip`: passed - Real data, comparing base and fixed builds: I imported a copy of the real `zod` package (747 entries, 1,717 relationships, with nested `v3`/`v4`/`mini` package manifests). - The sorted relationships are byte-identical between builds: no regressions. - Wall time is within noise: base 1.48 s and 1.52 s; fixed 1.51 s, 1.59 s and 1.77 s. The manifest pre-pass parses only `package.json` bytes already in memory. ## Compatibility, safety, and release - Breaking changes or migration steps: none. Relationships that named the wrong index now name Node's selection. - Real Hopper/Ghidra, browser, or OS coverage: not applicable. The test skips Windows, as before (reference-source reads need no-follow opens). - Package or release metadata impact: none. - Security, privacy, process, or containment review: no target execution; manifests are parsed as inert JSON. ## Author readiness Human owner: <!-- @GitHub-handle --> - [ ] I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work. - [ ] I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps. - [ ] I declare this PR ready for initial maintainer review. ## Before merge - [x] This PR addresses a concrete problem or an agreed enhancement. - [x] Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff. - [x] The PR has one focused outcome and the title follows `type(scope): outcome`. - [x] Related issue is linked, or the reason for not linking one is stated above. - [x] Tests cover changed observable behavior and meaningful failure paths. - [x] Owning docs, contracts, and generated metadata are updated where needed. - [x] User-visible CLI/MCP changes include representative output. - [x] I checked the final diff for secrets, unrelated cleanup, and unsupported claims. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-31 01:10:37 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
morluto/rea#5941
No description provided.