Skip to content

Commit 8bc139a

Browse files
refactor to save check promise
1 parent 5ec1a99 commit 8bc139a

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

src/client/datascience/activation.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class Activation implements IExtensionSingleActivationService {
2828
public async activate(): Promise<void> {
2929
this.disposables.push(this.notebookProvider.onDidOpenNotebookEditor(this.onDidOpenNotebookEditor, this));
3030
this.disposables.push(this.jupyterInterpreterService.onDidChangeInterpreter(this.onDidChangeInterpreter, this));
31-
this.testSavedInterpreter();
31+
this.jupyterInterpreterService.validateSavedInterpreter().ignoreErrors();
3232
await this.contextService.activate();
3333
}
3434

@@ -44,11 +44,6 @@ export class Activation implements IExtensionSingleActivationService {
4444
}
4545
}
4646

47-
private testSavedInterpreter(): void {
48-
// Do we have a saved interpreter? If so check it out to see if it's still valid
49-
this.jupyterInterpreterService.checkSavedInterpreter().ignoreErrors();
50-
}
51-
5247
@debounceAsync(500)
5348
@swallowExceptions('Failed to pre-warm daemon pool')
5449
private async PreWarmDaemonPool() {

src/client/datascience/jupyter/interpreter/jupyterInterpreterService.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export class JupyterInterpreterService {
2525
private _selectedInterpreter?: PythonInterpreter;
2626
private _selectedInterpreterPath?: string;
2727
private _onDidChangeInterpreter = new EventEmitter<PythonInterpreter>();
28+
private validateSavedInterpreterPromise: Promise<boolean> | undefined;
2829
public get onDidChangeInterpreter(): Event<PythonInterpreter> {
2930
return this._onDidChangeInterpreter.event;
3031
}
@@ -38,6 +39,13 @@ export class JupyterInterpreterService {
3839
private readonly interpreterConfiguration: JupyterInterpreterDependencyService,
3940
@inject(IInterpreterService) private readonly interpreterService: IInterpreterService
4041
) {}
42+
public async validateSavedInterpreter(): Promise<boolean> {
43+
if (!this.validateSavedInterpreterPromise) {
44+
this.validateSavedInterpreterPromise = this.validateSavedInterpreterImpl();
45+
}
46+
47+
return this.validateSavedInterpreterPromise;
48+
}
4149
/**
4250
* Gets the selected interpreter configured to run Jupyter.
4351
*
@@ -65,7 +73,17 @@ export class JupyterInterpreterService {
6573
return interpreter;
6674
}
6775

68-
const pythonPath = this._selectedInterpreterPath || this.interpreterSelectionState.selectedPythonPath;
76+
let pythonPath = this._selectedInterpreterPath;
77+
78+
if (!pythonPath && this.interpreterSelectionState.selectedPythonPath) {
79+
// On activate we kick off a check to see if the saved interpreter is still valid
80+
// make sure that has completed before we actually use it as a valid interpreter
81+
if (await this.validateSavedInterpreter()) {
82+
pythonPath = this.interpreterSelectionState.selectedPythonPath;
83+
}
84+
}
85+
86+
// If nothing saved, then check our current interpreter to see if we can use it
6987
if (!pythonPath) {
7088
// Check if current interpreter has all of the required dependencies.
7189
// If yes, then use that.
@@ -183,4 +201,32 @@ export class JupyterInterpreterService {
183201
this.interpreterSelectionState.updateSelectedPythonPath((this._selectedInterpreterPath = interpreter.path));
184202
sendTelemetryEvent(Telemetry.SelectJupyterInterpreter, undefined, { result: 'selected' });
185203
}
204+
205+
private async validateSavedInterpreterImpl(): Promise<boolean> {
206+
if (!this.interpreterSelectionState.selectedPythonPath) {
207+
// None set yet, so no need to check
208+
return false;
209+
}
210+
211+
try {
212+
const interpreterDetails = await this.interpreterService.getInterpreterDetails(
213+
this.interpreterSelectionState.selectedPythonPath,
214+
undefined
215+
);
216+
217+
if (interpreterDetails) {
218+
if (await this.interpreterConfiguration.areDependenciesInstalled(interpreterDetails, undefined)) {
219+
// Our saved interpreter was found and has dependencies installed
220+
return true;
221+
}
222+
}
223+
} catch (_err) {
224+
traceInfo('Saved Jupyter interpreter invalid');
225+
}
226+
227+
// At this point we failed some aspect of our checks regarding our saved interpreter, so clear it out
228+
this._selectedInterpreter = undefined;
229+
this.interpreterSelectionState.updateSelectedPythonPath(undefined);
230+
return false;
231+
}
186232
}

0 commit comments

Comments
 (0)