Skip to content

Commit ba52496

Browse files
authored
Resolve variables in the envFile of launch.json (microsoft#7386)
1 parent b9ce735 commit ba52496

4 files changed

Lines changed: 33 additions & 2 deletions

File tree

news/2 Fixes/7210.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Resolve variables such as `${workspaceFolder}` in the `envFile` setting of `launch.json`.

src/client/debugger/extension/configuration/resolvers/base.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ export abstract class BaseConfigurationResolver<T extends DebugConfiguration> im
5656
return editor.document.fileName;
5757
}
5858
}
59+
protected resolveAndUpdatePaths(workspaceFolder: Uri | undefined, debugConfiguration: LaunchRequestArguments): void {
60+
this.resolveAndUpdateEnvFilePath(workspaceFolder, debugConfiguration);
61+
this.resolveAndUpdatePythonPath(workspaceFolder, debugConfiguration);
62+
}
63+
protected resolveAndUpdateEnvFilePath(workspaceFolder: Uri | undefined, debugConfiguration: LaunchRequestArguments): void {
64+
if (!debugConfiguration) {
65+
return;
66+
}
67+
if (debugConfiguration.envFile && (workspaceFolder || debugConfiguration.cwd)) {
68+
const systemVariables = new SystemVariables((workspaceFolder ? workspaceFolder.fsPath : undefined) || debugConfiguration.cwd);
69+
debugConfiguration.envFile = systemVariables.resolveAny(debugConfiguration.envFile);
70+
}
71+
}
5972
protected resolveAndUpdatePythonPath(workspaceFolder: Uri | undefined, debugConfiguration: LaunchRequestArguments): void {
6073
if (!debugConfiguration) {
6174
return;
@@ -64,7 +77,7 @@ export abstract class BaseConfigurationResolver<T extends DebugConfiguration> im
6477
const pythonPath = this.configurationService.getSettings(workspaceFolder).pythonPath;
6578
debugConfiguration.pythonPath = pythonPath;
6679
this.pythonPathSource = PythonPathSource.settingsJson;
67-
} else{
80+
} else {
6881
this.pythonPathSource = PythonPathSource.launchJson;
6982
}
7083
}

src/client/debugger/extension/configuration/resolvers/launch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class LaunchConfigurationResolver extends BaseConfigurationResolver<Launc
5757
}
5858
// tslint:disable-next-line:cyclomatic-complexity
5959
protected async provideLaunchDefaults(workspaceFolder: Uri | undefined, debugConfiguration: LaunchRequestArguments): Promise<void> {
60-
this.resolveAndUpdatePythonPath(workspaceFolder, debugConfiguration);
60+
this.resolveAndUpdatePaths(workspaceFolder, debugConfiguration);
6161
if (typeof debugConfiguration.cwd !== 'string' && workspaceFolder) {
6262
debugConfiguration.cwd = workspaceFolder.fsPath;
6363
}

src/test/debugger/extension/configuration/resolvers/launch.unit.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,23 @@ getInfoPerOS().forEach(([osName, osType, path]) => {
661661
diagnosticsService.verifyAll();
662662
expect(debugConfig).to.not.be.equal(undefined, 'is undefined');
663663
});
664+
test('Resolve path to envFile', async () => {
665+
const pythonPath = `PythonPath_${new Date().toString()}`;
666+
const workspaceFolder = createMoqWorkspaceFolder(__dirname);
667+
const pythonFile = 'xyz.py';
668+
const expectedEnvFilePath = `${workspaceFolder.uri.fsPath}${osType === OSType.Windows ? '\\' : '/'}${'wow.envFile'}`;
669+
setupIoc(pythonPath);
670+
setupActiveEditor(pythonFile, PYTHON_LANGUAGE);
671+
672+
diagnosticsService.reset();
673+
diagnosticsService
674+
.setup(h => h.validatePythonPath(TypeMoq.It.isValue(pythonPath), TypeMoq.It.isAny(), TypeMoq.It.isAny()))
675+
.returns(() => Promise.resolve(true));
676+
677+
const debugConfig = await debugProvider.resolveDebugConfiguration!(workspaceFolder, { redirectOutput: false, pythonPath, envFile: path.join('${workspaceFolder}', 'wow.envFile') } as LaunchRequestArguments);
678+
679+
expect(debugConfig!.envFile).to.be.equal(expectedEnvFilePath);
680+
});
664681
async function testSetting(requestType: 'launch' | 'attach', settings: Record<string, boolean>, debugOptionName: DebugOptions, mustHaveDebugOption: boolean) {
665682
setupIoc('pythonPath');
666683
const debugConfiguration: DebugConfiguration = { request: requestType, type: 'python', name: '', ...settings };

0 commit comments

Comments
 (0)