forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show a prompt asking users if they want to create environment (micros…
…oft#22071) Criteria for showing prompts: 1. It has to be a workspace or multiroot workspace. 2. The workspace or workspace folder should not have ".venv" or ".conda" environments. 3. The selected python should be a global python, i.e., there is no workspace specific environment selected. 4. The workspace should **not** have any `pipfile`, `poetry.lock` etc. 5. The workspace should have files that match `*requirements*.txt` or `requirements/*.txt` pattern. There is a setting to enable this behavior: `python.createEnvironment.trigger` and default is `off` closes microsoft#21965
- Loading branch information
1 parent
01a0c53
commit 0d8a32d
Showing
18 changed files
with
711 additions
and
2 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
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
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
113 changes: 113 additions & 0 deletions
113
src/client/pythonEnvironments/creation/common/createEnvTriggerUtils.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,113 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import * as path from 'path'; | ||
import * as fsapi from 'fs-extra'; | ||
import { ConfigurationTarget, Uri, WorkspaceFolder } from 'vscode'; | ||
import { getPipRequirementsFiles } from '../provider/venvUtils'; | ||
import { getExtension } from '../../../common/vscodeApis/extensionsApi'; | ||
import { PVSC_EXTENSION_ID } from '../../../common/constants'; | ||
import { PythonExtension } from '../../../api/types'; | ||
import { traceVerbose } from '../../../logging'; | ||
import { getConfiguration } from '../../../common/vscodeApis/workspaceApis'; | ||
import { getWorkspaceStateValue, updateWorkspaceStateValue } from '../../../common/persistentState'; | ||
|
||
export const CREATE_ENV_TRIGGER_SETTING_PART = 'createEnvironment.trigger'; | ||
export const CREATE_ENV_TRIGGER_SETTING = `python.${CREATE_ENV_TRIGGER_SETTING_PART}`; | ||
|
||
export async function fileContainsInlineDependencies(_uri: Uri): Promise<boolean> { | ||
// This is a placeholder for the real implementation of inline dependencies support | ||
// For now we don't detect anything. Once PEP-722/PEP-723 are accepted we can implement | ||
// this properly. | ||
return false; | ||
} | ||
|
||
export async function hasRequirementFiles(workspace: WorkspaceFolder): Promise<boolean> { | ||
const files = await getPipRequirementsFiles(workspace); | ||
const found = (files?.length ?? 0) > 0; | ||
if (found) { | ||
traceVerbose(`Found requirement files: ${workspace.uri.fsPath}`); | ||
} | ||
return found; | ||
} | ||
|
||
export async function hasKnownFiles(workspace: WorkspaceFolder): Promise<boolean> { | ||
const filePaths: string[] = [ | ||
'poetry.lock', | ||
'conda.yaml', | ||
'environment.yaml', | ||
'conda.yml', | ||
'environment.yml', | ||
'Pipfile', | ||
'Pipfile.lock', | ||
].map((fileName) => path.join(workspace.uri.fsPath, fileName)); | ||
const result = await Promise.all(filePaths.map((f) => fsapi.pathExists(f))); | ||
const found = result.some((r) => r); | ||
if (found) { | ||
traceVerbose(`Found known files: ${workspace.uri.fsPath}`); | ||
} | ||
return found; | ||
} | ||
|
||
export async function isGlobalPythonSelected(workspace: WorkspaceFolder): Promise<boolean> { | ||
const extension = getExtension<PythonExtension>(PVSC_EXTENSION_ID); | ||
if (!extension) { | ||
return false; | ||
} | ||
const extensionApi: PythonExtension = extension.exports as PythonExtension; | ||
const interpreter = extensionApi.environments.getActiveEnvironmentPath(workspace.uri); | ||
const details = await extensionApi.environments.resolveEnvironment(interpreter); | ||
const isGlobal = details?.environment === undefined; | ||
if (isGlobal) { | ||
traceVerbose(`Selected python for [${workspace.uri.fsPath}] is [global] type: ${interpreter.path}`); | ||
} | ||
return isGlobal; | ||
} | ||
|
||
/** | ||
* Checks the setting `python.createEnvironment.trigger` to see if we should perform the checks | ||
* to prompt to create an environment. | ||
* @export | ||
* @returns : True if we should prompt to create an environment. | ||
*/ | ||
export function shouldPromptToCreateEnv(): boolean { | ||
const config = getConfiguration('python'); | ||
if (config) { | ||
const value = config.get<string>(CREATE_ENV_TRIGGER_SETTING_PART, 'off'); | ||
return value !== 'off'; | ||
} | ||
|
||
return getWorkspaceStateValue<string>(CREATE_ENV_TRIGGER_SETTING, 'off') !== 'off'; | ||
} | ||
|
||
/** | ||
* Sets `python.createEnvironment.trigger` to 'off' in the user settings. | ||
*/ | ||
export function disableCreateEnvironmentTrigger(): void { | ||
const config = getConfiguration('python'); | ||
if (config) { | ||
config.update('createEnvironment.trigger', 'off', ConfigurationTarget.Global); | ||
} | ||
} | ||
|
||
/** | ||
* Sets trigger to 'off' in workspace persistent state. This disables trigger check | ||
* for the current workspace only. In multi root case, it is disabled for all folders | ||
* in the multi root workspace. | ||
*/ | ||
export async function disableWorkspaceCreateEnvironmentTrigger(): Promise<void> { | ||
await updateWorkspaceStateValue(CREATE_ENV_TRIGGER_SETTING, 'off'); | ||
} | ||
|
||
let _alreadyCreateEnvCriteriaCheck = false; | ||
/** | ||
* Run-once wrapper function for the workspace check to prompt to create an environment. | ||
* @returns : True if we should prompt to c environment. | ||
*/ | ||
export function isCreateEnvWorkspaceCheckNotRun(): boolean { | ||
if (_alreadyCreateEnvCriteriaCheck) { | ||
return false; | ||
} | ||
_alreadyCreateEnvCriteriaCheck = true; | ||
return true; | ||
} |
Oops, something went wrong.