Skip to content

Commit f2c73f8

Browse files
committed
fix: address second round of review feedback
1 parent 521578b commit f2c73f8

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

src/managers/conda/condaUtils.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -512,10 +512,10 @@ async function buildShellActivationMapForConda(
512512
// P3: Handle Windows specifically ;this is carryover from vscode-python
513513
if (isWindows()) {
514514
logs.push('✓ Using Windows-specific activation configuration');
515-
// Get conda.sh for bash-based shells (Git Bash, WSL bash)
515+
// Get conda.sh for bash-based shells on Windows (e.g., Git Bash)
516516
const condaShPath = envManager.sourcingInformation.shellSourcingScripts?.sh;
517517
if (!condaShPath) {
518-
logs.push('conda.sh not found, falling back to global sourcing script for bash activation');
518+
logs.push('conda.sh not found, using preferred sourcing script path for bash activation');
519519
}
520520
shellMaps = await windowsExceptionGenerateConfig(
521521
preferredSourcingPath,
@@ -607,14 +607,24 @@ export async function windowsExceptionGenerateConfig(
607607

608608
// When condaShPath is available, it is an initialization script (conda.sh) and does not
609609
// itself activate an environment. In that case, first source conda.sh, then
610-
// run "conda activate <envIdentifier>". When falling back to sourceInitPath,
611-
// retain the existing single "source <activate-script> <env>" behavior.
612-
const bashActivate: PythonCommandRunConfiguration[] = condaShPath
613-
? [
614-
{ executable: 'source', args: [condaShPath.replace(/\\/g, '/')] },
615-
{ executable: 'conda', args: ['activate', quotedPrefix] },
616-
]
617-
: [{ executable: 'source', args: [sourceInitPath.replace(/\\/g, '/'), quotedPrefix] }];
610+
// run "conda activate <envIdentifier>".
611+
// When falling back to sourceInitPath, only emit a bash "source" command if the script
612+
// is bash-compatible; on Windows, sourceInitPath may point to "activate.bat", which
613+
// cannot be sourced by Git Bash, so in that case we skip emitting a Git Bash activation.
614+
let bashActivate: PythonCommandRunConfiguration[];
615+
if (condaShPath) {
616+
bashActivate = [
617+
{ executable: 'source', args: [condaShPath.replace(/\\/g, '/')] },
618+
{ executable: 'conda', args: ['activate', quotedPrefix] },
619+
];
620+
} else if (sourceInitPath.toLowerCase().endsWith('.bat')) {
621+
traceVerbose(
622+
`Skipping Git Bash activation fallback because sourceInitPath is a batch script: ${sourceInitPath}`,
623+
);
624+
bashActivate = [];
625+
} else {
626+
bashActivate = [{ executable: 'source', args: [sourceInitPath.replace(/\\/g, '/'), quotedPrefix] }];
627+
}
618628
traceVerbose(
619629
`Windows activation commands:
620630
PowerShell: ${JSON.stringify(pwshActivate)},

src/test/managers/conda/condaUtils.windowsActivation.unit.test.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import { windowsExceptionGenerateConfig } from '../../../managers/conda/condaUti
99
*
1010
* Key behavior tested:
1111
* - Git Bash uses conda.sh (initialization script) + conda activate when condaShPath is available
12-
* - Git Bash falls back to source <activate-script> <env> when condaShPath is not available
12+
* - Git Bash skips activation when condaShPath is not available and sourceInitPath is .bat
13+
* - Git Bash falls back to source <activate-script> <env> when condaShPath is not available and source is not .bat
1314
* - PowerShell uses ps1 hook + conda activate
1415
* - CMD uses activate.bat + conda activate
1516
*/
@@ -51,8 +52,8 @@ suite('Conda Utils - windowsExceptionGenerateConfig', () => {
5152
assert.deepStrictEqual(gitBashActivation[1].args, ['activate', 'myenv']);
5253
});
5354

54-
test('Falls back to single source command when condaShPath is undefined', async () => {
55-
// Arrange
55+
test('Skips Git Bash activation when condaShPath is undefined and sourceInitPath is .bat', async () => {
56+
// Arrange: sourceInitPath is a .bat file which Git Bash cannot source
5657
getCondaHookPs1PathStub.resolves(undefined);
5758
const sourceInitPath = 'C:\\conda\\Scripts\\activate.bat';
5859
const prefix = 'myenv';
@@ -62,14 +63,35 @@ suite('Conda Utils - windowsExceptionGenerateConfig', () => {
6263
// Act
6364
const result = await windowsExceptionGenerateConfig(sourceInitPath, prefix, condaFolder, condaShPath);
6465

66+
// Assert: Git Bash activation should be empty since .bat cannot be sourced
67+
const gitBashActivation = result.shellActivation.get(ShellConstants.GITBASH);
68+
assert.ok(gitBashActivation, 'Git Bash activation should be defined');
69+
assert.strictEqual(
70+
gitBashActivation.length,
71+
0,
72+
'Git Bash activation should be empty when sourceInitPath is .bat',
73+
);
74+
});
75+
76+
test('Falls back to single source command when condaShPath is undefined and source is not .bat', async () => {
77+
// Arrange: sourceInitPath is a bash-compatible script (no .bat extension)
78+
getCondaHookPs1PathStub.resolves(undefined);
79+
const sourceInitPath = 'C:\\conda\\Scripts\\activate'; // No .bat extension
80+
const prefix = 'myenv';
81+
const condaFolder = 'C:\\conda';
82+
const condaShPath = undefined; // Not available
83+
84+
// Act
85+
const result = await windowsExceptionGenerateConfig(sourceInitPath, prefix, condaFolder, condaShPath);
86+
6587
// Assert
6688
const gitBashActivation = result.shellActivation.get(ShellConstants.GITBASH);
6789
assert.ok(gitBashActivation, 'Git Bash activation should be defined');
68-
assert.strictEqual(gitBashActivation.length, 1, 'Should have 1 command when condaShPath not available');
90+
assert.strictEqual(gitBashActivation.length, 1, 'Should have 1 command when source is bash-compatible');
6991

7092
// Single command: source <activate-script> <env>
7193
assert.strictEqual(gitBashActivation[0].executable, 'source');
72-
assert.deepStrictEqual(gitBashActivation[0].args, ['C:/conda/Scripts/activate.bat', 'myenv']);
94+
assert.deepStrictEqual(gitBashActivation[0].args, ['C:/conda/Scripts/activate', 'myenv']);
7395
});
7496

7597
test('Converts Windows backslashes to forward slashes for bash', async () => {

0 commit comments

Comments
 (0)