Skip to content

fix(core): resolve TypeScript before JavaScript outside node_modules - #851

Merged
Brooooooklyn merged 2 commits into
mainfrom
fix/issue-840
Oct 16, 2026
Merged

Brooooooklyn merged 2 commits into
mainfrom
fix/issue-840

Conversation

@Brooooooklyn

Copy link
Copy Markdown
Member

Closes #840

Problem

The resolver put .js first in the .js extension alias ([".js", ".ts", ".tsx"]), and before .ts in extensions. With dep.js beside dep.ts, both ./dep.js and ./dep resolved to dep.js. tsc and tsx resolve both to dep.ts, so a project that compiles in place, or keeps generated .js beside its sources, ran stale output. The alias also had no .jsx, so import './dep.js' failed when only dep.jsx existed.

Fix

Two orders. resolver_for_conditions now caches resolvers by export conditions and an ExtensionOrder:

  • Source, for the entry point and importers outside node_modules:
    • aliases: .js → .ts, .tsx, .js, .jsx, .mjs → .mts, .mjs, .cjs → .cts, .cjs
    • extensions: .ts, .tsx, .js, .jsx, .mts, .mjs, .cts, .cjs, .json, .wasm, .node
  • Package, for importers inside node_modules: the existing JavaScript-first options, unchanged.

Packages keep their compiled output. A bare import from project code resolves with the Source order. That would pick a package's .ts sources when the package ships them beside its compiled .js. So when the Source order lands on a TypeScript file inside node_modules, the request is resolved again with the Package order. This retry only happens when a TypeScript file inside node_modules is picked, so it's rare.

Tests

  • Rust unit test for the Source order.

  • packages/integrate-vitest/__tests__/extension-order.spec.ts:

    • dep.ts wins over dep.js for both ./dep.js and ./dep (the issue's example)
    • .mts/.cts win over .mjs/.cjs
    • ./dep.js and ./dep find dep.jsx
    • a lone dep.js still resolves
    • a package shipping .ts beside .js still runs its .js, both from the project's bare import and from its own relative imports

    The first three fail on main.

Verification (macOS arm64, Node 24.22)

cargo test --all-features, cargo clippy --all-targets --all-features -- -D warnings, vp run test (215 passed) and vp run lint all pass.

Not covered

CommonJS require('./dep') from a .cts file goes through Node's own CommonJS resolver, not this one, and still picks dep.js; tsx picks dep.ts. Fixing that means hooking CommonJS filename resolution in register.mjs, so it is left for a follow-up.

🤖 Generated with Claude Code

With `dep.js` beside `dep.ts`, `./dep.js` and `./dep` resolved to
`dep.js`, so a project that compiles in place ran stale output, and
`./x.js` never reached `x.jsx`. Importers outside `node_modules` now use
a resolver that tries `.ts`/`.tsx` before `.js` (and `.mts`/`.cts`
before `.mjs`/`.cjs`), and maps `.js` to `.jsx` too, as tsc and tsx do.
Importers inside `node_modules` keep the JavaScript-first order, and a
TypeScript file the source order finds inside `node_modules` is
re-resolved with it, so packages keep running what they compiled.

Closes #840

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 16, 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-16T08:41:40.371248Z 0a5babe 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.

@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: c9a18b9a55

ℹ️ 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".

Comment thread src/lib.rs Resolved
The source order tries `.jsx` ahead of `.mjs`/`.cjs`, so a package whose
extensionless entry has `index.jsx` beside a compiled `index.mjs` loaded
the `.jsx`, which Node rejects inside node_modules. Re-resolve with the
package order for every extension the source order tries early: `.ts`,
`.tsx`, `.mts`, `.cts` and `.jsx`.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@Brooooooklyn
Brooooooklyn merged commit 66c004d into main Oct 16, 2026
67 checks passed
@Brooooooklyn
Brooooooklyn deleted the fix/issue-840 branch October 16, 2026 09:41
Brooooooklyn added a commit that referenced this pull request Oct 22, 2026
## Summary

Outside `node_modules`, `import "./App.jsx"` fails with
`ERR_MODULE_NOT_FOUND` when only `App.tsx` exists. tsc and tsx resolve a
`.jsx` specifier to `.tsx`, then `.ts`, then `.jsx`, then `.js`.

This change adds that `.jsx` extension alias to the source-first
resolver options, next to the existing `.js`, `.mjs` and `.cjs` aliases.
Files inside `node_modules` keep the package order and are unchanged.

As with `./dep.js` after #851, `./dep.jsx` now picks `dep.tsx` when both
files exist. The `resolved-format` test that kept `dep.tsx` and
`dep.jsx` side by side now uses `other.jsx` for its `.jsx` probe, so it
still checks the module format of a `.jsx` file.

## Test plan

- New integration test in `extension-order.spec.ts`: `./dep.jsx` picks
`dep.tsx` over `dep.jsx`, and `./util.jsx` finds `util.ts`. It fails on
`main` with `ERR_MODULE_NOT_FOUND` and passes with this change.
- `source_order_tries_typescript_first` asserts the new alias.
- `pnpm test`: 550 passed, 19 skipped. `cargo test`, `cargo clippy
--all-targets --all-features -- -D warnings`, `cargo fmt --check` and
`vp lint` pass.

Fixes #873

---------

Co-authored-by: LongYinan <lynweklm@gmail.com>
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

./dep.js and ./dep resolve to dep.js before dep.ts, unlike tsc and tsx

1 participant