Skip to content

fix(core): honor tsconfig verbatimModuleSyntax - #800

Merged
Brooooooklyn merged 2 commits into
mainfrom
fix/verbatim-module-syntax
Oct 8, 2026
Merged

Brooooooklyn merged 2 commits into
mainfrom
fix/verbatim-module-syntax

Conversation

@Brooooooklyn

@Brooooooklyn Brooooooklyn commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

Summary

verbatimModuleSyntax was ignored: only_remove_type_imports was hard-coded to false in the TypeScriptOptions literal, so oxc-node removed unused non-type imports and their module side effects never ran — diverging from both tsc and Node.js' own type stripping.

The reporter's repro — import { installed } from './polyfill.ts' with installed unused — now keeps the import, so polyfill.ts evaluates. The flag is wired straight through:

only_remove_type_imports: compiler_options
    .and_then(|c| c.verbatim_module_syntax)
    .unwrap_or(false),

Semantics, verified against tsc and Node's type stripping:

  • every non-type import is emitted verbatim, unused bindings included — side effects run
  • import type and per-specifier type modifiers stay elided
  • import x = require() keeps its live require() in CommonJS output, matching tsc's shouldEmitAliasDeclaration
  • the flag is inherited through extends chains (handled by oxc_resolver)

Second commit (from adversarial review): keeping import = verbatim exposed that the load hook passed Module::Preserve even for files Node runs as ESM, so a kept import = lowered to a bare require() and crashed with ReferenceError at run time (previously the unused declaration was deleted). tsc rejects this (TS1202/TS1203) and Node's type stripping rejects it at load time — the hook now passes Module::Esm for ES module files, which is emit-identical to Preserve but turns those constructs into a load-time transform error.

Closes #798

Test plan

New packages/integrate-vitest/__tests__/verbatim-module-syntax.spec.ts — 10 cases over the temp-project/subprocess pattern used by jsx-runtime.spec.ts: the issue repro (emitted code + end-to-end run order), flag-off and flag-absent elision, import type staying elided, per-specifier and all-type specifier lists, unused default/namespace imports, extends inheritance, bare side-effect imports, import = in CJS output, and import = in an ESM package failing with the TS1202 transform error instead of a runtime ReferenceError.

One known divergence locked in by a test: export { T } where T is only a type is emitted identically with and without the flag (oxc drops the specifier either way).

Known divergence (upstream, out of scope)

export { type T } from "./mod.ts" is dropped entirely, losing the module's side effects; tsc + verbatimModuleSyntax emits export {} from "./mod.js", which still evaluates the dependency. Fixing requires oxc_transformer's export-specifier cleanup (annotations.rs) to consult only_remove_type_imports — oxc-node only sets options. Similarly, rewriteRelativeImportExtensions does not rewrite the module reference inside import = (upstream rewrite_extensions.rs doesn't visit TSImportEqualsDeclaration).

Verified: vp fmt + vp check, cargo test, pnpm test (all integrate-* suites, 114 tests) green; the issue repro prints polyfill.ts evaluated then main.ts.

only_remove_type_imports was hard-coded to false, so unused non-type
imports were elided and their module side effects never ran — diverging
from tsc and Node's own type stripping. Wire verbatim_module_syntax
through so every non-type import is emitted verbatim; import type stays
elided either way.

Closes #798
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 8, 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-08T06:45:50.639241Z c5fc121 PR opened
ℹ️ 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.

The verbatimModuleSyntax fix keeps unused 'import x = require()' verbatim,
which lowered to a bare require() that crashed with ReferenceError when the
file runs as an ES module — where the previous elision had silently removed
it. tsc rejects import=/export= in ESM output (TS1202/TS1203) and Node's
type stripping rejects them at load time, so now the load hook passes
Module::Esm for files that are (or flip to) ES modules, surfacing the
transform error at load time instead of a runtime crash. Module::Esm emits
identically to Preserve for ESM input; it only enables the diagnostics.
@Brooooooklyn
Brooooooklyn merged commit 03f8086 into main Oct 8, 2026
67 checks passed
@Brooooooklyn
Brooooooklyn deleted the fix/verbatim-module-syntax branch October 8, 2026 07:07
Brooooooklyn added a commit that referenced this pull request Oct 8, 2026
…#801)

## Summary

`.jsx` and `.tsx` files in a `"type": "module"` package ran as CommonJS:
oxc_resolver's `ESM_FILE_FORMAT` consults the nearest `package.json`
`"type"` only for `.js` and `.ts`, so `resolution.module_type()`
returned `None` for `.jsx`/`.tsx` and the resolve hook fell through to
`"commonjs"`.

That had two visible symptoms (issue #797):

- `typeof require`/`module` were `function`/`object` instead of
`undefined`
- the automatic JSX runtime was emitted as
`require("react/jsx-runtime")`, going through Node's CommonJS resolution
— the resolve hook, and with it tsconfig `paths`, never applied

The fix supplies the package `type` where `module_type()` has no answer,
bounded to what Node's `ESM_FILE_FORMAT` treats as package-type
territory plus the source extensions `register.mjs` claims:

- **extensionless files** — Node answers them with the package type
- **`.jsx`, `.tsx`, `.es`, `.es6`** — registered source extensions Node
itself would reject (`ERR_UNKNOWN_FILE_EXTENSION`); we deliberately run
them, so the package type is the only consistent answer
- **explicit answers still win**: `.mts`/`.mjs`/`.cts`/`.cjs`/`.node`,
the resolved file's tsconfig ESM `module` override
(`default_module_from_tsconfig`), and `module_type()` all take
precedence
- **`.wasm` and unknown extensions keep the prior `commonjs` default** —
reclassifying them would feed non-source files through the transformer
as ES modules

Running as ESM additionally means `import =`/`export =` in such a file
surface the TS1202/TS1203 load-time error (merged with #800) instead of
a runtime `ReferenceError`, matching `tsc` and Node's type stripping.

Under `OXC_TRANSFORM_ALL`, the resolve hook now also answers formats for
`node_modules` files Node.js cannot classify
(`.jsx`/`.tsx`/`.es`/`.es6`, extensionless) — previously they died with
`ERR_UNKNOWN_FILE_EXTENSION` before `load` could transform them. Formats
Node.js *does* classify (`.ts`/`.cts`/`.mts`, `.wasm`, addons) still
defer to its loaders, so Node's own errors and load paths are preserved.

Closes #797

## Test plan

New `packages/integrate-vitest/__tests__/resolved-format.spec.ts`
(temp-project/subprocess pattern) plus a `.jsx` row in
`helper-imports.spec.ts`:

- issue repro verbatim: `a.ts`, `b.tsx`, `c.jsx` with no module syntax
in a `type: module` package all print `undefined` for `typeof
require`/`module`
- `.tsx`/`.jsx` **entry points** (no parent URL → the other resolve
path) run as ESM
- `type: commonjs` and no-`type` packages still run `.tsx`/`.jsx` as CJS
- a tsconfig `module: commonjs` cannot demote a `.tsx` file in a `type:
module` package
- `.es`/`.es6` and extensionless files follow the package type in both
directions
- `.wasm` and `.txt` keep `commonjs` even in a `type: module` package
(asserted via `createResolve`'s reported format — the fallback boundary)
- the automatic JSX runtime resolves through tsconfig `paths` (the
second symptom)

Verified: `vp fmt` + `vp check`, `cargo test`, `pnpm test` (all
integrate-* suites, 127 tests) green; the issue repro prints `a.ts:
module undefined undefined` / `b.tsx: module undefined undefined`.
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.

verbatimModuleSyntax is ignored, so unused imports and their side effects are removed

1 participant