Repository navigation
Description
🔎 Search Terms
transpileModule fileName normalized, transpileDeclaration diagnostic fileName, unstable/sync API canonicalize path, TranspileOptions fileName
🕗 Version & Regression Information
- This is the behavior in every version I tried:
[email protected](nightly), via thetypescript/unstable/syncAPIclass. Not a regression — undocumented existing behavior, as far as we can tell.
⏯ Playground Link
N/A — reproducible only via the typescript/unstable/sync Node API, not the Playground.
💻 Code
const { API } = require('typescript/unstable/sync');
const api = new API({});
const result = api.transpileModule('var a\n== 0;\n', {
compilerOptions: {},
fileName: 'C:\\Users\\me\\project\\app.ts',
reportDiagnostics: true,
});
console.log(result.diagnostics[0].fileName);
// "C:/Users/me/project/app.ts" - not what was passed in🙁 Actual behavior
fileName is silently canonicalized before use (separators to /, ./.. segments resolved — real path.normalize, not a slash swap), and every diagnostic echoes the canonical form instead of the caller's input:
fileName in |
diagnostics[0].fileName out |
|---|---|
C:\Users\me\project\app.ts |
C:/Users/me/project/app.ts |
C:/Users/me/../me/project/app.ts |
C:/Users/me/project/app.ts |
/tmp/foo/app.ts (already canonical) |
/tmp/foo/app.ts (unchanged) |
transpileDeclaration does the same. Neither behavior is documented on TranspileOptions.fileName or Diagnostic.fileName.
Not Windows-specific, though the backslash example is: POSIX paths never contain \, so that row won't reproduce on macOS/Linux, but the ./.. resolution will — pass a fileName like /tmp/foo/../foo/app.ts to see the same canonicalization on any OS.
This is a pain if you're trying to match up a diagnostic to the source file that it relates to. ts-loader's workaround is to canonicalize fileName itself before calling transpileModule, then use that same canonicalized string as the key: https://github.com/TypeStrong/ts-loader/blob/38d364e7d702138fa74672a9ef15c4447e6322ed/src/typeScriptApi.ts#L339
We found this reworking TypeStrong/ts-loader#1704: db8b27c added the canonicalization workaround to fix exactly this mismatch.
🙂 Expected behavior
It feels curious that the fileName that comes out differs from what is passed in. It would be marvellous if that didn't happen, but perhaps there are reasons it is necessary.
Here are some ideas that might improve things for consumers:
- Document on
TranspileOptions.fileName/Diagnostic.fileNamethatfileNameis canonicalized and diagnostics echo the canonical form, not the input. - Include the original filename as well as the processed one in the diagnostics for consumers to use.
originalFileNameperhaps - Specify the exact rule (or export it as a utility) so callers building their own location lookup can replicate it, rather than reverse-engineering it via a silently-wrong test result.
Additional information about the issue
Full writeup with more probe data: see the linked ts-loader PR/commits above. Andrew Branch (@andrewbranch) this may be interesting to you - but I wouldn't want to presume!
Activity
kritharth2005 commentedon Aug 29, 2026
andrewbranch commentedon Aug 31, 2026
This is working as intended and as far as I can tell it works the same way as the TS 6 API, so I'm not sure what necessitated the change when upgrading to TS 7. That's the canonicalization we do for SourceFiles internally, and in TS 6, you have a
diagnostic.fileSourceFile. In TS 7, we don't want to return a whole SourceFile on every Diagnostic, so you get the file name, but it's the samefileNamethat you would get from the SourceFile in either API version.It's a good idea to export the path normalization function. (5) in #63875 tracks that kind of thing.
johnnyreilly commentedon Aug 31, 2026
Actually I ended up not needing the change - it ended up being more of an observation that I thought might be significant; but it turns out not so much (which is fine!)
To avoid confusion, let me explain. I started pulling code out after the fixes to
transpileDeclarationetc landed in this commit:TypeStrong/ts-loader@6146ee1
Then I spotted the behaviour which I reported here. However I only spotted it, because before the actual transpile functionality had landed in the API I was faking a transpile mode in ts-loader and hadn't pulled all of the code related to that out. It effectively re-implemented the file error reporting in ts-loader rather than consuming from the API. This is what lead to me noticing the behaviour; it tripped up diagnostic reporting. When I noticed that, I pulled it out with this commit:
TypeStrong/ts-loader@38d364e
This resolved the original problem that lead me to raise this issue. But I wondered if this was something that mattered anyway and so I thought I'd leave the issue open in case.
For clarity, it makes no difference to ts-loader (and I imagine other consumers also) whether this change lands or not.
Yes that would be handy
refactor: use API-provided startPosition/endPosition instead of a man…
Support TypeScript 7.1 API (#1704)