Commit 5d919c1
Fix Git Bash conda activation when conda init bash was run (microsoft#1370) (microsoft#1609)
Closes microsoft#1370
## Context
On Windows, when a user has run `conda init bash` and has
`auto_activate_base` enabled (conda default), opening a Git Bash
terminal in VS Code with auto-activation enabled causes every subsequent
`conda` command to fail with:
```
bash: /cygdrive/c/Users/<user>/anaconda3/Scripts/conda.exe: No such file or directory
```
The terminal still displays `(base)` and appears active, but conda is
silently broken until the user closes and reopens the terminal.
## Root cause
A chain of three independent systems creates the failure:
1. The user's `~/.bashrc` (modified by `conda init bash`) initializes
conda on shell startup — defines the `conda` shell function and sets
`CONDA_EXE` correctly.
2. `auto_activate_base = true` activates `(base)`, which prepends
`anaconda3/Library/usr/bin` to `PATH`. That directory ships **Anaconda's
own (Cygwin-style) `cygpath`**, which now shadows Git Bash's MSYS
`cygpath`. The two emit different path formats:
- Git Bash's `cygpath C:\foo` → `/c/foo` ✅ (Git Bash understands)
- Anaconda's `cygpath C:\foo` → `/cygdrive/c/foo` ❌ (Git Bash does NOT
understand)
3. The extension then injects:
```bash
source <conda>/etc/profile.d/conda.sh && conda activate <env>
```
The first line of `conda.sh` is:
```bash
export CONDA_EXE="$(cygpath 'C:/.../conda.exe')"
```
Re-sourcing now runs the shadowed Cygwin `cygpath`, producing
`/cygdrive/c/...`. `CONDA_EXE` is corrupted. The `conda` shell function
(which delegates to `$CONDA_EXE`) fails for every subsequent invocation.
The corruption is entirely caused by **re-sourcing `conda.sh` in a
terminal that already initialized conda**. If we skip the re-sourcing,
the original (correct) `CONDA_EXE` remains intact and conda keeps
working.
## Why the existing safeguards do not catch this
`buildShellActivationMapForConda` has a P1 short-circuit that skips
re-sourcing when `isActiveOnLaunch` is true. That flag is derived from
`process.env.CONDA_SHLVL` **at VS Code startup**. The user launches VS
Code normally (not from a conda-active shell), so `CONDA_SHLVL` is unset
at the VS Code process level and the safeguard never fires — even though
conda will be active inside every new terminal.
The extension already has a separate signal that *does* fire in this
scenario: `checkCondaInitInShellProfiles()` reads the user's shell
profiles and reports `shellInitStatus.bash = true` when `conda
initialize` is present. The non-Windows path already accepts this
signal, but only uses it as a fallback when sourcing scripts are
missing. The Windows path doesn't accept it at all.
## The fix
Pass `shellInitStatus` through to `windowsExceptionGenerateConfig` and
add an early branch in the Git Bash logic:
```typescript
if (shellInitStatus?.bash) {
// .bashrc/.bash_profile already defines `conda` and sets CONDA_EXE
// correctly. Re-sourcing conda.sh here would corrupt CONDA_EXE on
// Windows when base is active (microsoft#1370).
bashActivate = [{ executable: 'conda', args: ['activate', quotedPrefix] }];
}
```
The new branch is the **first** condition checked. When the user has run
`conda init bash`, we emit only `conda activate <prefix>` instead of
`source conda.sh && conda activate <prefix>`.
## Why this fixes the issue
The bug is triggered by re-running `cygpath` (in a shadowed PATH
context) inside `conda.sh`. By skipping the redundant `source`, we never
re-execute that line, so `CONDA_EXE` retains the correct value set by
the original `conda init bash` hook in the user's shell profile. The
`conda activate` call then uses the already-loaded `conda` shell
function with the correct `CONDA_EXE`, and everything works.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent 7e3f696 commit 5d919c1
2 files changed
Lines changed: 135 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
523 | 523 | | |
524 | 524 | | |
525 | 525 | | |
| 526 | + | |
526 | 527 | | |
527 | 528 | | |
528 | 529 | | |
| |||
605 | 606 | | |
606 | 607 | | |
607 | 608 | | |
| 609 | + | |
608 | 610 | | |
609 | 611 | | |
610 | 612 | | |
| |||
625 | 627 | | |
626 | 628 | | |
627 | 629 | | |
628 | | - | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
629 | 636 | | |
630 | 637 | | |
631 | 638 | | |
| |||
Lines changed: 127 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
121 | 248 | | |
122 | 249 | | |
123 | 250 | | |
| |||
0 commit comments