Skip to content

feat: enhance source map handling in NLS and private field conversion#296957

Merged
jrieken merged 2 commits intomainfrom
joh/grim-viper
Feb 23, 2026
Merged

feat: enhance source map handling in NLS and private field conversion#296957
jrieken merged 2 commits intomainfrom
joh/grim-viper

Conversation

@jrieken
Copy link
Copy Markdown
Member

@jrieken jrieken commented Feb 23, 2026

  • Implemented inline source maps in the NLS plugin to ensure accurate mapping from transformed source back to original.
  • Modified transformToPlaceholders to return edits for source map adjustments.
  • Added adjustSourceMap function to update source maps based on text edits in convertPrivateFields.
  • Introduced tests for source map accuracy in both NLS and private field conversion scenarios.
  • Updated documentation to reflect changes in source map handling and the rationale behind accepting column imprecision.

- Implemented inline source maps in the NLS plugin to ensure accurate mapping from transformed source back to original.
- Modified `transformToPlaceholders` to return edits for source map adjustments.
- Added `adjustSourceMap` function to update source maps based on text edits in `convertPrivateFields`.
- Introduced tests for source map accuracy in both NLS and private field conversion scenarios.
- Updated documentation to reflect changes in source map handling and the rationale behind accepting column imprecision.
Copilot AI review requested due to automatic review settings February 23, 2026 11:19
@jrieken jrieken enabled auto-merge (squash) February 23, 2026 11:19
@jrieken jrieken self-assigned this Feb 23, 2026
@vs-code-engineering vs-code-engineering Bot added this to the February 2026 milestone Feb 23, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves source map correctness in the build/next esbuild-based pipeline, specifically around NLS placeholder transformation and post-bundle private-field mangling so that debugging/crash reporting points back to original sources more reliably.

Changes:

  • Add inline input source maps in the NLS esbuild plugin so esbuild can compose NLS transforms into final bundle source maps.
  • Return detailed text edits from convertPrivateFields and introduce adjustSourceMap to rewrite .map files accordingly during post-processing.
  • Add targeted tests for NLS and private-field conversion source map behavior, plus documentation updates describing remaining column-imprecision tradeoffs.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
build/next/working.md Documents source map corruption root causes, fixes applied, and remaining known limitations.
build/next/nls-plugin.ts Tracks NLS edits and appends an inline source map to transformed TS so esbuild can compose maps correctly.
build/next/private-to-property.ts Exposes applied edits and adds adjustSourceMap to shift generated columns based on those edits.
build/next/index.ts Integrates source map adjustment during bundling output post-processing.
build/next/test/nls-sourcemap.test.ts New tests validating NLS plugin source map composition and sourcesContent correctness.
build/next/test/private-to-property.test.ts New tests validating convertPrivateFields edit reporting and adjustSourceMap behavior.
Comments suppressed due to low confidence (1)

build/next/private-to-property.ts:287

  • Rebuilding the map via new SourceMapGenerator({ file: sourceMapJson.file }) drops other top-level sourcemap fields (e.g. sourceRoot and any vendor extensions like x_google_ignoreList) if they exist on the input map. If those are present in esbuild’s output for this repo, they should be preserved on the adjusted map as well.
	// Use source-map library to read, adjust, and write
	const consumer = new SourceMapConsumer(sourceMapJson);
	const generator = new SourceMapGenerator({ file: sourceMapJson.file });

	// Copy sourcesContent
	for (let i = 0; i < sourceMapJson.sources.length; i++) {
		const content = sourceMapJson.sourcesContent?.[i];
		if (content !== null && content !== undefined) {
			generator.setSourceContent(sourceMapJson.sources[i], content);
		}
	}

	// Walk every mapping, adjust the generated column, and add to the new generator
	consumer.eachMapping(mapping => {
		const lineEdits = editsByLine.get(mapping.generatedLine - 1); // 0-based for our data
		const adjustedCol = adjustColumn(mapping.generatedColumn, lineEdits);

		const newMapping: Mapping = {
			generated: { line: mapping.generatedLine, column: adjustedCol },
			original: { line: mapping.originalLine, column: mapping.originalColumn },
			source: mapping.source,
		};
		if (mapping.name !== null) {
			newMapping.name = mapping.name;
		}
		generator.addMapping(newMapping);
	});

	return JSON.parse(generator.toString());
}

Comment thread build/next/nls-plugin.ts
Comment thread build/next/nls-plugin.ts
Comment thread build/next/private-to-property.ts Outdated
Comment thread build/next/index.ts Outdated
Comment on lines 899 to 914
if (mangleResult.editCount > 0) {
mangleStats.push({ file: path.relative(path.join(REPO_ROOT, outDir), file.path), result: mangleResult });
mangleEdits.set(file.path, { preMangleCode, edits: mangleResult.edits });
}
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The private-field edits are computed after postProcessNLS (see preMangleCode = content after NLS replacement), but the .map file being adjusted is still the original esbuild map for the pre-postProcess output. That means the edit offsets/columns do not line up with the map’s generated positions when NLS is enabled, so adjustSourceMap will shift the wrong columns (potentially making maps worse). To keep coordinate systems consistent, either run convertPrivateFields before postProcessNLS, or adjust the source map for NLS edits first / translate the private-field edit offsets back to the pre-NLS code.

Copilot uses AI. Check for mistakes.
Comment thread build/next/test/nls-sourcemap.test.ts
@jrieken jrieken merged commit 9c2922a into main Feb 23, 2026
26 of 29 checks passed
@jrieken jrieken deleted the joh/grim-viper branch February 23, 2026 12:09
@vs-code-engineering vs-code-engineering Bot locked and limited conversation to collaborators Apr 9, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants