Skip to content

Commit

Permalink
Fix duplicate kernel generation (#4790)
Browse files Browse the repository at this point in the history
* Fix duplicate kernel generation

* Fix functional tests
  • Loading branch information
rchiodo committed Feb 12, 2021
1 parent a8c376f commit d611fff
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
1 change: 1 addition & 0 deletions news/2 Fixes/4720.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes problem with duplicate jupyter kernels being generated.
14 changes: 6 additions & 8 deletions src/client/datascience/kernel-launcher/kernelFinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,14 @@ export class KernelFinder implements IKernelFinder {
// Search all our local file system locations for installed kernel specs and return them
@captureTelemetry(Telemetry.KernelListingPerf)
public async listKernelSpecs(resource: Resource): Promise<IJupyterKernelSpec[]> {
if (!resource) {
// We need a resource to search for related kernel specs
return [];
}

// Get an id for the workspace folder, if we don't have one, use the fsPath of the resource
const workspaceFolderId = this.workspaceService.getWorkspaceFolderIdentifier(resource, resource.fsPath);
const workspaceFolderId = this.workspaceService.getWorkspaceFolderIdentifier(
resource,
resource?.fsPath || this.workspaceService.rootPath
);

// If we have not already searched for this resource, then generate the search
if (!this.workspaceToKernels.has(workspaceFolderId)) {
if (workspaceFolderId && !this.workspaceToKernels.has(workspaceFolderId)) {
this.workspaceToKernels.set(workspaceFolderId, this.findResourceKernelSpecs(resource));
}

Expand All @@ -119,7 +117,7 @@ export class KernelFinder implements IKernelFinder {
.then((items) =>
traceInfoIf(
!!process.env.VSC_JUPYTER_LOG_KERNEL_OUTPUT,
`Kernel specs for ${resource.toString()} are \n ${JSON.stringify(items)}`
`Kernel specs for ${resource?.toString() || 'undefined'} are \n ${JSON.stringify(items)}`
)
)
.catch(noop);
Expand Down
12 changes: 12 additions & 0 deletions src/test/datascience/dataScienceIocContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,9 @@ export class DataScienceIocContainer extends UnitTestIocContainer {
when(workspaceService.workspaceFolders).thenReturn(this.workspaceFolders);
when(workspaceService.rootPath).thenReturn(testWorkspaceFolder);
when(workspaceService.getWorkspaceFolder(anything())).thenCall(this.getWorkspaceFolder.bind(this));
when(workspaceService.getWorkspaceFolderIdentifier(anything(), anything())).thenCall(
this.getWorkspaceFolderIdentifier.bind(this)
);
this.addWorkspaceFolder(testWorkspaceFolder);
return workspaceService;
}
Expand All @@ -1282,6 +1285,15 @@ export class DataScienceIocContainer extends UnitTestIocContainer {
}
return undefined;
}
private getWorkspaceFolderIdentifier(uri: Resource, defaultValue: string | undefined): string | undefined {
if (uri) {
const folder = this.workspaceFolders.find((w) => w.ownedResources.has(uri.toString()));
if (folder) {
return folder.uri.fsPath;
}
}
return defaultValue;
}

private getResourceKey(resource: Resource): string {
if (!this.disposed) {
Expand Down

0 comments on commit d611fff

Please sign in to comment.