Skip to content

fix(core): resolve path aliases for files outside every tsconfig include - #836

Merged
Brooooooklyn merged 9 commits into
mainfrom
fix/835-tsconfig-paths-outside-include
Oct 16, 2026
Merged

Brooooooklyn merged 9 commits into
mainfrom
fix/835-tsconfig-paths-outside-include

Conversation

@Brooooooklyn

Copy link
Copy Markdown
Member

Closes #835.

Problem

A script outside a root tsconfig.json's include cannot use that config's paths, while tsx resolves them — the common layout for repository scripts (scripts/*.ts importing the same aliases as src/). Resolution goes through TsconfigSource::for_importer, which asks auto discovery for the config that claims the importer through files/include/exclude; no ancestor config claims such a script, so it resolves with no config and loses paths.

Fix

When nothing claims the importer, fall back to the nearest ancestor tsconfig.json for module resolution only, as the issue suggests. It runs in TsconfigSource::for_importer after discovery and the JavaScript probe, so a config that claims the file always wins, and compiler options keep the strict ownership rule in for_path (a decorator in scripts/ still fails exactly as before — verified by test).

Design choices, each matching tsx:

  • exclude does not gate resolution (option 1 in the issue). tsx applies a config's paths to every importer outside node_modules, including files exclude names. 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 keeps is_file_included_in_tsconfig / is_glob_match private, 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.
  • 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 (resolve_tsconfig_compiler_options), which tsx does not implement at all. Without this gate, an unclaimed importer's missing ./dep could silently execute a file from another configured root instead of failing with ERR_MODULE_NOT_FOUND as tsx and plain Node.js do (found in review, proven with a fixture, regression test added).
  • node_modules guard, matching both tsx's dependency gate and find_tsconfig.
  • Memoized per importer directory, including None. The fallback runs once per (importer, specifier) edge; syscall counting showed the uncached ancestor walk added ~2–5 stats 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.
  • Broken configs mirror discover: an unreadable (I/O error) tsconfig.json is 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, the node_modules guard, the rootDirs regression, 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 lint all green; the issue's fixture verified on Node 22, 24 and 26.

Handed back / out of scope

  • rootDirs resolution for files a config claims is pre-existing behavior on main (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.
  • If oxc_resolver later exposes an ownership/exclude matcher publicly, revisiting exclude-aware resolution (option 2) is a one-line policy change inside nearest.

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.
@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 ✅ Completed 2026-10-16T04:34:53.004942Z c6b48d2 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: 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".

Comment thread src/lib.rs Resolved

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

Comment thread src/lib.rs Resolved
…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.

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

Comment thread src/lib.rs Resolved
`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.

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

Comment thread src/lib.rs Resolved
…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.

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

Comment thread src/lib.rs Resolved
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).

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

Comment thread src/lib.rs Resolved
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.
@Brooooooklyn
Brooooooklyn merged commit 66c36c9 into main Oct 16, 2026
67 checks passed
@Brooooooklyn
Brooooooklyn deleted the fix/835-tsconfig-paths-outside-include branch October 16, 2026 04:48
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.

tsconfig paths do not apply to files outside the root config's include, unlike tsx

1 participant