Skip to content

Commit 4f0a569

Browse files
Copiloteleanorjboyd
andcommitted
Fix PowerShell conda activation to properly dot-source conda-hook.ps1
Co-authored-by: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com>
1 parent 7ce1f05 commit 4f0a569

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

src/managers/conda/condaUtils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,9 +547,8 @@ async function windowsExceptionGenerateConfig(
547547

548548
const ps1Hook = await getCondaHookPs1Path(condaFolder);
549549
traceVerbose(`PS1 hook path: ${ps1Hook ?? 'not found'}`);
550-
const activation = ps1Hook ? ps1Hook : sourceInitPath;
551550

552-
// For PowerShell, we need to dot-source the conda-hook.ps1 script
551+
// For PowerShell, we need to dot-source the conda-hook.ps1 script if available
553552
const pwshActivate = ps1Hook
554553
? [{ executable: '.', args: [ps1Hook] }, { executable: 'conda', args: ['activate', prefix] }]
555554
: [{ executable: sourceInitPath }, { executable: 'conda', args: ['activate', prefix] }];

src/test/features/terminal/shells/common/shellUtils.unit.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import * as assert from 'assert';
22
import {
33
extractProfilePath,
4+
getShellCommandAsString,
45
PROFILE_TAG_END,
56
PROFILE_TAG_START,
67
} from '../../../../../features/terminal/shells/common/shellUtils';
8+
import { ShellConstants } from '../../../../../features/common/shellConstants';
79

810
suite('Shell Utils', () => {
911
suite('extractProfilePath', () => {
@@ -77,4 +79,41 @@ suite('Shell Utils', () => {
7779
assert.strictEqual(result, expectedPath);
7880
});
7981
});
82+
83+
suite('getShellCommandAsString PowerShell Conda Activation', () => {
84+
test('should format PowerShell conda activation with dot-sourcing correctly', () => {
85+
const command = [
86+
{ executable: '.', args: ['/path/to/conda-hook.ps1'] },
87+
{ executable: 'conda', args: ['activate', 'myenv'] }
88+
];
89+
const result = getShellCommandAsString(ShellConstants.PWSH, command);
90+
assert.strictEqual(result, '(. /path/to/conda-hook.ps1) ; (conda activate myenv)');
91+
});
92+
93+
test('should format PowerShell conda activation with spaces in path correctly', () => {
94+
const command = [
95+
{ executable: '.', args: ['/path with spaces/conda-hook.ps1'] },
96+
{ executable: 'conda', args: ['activate', 'my env'] }
97+
];
98+
const result = getShellCommandAsString(ShellConstants.PWSH, command);
99+
assert.strictEqual(result, '(. "/path with spaces/conda-hook.ps1") ; (conda activate "my env")');
100+
});
101+
102+
test('should format PowerShell conda activation fallback correctly', () => {
103+
const command = [
104+
{ executable: '/path/to/activate.bat' },
105+
{ executable: 'conda', args: ['activate', 'myenv'] }
106+
];
107+
const result = getShellCommandAsString(ShellConstants.PWSH, command);
108+
assert.strictEqual(result, '(/path/to/activate.bat) ; (conda activate myenv)');
109+
});
110+
111+
test('should format single PowerShell command without parentheses', () => {
112+
const command = [
113+
{ executable: 'conda', args: ['activate', 'myenv'] }
114+
];
115+
const result = getShellCommandAsString(ShellConstants.PWSH, command);
116+
assert.strictEqual(result, 'conda activate myenv');
117+
});
118+
});
80119
});

0 commit comments

Comments
 (0)