Skip to content

Commit

Permalink
Merge pull request #204 from NativeScript/vladimirov/fix-debug-nsconfig
Browse files Browse the repository at this point in the history
fix: debugging is not working when app path is changed
  • Loading branch information
rosen-vladimirov authored Sep 12, 2018
2 parents 315503a + 73e1288 commit fdc029c
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.8.2
=====
## Bug Fixes
- [Unable to debug when project's app directory is renamed](https://github.com/NativeScript/nativescript-vscode-extension/issues/205)

0.8.1
=====
## Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nativescript",
"version": "0.8.1",
"version": "0.8.2",
"minNativescriptCliVersion": "2.5.0",
"icon": "images/icon.png",
"displayName": "NativeScript",
Expand Down
21 changes: 20 additions & 1 deletion src/debug-adapter/nativeScriptDebugAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { existsSync, readFileSync } from 'fs';
import { join } from 'path';
import { ChromeDebugAdapter, IRestartRequestArgs } from 'vscode-chrome-debug-core';
import { Event, TerminatedEvent } from 'vscode-debugadapter';
import { DebugProtocol } from 'vscode-debugprotocol';
Expand Down Expand Up @@ -108,14 +110,31 @@ export class NativeScriptDebugAdapter extends ChromeDebugAdapter {
this._session.sendEvent(new TerminatedEvent());
}

(this.pathTransformer as any).setTargetPlatform(args.platform);
const appDirPath = this.getAppDirPath(transformedArgs.webRoot);

(this.pathTransformer as any).setTransformOptions(args.platform, appDirPath);
(ChromeDebugAdapter as any).SET_BREAKPOINTS_TIMEOUT = 20000;

this.isLiveSync = args.watch;

return super.attach(transformedArgs);
}

private getAppDirPath(webRoot: string): string {
const pathToNsconfig = join(webRoot, 'nsconfig.json');

if (existsSync(pathToNsconfig)) {
try {
const content = readFileSync(pathToNsconfig).toString();
const jsonContent = JSON.parse(content);

return jsonContent.appPath;
} catch (err) {
// Ignore the error for the moment
}
}
}

private translateArgs(args): any {
if (args.diagnosticLogging) {
args.trace = args.diagnosticLogging;
Expand Down
8 changes: 7 additions & 1 deletion src/debug-adapter/nativeScriptPathTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ export class NativeScriptPathTransformer extends UrlPathTransformer {
};

private targetPlatform: string;
private appDirPath: string;

public setTargetPlatform(targetPlatform: string) {
public setTransformOptions(targetPlatform: string, appDirPath: string) {
this.targetPlatform = targetPlatform.toLowerCase();
this.appDirPath = appDirPath;
}

protected async targetUrlToClientPath(webRoot: string, scriptUrl: string): Promise<string> {
Expand Down Expand Up @@ -42,6 +44,10 @@ export class NativeScriptPathTransformer extends UrlPathTransformer {

relativePath = relativePath.replace('tns_modules', nodePath);

if (this.appDirPath) {
relativePath = relativePath.replace('app', this.appDirPath);
}

const absolutePath = path.resolve(path.join(webRoot, relativePath));

if (fs.existsSync(absolutePath)) {
Expand Down
6 changes: 3 additions & 3 deletions src/tests/nativeScriptDebugAdapter.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('NativeScriptDebugAdapter', () => {
pathTransformerMock = {
attach: () => ({}),
clearTargetContext: () => ({}),
setTargetPlatform: () => ({}),
setTransformOptions: () => ({}),
};

nativeScriptDebugAdapter = new NativeScriptDebugAdapter(
Expand All @@ -84,8 +84,8 @@ describe('NativeScriptDebugAdapter', () => {
sinon.assert.calledWith(spy, sinon.match({ event: extProtocol.BEFORE_DEBUG_START }));
});

it(`${method} for ${platform} should call project setTargetPlatform`, async () => {
const spy = sinon.spy(pathTransformerMock, 'setTargetPlatform');
it(`${method} for ${platform} should call project setTransformOptions`, async () => {
const spy = sinon.spy(pathTransformerMock, 'setTransformOptions');

await nativeScriptDebugAdapter[method](argsMock);

Expand Down
10 changes: 7 additions & 3 deletions src/tests/nativeScriptPathTransformer.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ describe('NativeScriptPathTransformer', () => {
const webRoot = 'C:\\projectpath';

for (const test of tests as any) {
it(`should transform [${test.platform}] device path ${test.scriptUrl} -> ${test.expectedResult}`, async () => {
const nsConfigPartInTestName = test.nsconfig ? " when there's nsconfig" : '';

it(`should transform [${test.platform}] device path ${test.scriptUrl} -> ${test.expectedResult}${nsConfigPartInTestName}`, async () => {
(path as any).join = path.win32.join;
(path as any).resolve = path.win32.resolve;
nativeScriptPathTransformer.setTargetPlatform(test.platform);
existsSyncStub = sinon.stub(fs, 'existsSync').callsFake((arg: string) => arg === test.existingPath);
nativeScriptPathTransformer.setTransformOptions(test.platform, test.nsconfig ? test.nsconfig.appPath : null);

existsSyncStub = sinon
.stub(fs, 'existsSync')
.callsFake((arg: string) => arg === test.existingPath);
const result = await nativeScriptPathTransformer.targetUrlToClientPath(webRoot, test.scriptUrl);

assert.equal(result, test.expectedResult);
Expand Down
3 changes: 3 additions & 0 deletions src/tests/pathTransformData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const tests = [
{ platform: 'android', scriptUrl: 'file:///data/data/org.nativescript.TabNavigation/files/app/tns_modules/tns-core-modules/ui/page/page.js', expectedResult: 'C:\\projectpath\\node_modules\\tns-core-modules\\ui\\page\\page.android.js', existingPath: 'C:\\projectpath\\node_modules\\tns-core-modules\\ui\\page\\page.android.js' },
{ platform: 'android', scriptUrl: 'file:///data/data/org.nativescript.TabNavigation/files/app/tns_modules/tns-core-modules/ui/layouts/layout-base.js', expectedResult: 'C:\\projectpath\\node_modules\\tns-core-modules\\ui\\layouts\\layout-base.android.js', existingPath: 'C:\\projectpath\\node_modules\\tns-core-modules\\ui\\layouts\\layout-base.android.js' },
{ platform: 'android', scriptUrl: 'ng:///css/0/data/data/org.nativescript.TabNavigation/files/app/tabs/tabs.component.scss.ngstyle.js', expectedResult: 'ng:///css/0/data/data/org.nativescript.TabNavigation/files/app/tabs/tabs.component.scss.ngstyle.js' },
{ platform: 'android', scriptUrl: 'file:///data/data/org.nativescript.TabNavigation/files/app/main.js', expectedResult: 'C:\\projectpath\\src\\main.js', nsconfig: { appPath: 'src' }, existingPath: 'C:\\projectpath\\src\\main.js' },
{ platform: 'android', scriptUrl: 'file:///data/data/org.nativescript.TabNavigation/files/app/app/main.js', expectedResult: 'C:\\projectpath\\src\\app\\main.js', nsconfig: { appPath: 'src' }, existingPath: 'C:\\projectpath\\src\\app\\main.js' },
{ platform: 'android', scriptUrl: 'file:///data/data/org.nativescript.app1/files/app/app/main.js', expectedResult: 'C:\\projectpath\\src\\app\\main.js', nsconfig: { appPath: 'src' }, existingPath: 'C:\\projectpath\\src\\app\\main.js' },
];

export = tests;

0 comments on commit fdc029c

Please sign in to comment.