diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ec790b..617d2d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/package.json b/package.json index b0fe3cd..bad8702 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nativescript", - "version": "0.8.1", + "version": "0.8.2", "minNativescriptCliVersion": "2.5.0", "icon": "images/icon.png", "displayName": "NativeScript", diff --git a/src/debug-adapter/nativeScriptDebugAdapter.ts b/src/debug-adapter/nativeScriptDebugAdapter.ts index 2e7b6cf..18c7ff2 100644 --- a/src/debug-adapter/nativeScriptDebugAdapter.ts +++ b/src/debug-adapter/nativeScriptDebugAdapter.ts @@ -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'; @@ -108,7 +110,9 @@ 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; @@ -116,6 +120,21 @@ export class NativeScriptDebugAdapter extends ChromeDebugAdapter { 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; diff --git a/src/debug-adapter/nativeScriptPathTransformer.ts b/src/debug-adapter/nativeScriptPathTransformer.ts index 49237f3..b0e3346 100644 --- a/src/debug-adapter/nativeScriptPathTransformer.ts +++ b/src/debug-adapter/nativeScriptPathTransformer.ts @@ -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 { @@ -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)) { diff --git a/src/tests/nativeScriptDebugAdapter.tests.ts b/src/tests/nativeScriptDebugAdapter.tests.ts index d46137a..49c7340 100644 --- a/src/tests/nativeScriptDebugAdapter.tests.ts +++ b/src/tests/nativeScriptDebugAdapter.tests.ts @@ -58,7 +58,7 @@ describe('NativeScriptDebugAdapter', () => { pathTransformerMock = { attach: () => ({}), clearTargetContext: () => ({}), - setTargetPlatform: () => ({}), + setTransformOptions: () => ({}), }; nativeScriptDebugAdapter = new NativeScriptDebugAdapter( @@ -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); diff --git a/src/tests/nativeScriptPathTransformer.tests.ts b/src/tests/nativeScriptPathTransformer.tests.ts index 691bd1b..e33d65b 100644 --- a/src/tests/nativeScriptPathTransformer.tests.ts +++ b/src/tests/nativeScriptPathTransformer.tests.ts @@ -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); diff --git a/src/tests/pathTransformData.ts b/src/tests/pathTransformData.ts index 4e0c978..e046e6d 100644 --- a/src/tests/pathTransformData.ts +++ b/src/tests/pathTransformData.ts @@ -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;