forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for Create Env command to re-create env for venv (microsoft#2…
…1829) Closes microsoft#21827
- Loading branch information
1 parent
ed93ade
commit be686dd
Showing
11 changed files
with
685 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/client/pythonEnvironments/creation/provider/venvDeleteUtils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import * as fs from 'fs-extra'; | ||
import * as path from 'path'; | ||
import { WorkspaceFolder } from 'vscode'; | ||
import { traceError, traceInfo } from '../../../logging'; | ||
import { getVenvPath, showErrorMessageWithLogs } from '../common/commonUtils'; | ||
import { CreateEnv } from '../../../common/utils/localize'; | ||
import { sleep } from '../../../common/utils/async'; | ||
import { switchSelectedPython } from './venvSwitchPython'; | ||
|
||
async function tryDeleteFile(file: string): Promise<boolean> { | ||
try { | ||
if (!(await fs.pathExists(file))) { | ||
return true; | ||
} | ||
await fs.unlink(file); | ||
return true; | ||
} catch (err) { | ||
traceError(`Failed to delete file [${file}]:`, err); | ||
return false; | ||
} | ||
} | ||
|
||
async function tryDeleteDir(dir: string): Promise<boolean> { | ||
try { | ||
if (!(await fs.pathExists(dir))) { | ||
return true; | ||
} | ||
await fs.rmdir(dir, { | ||
recursive: true, | ||
maxRetries: 10, | ||
retryDelay: 200, | ||
}); | ||
return true; | ||
} catch (err) { | ||
traceError(`Failed to delete directory [${dir}]:`, err); | ||
return false; | ||
} | ||
} | ||
|
||
export async function deleteEnvironmentNonWindows(workspaceFolder: WorkspaceFolder): Promise<boolean> { | ||
const venvPath = getVenvPath(workspaceFolder); | ||
if (await tryDeleteDir(venvPath)) { | ||
traceInfo(`Deleted venv dir: ${venvPath}`); | ||
return true; | ||
} | ||
showErrorMessageWithLogs(CreateEnv.Venv.errorDeletingEnvironment); | ||
return false; | ||
} | ||
|
||
export async function deleteEnvironmentWindows( | ||
workspaceFolder: WorkspaceFolder, | ||
interpreter: string | undefined, | ||
): Promise<boolean> { | ||
const venvPath = getVenvPath(workspaceFolder); | ||
const venvPythonPath = path.join(venvPath, 'Scripts', 'python.exe'); | ||
|
||
if (await tryDeleteFile(venvPythonPath)) { | ||
traceInfo(`Deleted python executable: ${venvPythonPath}`); | ||
if (await tryDeleteDir(venvPath)) { | ||
traceInfo(`Deleted ".venv" dir: ${venvPath}`); | ||
return true; | ||
} | ||
|
||
traceError(`Failed to delete ".venv" dir: ${venvPath}`); | ||
traceError( | ||
'This happens if the virtual environment is still in use, or some binary in the venv is still running.', | ||
); | ||
traceError(`Please delete the ".venv" manually: [${venvPath}]`); | ||
showErrorMessageWithLogs(CreateEnv.Venv.errorDeletingEnvironment); | ||
return false; | ||
} | ||
traceError(`Failed to delete python executable: ${venvPythonPath}`); | ||
traceError('This happens if the virtual environment is still in use.'); | ||
|
||
if (interpreter) { | ||
traceError('We will attempt to switch python temporarily to delete the ".venv"'); | ||
|
||
await switchSelectedPython(interpreter, workspaceFolder.uri, 'temporarily to delete the ".venv"'); | ||
|
||
traceInfo(`Attempting to delete ".venv" again: ${venvPath}`); | ||
const ms = 500; | ||
for (let i = 0; i < 5; i = i + 1) { | ||
traceInfo(`Waiting for ${ms}ms to let processes exit, before a delete attempt.`); | ||
await sleep(ms); | ||
if (await tryDeleteDir(venvPath)) { | ||
traceInfo(`Deleted ".venv" dir: ${venvPath}`); | ||
return true; | ||
} | ||
traceError(`Failed to delete ".venv" dir [${venvPath}] (attempt ${i + 1}/5).`); | ||
} | ||
} else { | ||
traceError(`Please delete the ".venv" dir manually: [${venvPath}]`); | ||
} | ||
showErrorMessageWithLogs(CreateEnv.Venv.errorDeletingEnvironment); | ||
return false; | ||
} |
28 changes: 28 additions & 0 deletions
28
src/client/pythonEnvironments/creation/provider/venvSwitchPython.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import * as path from 'path'; | ||
import { Disposable, Uri } from 'vscode'; | ||
import { createDeferred } from '../../../common/utils/async'; | ||
import { getExtension } from '../../../common/vscodeApis/extensionsApi'; | ||
import { PVSC_EXTENSION_ID, PythonExtension } from '../../../api/types'; | ||
import { traceInfo } from '../../../logging'; | ||
|
||
export async function switchSelectedPython(interpreter: string, uri: Uri, purpose: string): Promise<void> { | ||
let dispose: Disposable | undefined; | ||
try { | ||
const deferred = createDeferred<void>(); | ||
const api: PythonExtension = getExtension(PVSC_EXTENSION_ID)?.exports as PythonExtension; | ||
dispose = api.environments.onDidChangeActiveEnvironmentPath(async (e) => { | ||
if (path.normalize(e.path) === path.normalize(interpreter)) { | ||
traceInfo(`Switched to interpreter ${purpose}: ${interpreter}`); | ||
deferred.resolve(); | ||
} | ||
}); | ||
api.environments.updateActiveEnvironmentPath(interpreter, uri); | ||
traceInfo(`Switching interpreter ${purpose}: ${interpreter}`); | ||
await deferred.promise; | ||
} finally { | ||
dispose?.dispose(); | ||
} | ||
} |
Oops, something went wrong.