Repository navigation
fix(importer): reach package-main selection for trailing-slash directory references - #4219
Conversation
…ory references
posix.normalize preserves a trailing slash in the specifier, so normalized was sub/ while dirname(match) was sub; the resolvedThroughIndex comparison failed and selectPackageMain never ran for require("./sub/"). Compare against the slash-stripped directory so trailing-slash directory references take the same package-main path as the plain spelling. Boundary rows pair each trailing-slash request with the existing manifest fixtures and are validated against Node's own resolver in the same table.
Fixes morluto#4189
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
[Medium impact] The reviewed changes appear safe to merge. SummaryFixes package-main selection for directory references with trailing slashes.
Reviews (4) · Last reviewed commit: "fix(importer): normalize package-main ta..." · Reviewed by Greptile |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d15e3190f6
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| // dirname(match) never carries one; compare against the slash-stripped | ||
| // directory so a trailing-slash reference still reaches package-main | ||
| // selection the same way the plain spelling does. | ||
| const directory = normalized.replace(/(?<=.)\/+$/u, ""); |
There was a problem hiding this comment.
Derive trailing-slash directory identity with path semantics
For a legal POSIX directory whose final component ends in a line terminator (for example an escaped newline in require("./pkg\n/")), . in this lookbehind does not match that terminator, so the slash survives, resolvedThroughIndex stays false, and the importer still reports pkg\n/index.js instead of the manifest's declared main even though Node resolves both spellings to that main. Derive the directory lookup identity once with node:path semantics rather than stripping the serialized path with this regex.
AGENTS.md reference: AGENTS.md:L23-L23
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 323010f. Directory identity is now derived with path semantics: posix.join(normalized, ".") collapses any trailing-slash run regardless of the preceding character, so a specifier whose final component ends in a line terminator compares equal to dirname(match) the same way ./pkg/ does. The lookbehind regex is gone.
Regression pinned in the boundary table: a directory carrying package.json (main), actual.js and index.js forces the index-found path (the no-match package-main branch would otherwise mask it), and the row is validated against Node's own resolver in the same table. RED without the fix (fails on package-main-newline-slash.cjs), GREEN with it; check:fast clean.
Main added package-main selection for directories that match no candidate and restructured the directory-import test fixtures. Keep main's no-match branch, keep this branch's slash-stripped directory comparison for the index path, and add this branch's two trailing-slash cases (./pkg/, ./stale/) to main's fixture table. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Resolved the conflict with main (a66f236). Main had added package-main selection for directories that match no candidate and moved the directory-import fixtures into a shared table. I kept main's no-match branch and this PR's slash-stripped directory comparison for the index path, and added this PR's |
…antics
The lookbehind strip kept a trailing slash when the final component ended in a line terminator (require("./pkg\n/")), so dirname(match) still never matched and the trailing-slash path skipped package-main selection for those directories. Join the normalized path with "." instead: path semantics collapse any trailing slash run regardless of the preceding character, and behave identically to the strip on every other spelling. Boundary fixture gains a newline directory with a package.json, an index and a declared main so the index path (not the no-match branch) exercises the comparison.
Addresses Codex P1 on morluto#4219
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 323010fbd9
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| // semantics (joining "." collapses trailing slashes, including ones a regex | ||
| // would keep behind a line terminator) so a trailing-slash reference still | ||
| // reaches package-main selection the same way the plain spelling does. | ||
| const directory = posix.join(normalized, "."); |
There was a problem hiding this comment.
Normalize package-main targets with path semantics
When require("./pkg/") targets a package whose manifest declares main as a slash-terminated name whose final component ends in a line terminator (for example "lib\n/"), this new directory normalization reaches selectPackageMain, but that function still uses replace(/(?<=.)\/+$/u, ""); . does not match the newline, so with both files present the importer selects lib\n/index.js while Node selects lib\n.js. Normalize the package-main target with the same path semantics instead of leaving the duplicate regex.
AGENTS.md reference: AGENTS.md:L27-L27
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Root cause: selectPackageMain derived the entry target with posix.normalize(posix.join(packageDirectory, manifest.main)).replace(/(?<=.)\/+$/u, ""). . in the lookbehind never matches line terminators, so main: "lib\n/" kept its trailing separator: file candidates became lib\n/.js-shaped misses while the directory suffix reached lib\n/index.js, and the index won even though Node's native resolver selects lib\n.js.
Structural fix: the strip now happens at this owning boundary with path semantics — posix.join(joined, ".") collapses any trailing-separator run regardless of the preceding character (the same idiom the directory-identity fix in 323010f established) and preserves a lone root /. No separator-stripping regex remains in either stage.
Validation: pinned the competing file/index case in the shared table — nlpkg/package.json declares main: "lib\n/" with both nlpkg/lib\n.js and nlpkg/lib\n/index.js present, reached via require("./nlpkg/") exactly as in the repro. The harness runs real createRequire().resolve as its control first: Node v24.15.0 resolves nlpkg/lib\n.js (file), and the graph now agrees. Verified red with the fixture alone (index selected), green with the fix: 6 reference-source suites / 40 tests pass, check:fast clean. Pushed as a8abfed.
Remaining cleanup: none — both identity sites now share the path-semantics idiom and the lookbehind regex is gone.
baron
left a comment
There was a problem hiding this comment.
The first trailing-slash fix is present, but the directly related producer cleanup in the existing package-main thread remains applicable at this head. I am not adding a duplicate inline comment.
Reproduced with pinned Node 24.18.0 using exact resolver/helper source slices and supplied inventory facts: for require("./pkg/"), manifest main: "lib\n/", and both pkg/lib\n.js and pkg/lib\n/index.js, the resolver returns the index while Node's native resolver selects pkg/lib\n.js. selectPackageMain still strips the serialized target with the old lookbehind regex, so the same identity interpretation remains duplicated across the two stages. Normalize at that owning boundary with path semantics and cover the competing file/index case; do not repair the graph downstream.
Scope: bounded resolver/native-resolution probe only, not complete parser/CLI/MCP or real-provider execution. No build/install/full suite ran. Repository CI including CI required is green, but the source defect is independent of that receipt.
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
|
@baron Following up on your CHANGES_REQUESTED review (2026-10-17 07:51): the Short version of what changed at the owning boundary:
Author readiness declarations are ticked, and |
|
TL;DR: The fix works on Linux. Both open bot findings were real on the old code. Your head a8abfed fixes them, and your new test fails without the fix. Thanks, hemv-857, for the clear fix and the careful tests. I tested head a8abfed. Your PR base is 42fc6c5.
With the base importer swapped in and rebuilt, three rows fail: Bot threads
I also ran the old regex and Not run
Test setupCommandsInstall, build and test:prepare ran on Node 24, exit 0 each. The test ran once per Node version on the head, exit 0 each. For the failing run I copied src/application/ReferenceSourceImportEntries.ts from 42fc6c5 over the head's file, ran The string check was a ten-line script with the old regex and 🤖 Generated with Claude Code (Sonnet 5.5, verified by Opus 5.5) 🦊 |
|
@baron pinging since Oct 17 CHANGES_REQUESTED — the fix landed in
Both bot threads (Directory identity, Package-main target) confirmed fixed. The lookbehind regex is gone; Ready for your re-review when you have a moment. Thanks! |
baron
left a comment
There was a problem hiding this comment.
Re-reviewed this exact head. The earlier requested changes are addressed at the producing boundaries: directory identity and package-main targets now use path semantics. The regression includes competing newline-terminated file/index targets and checks Node's native resolver before asserting the imported graph.
On the head's pinned Node 24.18.0/npm 11.16.0, the focused compiled CLI test and 6 source tests pass. The integration with recorded main d361c53c504590b5ec90e9c690b5a2d6ae67cc38 passes all 16 selected CLI/source tests on Node 24.22.1, including main's newer parser and path-identity behavior. I checked the author replies against the actual fixes; the two still-open thread UIs do not represent remaining material findings.
Required head CI is green and the author readiness declaration is present. Windows and the complete suite were not run locally; no external provider was needed. Approval is a review verdict, not merge permission.
What
Fixes #4189.
resolveInternalSpecifierskipped package-main selection for trailing-slash directory references.Why
posix.normalize(posix.join(dirname, "./sub/"))preserves the trailing slash, sonormalizedissub/whileposix.dirname(match)issub. TheresolvedThroughIndexequality fails,selectPackageMainnever runs, andrequire("./sub/")resolves to the directory index even whensub/package.jsondeclares a differentmain. The plain spellingrequire("./sub")normalizes without the slash and does reach the manifest, so the two spellings disagree on the same directory.How
resolvedThroughIndexwith path semantics (posix.join(normalized, ".")), so a trailing-slash reference takes the identical package-main path as the plain spelling, including behind line terminators.selectPackageMain: joining"."collapses the trailing separator run regardless of the preceding character, so a manifest likemain: "lib\n/"selects the declared file exactly as Node does. The lookbehind regex that missed line terminators is gone from both stages../pkg/→pkg/actual.js,./stale/→stale/index.js) and are validated against Node's own resolver inside the same table; a new row pins the competing file/index case (main: "lib\n/"with bothlib\n.jsandlib\n/index.jspresent resolves tolib\n.js).Tests
npm run test:focused -- tests/boundary/cli/referenceSourceDirectoryImports.test.ts— 1 passed (trailing-slash rows RED without the fix, GREEN with it)npm run check:fast— typecheck + lint passAuthor readiness
Human owner: @hemv-857
Re-triggering the readiness check after GitHub's commit-association outage on 2026-10-19.