Fix source path duplication in NLS plugin source maps and add corresponding test (#300487)

This commit is contained in:
Johannes Rieken
2026-03-10 18:57:33 +01:00
committed by GitHub
parent 0a8edf7b2f
commit cb61e0d5bd
2 changed files with 27 additions and 1 deletions

View File

@@ -427,7 +427,11 @@ export function nlsPlugin(options: NLSPluginOptions): esbuild.Plugin {
// back to the original. Embed it inline so esbuild composes it
// with its own bundle source map, making the final map point to
// the original TS source.
const sourceName = relativePath.replace(/\\/g, '/');
// This inline source map is resolved relative to esbuild's sourcefile
// for args.path. Using the full repo-relative path here makes esbuild
// resolve it against the file's own directory, which duplicates the
// directory segments in the final bundled source map.
const sourceName = path.basename(args.path);
const sourcemap = generateNLSSourceMap(source, sourceName, edits);
const encodedMap = Buffer.from(sourcemap).toString('base64');
const contentsWithMap = code + `\n//# sourceMappingURL=data:application/json;base64,${encodedMap}\n`;

View File

@@ -220,6 +220,28 @@ suite('NLS plugin source maps', () => {
}
});
test('NLS-affected nested file keeps a non-duplicated source path', async () => {
const source = [
'import { localize } from "../../vs/nls";',
'export const msg = localize("myKey", "Hello World");',
].join('\n');
const { mapJson, cleanup } = await bundleWithNLS(
{ 'nested/deep/file.ts': source },
'nested/deep/file.ts',
);
try {
const sources: string[] = mapJson.sources ?? [];
const nestedSource = sources.find((s: string) => s.endsWith('/nested/deep/file.ts'));
assert.ok(nestedSource, 'Should find nested/deep/file.ts in sources');
assert.ok(!nestedSource.includes('/nested/deep/nested/deep/file.ts'),
`Source path should not duplicate directory segments. Actual: ${nestedSource}`);
} finally {
cleanup();
}
});
test('line mapping correct for code after localize calls', async () => {
const source = [
'import { localize } from "../vs/nls";', // 1