Skip to content

Commit fdc029c

Browse files
Merge pull request #204 from NativeScript/vladimirov/fix-debug-nsconfig
fix: debugging is not working when app path is changed
2 parents 315503a + 73e1288 commit fdc029c

7 files changed

+46
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
0.8.2
2+
=====
3+
## Bug Fixes
4+
- [Unable to debug when project's app directory is renamed](https://github.com/NativeScript/nativescript-vscode-extension/issues/205)
5+
16
0.8.1
27
=====
38
## Bug Fixes

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript",
3-
"version": "0.8.1",
3+
"version": "0.8.2",
44
"minNativescriptCliVersion": "2.5.0",
55
"icon": "images/icon.png",
66
"displayName": "NativeScript",

src/debug-adapter/nativeScriptDebugAdapter.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { existsSync, readFileSync } from 'fs';
2+
import { join } from 'path';
13
import { ChromeDebugAdapter, IRestartRequestArgs } from 'vscode-chrome-debug-core';
24
import { Event, TerminatedEvent } from 'vscode-debugadapter';
35
import { DebugProtocol } from 'vscode-debugprotocol';
@@ -108,14 +110,31 @@ export class NativeScriptDebugAdapter extends ChromeDebugAdapter {
108110
this._session.sendEvent(new TerminatedEvent());
109111
}
110112

111-
(this.pathTransformer as any).setTargetPlatform(args.platform);
113+
const appDirPath = this.getAppDirPath(transformedArgs.webRoot);
114+
115+
(this.pathTransformer as any).setTransformOptions(args.platform, appDirPath);
112116
(ChromeDebugAdapter as any).SET_BREAKPOINTS_TIMEOUT = 20000;
113117

114118
this.isLiveSync = args.watch;
115119

116120
return super.attach(transformedArgs);
117121
}
118122

123+
private getAppDirPath(webRoot: string): string {
124+
const pathToNsconfig = join(webRoot, 'nsconfig.json');
125+
126+
if (existsSync(pathToNsconfig)) {
127+
try {
128+
const content = readFileSync(pathToNsconfig).toString();
129+
const jsonContent = JSON.parse(content);
130+
131+
return jsonContent.appPath;
132+
} catch (err) {
133+
// Ignore the error for the moment
134+
}
135+
}
136+
}
137+
119138
private translateArgs(args): any {
120139
if (args.diagnosticLogging) {
121140
args.trace = args.diagnosticLogging;

src/debug-adapter/nativeScriptPathTransformer.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ export class NativeScriptPathTransformer extends UrlPathTransformer {
1010
};
1111

1212
private targetPlatform: string;
13+
private appDirPath: string;
1314

14-
public setTargetPlatform(targetPlatform: string) {
15+
public setTransformOptions(targetPlatform: string, appDirPath: string) {
1516
this.targetPlatform = targetPlatform.toLowerCase();
17+
this.appDirPath = appDirPath;
1618
}
1719

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

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

47+
if (this.appDirPath) {
48+
relativePath = relativePath.replace('app', this.appDirPath);
49+
}
50+
4551
const absolutePath = path.resolve(path.join(webRoot, relativePath));
4652

4753
if (fs.existsSync(absolutePath)) {

src/tests/nativeScriptDebugAdapter.tests.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe('NativeScriptDebugAdapter', () => {
5858
pathTransformerMock = {
5959
attach: () => ({}),
6060
clearTargetContext: () => ({}),
61-
setTargetPlatform: () => ({}),
61+
setTransformOptions: () => ({}),
6262
};
6363

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

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

9090
await nativeScriptDebugAdapter[method](argsMock);
9191

src/tests/nativeScriptPathTransformer.tests.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ describe('NativeScriptPathTransformer', () => {
1717
const webRoot = 'C:\\projectpath';
1818

1919
for (const test of tests as any) {
20-
it(`should transform [${test.platform}] device path ${test.scriptUrl} -> ${test.expectedResult}`, async () => {
20+
const nsConfigPartInTestName = test.nsconfig ? " when there's nsconfig" : '';
21+
22+
it(`should transform [${test.platform}] device path ${test.scriptUrl} -> ${test.expectedResult}${nsConfigPartInTestName}`, async () => {
2123
(path as any).join = path.win32.join;
2224
(path as any).resolve = path.win32.resolve;
23-
nativeScriptPathTransformer.setTargetPlatform(test.platform);
24-
existsSyncStub = sinon.stub(fs, 'existsSync').callsFake((arg: string) => arg === test.existingPath);
25+
nativeScriptPathTransformer.setTransformOptions(test.platform, test.nsconfig ? test.nsconfig.appPath : null);
2526

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

2832
assert.equal(result, test.expectedResult);

src/tests/pathTransformData.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ const tests = [
1515
{ 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' },
1616
{ 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' },
1717
{ 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' },
18+
{ 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' },
19+
{ 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' },
20+
{ 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' },
1821
];
1922

2023
export = tests;

0 commit comments

Comments
 (0)