diff --git a/src/managers/conda/condaUtils.ts b/src/managers/conda/condaUtils.ts index 418fb62e..056a5db2 100644 --- a/src/managers/conda/condaUtils.ts +++ b/src/managers/conda/condaUtils.ts @@ -547,9 +547,11 @@ async function windowsExceptionGenerateConfig( const ps1Hook = await getCondaHookPs1Path(condaFolder); traceVerbose(`PS1 hook path: ${ps1Hook ?? 'not found'}`); - const activation = ps1Hook ? ps1Hook : sourceInitPath; - const pwshActivate = [{ executable: activation }, { executable: 'conda', args: ['activate', prefix] }]; + // For PowerShell, we need to dot-source the conda-hook.ps1 script if available + const pwshActivate = ps1Hook + ? [{ executable: '.', args: [ps1Hook] }, { executable: 'conda', args: ['activate', prefix] }] + : [{ executable: sourceInitPath }, { executable: 'conda', args: ['activate', prefix] }]; const cmdActivate = [{ executable: sourceInitPath }, { executable: 'conda', args: ['activate', prefix] }]; const bashActivate = [{ executable: 'source', args: [sourceInitPath.replace(/\\/g, '/'), prefix] }]; diff --git a/src/test/features/terminal/shells/common/shellUtils.unit.test.ts b/src/test/features/terminal/shells/common/shellUtils.unit.test.ts index 12eedfdb..effcf119 100644 --- a/src/test/features/terminal/shells/common/shellUtils.unit.test.ts +++ b/src/test/features/terminal/shells/common/shellUtils.unit.test.ts @@ -1,9 +1,11 @@ import * as assert from 'assert'; import { extractProfilePath, + getShellCommandAsString, PROFILE_TAG_END, PROFILE_TAG_START, } from '../../../../../features/terminal/shells/common/shellUtils'; +import { ShellConstants } from '../../../../../features/common/shellConstants'; suite('Shell Utils', () => { suite('extractProfilePath', () => { @@ -77,4 +79,41 @@ suite('Shell Utils', () => { assert.strictEqual(result, expectedPath); }); }); + + suite('getShellCommandAsString PowerShell Conda Activation', () => { + test('should format PowerShell conda activation with dot-sourcing correctly', () => { + const command = [ + { executable: '.', args: ['/path/to/conda-hook.ps1'] }, + { executable: 'conda', args: ['activate', 'myenv'] } + ]; + const result = getShellCommandAsString(ShellConstants.PWSH, command); + assert.strictEqual(result, '(. /path/to/conda-hook.ps1) ; (conda activate myenv)'); + }); + + test('should format PowerShell conda activation with spaces in path correctly', () => { + const command = [ + { executable: '.', args: ['/path with spaces/conda-hook.ps1'] }, + { executable: 'conda', args: ['activate', 'my env'] } + ]; + const result = getShellCommandAsString(ShellConstants.PWSH, command); + assert.strictEqual(result, '(. "/path with spaces/conda-hook.ps1") ; (conda activate "my env")'); + }); + + test('should format PowerShell conda activation fallback correctly', () => { + const command = [ + { executable: '/path/to/activate.bat' }, + { executable: 'conda', args: ['activate', 'myenv'] } + ]; + const result = getShellCommandAsString(ShellConstants.PWSH, command); + assert.strictEqual(result, '(/path/to/activate.bat) ; (conda activate myenv)'); + }); + + test('should format single PowerShell command without parentheses', () => { + const command = [ + { executable: 'conda', args: ['activate', 'myenv'] } + ]; + const result = getShellCommandAsString(ShellConstants.PWSH, command); + assert.strictEqual(result, 'conda activate myenv'); + }); + }); });