Skip to content

Commit 79e8a13

Browse files
authored
Always use environment path when running conda environment commands (#24807)
Attempt at fixing #24585 There are many edge scenarious where refering to the name of the environment rather than the path can cause breaks in the extension. Some examples 1 -**If we have two anonymous environments with the same name in different folders** /path1/my-env /path2/my-env (where my active vscode python interpreter is) by using conda -n my-env it'll always use the first env. 2 - **Some times people avoid actually activating their conda envs when using conda-pack** https://github.com/conda/conda-pack This is because the activation scripts are known to be flaky and not very reliable 3 - **The environment may have been created by a conda-compliant replacement** Therefore conda itself is not aware of it by name but can work with it properly using the path. This is the case of [hawk](https://community.palantir.com/t/introducing-hawk-for-python-package-management-in-code-repositories/500) or frankly anyone building their own conda package manager on top of [rattler](https://github.com/conda/rattler). Some of these points are also hinted at #24627 (comment) , and supported by a conda maintainer in #24585 (comment) This PR has a minimal attempt at changing that by always forcing -p usage
1 parent b4aa112 commit 79e8a13

File tree

3 files changed

+9
-12
lines changed

3 files changed

+9
-12
lines changed

src/client/pythonEnvironments/common/environmentManagers/conda.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,8 @@ export class Conda {
564564
return undefined;
565565
}
566566
const args = [];
567-
if (env.name) {
568-
args.push('-n', env.name);
569-
} else {
570-
args.push('-p', env.prefix);
571-
}
567+
args.push('-p', env.prefix);
568+
572569
const python = [
573570
forShellExecution ? this.shellCommand : this.command,
574571
'run',

src/test/common/process/pythonEnvironment.unit.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -284,16 +284,16 @@ suite('CondaEnvironment', () => {
284284

285285
teardown(() => sinon.restore());
286286

287-
test('getExecutionInfo with a named environment should return execution info using the environment name', async () => {
287+
test('getExecutionInfo with a named environment should return execution info using the environment path', async () => {
288288
const condaInfo = { name: 'foo', path: 'bar' };
289289
const env = await createCondaEnv(condaInfo, processService.object, fileSystem.object);
290290

291291
const result = env?.getExecutionInfo(args, pythonPath);
292292

293293
expect(result).to.deep.equal({
294294
command: condaFile,
295-
args: ['run', '-n', condaInfo.name, '--no-capture-output', 'python', OUTPUT_MARKER_SCRIPT, ...args],
296-
python: [condaFile, 'run', '-n', condaInfo.name, '--no-capture-output', 'python', OUTPUT_MARKER_SCRIPT],
295+
args: ['run', '-p', condaInfo.path, '--no-capture-output', 'python', OUTPUT_MARKER_SCRIPT, ...args],
296+
python: [condaFile, 'run', '-p', condaInfo.path, '--no-capture-output', 'python', OUTPUT_MARKER_SCRIPT],
297297
pythonExecutable: pythonPath,
298298
});
299299
});
@@ -312,12 +312,12 @@ suite('CondaEnvironment', () => {
312312
});
313313
});
314314

315-
test('getExecutionObservableInfo with a named environment should return execution info using conda full path with the name', async () => {
315+
test('getExecutionObservableInfo with a named environment should return execution info using conda full path with the path', async () => {
316316
const condaInfo = { name: 'foo', path: 'bar' };
317317
const expected = {
318318
command: condaFile,
319-
args: ['run', '-n', condaInfo.name, '--no-capture-output', 'python', OUTPUT_MARKER_SCRIPT, ...args],
320-
python: [condaFile, 'run', '-n', condaInfo.name, '--no-capture-output', 'python', OUTPUT_MARKER_SCRIPT],
319+
args: ['run', '-p', condaInfo.path, '--no-capture-output', 'python', OUTPUT_MARKER_SCRIPT, ...args],
320+
python: [condaFile, 'run', '-p', condaInfo.path, '--no-capture-output', 'python', OUTPUT_MARKER_SCRIPT],
321321
pythonExecutable: pythonPath,
322322
};
323323
const env = await createCondaEnv(condaInfo, processService.object, fileSystem.object);

src/test/pythonEnvironments/common/environmentManagers/conda.unit.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ suite('Conda and its environments are located correctly', () => {
536536
expect(args).to.not.equal(undefined);
537537
assert.deepStrictEqual(
538538
args,
539-
['conda', 'run', '-n', 'envName', '--no-capture-output', 'python', OUTPUT_MARKER_SCRIPT],
539+
['conda', 'run', '-p', 'envPrefix', '--no-capture-output', 'python', OUTPUT_MARKER_SCRIPT],
540540
'Incorrect args for case 1',
541541
);
542542

0 commit comments

Comments
 (0)