Skip to content
This repository was archived by the owner on Dec 23, 2021. It is now read-only.

Commit 3d21ebb

Browse files
authored
Venv fixes (#228)
1 parent 89d9fe8 commit 3d21ebb

File tree

1 file changed

+72
-96
lines changed

1 file changed

+72
-96
lines changed

src/extension_utils/utils.ts

Lines changed: 72 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -345,23 +345,11 @@ export const promptInstallVenv = (
345345
if (selection === DialogResponses.YES) {
346346
return installPythonVenv(context, pythonExecutable);
347347
} else {
348-
return vscode.window
349-
.showInformationMessage(
350-
CONSTANTS.INFO.ARE_YOU_SURE,
351-
DialogResponses.INSTALL_NOW,
352-
DialogResponses.DONT_INSTALL
353-
)
354-
.then((installChoice: vscode.MessageItem | undefined) => {
355-
if (installChoice === DialogResponses.INSTALL_NOW) {
356-
return installPythonVenv(context, pythonExecutable);
357-
} else {
358-
// return an empty string, notifying the caller
359-
// that the user was unwilling to create venv
360-
// and by default, this will trigger the extension to
361-
// try using pythonExecutable
362-
return "";
363-
}
364-
});
348+
// return pythonExecutable, notifying the caller
349+
// that the user was unwilling to create venv
350+
// and by default, this will trigger the extension to
351+
// try using pythonExecutable
352+
return pythonExecutable;
365353
}
366354
});
367355
};
@@ -410,22 +398,7 @@ export const installPythonVenv = async (
410398
return pythonExecutable;
411399
}
412400

413-
if (!(await installDependencies(context, pythonPath))) {
414-
vscode.window
415-
.showErrorMessage(
416-
`${CONSTANTS.ERROR.DEPENDENCY_DOWNLOAD_ERROR} Using original interpreter at: ${pythonExecutable}.`,
417-
DialogResponses.READ_INSTALL_MD
418-
)
419-
.then((selection: vscode.MessageItem | undefined) => {
420-
if (selection === DialogResponses.READ_INSTALL_MD) {
421-
open(CONSTANTS.LINKS.INSTALL);
422-
}
423-
});
424-
425-
return pythonExecutable;
426-
}
427-
428-
return pythonPath;
401+
return installDependenciesWrapper(context, pythonPath, pythonExecutable);
429402
};
430403

431404
export const areDependenciesInstalled = async (
@@ -481,6 +454,30 @@ export const installDependencies = async (
481454
}
482455
};
483456

457+
export const installDependenciesWrapper = async (
458+
context: vscode.ExtensionContext,
459+
pythonPath: string,
460+
backupPythonPath: string = ""
461+
) => {
462+
let errMessage = CONSTANTS.ERROR.DEPENDENCY_DOWNLOAD_ERROR;
463+
if (backupPythonPath !== "") {
464+
errMessage = `${errMessage} Using original interpreter at: ${backupPythonPath}.`;
465+
}
466+
if (!(await installDependencies(context, pythonPath))) {
467+
vscode.window
468+
.showErrorMessage(
469+
CONSTANTS.ERROR.DEPENDENCY_DOWNLOAD_ERROR,
470+
DialogResponses.READ_INSTALL_MD
471+
)
472+
.then((selection: vscode.MessageItem | undefined) => {
473+
if (selection === DialogResponses.READ_INSTALL_MD) {
474+
open(CONSTANTS.LINKS.INSTALL);
475+
}
476+
});
477+
return backupPythonPath;
478+
}
479+
return pythonPath;
480+
};
484481
export const getCurrentPythonExecutableName = async () => {
485482
let originalPythonExecutableName = "";
486483

@@ -549,40 +546,22 @@ export const setupEnv = async (
549546
let pythonExecutableName = originalPythonExecutableName;
550547

551548
if (!(await areDependenciesInstalled(context, pythonExecutableName))) {
549+
const pythonExecutableNameVenv = await getPythonVenv(context);
552550
// environment needs to install dependencies
553551
if (!(await checkIfVenv(context, pythonExecutableName))) {
554-
pythonExecutableName = await getPythonVenv(context);
555552
if (await hasVenv(context)) {
556553
// venv in extention exists with wrong dependencies
557554
if (
558555
!(await areDependenciesInstalled(
559556
context,
560-
pythonExecutableName
557+
pythonExecutableNameVenv
561558
))
562559
) {
563-
if (
564-
!(await installDependencies(
565-
context,
566-
pythonExecutableName
567-
))
568-
) {
569-
vscode.window
570-
.showErrorMessage(
571-
`${CONSTANTS.ERROR.DEPENDENCY_DOWNLOAD_ERROR} Using original interpreter at: ${pythonExecutableName}.`,
572-
DialogResponses.READ_INSTALL_MD
573-
)
574-
.then(
575-
(selection: vscode.MessageItem | undefined) => {
576-
if (
577-
selection ===
578-
DialogResponses.READ_INSTALL_MD
579-
) {
580-
open(CONSTANTS.LINKS.INSTALL);
581-
}
582-
}
583-
);
584-
return pythonExecutableName;
585-
}
560+
pythonExecutableName = await installDependenciesWrapper(
561+
context,
562+
pythonExecutableNameVenv,
563+
pythonExecutableName
564+
);
586565
}
587566
} else {
588567
pythonExecutableName = await promptInstallVenv(
@@ -591,8 +570,14 @@ export const setupEnv = async (
591570
);
592571
}
593572
}
594-
595-
if (pythonExecutableName === originalPythonExecutableName) {
573+
if (pythonExecutableName === pythonExecutableNameVenv) {
574+
vscode.window.showInformationMessage(
575+
CONSTANTS.INFO.UPDATED_TO_EXTENSION_VENV
576+
);
577+
vscode.workspace
578+
.getConfiguration()
579+
.update(CONFIG.PYTHON_PATH, pythonExecutableName);
580+
} else if (pythonExecutableName === originalPythonExecutableName) {
596581
// going with original interpreter, either because
597582
// already in venv or error in creating custom venv
598583
if (checkConfig(CONFIG.SHOW_DEPENDENCY_INSTALL)) {
@@ -607,47 +592,38 @@ export const setupEnv = async (
607592
installChoice: vscode.MessageItem | undefined
608593
) => {
609594
if (installChoice === DialogResponses.INSTALL_NOW) {
610-
if (
611-
!(await installDependencies(
612-
context,
613-
pythonExecutableName
614-
))
615-
) {
616-
vscode.window
617-
.showErrorMessage(
618-
CONSTANTS.ERROR
619-
.DEPENDENCY_DOWNLOAD_ERROR,
620-
DialogResponses.READ_INSTALL_MD
621-
)
622-
.then(
623-
(
624-
selection:
625-
| vscode.MessageItem
626-
| undefined
627-
) => {
628-
if (
629-
selection ===
630-
DialogResponses.READ_INSTALL_MD
631-
) {
632-
open(
633-
CONSTANTS.LINKS.INSTALL
634-
);
635-
}
595+
await installDependenciesWrapper(
596+
context,
597+
pythonExecutableName
598+
);
599+
} else {
600+
await vscode.window
601+
.showInformationMessage(
602+
CONSTANTS.INFO.ARE_YOU_SURE,
603+
DialogResponses.INSTALL_NOW,
604+
DialogResponses.DONT_INSTALL
605+
)
606+
.then(
607+
async (
608+
installChoice2:
609+
| vscode.MessageItem
610+
| undefined
611+
) => {
612+
if (
613+
installChoice2 ===
614+
DialogResponses.INSTALL_NOW
615+
) {
616+
await installDependenciesWrapper(
617+
context,
618+
pythonExecutableName
619+
);
636620
}
637-
);
638-
return pythonExecutableName;
639-
}
621+
}
622+
);
640623
}
641624
}
642625
);
643626
}
644-
} else {
645-
vscode.window.showInformationMessage(
646-
CONSTANTS.INFO.UPDATED_TO_EXTENSION_VENV
647-
);
648-
vscode.workspace
649-
.getConfiguration()
650-
.update(CONFIG.PYTHON_PATH, pythonExecutableName);
651627
}
652628
} else if (needsResponse) {
653629
vscode.window.showInformationMessage(

0 commit comments

Comments
 (0)