Skip to content

Commit 23be12b

Browse files
committed
bug: fix gitignore add for more scenarios
1 parent 11bb928 commit 23be12b

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

src/managers/builtin/venvManager.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
} from '../../api';
3131
import { PYTHON_EXTENSION_ID } from '../../common/constants';
3232
import { VenvManagerStrings } from '../../common/localize';
33-
import { traceError } from '../../common/logging';
33+
import { traceError, traceWarn } from '../../common/logging';
3434
import { createDeferred, Deferred } from '../../common/utils/deferred';
3535
import { showErrorMessage, withProgress } from '../../common/window.apis';
3636
import { findParentIfFile } from '../../features/envCommands';
@@ -186,12 +186,29 @@ export class VenvManager implements EnvironmentManager {
186186

187187
// Add .gitignore to the .venv folder
188188
try {
189-
const venvDir = environment.environmentPath.fsPath;
190-
const gitignorePath = path.join(venvDir, '.gitignore');
189+
// determine if env path is python binary or environment folder
190+
let envPath = environment.environmentPath;
191+
try {
192+
const stat = await fs.stat(envPath.fsPath);
193+
if (!stat.isDirectory()) {
194+
// If the env path is a file (likely the python binary), use its parent directory
195+
envPath = Uri.file(path.dirname(envPath.fsPath));
196+
}
197+
} catch (err) {
198+
// If stat fails, fallback to original envPath
199+
traceWarn(
200+
`Failed to stat environment path: ${envPath.fsPath}. Error: ${
201+
err instanceof Error ? err.message : String(err)
202+
}, continuing to attempt to create .gitignore.`,
203+
);
204+
}
205+
const gitignorePath = path.join(envPath.fsPath, '.gitignore');
191206
await fs.writeFile(gitignorePath, '*\n', { flag: 'w' });
192207
} catch (err) {
193208
traceError(
194-
`Failed to create .gitignore in venv: ${err instanceof Error ? err.message : String(err)}`,
209+
`Failed to create .gitignore in venv: ${
210+
err instanceof Error ? err.message : String(err)
211+
}, continuing.`,
195212
);
196213
}
197214

0 commit comments

Comments
 (0)