Skip to content

Content mappers: moduleSuffixes probes card.foo.web instead of card.web.foo for a registered extension #64548

Closed

Description

@leonidaz

With "moduleSuffixes": [".web", ""], a fully specified import of a content-mapper extension puts the suffix after the extension: import "./card.foo" probes card.foo.web instead of card.web.foo, so it resolves to card.foo. For a built-in extension, import "./target.ts" probes target.web.ts and resolves to it.

Demo Repo

https://gist.github.com/leonidaz/a68ed721ae38e2a90eada142074c24f9

git clone https://gist.github.com/leonidaz/a68ed721ae38e2a90eada142074c24f9.git content-mapper-module-suffixes-repro
cd content-mapper-module-suffixes-repro
pnpm install --ignore-scripts
pnpm run setup
pnpm run check           # exit 2: TS2322, './card.foo' resolved to card.foo
pnpm run check:standard  # exit 0: './target.ts' resolved to target.web.ts

The mapper is the 47-line identity mapper from #64546: .foo files contain TypeScript, returned unchanged as .ts with an identity span map. setup.mjs only writes the local mapper manifest under node_modules. pnpm run check is exactly tsc --runExternalCode --pretty false -p tsconfig.json.

Which of the following problems are you reporting?

The module specifier resolves, but to the wrong file

Demonstrate the defect described above with a code sample.

card.foo exports platform = "default", card.web.foo exports platform = "web":

// main.ts
import { platform } from "./card.foo";
const check: "web" = platform; // TS2322: Type '"default"' is not assignable to type '"web"'.

Run tsc --showConfig and paste its output here

{
    "compilerOptions": {
        "allowImportingTsExtensions": true,
        "module": "esnext",
        "moduleResolution": "bundler",
        "moduleSuffixes": [
            ".web",
            ""
        ],
        "noEmit": true,
        "strict": true,
        "target": "es2022",
        "types": []
    },
    "files": [
        "./main.ts"
    ]
}

--showConfig omits contentMappers in this nightly; tsconfig.json registers identity-foo-mapper for [".foo"].

Run tsc --traceResolution and paste its output here

======== Resolving module './card.foo' from '/repro/main.ts'. ========
Explicitly specified module resolution kind: 'Bundler'.
Resolving in CJS mode with conditions 'import', 'types'.
Loading module as file / folder, candidate module location '/repro/card.foo', target file types: TypeScript, JavaScript, Declaration, JSON.
File name '/repro/card.foo' has a '.foo' extension - stripping it.
File '/repro/card.foo.web' does not exist.
File '/repro/card.foo' exists - use it as a name resolution result.
======== Module name './card.foo' was successfully resolved to '/repro/card.foo'. ========
main.ts(2,7): error TS2322: Type '"default"' is not assignable to type '"web"'.

The control, import "./target.ts" from standard.ts with target.ts and target.web.ts:

======== Resolving module './target.ts' from '/repro/standard.ts'. ========
Explicitly specified module resolution kind: 'Bundler'.
Resolving in CJS mode with conditions 'import', 'types'.
Loading module as file / folder, candidate module location '/repro/target.ts', target file types: TypeScript, JavaScript, Declaration, JSON.
File name '/repro/target.ts' has a '.ts' extension - stripping it.
File '/repro/target.web.ts' exists - use it as a name resolution result.
======== Module name './target.ts' was successfully resolved to '/repro/target.web.ts'. ========

Paste the package.json of the importing module, if it exists

{
  "name": "typescript-content-mapper-module-suffixes-repro",
  "private": true,
  "type": "module",
  "scripts": {
    "setup": "node setup.mjs",
    "check": "tsc --runExternalCode --pretty false -p tsconfig.json",
    "check:standard": "tsc --runExternalCode --pretty false -p tsconfig.standard.json",
    "trace": "tsc --runExternalCode --pretty false -p tsconfig.json --traceResolution"
  },
  "devDependencies": {
    "typescript": "7.1.0-dev.20260929.1"
  }
}

Paste the package.json of the target module, if it exists

Same package: card.foo, card.web.foo, target.ts and target.web.ts sit next to the importers.

Any other comments can go here

  • Reproduced with [email protected] (typescript@next on 2026-09-29, gitHead 0681ef7fa3a2378ccf49645b6d5b7463bdca74bb), macOS 26.6.2 x64, Node.js 24.18.0, pnpm 10.33.4. Verified from a fresh clone of the gist.

  • There is no runtime step in the demo: the expected placement is TypeScript's own, shown by the .ts control and by the moduleSuffixes docs (foo.ios.ts, not foo.ts.ios).

  • Cause, from main at b85298b: tryFile takes the extension from tspath.TryGetExtensionFromPath, which only knows built-in extensions. For card.foo it returns "", so the suffix lands after the whole file name. With this change, a local build of main passes the demo, the control is unchanged, and go test ./internal/module/... ./internal/modulespecifiers/... ./internal/contentmapper/... passes:

    --- a/tsc/internal/module/resolver.go
    +++ b/tsc/internal/module/resolver.go
    @@ -1578,6 +1578,11 @@ func (r *resolutionState) tryFile(fileName string) (string, bool) {
     	}
     
     	ext := tspath.TryGetExtensionFromPath(fileName)
    +	if ext == "" {
    +		// Content-mapper extensions are not built in: keep the suffix in front of them
    +		// (card.web.foo, not card.foo.web).
    +		ext = tspath.GetLongestExtensionFromPath(fileName, r.resolver.extraExtensions, false)
    +	}
     	fileNameNoExtension := tspath.RemoveExtension(fileName, ext)
     	for _, suffix := range r.compilerOptions.ModuleSuffixes {
     		path := fileNameNoExtension + suffix + ext
  • Found while looking into Content mappers: registered extensions are not probed for extensionless imports in bundler mode #64546. Platform variants such as Card.web.tsrx / Card.ios.tsrx in React Native-style projects are the use case.

The reproduction and report were prepared with Claude Code; the commands above were run and the results verified.

Activity

  1. RyanCavanaugh commented on Sep 30, 2026

    @RyanCavanaugh
    Member

    This is the intended behavior of moduleSuffixes; content mappers are not intended to change how it works.

    To be 100% clear: in order to use a content mapper, you are going to have to put the content mapper's extension in the import specifier. Implied extensions of CM'd files isn't a supported scenario.

  2. added
    Working as Intended
    on Sep 30, 2026
  3. leonidaz commented on Sep 30, 2026

    @leonidaz
    Author

    Understood that the compiler option's placement is intended. I've filed #64560 as a suggestion: an optional moduleSuffixes list on a contentMappers entry, used instead of the compiler option for that entry's extensions, with the suffix placed before the extension (card.web.foo). Entries without it, and the compiler option itself, resolve exactly as today. It includes a prototype against main and a results table.

    Fine to close this one in favor of #64560.

  4. typescript-automation commented on Oct 3, 2026

    @typescript-automation
    Contributor

    This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  5. andrewbranch commented on Nov 9, 2026

    @andrewbranch
    Member

    I'm not sure this is working as intended—this part is definitely true:

    in order to use a content mapper, you are going to have to put the content mapper's extension in the import specifier. Implied extensions of CM'd files isn't a supported scenario.

    But using moduleSuffixes doesn't depend on omitting an extension. You can write import Component from "./Component.ts" and Component.ios.ts will still be tried. The implementation rule that's developing as content mappers gain more capabilities is that registering an extension with a content mapper makes it act like any other built-in extension that doesn't have special module resolution behavior (e.g. allowing the extension to be omitted), like .mts or .cts. Since import "./Component.mts" resolves moduleSuffixes by putting them before the recognized extension (e.g. Component.ios.mts), I think content mapper extensions should probably act the same.

  6. added
    Needs Investigation
    and removed
    Working as Intended
    on Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Domain: Content MappersNeeds Investigation

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions