Skip to content

Commit aa30733

Browse files
committed
fix: hook into the source map paths overriding in order to support platform specific and linked files
1 parent 8d1c227 commit aa30733

File tree

1 file changed

+97
-1
lines changed

1 file changed

+97
-1
lines changed

src/debug-adapter/nativeScriptSourceMapTransformer.ts

+97-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,102 @@
1-
import { BaseSourceMapTransformer } from 'vscode-chrome-debug-core';
1+
import * as path from 'path';
2+
import { BaseSourceMapTransformer, logger as vsCodeChromeDebugLogger, utils as vsCodeChromeDebugUtils } from 'vscode-chrome-debug-core';
3+
import * as sourceMapUtils from 'vscode-chrome-debug-core/out/src/sourceMaps/sourceMapUtils'; // tslint:disable-line
24
import { NativeScriptDebugAdapter } from './nativeScriptDebugAdapter';
35

6+
// a workaround for https://github.com/NativeScript/nativescript-vscode-extension/issues/252
7+
// tslint:disable
8+
//--- begin part copied from vscode-chrome-debug-core (https://github.com/microsoft/vscode-chrome-debug-core/blob/d1fe8ca062e277a3480879e1fb63a8ef3b48015a/src/sourceMaps/sourceMapUtils.ts#L78)
9+
//
10+
// VS Code - Debugger for Chrome
11+
//
12+
// Copyright (c) Microsoft Corporation
13+
//
14+
// All rights reserved.
15+
//
16+
// MIT License
17+
//
18+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
19+
//
20+
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
21+
//
22+
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
24+
sourceMapUtils.applySourceMapPathOverrides = function applySourceMapPathOverrides(sourcePath, sourceMapPathOverrides, isVSClient = false) {
25+
const forwardSlashSourcePath = sourcePath.replace(/\\/g, '/');
26+
// Sort the overrides by length, large to small
27+
const sortedOverrideKeys = Object.keys(sourceMapPathOverrides)
28+
.sort((a, b) => b.length - a.length);
29+
30+
// Iterate the key/vals, only apply the first one that matches.
31+
for (const leftPattern of sortedOverrideKeys) {
32+
const rightPattern = sourceMapPathOverrides[leftPattern];
33+
const entryStr = `"${leftPattern}": "${rightPattern}"`;
34+
const asterisks = leftPattern.match(/\*/g) || [];
35+
36+
if (asterisks.length > 1) {
37+
vsCodeChromeDebugLogger.log(`Warning: only one asterisk allowed in a sourceMapPathOverrides entry - ${entryStr}`);
38+
continue;
39+
}
40+
const replacePatternAsterisks = rightPattern.match(/\*/g) || [];
41+
42+
if (replacePatternAsterisks.length > asterisks.length) {
43+
vsCodeChromeDebugLogger.log(`Warning: the right side of a sourceMapPathOverrides entry must have 0 or 1 asterisks - ${entryStr}}`);
44+
continue;
45+
}
46+
// Does it match?
47+
const escapedLeftPattern = vsCodeChromeDebugUtils.escapeRegexSpecialChars(leftPattern, '/*');
48+
const leftRegexSegment = escapedLeftPattern
49+
.replace(/\*/g, '(.*)')
50+
.replace(/\\\\/g, '/');
51+
const leftRegex = new RegExp(`^${leftRegexSegment}$`, 'i');
52+
const overridePatternMatches = forwardSlashSourcePath.match(leftRegex);
53+
54+
if (!overridePatternMatches) {
55+
continue;
56+
}
57+
// Grab the value of the wildcard from the match above, replace the wildcard in the
58+
// replacement pattern, and return the result.
59+
const wildcardValue = overridePatternMatches[1];
60+
61+
// ***************** CUSTOM CODE START *****************
62+
// handle linked files
63+
let mappedPath = path.isAbsolute(wildcardValue) ? wildcardValue : rightPattern.replace(/\*/g, wildcardValue);
64+
// ***************** CUSTOM CODE END *****************
65+
66+
mappedPath = path.join(mappedPath); // Fix any ..
67+
68+
// ***************** CUSTOM CODE START *****************
69+
// handle platform-specific files
70+
const platform = 'android';
71+
const { dir, name, ext } = path.parse(mappedPath);
72+
73+
const platformFileName = `${name}.${platform}${ext}`;
74+
const platformPath = path.join(dir, platformFileName);
75+
76+
if (vsCodeChromeDebugUtils.existsSync(platformPath)) {
77+
mappedPath = platformPath;
78+
}
79+
// ***************** CUSTOM CODE END *****************
80+
81+
if (isVSClient && leftPattern === 'webpack:///./*' && !vsCodeChromeDebugUtils.existsSync(mappedPath)) {
82+
// This is a workaround for a bug in ASP.NET debugging in VisualStudio because the wwwroot is not properly configured
83+
const pathFixingASPNETBug = path.join(rightPattern.replace(/\*/g, path.join('../ClientApp', wildcardValue)));
84+
85+
if (vsCodeChromeDebugUtils.existsSync(pathFixingASPNETBug)) {
86+
mappedPath = pathFixingASPNETBug;
87+
}
88+
}
89+
vsCodeChromeDebugLogger.log(`SourceMap: mapping ${sourcePath} => ${mappedPath}, via sourceMapPathOverrides entry - ${entryStr}`);
90+
91+
return mappedPath;
92+
}
93+
94+
return sourcePath;
95+
};
96+
97+
//--- end part copied from vscode-chrome-debug-core
98+
// tslint:enable
99+
4100
export class NativeScriptSourceMapTransformer extends BaseSourceMapTransformer {
5101
private debugAdapter: NativeScriptDebugAdapter;
6102

0 commit comments

Comments
 (0)