From 23be12b22f020c7336e986e49ba593c8fd2aea41 Mon Sep 17 00:00:00 2001 From: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com> Date: Tue, 22 Jul 2025 15:13:14 -0700 Subject: [PATCH 1/3] bug: fix gitignore add for more scenarios --- src/managers/builtin/venvManager.ts | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/managers/builtin/venvManager.ts b/src/managers/builtin/venvManager.ts index e02031ba..b8c5650c 100644 --- a/src/managers/builtin/venvManager.ts +++ b/src/managers/builtin/venvManager.ts @@ -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'; @@ -186,12 +186,29 @@ 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; + try { + const stat = await fs.stat(envPath.fsPath); + if (!stat.isDirectory()) { + // If the env path is a file (likely the python binary), use its parent directory + envPath = Uri.file(path.dirname(envPath.fsPath)); + } + } catch (err) { + // If stat fails, fallback to original envPath + traceWarn( + `Failed to stat environment path: ${envPath.fsPath}. Error: ${ + err instanceof Error ? err.message : String(err) + }, continuing to attempt to create .gitignore.`, + ); + } + const gitignorePath = path.join(envPath.fsPath, '.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.`, ); } From 239ce44d981e2b3bc6729270c5a271c7e5afb4a0 Mon Sep 17 00:00:00 2001 From: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com> Date: Tue, 22 Jul 2025 15:20:34 -0700 Subject: [PATCH 2/3] update for parent-parent --- src/managers/builtin/venvManager.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/managers/builtin/venvManager.ts b/src/managers/builtin/venvManager.ts index b8c5650c..97484e26 100644 --- a/src/managers/builtin/venvManager.ts +++ b/src/managers/builtin/venvManager.ts @@ -191,8 +191,9 @@ export class VenvManager implements EnvironmentManager { try { const stat = await fs.stat(envPath.fsPath); if (!stat.isDirectory()) { - // If the env path is a file (likely the python binary), use its parent directory - envPath = Uri.file(path.dirname(envPath.fsPath)); + // 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 From 80fc3e3388aa2703dbe4a266429a1fc6c4861722 Mon Sep 17 00:00:00 2001 From: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com> Date: Tue, 22 Jul 2025 15:21:39 -0700 Subject: [PATCH 3/3] simplify path var --- src/managers/builtin/venvManager.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/managers/builtin/venvManager.ts b/src/managers/builtin/venvManager.ts index 97484e26..cf5027a9 100644 --- a/src/managers/builtin/venvManager.ts +++ b/src/managers/builtin/venvManager.ts @@ -187,23 +187,23 @@ export class VenvManager implements EnvironmentManager { // Add .gitignore to the .venv folder try { // determine if env path is python binary or environment folder - let envPath = environment.environmentPath; + let envPath = environment.environmentPath.fsPath; try { - const stat = await fs.stat(envPath.fsPath); + 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))); + 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.fsPath}. Error: ${ + `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.fsPath, '.gitignore'); + const gitignorePath = path.join(envPath, '.gitignore'); await fs.writeFile(gitignorePath, '*\n', { flag: 'w' }); } catch (err) { traceError(