Skip to content

fix: hook into the source map paths overriding in order to support platform specific and linked files #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 97 additions & 1 deletion src/debug-adapter/nativeScriptSourceMapTransformer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,102 @@
import { BaseSourceMapTransformer } from 'vscode-chrome-debug-core';
import * as path from 'path';
import { BaseSourceMapTransformer, logger as vsCodeChromeDebugLogger, utils as vsCodeChromeDebugUtils } from 'vscode-chrome-debug-core';
import * as sourceMapUtils from 'vscode-chrome-debug-core/out/src/sourceMaps/sourceMapUtils'; // tslint:disable-line
import { NativeScriptDebugAdapter } from './nativeScriptDebugAdapter';

// a workaround for https://github.com/NativeScript/nativescript-vscode-extension/issues/252
// tslint:disable
//--- begin part copied from vscode-chrome-debug-core (https://github.com/microsoft/vscode-chrome-debug-core/blob/d1fe8ca062e277a3480879e1fb63a8ef3b48015a/src/sourceMaps/sourceMapUtils.ts#L78)
//
// VS Code - Debugger for Chrome
//
// Copyright (c) Microsoft Corporation
//
// All rights reserved.
//
// MIT License
//
// 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:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// 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.

sourceMapUtils.applySourceMapPathOverrides = function applySourceMapPathOverrides(sourcePath, sourceMapPathOverrides, isVSClient = false) {
const forwardSlashSourcePath = sourcePath.replace(/\\/g, '/');
// Sort the overrides by length, large to small
const sortedOverrideKeys = Object.keys(sourceMapPathOverrides)
.sort((a, b) => b.length - a.length);

// Iterate the key/vals, only apply the first one that matches.
for (const leftPattern of sortedOverrideKeys) {
const rightPattern = sourceMapPathOverrides[leftPattern];
const entryStr = `"${leftPattern}": "${rightPattern}"`;
const asterisks = leftPattern.match(/\*/g) || [];

if (asterisks.length > 1) {
vsCodeChromeDebugLogger.log(`Warning: only one asterisk allowed in a sourceMapPathOverrides entry - ${entryStr}`);
continue;
}
const replacePatternAsterisks = rightPattern.match(/\*/g) || [];

if (replacePatternAsterisks.length > asterisks.length) {
vsCodeChromeDebugLogger.log(`Warning: the right side of a sourceMapPathOverrides entry must have 0 or 1 asterisks - ${entryStr}}`);
continue;
}
// Does it match?
const escapedLeftPattern = vsCodeChromeDebugUtils.escapeRegexSpecialChars(leftPattern, '/*');
const leftRegexSegment = escapedLeftPattern
.replace(/\*/g, '(.*)')
.replace(/\\\\/g, '/');
const leftRegex = new RegExp(`^${leftRegexSegment}$`, 'i');
const overridePatternMatches = forwardSlashSourcePath.match(leftRegex);

if (!overridePatternMatches) {
continue;
}
// Grab the value of the wildcard from the match above, replace the wildcard in the
// replacement pattern, and return the result.
const wildcardValue = overridePatternMatches[1];

// ***************** CUSTOM CODE START *****************
// handle linked files
let mappedPath = path.isAbsolute(wildcardValue) ? wildcardValue : rightPattern.replace(/\*/g, wildcardValue);
// ***************** CUSTOM CODE END *****************

mappedPath = path.join(mappedPath); // Fix any ..

// ***************** CUSTOM CODE START *****************
// handle platform-specific files
const platform = 'android';
const { dir, name, ext } = path.parse(mappedPath);

const platformFileName = `${name}.${platform}${ext}`;
const platformPath = path.join(dir, platformFileName);

if (vsCodeChromeDebugUtils.existsSync(platformPath)) {
mappedPath = platformPath;
}
// ***************** CUSTOM CODE END *****************

if (isVSClient && leftPattern === 'webpack:///./*' && !vsCodeChromeDebugUtils.existsSync(mappedPath)) {
// This is a workaround for a bug in ASP.NET debugging in VisualStudio because the wwwroot is not properly configured
const pathFixingASPNETBug = path.join(rightPattern.replace(/\*/g, path.join('../ClientApp', wildcardValue)));

if (vsCodeChromeDebugUtils.existsSync(pathFixingASPNETBug)) {
mappedPath = pathFixingASPNETBug;
}
}
vsCodeChromeDebugLogger.log(`SourceMap: mapping ${sourcePath} => ${mappedPath}, via sourceMapPathOverrides entry - ${entryStr}`);

return mappedPath;
}

return sourcePath;
};

//--- end part copied from vscode-chrome-debug-core
// tslint:enable

export class NativeScriptSourceMapTransformer extends BaseSourceMapTransformer {
private debugAdapter: NativeScriptDebugAdapter;

Expand Down