Repository navigation
fix(core): resolve path aliases for files outside every tsconfig include - #836
Conversation
When no ancestor tsconfig claims the importer through files/include/exclude (the common scripts/ directory next to a root config covering only src/), auto discovery returns no config and the importer resolves with no `paths` at all, so aliases fail with ERR_MODULE_NOT_FOUND while tsx resolves them. Fall back to the nearest ancestor tsconfig.json for module resolution only, as the issue suggests: it runs after discovery and the JavaScript probe in `TsconfigSource::for_importer`, so a config that claims the file always wins, and compiler options keep the strict ownership rule in `for_path`. Details, all matching tsx: - `exclude` does not gate resolution: tsx applies a config's `paths` to every importer outside node_modules, including files `exclude` names, so the old "the JavaScript probe does not resurrect a config that excludes the directory" test now asserts resolution instead. - The fallback serves bare specifiers only. `paths`/`baseUrl` never match a `.`-prefixed specifier, and the one thing a tsconfig context adds to relative resolution in oxc_resolver is `rootDirs`, which tsx does not implement — an unclaimed importer's missing `./dep` must fail exactly as it does under tsx and plain Node.js, never escape to another root. - No fallback inside node_modules, and the outcome (found config or None) is memoized per importer directory: the fallback runs once per (importer, specifier) edge, and the uncached ancestor walk otherwise repeated the same stat syscalls on every edge. - A broken nearest config behaves as `discover` treats broken ancestors: unreadable (I/O error) configs are skipped and the walk continues, an unparseable one means no config and stops the walk. Closes #835.
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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f518317c50
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0e8bfc11ee
ℹ️ 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".
…g fallback A rooted or scheme/drive-prefixed specifier names a file, but resolve_with_context tries tsconfig `paths` before absolute resolution, so a catch-all mapping like "*" -> src/shim.ts could redirect import "/opt/shared/x.ts" to the shim; tsx and plain Node.js resolve the named file. Restrict the fallback to bare package specifiers with an explicit is_bare_specifier check (relative, rooted and RFC 3986 scheme/drive prefixes excluded), with a regression test. Addresses review feedback on #836.
`is_bare_specifier` ran an RFC 3986 scheme-validation loop on every bare
specifier on every platform, but drive prefixes and `\`-rooted/UNC paths
are Windows-only concrete targets. Unix now stops at the first-byte checks
(`.`, `/`, `#`) plus a memchr-backed `contains("://")` for URL schemes
(data:/file:/node: never reach the fallback); the `\\` and `C:` checks are
compiled out except on Windows. This also matches tsx's gate more closely:
its exclusion regex is anchored on `scheme://`, so scheme-without-`://`
specifiers stay alias-eligible exactly as tsx treats them, and `#` imports
keys containing `://` remain alias-eligible.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7543023b69
ℹ️ 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".
`file:/tmp/x.ts` carries no `://`, so the scheme check introduced in the gate rework treated it as a bare specifier — but Node.js accepts the single-slash spelling (WHATWG normalizes it to `file:///x`), and a catch-all tsconfig `paths` mapping then redirected the import to the shim instead of the named file. Plain Node.js and tsx both load the named file (tsx's gate excludes `file:` outright). Exclude the `file:` scheme explicitly, case-insensitively, and cover the spelling in the regression test. Addresses review feedback on #836.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e95b72eb5a
ℹ️ 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".
…allback
The previous gate special-cased `file:` and `scheme://`, but scheme-only
specifiers (`virtual:module`, `custom:foo`) were still treated as bare:
with a catch-all `paths` mapping, resolve_with_context resolved them to
the mapped file, stealing the specifier from Node.js or a downstream
loader that owns the scheme. Detect the RFC 3986 scheme prefix instead
(`find(':')` first — memchr-backed in std, so bare names, which never
contain a colon, cost one vectorized scan and no per-byte validation),
keeping the intentional `#` imports exception. This subsumes the `file:`
special case and the Windows drive check (a drive is a one-letter
scheme), leaving only the `\`-rooted/UNC test cfg-gated to Windows. The
rule is deliberately broader than tsx's gate for schemes, which aliases
them — documented in the code and asserted by the regression test.
Addresses review feedback on #836.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ae1e43daa1
ℹ️ 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".
Node.js parses specifiers with the WHATWG URL parser, which trims leading/trailing C0 controls and spaces and drops ASCII tab/newline: `" file:/x"` loads as a file URL under plain Node.js. The bare-specifier gate saw the raw bytes, so such an import classified as bare and a catch-all tsconfig `paths` mapping redirected it to the shim. Normalize the spelling for classification only — the raw specifier still drives resolution, so Node.js applies its own parsing to whatever the gate passes through. Covers both verified cases: the space-padded file URL now loads the named file, and the tab-mangled one fails exactly as it does under plain Node.js instead of executing the shim. Addresses review feedback on #836.
The space-url and tab-url fixture files embedded join() output directly, so on Windows the single backslashes formed invalid escape sequences and the file failed to parse before any resolution happened. JSON.stringify the constructed specifier like the neighboring fixtures do, and make the tab-url assertion tolerant of platform differences in how Node.js treats the tab-mangled URL afterward (load vs reject is Node's own behavior; the invariant under test is that the catch-all shim never executes).
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6e965624e8
ℹ️ 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".
The nearest-tsconfig fallback memoized every terminal outcome per importer directory, including "the config exists but does not parse". oxc_resolver's discovery never caches parse errors — get_or_try_init retries them — so a corrected tsconfig.json is picked up on the very next resolution there, while the fallback kept answering None for the rest of the process. Split the walk outcome into a Nearest enum (Found / NotFound / Unusable) and memoize only Found and NotFound. An unparseable config still stops the walk (never fall through to a grandparent), matching find_tsconfig_auto, but the failure is recomputed each time so a config corrected mid-process takes effect immediately. Covered by a regression test that imports through a broken tsconfig, fixes it, and imports again in the same process; without the change the second import stays failed.
Closes #835.
Problem
A script outside a root
tsconfig.json'sincludecannot use that config'spaths, while tsx resolves them — the common layout for repository scripts (scripts/*.tsimporting the same aliases assrc/). Resolution goes throughTsconfigSource::for_importer, which asks auto discovery for the config that claims the importer throughfiles/include/exclude; no ancestor config claims such a script, so it resolves with no config and losespaths.Fix
When nothing claims the importer, fall back to the nearest ancestor
tsconfig.jsonfor module resolution only, as the issue suggests. It runs inTsconfigSource::for_importerafter discovery and the JavaScript probe, so a config that claims the file always wins, and compiler options keep the strict ownership rule infor_path(a decorator inscripts/still fails exactly as before — verified by test).Design choices, each matching tsx:
excludedoes not gate resolution (option 1 in the issue). tsx applies a config'spathsto every importer outsidenode_modules, including filesexcludenames. The exclude-aware option (option 2) needs the ownership matcher oxc_resolver keeps private: 11.24.3 is the latest release and oxc main still keepsis_file_included_in_tsconfig/is_glob_matchprivate, and a local port of TypeScript's glob semantics would be neither small nor faithful. The previous "excludes the directory" test now asserts resolution, with the rationale in the code comments.paths/baseUrlnever match a.-prefixed specifier, and the one thing a tsconfig context adds to relative resolution in oxc_resolver isrootDirs(resolve_tsconfig_compiler_options), which tsx does not implement at all. Without this gate, an unclaimed importer's missing./depcould silently execute a file from another configured root instead of failing withERR_MODULE_NOT_FOUNDas tsx and plain Node.js do (found in review, proven with a fixture, regression test added).node_modulesguard, matching both tsx's dependency gate andfind_tsconfig.None. The fallback runs once per (importer, specifier) edge; syscall counting showed the uncached ancestor walk added ~2–5stats per edge (25–46% of all stat-family syscalls in the process) while everything around it is amortized to zero per edge (oxc_resolver caches per-directory discovery, per-file results, metadata and parsed configs for the process lifetime). Found in review; the memo adds no new staleness class.discover: an unreadable (I/O error)tsconfig.jsonis skipped and the walk continues to a grandparent; an unparseable one means no config and the walk stops.Tests
packages/integrate-vitest/__tests__/tsconfig-discovery.spec.ts: the issue's exact layout (TS + JS importers), exclude parity, no-compiler-options-leak, nearest-wins over a farther config, thenode_modulesguard, therootDirsregression, and the broken-config edge.vp run test(185 passed),cargo test --all-features,cargo clippy --all-targets --all-features -- -D warnings,vp check,vp run lintall green; the issue's fixture verified on Node 22, 24 and 26.Handed back / out of scope
rootDirsresolution for files a config claims is pre-existing behavior onmain(the full config was always passed for claimed importers) and diverges from tsx the same way; this PR only avoids extending it to unclaimed importers. Worth a separate decision.excludematcher publicly, revisiting exclude-aware resolution (option 2) is a one-line policy change insidenearest.