Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/managers/builtin/venvManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
} from '../../api';
import { PYTHON_EXTENSION_ID } from '../../common/constants';
import { VenvManagerStrings } from '../../common/localize';
import { traceError } from '../../common/logging';
import { traceError, traceWarn } from '../../common/logging';
import { createDeferred, Deferred } from '../../common/utils/deferred';
import { showErrorMessage, withProgress } from '../../common/window.apis';
import { findParentIfFile } from '../../features/envCommands';
Expand Down Expand Up @@ -186,12 +186,30 @@ export class VenvManager implements EnvironmentManager {

// Add .gitignore to the .venv folder
try {
const venvDir = environment.environmentPath.fsPath;
const gitignorePath = path.join(venvDir, '.gitignore');
// determine if env path is python binary or environment folder
let envPath = environment.environmentPath.fsPath;
try {
const stat = await fs.stat(envPath);
if (!stat.isDirectory()) {
// If the env path is a file (likely the python binary), use parent-parent as the env path
// following format of .venv/bin/python or .venv\Scripts\python.exe
envPath = Uri.file(path.dirname(path.dirname(envPath))).fsPath;
}
} catch (err) {
// If stat fails, fallback to original envPath
traceWarn(
`Failed to stat environment path: ${envPath}. Error: ${
err instanceof Error ? err.message : String(err)
}, continuing to attempt to create .gitignore.`,
);
}
const gitignorePath = path.join(envPath, '.gitignore');
await fs.writeFile(gitignorePath, '*\n', { flag: 'w' });
} catch (err) {
traceError(
`Failed to create .gitignore in venv: ${err instanceof Error ? err.message : String(err)}`,
`Failed to create .gitignore in venv: ${
err instanceof Error ? err.message : String(err)
}, continuing.`,
);
}

Expand Down