Skip to content

fix(importer): reach package-main selection for trailing-slash directory references - #4219

Merged
baron merged 4 commits into
morluto:mainfrom
hemv-857:fix-4189-trailing-slash-package-main
Oct 24, 2026
Merged

baron merged 4 commits into
morluto:mainfrom
hemv-857:fix-4189-trailing-slash-package-main

Conversation

@hemv-857

@hemv-857 hemv-857 commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

What

Fixes #4189. resolveInternalSpecifier skipped package-main selection for trailing-slash directory references.

Why

posix.normalize(posix.join(dirname, "./sub/")) preserves the trailing slash, so normalized is sub/ while posix.dirname(match) is sub. The resolvedThroughIndex equality fails, selectPackageMain never runs, and require("./sub/") resolves to the directory index even when sub/package.json declares a different main. The plain spelling require("./sub") normalizes without the slash and does reach the manifest, so the two spellings disagree on the same directory.

How

  • Derive the directory identity used by resolvedThroughIndex with path semantics (posix.join(normalized, ".")), so a trailing-slash reference takes the identical package-main path as the plain spelling, including behind line terminators.
  • Normalize the package-main target the same way inside selectPackageMain: joining "." collapses the trailing separator run regardless of the preceding character, so a manifest like main: "lib\n/" selects the declared file exactly as Node does. The lookbehind regex that missed line terminators is gone from both stages.
  • Boundary rows pair each trailing-slash request with the existing manifest fixtures (./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 both lib\n.js and lib\n/index.js present resolves to lib\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)
  • Same suite after a8abfed: manifest-newline row RED with the fixture alone (index selected), GREEN with the path-semantics normalization
  • 6 reference-source suites, 40 tests pass together; npm run check:fast — typecheck + lint pass

Author readiness

Human owner: @hemv-857

  • 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.

Re-triggering the readiness check after GitHub's commit-association outage on 2026-10-19.

…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
@hemv-857
hemv-857 marked this pull request as ready for review October 15, 2026 19:31
Copilot AI balanced review requested due to automatic review settings October 15, 2026 19:31

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 15, 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 ⚠️ Failed 2026-10-17T09:01:04.419128Z a8abfed New commits
ℹ️ 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.

@greptile-apps

greptile-apps Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The reviewed changes appear safe to merge.

Summary

Fixes package-main selection for directory references with trailing slashes.

  • Trailing-slash directory requests now follow package main entries.

Reviews (4) · Last reviewed commit: "fix(importer): normalize package-main ta..." · Reviewed by Greptile

@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: 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, "");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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]>
@kaoru0822-kitauji

Copy link
Copy Markdown
Collaborator

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 ./pkg/ and ./stale/ cases to main's fixture table. referenceSourceDirectoryImports.test.ts passes locally. I also checked that the ./pkg/ case fails if the comparison goes back to normalized, so the fix still matters on top of main.

@kaoru0822-kitauji kaoru0822-kitauji removed the ci-fix-wip Claimed by the CI-fix track label Oct 16, 2026
…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

@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: 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, ".");

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 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 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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 baron left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@hemv-857

Copy link
Copy Markdown
Contributor Author

@baron Following up on your CHANGES_REQUESTED review (2026-10-17 07:51): the selectPackageMain cleanup you asked for landed as a8abfedcb ten minutes later, and both your package-main thread and the P1/P2 threads carry the full root cause / structural fix / validation replies.

Short version of what changed at the owning boundary:

  • The lookbehind strip (replace(/(?<=.)\/+$/u, "")) is gone. Trailing separators now collapse with path semantics — posix.join(joined, ".") — the same idiom the directory-identity fix in 323010fb established, so a final component ending in a line terminator behaves like ./pkg/. No separator-stripping regex remains in either stage, and nothing was repaired downstream in the graph.
  • Your repro's competing file/index case is pinned 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/"). The harness runs real createRequire().resolve as the control first (Node v24.15.0 selects the file), then asserts the graph agrees. RED with the fixture alone (index wrongly selected), GREEN with the fix; 6 reference-source suites / 40 tests and check:fast clean.

Author readiness declarations are ticked, and CI required is green at a8abfedcb. Ready for another look.

@hemv-857
hemv-857 requested review from baron and a balanced review from Copilot October 20, 2026 14:19

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@CassieFox23

Copy link
Copy Markdown
Contributor

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.

Check Node 22.23.1 Node 24.18.0 Node 26.11.1
referenceSourceDirectoryImports.test.ts on the head 1 passed 1 passed 1 passed
Same file with the base importer swapped in not run 1 failed not run

With the base importer swapped in and rebuilt, three rows fail: package-main-slash-specifier.cjs, package-main-newline-slash.cjs and package-main-manifest-newline-slash.cjs. The row package-main-fallback-slash.cjs passes on both. That matches the test, because its manifest has no valid main.

Bot threads

Thread Finding Result
Directory identity A lookbehind regex keeps a slash behind a line terminator Real on the old code. Fixed on the head: no regex is left, and the row package-main-newline-slash.cjs passes.
Package-main target The same regex in selectPackageMain picks lib\n/index.js where Node picks lib\n.js Real on the old code. Fixed on the head: the row package-main-manifest-newline-slash.cjs passes.

I also ran the old regex and posix.join(x, ".") on 11 strings. Both strip pkg/ and pkg//. Only the new idiom strips the slash in pkg\n/, lib\n/ and a\r/. Both keep a lone /. An empty string becomes . with the new idiom. I did not trace whether a real call reaches that input.

Not run

  • Merge with the newest main.
  • The other five reference-source suites from your PR body. I ran only the one touched file.
  • npm run check:fast, typecheck and lint.
  • macOS and Windows.
Test setup
  • Box: Nobara 44 (Fedora 44 base), Linux 7.2.11
  • CPU: AMD Ryzen 9 5900X, 12 cores / 24 threads
  • RAM: 32 GB DDR4-3600
  • Node: 22.23.1, 24.18.0, 26.11.1
  • Head / base: a8abfed / 42fc6c5
  • Other: the box was shared with other test jobs.
Commands
npm ci --ignore-scripts
npm run build:cached
npm run test:prepare
npm run test:focused -- tests/boundary/cli/referenceSourceDirectoryImports.test.ts

Install, 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 npm run build:cached (exit 0), then the same test on Node 24 (exit 1).

The string check was a ten-line script with the old regex and posix.join(x, "."), run on Node 22, exit 0.

🤖 Generated with Claude Code (Sonnet 5.5, verified by Opus 5.5) 🦊

@hemv-857

Copy link
Copy Markdown
Contributor Author

@baron pinging since Oct 17 CHANGES_REQUESTED — the fix landed in a8abfedcb and we now have independent validation from CassieFox23 (Oct 20):

TL;DR: Fix works on Linux across Node 22/24/26. Both bot findings were real on old code; head a8abfedcb fixes them. Regression test: swap base importer → 3 bug rows fail; head passes.

Check Node 22.23.1 Node 24.18.0 Node 26.11.1
referenceSourceDirectoryImports.test.ts on head ✓ ✓ ✓
Base importer swapped in — 3 rows fail —

Both bot threads (Directory identity, Package-main target) confirmed fixed. The lookbehind regex is gone; selectPackageMain uses posix.join(x, ".") now. All 5 test shards + full CI green.

Ready for your re-review when you have a moment. Thanks!

@baron baron left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

@baron
baron merged commit 791524c into morluto:main Oct 24, 2026
45 of 46 checks passed
@morluto morluto mentioned this pull request Oct 25, 2026
3 tasks done
@morluto morluto added area: build Build, CI, packaging, and publishing bug Something isn't working labels 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 bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Trailing-slash CommonJS require ignores package main in reference import

6 participants