-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Define preferred Python Env in new Kernel Picker (#14051)
- Loading branch information
1 parent
0e016b0
commit 399bc7c
Showing
6 changed files
with
122 additions
and
8 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
68 changes: 68 additions & 0 deletions
68
src/notebooks/controllers/preferredKernelConnectionService.node.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,68 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
import { NotebookDocument, workspace, Uri } from 'vscode'; | ||
import * as path from '../../platform/vscode-path/resources'; | ||
import { isParentPath } from '../../platform/common/platform/fileUtils'; | ||
import { EnvironmentType } from '../../platform/pythonEnvironments/info'; | ||
import { getEnvironmentType } from '../../platform/interpreter/helpers'; | ||
import { Environment, ProposedExtensionAPI } from '../../platform/api/pythonApiTypes'; | ||
|
||
export function findPreferredPythonEnvironment( | ||
notebook: NotebookDocument, | ||
pythonApi: ProposedExtensionAPI | ||
): Environment | undefined { | ||
// 1. Check if we have a .conda or .venv virtual env in the local workspace folder. | ||
const localEnv = findPythonEnvironmentClosestToNotebook(notebook, pythonApi.environments.known); | ||
if (localEnv) { | ||
return localEnv; | ||
} | ||
|
||
const findMatchingActiveEnvironment = () => { | ||
const envPath = pythonApi.environments.getActiveEnvironmentPath(notebook.uri); | ||
return envPath && pythonApi.environments.known.find((e) => e.id === envPath.id); | ||
}; | ||
// 3. Fall back to the active interpreter. | ||
return findMatchingActiveEnvironment(); | ||
} | ||
|
||
function findPythonEnvironmentClosestToNotebook(notebook: NotebookDocument, envs: readonly Environment[]) { | ||
const defaultFolder = | ||
workspace.getWorkspaceFolder(notebook.uri)?.uri || | ||
(workspace.workspaceFolders?.length === 1 ? workspace.workspaceFolders[0].uri : undefined); | ||
const localEnvNextToNbFile = findPythonEnvBelongingToFolder(path.dirname(notebook.uri), envs); | ||
if (localEnvNextToNbFile) { | ||
return localEnvNextToNbFile; | ||
} | ||
if (defaultFolder) { | ||
return findPythonEnvBelongingToFolder(defaultFolder, envs); | ||
} | ||
} | ||
|
||
function findPythonEnvBelongingToFolder(folder: Uri, pythonEnvs: readonly Environment[]) { | ||
const localEnvs = pythonEnvs.filter((p) => | ||
// eslint-disable-next-line local-rules/dont-use-fspath | ||
isParentPath(p.environment?.folderUri?.fsPath || p.executable.uri?.fsPath || p.path, folder.fsPath) | ||
); | ||
|
||
// Find an environment that is a .venv or .conda environment. | ||
// Give preference to .venv over .conda. | ||
// & give preference to .venv or .conda over any other environment. | ||
return localEnvs.find( | ||
(e) => getEnvironmentType(e) === EnvironmentType.Venv && e.environment?.name?.toLowerCase() === '.venv' | ||
) || | ||
localEnvs.find( | ||
(e) => getEnvironmentType(e) === EnvironmentType.Conda && e.environment?.name?.toLowerCase() === '.conda' | ||
) || | ||
localEnvs.find( | ||
(e) => | ||
[EnvironmentType.VirtualEnv, EnvironmentType.VirtualEnvWrapper].includes(getEnvironmentType(e)) && | ||
e.environment?.name?.toLowerCase() === '.venv' | ||
) || | ||
localEnvs.find( | ||
(e) => e.environment?.name?.toLowerCase() === '.venv' || e.environment?.name?.toLowerCase() === '.conda' | ||
) || | ||
localEnvs.length | ||
? localEnvs[0] | ||
: undefined; | ||
} |
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
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