Fix decoding source map with VS Code internal files

This makes it possible to decode source maps containing references to
code that is not part of the extension. If it finds any such references,
it will simply not decode the source map and use the original stack
trace instead.
This commit is contained in:
Koen Vlaswinkel
2023-10-17 13:34:11 +02:00
parent 8d5574e468
commit 67f6f8f160

View File

@@ -115,21 +115,35 @@ async function extractSourceMap() {
}
if (stacktrace.includes("at")) {
const rawSourceMaps = new Map<string, RawSourceMap>();
const rawSourceMaps = new Map<string, RawSourceMap | null>();
const mappedStacktrace = await replaceAsync(
stacktrace,
stackLineRegex,
async (match, name, file, line, column) => {
if (!rawSourceMaps.has(file)) {
const rawSourceMap: RawSourceMap = await readJSON(
resolve(sourceMapsDirectory, `${basename(file)}.map`),
);
rawSourceMaps.set(file, rawSourceMap);
try {
const rawSourceMap: RawSourceMap = await readJSON(
resolve(sourceMapsDirectory, `${basename(file)}.map`),
);
rawSourceMaps.set(file, rawSourceMap);
} catch (e: unknown) {
// If the file is not found, we will not decode it and not try reading this source map again
if (e instanceof Error && "code" in e && e.code === "ENOENT") {
rawSourceMaps.set(file, null);
} else {
throw e;
}
}
}
const sourceMap = rawSourceMaps.get(file) as RawSourceMap | null;
if (!sourceMap) {
return match;
}
const originalPosition = await SourceMapConsumer.with(
rawSourceMaps.get(file) as RawSourceMap,
sourceMap,
null,
async function (consumer) {
return consumer.originalPositionFor({