Skip to content

fix: replace a hardcoded platform in the source maps with a dynamic one #255

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 14, 2019
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/debug-adapter/nativeScriptDebugAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export class NativeScriptDebugAdapter extends ChromeDebugAdapter {
const appDirPath = this.getAppDirPath(transformedArgs.webRoot);

(this.pathTransformer as any).setTransformOptions(args.platform, appDirPath, transformedArgs.webRoot);
(this.sourceMapTransformer as NativeScriptSourceMapTransformer).setDebugAdapter(this);
(this.sourceMapTransformer as NativeScriptSourceMapTransformer).setTransformOptions(args.platform.toLowerCase(), this);
(ChromeDebugAdapter as any).SET_BREAKPOINTS_TIMEOUT = 20000;

this.isLiveSync = args.watch;
Expand Down
194 changes: 99 additions & 95 deletions src/debug-adapter/nativeScriptSourceMapTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,108 +3,17 @@ import { BaseSourceMapTransformer, logger as vsCodeChromeDebugLogger, utils as v
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;
private targetPlatform: string;

constructor(sourceHandles: any) {
super(sourceHandles);
this.setupSourceMapPathOverrides();
}

public setDebugAdapter(debugAdapter: NativeScriptDebugAdapter): void {
public setTransformOptions(targetPlatform: string, debugAdapter: NativeScriptDebugAdapter) {
this.targetPlatform = targetPlatform.toLowerCase();
this.debugAdapter = debugAdapter;
}

Expand All @@ -119,4 +28,99 @@ export class NativeScriptSourceMapTransformer extends BaseSourceMapTransformer {

return scriptParsedResult;
}

private setupSourceMapPathOverrides() {
// 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 = (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 { dir, name, ext } = path.parse(mappedPath);

const platformFileName = `${name}.${this.targetPlatform}${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
}
}
4 changes: 2 additions & 2 deletions src/services/iOSTeamService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class iOSTeamService {

for (const file of files) {
const filePath = path.join(dir, file);
const data = fs.readFileSync(filePath, {encoding: 'utf8'});
const data = fs.readFileSync(filePath, { encoding: 'utf8' });
const teamId = this.getProvisioningProfileValue('TeamIdentifier', data);
const teamName = this.getProvisioningProfileValue('TeamName', data);

Expand All @@ -64,7 +64,7 @@ export class iOSTeamService {
const teamIdsArray = new Array<{ id: string, name: string }>();

for (const teamId in teamIds) {
teamIdsArray.push({id: teamId, name: teamIds[teamId]});
teamIdsArray.push({ id: teamId, name: teamIds[teamId] });
}

return teamIdsArray;
Expand Down
4 changes: 2 additions & 2 deletions src/tests/nativeScriptDebugAdapter.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const defaultArgsMock: any = {
};

const mockConstructor = (mockObject: any): any => {
return function() {
return function () {
return mockObject;
};
};
Expand Down Expand Up @@ -64,7 +64,7 @@ describe('NativeScriptDebugAdapter', () => {

sourceMapTransformer = {
clearTargetContext: () => undefined,
setDebugAdapter: () => undefined,
setTransformOptions: () => undefined,
};

nativeScriptDebugAdapter = new NativeScriptDebugAdapter({
Expand Down
38 changes: 31 additions & 7 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@
"tslint:latest"
],
"rules": {
"quotemark": [true,
"space-before-function-paren": [
true,
{
"anonymous": "always",
"named": "never",
"asyncArrow": "always"
}
],
"quotemark": [
true,
"single",
"avoid-escape"
],
"whitespace": [true,
"whitespace": [
true,
"check-branch",
"check-decl",
"check-operator",
Expand All @@ -19,13 +29,27 @@
"newline-before-return": true,
"no-unused-variable": true,
"no-duplicate-variable": true,
"no-unused-expression": [true, "allow-fast-null-checks"],
"no-unused-expression": [
true,
"allow-fast-null-checks"
],
"ter-newline-after-var": true,
"max-line-length": [ true, 160 ],
"max-line-length": [
true,
160
],
"max-classes-per-file": false,
"variable-name": [ true, "allow-leading-underscore" ],
"variable-name": [
true,
"allow-leading-underscore"
],
"forin": false,
"no-implicit-dependencies": [true, "dev"],
"only-arrow-functions": [false]
"no-implicit-dependencies": [
true,
"dev"
],
"only-arrow-functions": [
false
]
}
}