Skip to content

Merge fixes related to remembering interpreter #14270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/client/datascience/jupyter/kernels/kernelSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.
import type { nbformat } from '@jupyterlab/coreutils';
import type { Kernel } from '@jupyterlab/services';
import { sha256 } from 'hash.js';
import { inject, injectable } from 'inversify';
// tslint:disable-next-line: no-require-imports
import cloneDeep = require('lodash/cloneDeep');
Expand All @@ -18,6 +19,7 @@ import { PythonEnvironment } from '../../../pythonEnvironments/info';
import { IEventNamePropertyMapping, sendTelemetryEvent } from '../../../telemetry';
import { Commands, KnownNotebookLanguages, Settings, Telemetry } from '../../constants';
import { IKernelFinder } from '../../kernel-launcher/types';
import { getInterpreterInfoStoredInMetadata } from '../../notebookStorage/baseModel';
import { reportAction } from '../../progress/decorator';
import { ReportableAction } from '../../progress/types';
import {
Expand Down Expand Up @@ -486,6 +488,17 @@ export class KernelSelector implements IKernelSelectionUsage {
}
}
}
private async findInterpreterStoredInNotebookMetadata(
resource: Resource,
notebookMetadata?: nbformat.INotebookMetadata
): Promise<PythonEnvironment | undefined> {
const info = getInterpreterInfoStoredInMetadata(notebookMetadata);
if (!info) {
return;
}
const interpreters = await this.interpreterService.getInterpreters(resource);
return interpreters.find((item) => sha256().update(item.path).digest('hex') === info.hash);
}

// Get our kernelspec and interpreter for a local raw connection
private async getKernelForLocalRawConnection(
Expand All @@ -494,6 +507,19 @@ export class KernelSelector implements IKernelSelectionUsage {
cancelToken?: CancellationToken,
ignoreDependencyCheck?: boolean
): Promise<KernelSpecConnectionMetadata | PythonKernelConnectionMetadata | undefined> {
// If user had selected an interpreter (raw kernel), then that interpreter would be stored in the kernelspec metadata.
// Find this matching interpreter & start that using raw kernel.
const interpreterStoredInKernelSpec = await this.findInterpreterStoredInNotebookMetadata(
resource,
notebookMetadata
);
if (interpreterStoredInKernelSpec) {
return {
kind: 'startUsingPythonInterpreter',
interpreter: interpreterStoredInKernelSpec
};
}

// First use our kernel finder to locate a kernelspec on disk
const kernelSpec = await this.kernelFinder.findKernelSpec(
resource,
Expand Down
13 changes: 13 additions & 0 deletions src/client/datascience/notebookStorage/baseModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ type KernelIdListEntry = {
kernelId: string | undefined;
};

export function getInterpreterInfoStoredInMetadata(
metadata?: nbformat.INotebookMetadata
): { displayName: string; hash: string } | undefined {
if (!metadata || !metadata.kernelspec || !metadata.kernelspec.name) {
return;
}
// See `updateNotebookMetadata` to determine how & where exactly interpreter hash is stored.
// tslint:disable-next-line: no-any
const kernelSpecMetadata: undefined | any = metadata.kernelspec.metadata as any;
const interpreterHash = kernelSpecMetadata?.interpreter?.hash;
return interpreterHash ? { displayName: metadata.kernelspec.name, hash: interpreterHash } : undefined;
}

// tslint:disable-next-line: cyclomatic-complexity
export function updateNotebookMetadata(
metadata?: nbformat.INotebookMetadata,
Expand Down