Skip to content

Commit a975862

Browse files
authored
Use workspaceFolder token instead of workspaceRoot (#267)
Fixes #258 * use workspaceFolder token instead of workspaceRoot * fix typo * updated to properly describe intent of test
1 parent f37fe13 commit a975862

File tree

9 files changed

+148
-92
lines changed

9 files changed

+148
-92
lines changed

.vscode/launch.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,44 @@
88
"request": "launch",
99
"runtimeExecutable": "${execPath}",
1010
"args": [
11-
"--extensionDevelopmentPath=${workspaceRoot}"
11+
"--extensionDevelopmentPath=${workspaceFolder}"
1212
],
1313
"stopOnEntry": false,
1414
"sourceMaps": true,
1515
"outFiles": [
16-
"${workspaceRoot}/out/**/*.js"
16+
"${workspaceFolder}/out/**/*.js"
1717
],
1818
"preLaunchTask": "compile"
1919
},
2020
{
2121
"name": "Launch Extension as debugServer", // https://code.visualstudio.com/docs/extensions/example-debuggers
2222
"type": "node",
2323
"request": "launch",
24-
"program": "${workspaceRoot}/out/client/debugger/Main.js",
24+
"program": "${workspaceFolder}/out/client/debugger/Main.js",
2525
"stopOnEntry": false,
2626
"args": [
2727
"--server=4711"
2828
],
2929
"sourceMaps": true,
3030
"outFiles": [
31-
"${workspaceRoot}/out/client/**/*.js"
31+
"${workspaceFolder}/out/client/**/*.js"
3232
],
33-
"cwd": "${workspaceRoot}"
33+
"cwd": "${workspaceFolder}"
3434
},
3535
{
3636
"name": "Launch Tests",
3737
"type": "extensionHost",
3838
"request": "launch",
3939
"runtimeExecutable": "${execPath}",
4040
"args": [
41-
"${workspaceRoot}/src/test",
42-
"--extensionDevelopmentPath=${workspaceRoot}",
43-
"--extensionTestsPath=${workspaceRoot}/out/test"
41+
"${workspaceFolder}/src/test",
42+
"--extensionDevelopmentPath=${workspaceFolder}",
43+
"--extensionTestsPath=${workspaceFolder}/out/test"
4444
],
4545
"stopOnEntry": false,
4646
"sourceMaps": true,
4747
"outFiles": [
48-
"${workspaceRoot}/out/**/*.js"
48+
"${workspaceFolder}/out/**/*.js"
4949
],
5050
"preLaunchTask": "compile"
5151
},
@@ -55,14 +55,14 @@
5555
"request": "launch",
5656
"runtimeExecutable": "${execPath}",
5757
"args": [
58-
"${workspaceRoot}/src/testMultiRootWkspc/multi.code-workspace",
59-
"--extensionDevelopmentPath=${workspaceRoot}",
60-
"--extensionTestsPath=${workspaceRoot}/out/test"
58+
"${workspaceFolder}/src/testMultiRootWkspc/multi.code-workspace",
59+
"--extensionDevelopmentPath=${workspaceFolder}",
60+
"--extensionTestsPath=${workspaceFolder}/out/test"
6161
],
6262
"stopOnEntry": false,
6363
"sourceMaps": true,
6464
"outFiles": [
65-
"${workspaceRoot}/out/**/*.js"
65+
"${workspaceFolder}/out/**/*.js"
6666
],
6767
"preLaunchTask": "compile"
6868
}

package.json

Lines changed: 55 additions & 55 deletions
Large diffs are not rendered by default.

src/client/common/systemVariables.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,27 +131,35 @@ export abstract class AbstractSystemVariables implements ISystemVariables {
131131

132132

133133
export class SystemVariables extends AbstractSystemVariables {
134-
private _workspaceRoot: string;
135-
private _workspaceRootFolderName: string;
134+
private _workspaceFolder: string;
135+
private _workspaceFolderName: string;
136136

137-
constructor(workspaceRoot?: string) {
137+
constructor(workspaceFolder?: string) {
138138
super();
139-
this._workspaceRoot = typeof workspaceRoot === 'string' ? workspaceRoot : __dirname;
140-
this._workspaceRootFolderName = Path.basename(this._workspaceRoot);
139+
this._workspaceFolder = typeof workspaceFolder === 'string' ? workspaceFolder : __dirname;
140+
this._workspaceFolderName = Path.basename(this._workspaceFolder);
141141
Object.keys(process.env).forEach(key => {
142142
this[`env:${key}`] = this[`env.${key}`] = process.env[key];
143143
});
144144
}
145145

146146
public get cwd(): string {
147-
return this.workspaceRoot;
147+
return this.workspaceFolder;
148148
}
149149

150150
public get workspaceRoot(): string {
151-
return this._workspaceRoot;
151+
return this._workspaceFolder;
152+
}
153+
154+
public get workspaceFolder(): string {
155+
return this._workspaceFolder;
152156
}
153157

154158
public get workspaceRootFolderName(): string {
155-
return this._workspaceRootFolderName;
159+
return this._workspaceFolderName;
160+
}
161+
162+
public get workspaceFolderBasename(): string {
163+
return this._workspaceFolderName;
156164
}
157165
}

src/client/interpreter/configuration/services/workspaceFolderUpdaterService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class WorkspaceFolderPythonPathUpdaterService implements IPythonPathUpdat
1414
}
1515
if (pythonPath.startsWith(this.workspaceFolder.fsPath)) {
1616
// tslint:disable-next-line:no-invalid-template-strings
17-
pythonPath = path.join('${workspaceRoot}', path.relative(this.workspaceFolder.fsPath, pythonPath));
17+
pythonPath = path.join('${workspaceFolder}', path.relative(this.workspaceFolder.fsPath, pythonPath));
1818
}
1919
await pythonConfig.update('pythonPath', pythonPath, ConfigurationTarget.WorkspaceFolder);
2020
}

src/client/interpreter/configuration/services/workspaceUpdaterService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class WorkspacePythonPathUpdaterService implements IPythonPathUpdaterServ
1414
}
1515
if (pythonPath.startsWith(this.wkspace.fsPath)) {
1616
// tslint:disable-next-line:no-invalid-template-strings
17-
pythonPath = path.join('${workspaceRoot}', path.relative(this.wkspace.fsPath, pythonPath));
17+
pythonPath = path.join('${workspaceFolder}', path.relative(this.wkspace.fsPath, pythonPath));
1818
}
1919
await pythonConfig.update('pythonPath', pythonPath, false);
2020
}

src/test/common/configSettings.multiroot.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ suite('Multiroot Config Settings', () => {
162162
});
163163

164164
// tslint:disable-next-line:no-invalid-template-strings
165-
test('${workspaceRoot} variable in settings should be replaced with the right value', async () => {
165+
test('${workspaceFolder} variable in settings should be replaced with the right value', async () => {
166166
const workspace2Uri = Uri.file(path.join(multirootPath, 'workspace2'));
167167
let fileToOpen = path.join(workspace2Uri.fsPath, 'file.py');
168168

src/test/interpreters/pythonPathUpdater.multiroot.test.ts

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as assert from 'assert';
22
import * as path from 'path';
33
import { ConfigurationTarget, Uri, workspace } from 'vscode';
4+
import { PythonSettings } from '../../client/common/configSettings';
45
import { PythonPathUpdaterService } from '../../client/interpreter/configuration/pythonPathUpdaterService';
56
import { PythonPathUpdaterServiceFactory } from '../../client/interpreter/configuration/pythonPathUpdaterServiceFactory';
67
import { WorkspaceFolderPythonPathUpdaterService } from '../../client/interpreter/configuration/services/workspaceFolderUpdaterService';
@@ -32,40 +33,82 @@ suite('Multiroot Python Path Settings Updater', () => {
3233

3334
test('Updating Workspace Folder Python Path should work', async () => {
3435
const workspaceUri = workspace3Uri;
35-
const workspaceUpdater = new WorkspaceFolderPythonPathUpdaterService(workspace.getWorkspaceFolder(workspaceUri).uri);
36+
const workspaceUpdater = new WorkspaceFolderPythonPathUpdaterService(workspace.getWorkspaceFolder(workspaceUri)!.uri);
3637
const pythonPath = `xWorkspacePythonPath${new Date().getMilliseconds()}`;
3738
await workspaceUpdater.updatePythonPath(pythonPath);
38-
const folderValue = workspace.getConfiguration('python', workspace3Uri).inspect('pythonPath').workspaceFolderValue;
39+
const folderValue = workspace.getConfiguration('python', workspace3Uri).inspect('pythonPath')!.workspaceFolderValue!;
3940
assert.equal(folderValue, pythonPath, 'Workspace Python Path not updated');
4041
});
4142

43+
test('Updating Workspace Folder Python Path when Python Path is in a sub directory of Workspace Folder', async () => {
44+
const workspaceUri = workspace3Uri;
45+
const workspaceFolder = workspace.getWorkspaceFolder(workspaceUri)!.uri;
46+
const workspaceUpdater = new WorkspaceFolderPythonPathUpdaterService(workspaceFolder);
47+
48+
// Python path within a sub directory of the workspace folder.
49+
const pythonExec = path.join('virtual Envs', 'env1', `xWorkspacePythonPath${new Date().getMilliseconds()}`);
50+
51+
// Expected path to python executable where ${workspaceFolder} token represents the fully qualified path to executable.
52+
// tslint:disable-next-line:no-invalid-template-strings
53+
const rawPythonPath = path.join('${workspaceFolder}', pythonExec);
54+
55+
// Fully qualified path to the python executable.
56+
const pythonPath = path.join(workspaceFolder.fsPath, pythonExec);
57+
await workspaceUpdater.updatePythonPath(pythonPath);
58+
PythonSettings.dispose();
59+
60+
const folderValue = workspace.getConfiguration('python', workspace3Uri).inspect('pythonPath')!.workspaceFolderValue!;
61+
assert.equal(folderValue, rawPythonPath, 'Raw Workspace Python Path not updated with a path relative to workspace folder');
62+
const resolvedPythonPath = PythonSettings.getInstance(workspaceUri).pythonPath;
63+
assert.equal(resolvedPythonPath, pythonPath, 'Resolved Workspace Python Path is incorrect');
64+
});
65+
4266
test('Updating Workspace Folder Python Path using the factor service should work', async () => {
4367
const workspaceUri = workspace3Uri;
4468
const factory = new PythonPathUpdaterServiceFactory();
45-
const workspaceUpdater = factory.getWorkspaceFolderPythonPathConfigurationService(workspace.getWorkspaceFolder(workspaceUri).uri);
69+
const workspaceUpdater = factory.getWorkspaceFolderPythonPathConfigurationService(workspace.getWorkspaceFolder(workspaceUri)!.uri);
4670
const pythonPath = `xWorkspacePythonPathFromFactory${new Date().getMilliseconds()}`;
4771
await workspaceUpdater.updatePythonPath(pythonPath);
48-
const folderValue = workspace.getConfiguration('python', workspace3Uri).inspect('pythonPath').workspaceFolderValue;
72+
const folderValue = workspace.getConfiguration('python', workspace3Uri).inspect('pythonPath')!.workspaceFolderValue!;
4973
assert.equal(folderValue, pythonPath, 'Workspace Python Path not updated');
5074
});
5175

5276
test('Updating Workspace Python Path using the PythonPathUpdaterService should work', async () => {
5377
const workspaceUri = workspace3Uri;
5478
const updaterService = new PythonPathUpdaterService(new PythonPathUpdaterServiceFactory(), new InterpreterVersionService());
5579
const pythonPath = `xWorkspacePythonPathFromUpdater${new Date().getMilliseconds()}`;
56-
await updaterService.updatePythonPath(pythonPath, ConfigurationTarget.WorkspaceFolder, 'ui', workspace.getWorkspaceFolder(workspaceUri).uri);
57-
const folderValue = workspace.getConfiguration('python', workspace3Uri).inspect('pythonPath').workspaceFolderValue;
80+
await updaterService.updatePythonPath(pythonPath, ConfigurationTarget.WorkspaceFolder, 'ui', workspace.getWorkspaceFolder(workspaceUri)!.uri);
81+
const folderValue = workspace.getConfiguration('python', workspace3Uri).inspect('pythonPath')!.workspaceFolderValue!;
5882
assert.equal(folderValue, pythonPath, 'Workspace Python Path not updated');
5983
});
6084

61-
test('Python Path should be relative to workspace', async () => {
62-
const workspaceUri = workspace.getWorkspaceFolder(workspace3Uri).uri;
85+
// tslint:disable-next-line:no-invalid-template-strings
86+
test('Python Paths containing ${workspaceRoot} should be resolved as ${workspaceFolder}', async () => {
87+
const workspaceUri = workspace.getWorkspaceFolder(workspace3Uri)!.uri;
88+
const pythonInterpreter = `xWorkspacePythonPath${new Date().getMilliseconds()}`;
89+
// tslint:disable-next-line:no-invalid-template-strings
90+
const pythonPath = path.join('${workspaceRoot}', 'x', 'y', 'z', pythonInterpreter);
91+
const workspaceUpdater = new WorkspacePythonPathUpdaterService(workspaceUri);
92+
await workspaceUpdater.updatePythonPath(pythonPath);
93+
const workspaceValue = workspace.getConfiguration('python').inspect('pythonPath')!.workspaceValue!;
94+
const resolvedPythonPath = path.join(workspaceUri.fsPath, 'x', 'y', 'z', pythonInterpreter);
95+
// tslint:disable-next-line:no-invalid-template-strings
96+
assert.equal(workspaceValue, pythonPath, 'Workspace Python Path not updated');
97+
PythonSettings.dispose();
98+
assert.equal(PythonSettings.getInstance(workspace3Uri).pythonPath, resolvedPythonPath, 'Resolved Workspace Python Path is incorrect');
99+
});
100+
101+
// tslint:disable-next-line:no-invalid-template-strings
102+
test('Python Path should be relative to workspace when using ${workspaceFolder}', async () => {
103+
const workspaceUri = workspace.getWorkspaceFolder(workspace3Uri)!.uri;
63104
const pythonInterpreter = `xWorkspacePythonPath${new Date().getMilliseconds()}`;
64105
const pythonPath = path.join(workspaceUri.fsPath, 'x', 'y', 'z', pythonInterpreter);
65106
const workspaceUpdater = new WorkspacePythonPathUpdaterService(workspaceUri);
66107
await workspaceUpdater.updatePythonPath(pythonPath);
67-
const workspaceValue = workspace.getConfiguration('python').inspect('pythonPath').workspaceValue;
108+
const workspaceValue = workspace.getConfiguration('python').inspect('pythonPath')!.workspaceValue!;
68109
// tslint:disable-next-line:no-invalid-template-strings
69-
assert.equal(workspaceValue, path.join('${workspaceRoot}', 'x', 'y', 'z', pythonInterpreter), 'Workspace Python Path not updated');
110+
assert.equal(workspaceValue, path.join('${workspaceFolder}', 'x', 'y', 'z', pythonInterpreter), 'Workspace Python Path not updated');
111+
PythonSettings.dispose();
112+
assert.equal(PythonSettings.getInstance(workspace3Uri).pythonPath, pythonPath, 'Resolved Workspace Python Path is incorrect');
70113
});
71114
});

src/test/interpreters/pythonPathUpdater.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import * as assert from 'assert';
22
import * as path from 'path';
33
import { ConfigurationTarget, Uri, workspace } from 'vscode';
4+
import { PythonSettings } from '../../client/common/configSettings';
45
import { PythonPathUpdaterService } from '../../client/interpreter/configuration/pythonPathUpdaterService';
56
import { PythonPathUpdaterServiceFactory } from '../../client/interpreter/configuration/pythonPathUpdaterServiceFactory';
7+
import { GlobalPythonPathUpdaterService } from '../../client/interpreter/configuration/services/globalUpdaterService';
68
import { WorkspacePythonPathUpdaterService } from '../../client/interpreter/configuration/services/workspaceUpdaterService';
79
import { InterpreterVersionService } from '../../client/interpreter/interpreterVersion';
810
import { closeActiveWindows, initialize, initializeTest } from '../initialize';
@@ -76,14 +78,17 @@ suite('Python Path Settings Updater', () => {
7678
assert.equal(workspaceValue, pythonPath, 'Workspace Python Path not updated');
7779
});
7880

79-
test('Python Path should be relative to workspace', async () => {
81+
test('Python Path should be relative to workspaceFolder', async () => {
8082
const workspaceUri = workspace.getWorkspaceFolder(Uri.file(workspaceRoot)).uri;
8183
const pythonInterpreter = `xWorkspacePythonPath${new Date().getMilliseconds()}`;
8284
const pythonPath = path.join(workspaceUri.fsPath, 'x', 'y', 'z', pythonInterpreter);
8385
const workspaceUpdater = new WorkspacePythonPathUpdaterService(workspaceUri);
8486
await workspaceUpdater.updatePythonPath(pythonPath);
8587
const workspaceValue = workspace.getConfiguration('python').inspect('pythonPath').workspaceValue;
8688
// tslint:disable-next-line:no-invalid-template-strings
87-
assert.equal(workspaceValue, path.join('${workspaceRoot}', 'x', 'y', 'z', pythonInterpreter), 'Workspace Python Path not updated');
89+
assert.equal(workspaceValue, path.join('${workspaceFolder}', 'x', 'y', 'z', pythonInterpreter), 'Workspace Python Path not updated');
90+
const resolvedPath = PythonSettings.getInstance(Uri.file(workspaceRoot)).pythonPath;
91+
assert.equal(resolvedPath, pythonPath, 'Resolved Workspace Python Path not updated');
8892
});
93+
8994
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"python.workspaceSymbols.tagFilePath": "${workspaceRoot}/workspace2.tags.file",
2+
"python.workspaceSymbols.tagFilePath": "${workspaceFolder}/workspace2.tags.file",
33
"python.workspaceSymbols.enabled": false
44
}

0 commit comments

Comments
 (0)