Skip to content

Native tsc: GetEmitModuleFormatOfFile ignores content-mapper VirtualFileName (*.mts treated as CJS under module:commonjs) #65135

Closed

Description

🔎 Search Terms

content mapper, VirtualFileName, GetEmitModuleFormatOfFile, GetImpliedNodeFormatForEmit, ImpliedNodeFormat, .mts, module commonjs

🕗 Version & Regression Information

  • Present on current main (native Go port)
  • During load, filesparser correctly sets SourceFileMetaData.ImpliedNodeFormat from VirtualFileName (e.g. app.box.mts → ESNext)
  • But Program.GetEmitModuleFormatOfFile / GetImpliedNodeFormatForEmit pass the physical FileName() (e.g. app.box) into GetImpliedNodeFormatForEmitWorker

⏯ Playground Link

N/A (content mapper / program API)

💻 Code

Content mapper that sets output extension .mts for app.box, with:

{ "compilerOptions": { "module": "commonjs", "moduleResolution": "node10" }, "contentMappers": [{ "package": "mapper", "extensions": [".box"] }] }
// app.box  (mapped virtual name: app.box.mts)
export const value = 1;

Observed via program API after opening a project that loads the mapped file:

mapped.VirtualFileName()                          == "/home/project/app.box.mts"
meta.ImpliedNodeFormat                            == ESNext
GetEmitModuleFormatOfFileWorker(VirtualFileName)  == ESNext
GetEmitModuleFormatOfFileWorker(FileName)         == CommonJS   // fallback to options
program.GetEmitModuleFormatOfFile(mapped)         == CommonJS   // uses FileName
program.GetImpliedNodeFormatForEmit(mapped)       == None       // uses FileName

Root cause in tsc/internal/compiler/program.go:

func (p *Program) GetEmitModuleFormatOfFile(sourceFile ast.HasFileName) core.ModuleKind {
	return ast.GetEmitModuleFormatOfFileWorker(sourceFile.FileName(), ...)
}
func (p *Program) GetImpliedNodeFormatForEmit(sourceFile ast.HasFileName) core.ResolutionMode {
	return ast.GetImpliedNodeFormatForEmitWorker(sourceFile.FileName(), ...)
}

For non-node16/nodenext emit module kinds, GetImpliedNodeFormatForEmitWorker only keeps ImpliedNodeFormat == ESNext when package.json#type is "module" or the filename ends in .mjs/.mts. The physical .box name fails that gate, so format falls back to compilerOptions.module (CommonJS). The same pattern affects GetModeForUsageLocation / GetEmitSyntaxForUsageLocation.

Note: under module: nodenext the worker returns metadata.ImpliedNodeFormat directly, so the bug is masked there.

🙁 Actual behavior

An ESM-mapped content-mapper file (VirtualFileName *.mts, ImpliedNodeFormat ESNext) reports emit/implied format as CommonJS / None under module: commonjs, so downstream emit/module transformers and format diagnostics see CJS.

🙂 Expected behavior

When VirtualFileName is set, emit/implied format (and usage-location mode helpers) should use it — consistent with load-time metadata derivation in filesparser.go — so *.mts mapped outputs stay ESM under module: commonjs.

Additional information

Same helpers also take FileName() in:

  • GetModeForUsageLocation
  • GetEmitSyntaxForUsageLocation

Activity

  1. timurzvural commented on Nov 3, 2026

    @timurzvural
    Author

    User-visible impact (with verbatimModuleSyntax: true + module: commonjs):

    Content-mapped app.box → virtual app.box.mts also gets false diagnostics:

    • TS1287 on the mapped file — top-level export treated as CJS under verbatimModuleSyntax
    • TS1295 on importers — ESM import from that file treated as illegal in CJS

    A physical app.mts under the same options is clean (.mts forces ESM emit format). So this is not only an API divergence — it surfaces as incorrect verbatimModuleSyntax errors for content-mapped ESM outputs.

  2. added
    Needs Investigation
    on Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Needs Investigation

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions