Skip to content

Commit 5ff1aab

Browse files
committed
Fix: Validate existence of pipenv, poetry, and pyenv paths before usage
1 parent 71853d2 commit 5ff1aab

File tree

3 files changed

+53
-26
lines changed

3 files changed

+53
-26
lines changed

src/managers/pipenv/pipenvUtils.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,32 @@ function getPipenvPathFromSettings(): string | undefined {
5454

5555
export async function getPipenv(native?: NativePythonFinder): Promise<string | undefined> {
5656
if (pipenvPath) {
57-
return pipenvPath;
57+
if (await fs.exists(untildify(pipenvPath))) {
58+
return untildify(pipenvPath);
59+
}
60+
pipenvPath = undefined;
5861
}
5962

6063
const state = await getWorkspacePersistentState();
61-
pipenvPath = await state.get<string>(PIPENV_PATH_KEY);
62-
if (pipenvPath) {
63-
traceInfo(`Using pipenv from persistent state: ${pipenvPath}`);
64-
return pipenvPath;
64+
const storedPath = await state.get<string>(PIPENV_PATH_KEY);
65+
if (storedPath) {
66+
if (await fs.exists(untildify(storedPath))) {
67+
pipenvPath = storedPath;
68+
traceInfo(`Using pipenv from persistent state: ${pipenvPath}`);
69+
return untildify(pipenvPath);
70+
}
71+
await state.set(PIPENV_PATH_KEY, undefined);
6572
}
6673

6774
// try to get from settings
6875
const settingPath = getPipenvPathFromSettings();
6976
if (settingPath) {
70-
pipenvPath = settingPath;
71-
traceInfo(`Using pipenv from settings: ${settingPath}`);
72-
return pipenvPath;
77+
if (await fs.exists(untildify(settingPath))) {
78+
pipenvPath = settingPath;
79+
traceInfo(`Using pipenv from settings: ${settingPath}`);
80+
return untildify(pipenvPath);
81+
}
82+
traceInfo(`Pipenv path from settings does not exist: ${settingPath}`);
7383
}
7484

7585
// Try to find pipenv in PATH

src/managers/poetry/poetryUtils.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,28 +106,38 @@ export async function setPoetryForWorkspaces(fsPath: string[], envPath: string |
106106

107107
export async function getPoetry(native?: NativePythonFinder): Promise<string | undefined> {
108108
if (poetryPath) {
109-
return poetryPath;
109+
if (await fs.exists(untildify(poetryPath))) {
110+
return untildify(poetryPath);
111+
}
112+
poetryPath = undefined;
110113
}
111114

112115
const state = await getWorkspacePersistentState();
113-
poetryPath = await state.get<string>(POETRY_PATH_KEY);
114-
if (poetryPath) {
115-
traceInfo(`Using poetry from persistent state: ${poetryPath}`);
116-
// Also retrieve the virtualenvs path if we haven't already
117-
if (!poetryVirtualenvsPath) {
118-
getPoetryVirtualenvsPath(untildify(poetryPath)).catch((e) =>
119-
traceError(`Error getting Poetry virtualenvs path: ${e}`),
120-
);
116+
const storedPath = await state.get<string>(POETRY_PATH_KEY);
117+
if (storedPath) {
118+
if (await fs.exists(untildify(storedPath))) {
119+
poetryPath = storedPath;
120+
traceInfo(`Using poetry from persistent state: ${poetryPath}`);
121+
// Also retrieve the virtualenvs path if we haven't already
122+
if (!poetryVirtualenvsPath) {
123+
getPoetryVirtualenvsPath(untildify(poetryPath)).catch((e) =>
124+
traceError(`Error getting Poetry virtualenvs path: ${e}`),
125+
);
126+
}
127+
return untildify(poetryPath);
121128
}
122-
return untildify(poetryPath);
129+
await state.set(POETRY_PATH_KEY, undefined);
123130
}
124131

125132
// try to get from settings
126133
const settingPath = getPoetryPathFromSettings();
127134
if (settingPath) {
128-
poetryPath = settingPath;
129-
traceInfo(`Using poetry from settings: ${settingPath}`);
130-
return poetryPath;
135+
if (await fs.exists(untildify(settingPath))) {
136+
poetryPath = settingPath;
137+
traceInfo(`Using poetry from settings: ${settingPath}`);
138+
return untildify(poetryPath);
139+
}
140+
traceInfo(`Poetry path from settings does not exist: ${settingPath}`);
131141
}
132142

133143
// Check in standard PATH locations

src/managers/pyenv/pyenvUtils.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,21 @@ export async function setPyenvForWorkspaces(fsPath: string[], envPath: string |
9898

9999
export async function getPyenv(native?: NativePythonFinder): Promise<string | undefined> {
100100
if (pyenvPath) {
101-
return pyenvPath;
101+
if (await fs.exists(untildify(pyenvPath))) {
102+
return untildify(pyenvPath);
103+
}
104+
pyenvPath = undefined;
102105
}
103106

104107
const state = await getWorkspacePersistentState();
105-
pyenvPath = await state.get<string>(PYENV_PATH_KEY);
106-
if (pyenvPath) {
107-
traceInfo(`Using pyenv from persistent state: ${pyenvPath}`);
108-
return untildify(pyenvPath);
108+
const storedPath = await state.get<string>(PYENV_PATH_KEY);
109+
if (storedPath) {
110+
if (await fs.exists(untildify(storedPath))) {
111+
pyenvPath = storedPath;
112+
traceInfo(`Using pyenv from persistent state: ${pyenvPath}`);
113+
return untildify(pyenvPath);
114+
}
115+
await state.set(PYENV_PATH_KEY, undefined);
109116
}
110117

111118
const pyenvBin = isWindows() ? 'pyenv.exe' : 'pyenv';

0 commit comments

Comments
 (0)