Skip to content

Commit 01f4937

Browse files
authored
fix(source-maps): Use alternate source map bias only if the default returns null and the alternate does not. Remove as user option to reduce config confusion. (#303)
In an effort to reduce config options (and confusion), this removes the sourceMapBias param from the launch config. The default source map bias is 'leastUpperBound', which works with our published ts config in TS Starter. This config is also used in the source-map unit tests. If leastUpperBound fails, it will try greatestLowerBound but will not switch to that as the preferred bias unless it succeeds.
1 parent bf75da4 commit 01f4937

File tree

3 files changed

+31
-26
lines changed

3 files changed

+31
-26
lines changed

package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,6 @@
128128
"type": "object",
129129
"description": "Module mapping for imports. Each key is an import name that will be mapped to the provided value. Used if modules are external (i.e. included as part of minecraft). Defaults to an empty object."
130130
},
131-
"sourceMapBias": {
132-
"type": "string",
133-
"description": "The bias to use when mapping from generated code to source code. Can be either 'leastUpperBound' or 'greatestLowerBound'. Defaults to 'leastUpperBound'."
134-
},
135131
"targetModuleUuid": {
136132
"type": "string",
137133
"description": "The script module uuid from the manifest.json of the Minecraft Add-On being debugged. Necessary if there are multiple Add-Ons active."

src/session.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ interface IAttachRequestArguments extends DebugProtocol.AttachRequestArguments {
7474
port?: number;
7575
inputPort?: string;
7676
moduleMapping?: ModuleMapping;
77-
sourceMapBias?: string;
7877
targetModuleUuid?: string;
7978
passcode?: string;
8079
}
@@ -135,7 +134,6 @@ export class Session extends DebugSession {
135134
private _generatedSourceRoot?: string;
136135
private _inlineSourceMap: boolean = false;
137136
private _moduleMapping?: ModuleMapping;
138-
private _sourceMapBias?: string;
139137
private _targetModuleUuid?: string;
140138
private _clientProtocolVersion: number = ProtocolVersion._Unknown; // determined after connection
141139
private _minecraftCapabilities: MinecraftCapabilities = { supportsCommands: false, supportsProfiler: false };
@@ -301,7 +299,6 @@ export class Session extends DebugSession {
301299
this._sourceMapRoot = args.sourceMapRoot ? path.normalize(args.sourceMapRoot) : undefined;
302300
this._generatedSourceRoot = args.generatedSourceRoot ? path.normalize(args.generatedSourceRoot) : undefined;
303301
this._moduleMapping = args.moduleMapping;
304-
this._sourceMapBias = args.sourceMapBias;
305302

306303
// Listen or connect (default), depending on mode.
307304
// Attach makes more sense to use connect, but some MC platforms require using listen.
@@ -688,8 +685,7 @@ export class Session extends DebugSession {
688685
this._localRoot,
689686
this._sourceMapRoot,
690687
this._generatedSourceRoot,
691-
this._inlineSourceMap,
692-
this._sourceMapBias
688+
this._inlineSourceMap
693689
);
694690

695691
// watch for source map changes

src/source-maps.ts

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -178,26 +178,16 @@ export class SourceMaps {
178178
private _sourceMapRoot?: string;
179179
private _sourceMapCache: SourceMapCache;
180180
private _sourceMapBias: number = SourceMapConsumer.LEAST_UPPER_BOUND;
181-
private _sourceMapBiasAlternate: number = SourceMapConsumer.GREATEST_LOWER_BOUND;
182181

183182
public constructor(
184183
localRoot: string,
185184
sourceMapRoot?: string,
186185
generatedSourceRoot?: string,
187-
inlineSourceMap: boolean = false,
188-
sourceMapBias?: string
186+
inlineSourceMap: boolean = false
189187
) {
190188
this._localRoot = path.normalize(localRoot);
191189
this._sourceMapRoot = sourceMapRoot ? path.normalize(sourceMapRoot) : undefined;
192190
this._sourceMapCache = new SourceMapCache(this._sourceMapRoot, generatedSourceRoot, inlineSourceMap);
193-
this._sourceMapBias =
194-
sourceMapBias === 'greatestLowerBound'
195-
? SourceMapConsumer.GREATEST_LOWER_BOUND
196-
: SourceMapConsumer.LEAST_UPPER_BOUND;
197-
this._sourceMapBiasAlternate =
198-
this._sourceMapBias === SourceMapConsumer.LEAST_UPPER_BOUND
199-
? SourceMapConsumer.GREATEST_LOWER_BOUND
200-
: SourceMapConsumer.LEAST_UPPER_BOUND;
201191
}
202192

203193
public clearCache() {
@@ -231,17 +221,20 @@ export class SourceMaps {
231221
source: mapInfo.preferAbsolute ? mapInfo.sourceAbsolutePath : mapInfo.originalSourceRelativePath,
232222
line: originalPosition.line,
233223
column: originalPosition.column,
234-
bias: this._sourceMapBias,
224+
bias: this._getSourceMapBias(),
235225
});
236226

237-
// if specified bias did not find a result, try the alternate
227+
// default bias did not find a result, try the alternate
238228
if (generatedPosition.line === null) {
239229
generatedPosition = mapInfo.sourceMap.generatedPositionFor({
240230
source: mapInfo.preferAbsolute ? mapInfo.sourceAbsolutePath : mapInfo.originalSourceRelativePath,
241231
line: originalPosition.line,
242232
column: originalPosition.column,
243-
bias: this._sourceMapBiasAlternate,
233+
bias: this._getAlternateSourceMapBias(),
244234
});
235+
if (generatedPosition.line) {
236+
this._switchSourceMapBias(); // alternate worked, make it primary
237+
}
245238
}
246239

247240
return generatedPosition;
@@ -260,16 +253,19 @@ export class SourceMaps {
260253
let originalPos = mapInfo.sourceMap.originalPositionFor({
261254
line: generatedPosition.line,
262255
column: generatedPosition.column,
263-
bias: this._sourceMapBias,
256+
bias: this._getSourceMapBias(),
264257
});
265258

266-
// if specified bias did not find a result, try the alternate
259+
// default bias did not find a result, try the alternate
267260
if (originalPos.line === null) {
268261
originalPos = mapInfo.sourceMap.originalPositionFor({
269262
line: generatedPosition.line,
270263
column: generatedPosition.column,
271-
bias: this._sourceMapBiasAlternate,
264+
bias: this._getAlternateSourceMapBias(),
272265
});
266+
if (originalPos.line) {
267+
this._switchSourceMapBias();
268+
}
273269
}
274270

275271
if (originalPos.line !== null && originalPos.column !== null && originalPos.source !== null) {
@@ -287,4 +283,21 @@ export class SourceMaps {
287283
`Could not map original position for ${generatedPosition.source} at line ${generatedPosition.line}.`
288284
);
289285
}
286+
287+
private _getSourceMapBias() {
288+
return this._sourceMapBias;
289+
}
290+
291+
private _getAlternateSourceMapBias() {
292+
return this._sourceMapBias === SourceMapConsumer.LEAST_UPPER_BOUND
293+
? SourceMapConsumer.GREATEST_LOWER_BOUND
294+
: SourceMapConsumer.LEAST_UPPER_BOUND;
295+
}
296+
297+
private _switchSourceMapBias() {
298+
this._sourceMapBias =
299+
this._sourceMapBias === SourceMapConsumer.LEAST_UPPER_BOUND
300+
? SourceMapConsumer.GREATEST_LOWER_BOUND
301+
: SourceMapConsumer.LEAST_UPPER_BOUND;
302+
}
290303
}

0 commit comments

Comments
 (0)