Skip to content

Commit 1befa37

Browse files
authored
feat: add .gitignore file creation for new Python environments in venv and conda managers (#555)
1 parent 3426c24 commit 1befa37

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ export interface EnvironmentManager {
379379
quickCreateConfig?(): QuickCreateConfig | undefined;
380380

381381
/**
382-
* Creates a new Python environment within the specified scope.
382+
* Creates a new Python environment within the specified scope. Create should support adding a .gitignore file if it creates a folder within the workspace.
383383
* @param scope - The scope within which to create the environment.
384384
* @param options - Optional parameters for creating the Python environment.
385385
* @returns A promise that resolves to the created Python environment, or undefined if creation failed.

src/managers/builtin/venvManager.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as fs from 'fs/promises';
12
import * as path from 'path';
23
import { EventEmitter, l10n, LogOutputChannel, MarkdownString, ProgressLocation, ThemeIcon, Uri } from 'vscode';
34
import {
@@ -20,6 +21,7 @@ import {
2021
} from '../../api';
2122
import { PYTHON_EXTENSION_ID } from '../../common/constants';
2223
import { VenvManagerStrings } from '../../common/localize';
24+
import { traceError } from '../../common/logging';
2325
import { createDeferred, Deferred } from '../../common/utils/deferred';
2426
import { showErrorMessage, withProgress } from '../../common/window.apis';
2527
import { findParentIfFile } from '../../features/envCommands';
@@ -162,6 +164,17 @@ export class VenvManager implements EnvironmentManager {
162164
}
163165
if (environment) {
164166
this.addEnvironment(environment, true);
167+
168+
// Add .gitignore to the .venv folder
169+
try {
170+
const venvDir = environment.environmentPath.fsPath;
171+
const gitignorePath = path.join(venvDir, '.gitignore');
172+
await fs.writeFile(gitignorePath, '*\n', { flag: 'w' });
173+
} catch (err) {
174+
traceError(
175+
`Failed to create .gitignore in venv: ${err instanceof Error ? err.message : String(err)}`,
176+
);
177+
}
165178
}
166179
return environment;
167180
} finally {

src/managers/conda/condaEnvManager.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as fs from 'fs-extra';
12
import * as path from 'path';
23
import { Disposable, EventEmitter, l10n, LogOutputChannel, MarkdownString, ProgressLocation, Uri } from 'vscode';
34
import {
@@ -19,6 +20,7 @@ import {
1920
SetEnvironmentScope,
2021
} from '../../api';
2122
import { CondaStrings } from '../../common/localize';
23+
import { traceError } from '../../common/logging';
2224
import { createDeferred, Deferred } from '../../common/utils/deferred';
2325
import { showErrorMessage, withProgress } from '../../common/window.apis';
2426
import { NativePythonFinder } from '../common/nativePythonFinder';
@@ -167,6 +169,20 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
167169
}
168170
if (result) {
169171
this.addEnvironment(result);
172+
173+
// If the environment is inside the workspace, add a .gitignore file
174+
try {
175+
const projectUris = this.api.getPythonProjects().map((p) => p.uri.fsPath);
176+
const envPath = result.environmentPath?.fsPath;
177+
if (envPath && projectUris.some((root) => envPath.startsWith(root))) {
178+
const gitignorePath = path.join(envPath, '.gitignore');
179+
await fs.writeFile(gitignorePath, '*\n', { flag: 'w' });
180+
}
181+
} catch (err) {
182+
traceError(
183+
`Failed to create .gitignore in conda env: ${err instanceof Error ? err.message : String(err)}`,
184+
);
185+
}
170186
}
171187

172188
return result;

0 commit comments

Comments
 (0)