Skip to content
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

Finalizing design of proposed API for python environments #19841

Merged
merged 60 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
0af181c
Add a definitions file containing environments API
Aug 16, 2022
cf40603
Move definitions file
Aug 16, 2022
6e5976b
Update apis
Aug 16, 2022
f885fd3
Again
Aug 16, 2022
feafa67
Update API based on api sync
Aug 17, 2022
53a4b89
Turn getEnvironmentPaths API to sync
Aug 31, 2022
bca26b1
Add to types
Aug 31, 2022
1fa9a14
Emit the workspace folder in event
Aug 31, 2022
5a45c9c
Merge EnvironmentPath into event type
Aug 31, 2022
b99c71d
Jupyter API review
Sep 7, 2022
51a1c55
Jupyter review Day 2
Sep 8, 2022
5bbb7e5
Jupyter review 3
Sep 10, 2022
0701b60
Derive Environment type using ResolvedEnvironment
Sep 12, 2022
bbba12a
Derive ResolvedEnvironment using Environment type
Sep 12, 2022
5e3f510
Use non-nullable types
Sep 12, 2022
f5716a6
Rename getRefreshPromise to waitOnRefresh
Sep 12, 2022
b824bf9
Update types based on guidelines
Sep 12, 2022
6ebfa8f
Remove `bestEffortRefresh` in favor of a new option
Sep 12, 2022
388699f
Add implementation
Sep 15, 2022
133aa66
Add documentation
Sep 15, 2022
be4a3c5
Some updates
Sep 15, 2022
7d6b914
Simplify buffer decoder. (#19836)
karthiknadig Sep 14, 2022
8109967
Rename api
Sep 15, 2022
54d8814
API sync review part 1
Sep 15, 2022
c1ac005
API sync review part 2 - update refresh API
Sep 15, 2022
d32f6e6
Update activeEnvironment API
Sep 15, 2022
7676a49
Add a few tests
Sep 15, 2022
803a137
Be consistent with interface names and types
Sep 16, 2022
62e2ce6
Clarify using NonNullable
Sep 16, 2022
ef4be8f
Use env type to figure out type information
Sep 19, 2022
ae1c81c
Try kind to infer the source of the environment
Sep 19, 2022
ebaae15
Flatten the APIs out under the environment umbrella
Sep 20, 2022
ae525bd
Modify refresh options
Sep 20, 2022
62d8d03
Add deprecated type
Sep 20, 2022
101f160
Rename source to tools and pluck it out of the `environment` umbrella
Sep 20, 2022
4689bea
Add test
Sep 20, 2022
4e436cd
Fix some tests
Sep 20, 2022
bd6f16d
Add test for refresh state
Sep 20, 2022
5edeab1
Rearrange types
Sep 20, 2022
c657d91
Fix tests
Sep 20, 2022
0f7b55d
Modify activeEnvironment API
Sep 21, 2022
bd7cd67
API sync
Sep 21, 2022
fcf788c
Use getEnvId everywhere
Sep 21, 2022
8f87701
Update `package.json`
Sep 21, 2022
310e401
Update `package-lock`
Sep 21, 2022
0cb3047
Install stack trace types
Sep 21, 2022
eb829f7
Update `package-lock`
Sep 21, 2022
7e85709
Add API for determining extension from callstack
Sep 21, 2022
6b4734d
Address some comments
Sep 23, 2022
ffc3ed2
Make all properties readonly
Sep 23, 2022
895aaf6
Make environments API inline
Sep 26, 2022
b5769ff
Address internal team reviews
Sep 26, 2022
5fe5e94
Modify KnownEnvType
Sep 26, 2022
e7f261e
Add telemetry for each API
Sep 26, 2022
3c081b7
Add tests for telemetry
Sep 26, 2022
db83f3e
Add comments
Sep 26, 2022
c50f13d
Add support for deprecated API types
Sep 26, 2022
f0763f8
Add logging and telemetry for deprecated APIs
Sep 26, 2022
7303339
Add delay when sending telemetry
Sep 26, 2022
fff4e11
Fix tests
Sep 26, 2022
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
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,6 @@ src/client/common/application/languageService.ts
src/client/common/application/clipboard.ts
src/client/common/application/workspace.ts
src/client/common/application/debugSessionTelemetry.ts
src/client/common/application/extensions.ts
src/client/common/application/documentManager.ts
src/client/common/application/debugService.ts
src/client/common/application/commands/reloadCommand.ts
Expand Down
18 changes: 15 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1805,6 +1805,7 @@
"request-progress": "^3.0.0",
"rxjs": "^6.5.4",
"rxjs-compat": "^6.5.4",
"stack-trace": "0.0.10",
"semver": "^5.5.0",
"sudo-prompt": "^9.2.1",
"tmp": "^0.0.33",
Expand Down Expand Up @@ -1841,11 +1842,12 @@
"@types/semver": "^5.5.0",
"@types/shortid": "^0.0.29",
"@types/sinon": "^10.0.11",
"@types/stack-trace": "0.0.29",
"@types/tmp": "^0.0.33",
"@types/uuid": "^8.3.4",
"@types/vscode": "~1.68.0",
"@types/winreg": "^1.2.30",
"@types/which": "^2.0.1",
"@types/winreg": "^1.2.30",
"@types/xml2js": "^0.4.2",
"@typescript-eslint/eslint-plugin": "^3.7.0",
"@typescript-eslint/parser": "^3.7.0",
Expand Down
136 changes: 0 additions & 136 deletions src/client/apiTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import { Event, Uri } from 'vscode';
import { Resource } from './common/types';
import { IDataViewerDataProvider, IJupyterUriProvider } from './jupyter/types';
import { EnvPathType, PythonEnvKind } from './pythonEnvironments/base/info';
import { GetRefreshEnvironmentsOptions, ProgressNotificationEvent } from './pythonEnvironments/base/locator';

/*
* Do not introduce any breaking changes to this API.
Expand Down Expand Up @@ -88,137 +86,3 @@ export interface IExtensionApi {
registerRemoteServerProvider(serverProvider: IJupyterUriProvider): void;
};
}

export interface EnvironmentDetailsOptions {
useCache: boolean;
}

export interface EnvironmentDetails {
interpreterPath: string;
envFolderPath?: string;
version: string[];
environmentType: PythonEnvKind[];
metadata: Record<string, unknown>;
}

export interface EnvironmentsChangedParams {
/**
* Path to environment folder or path to interpreter that uniquely identifies an environment.
* Virtual environments lacking an interpreter are identified by environment folder paths,
* whereas other envs can be identified using interpreter path.
*/
path?: string;
type: 'add' | 'remove' | 'update' | 'clear-all';
}

export interface ActiveEnvironmentChangedParams {
/**
* Path to environment folder or path to interpreter that uniquely identifies an environment.
* Virtual environments lacking an interpreter are identified by environment folder paths,
* whereas other envs can be identified using interpreter path.
*/
path: string;
resource?: Uri;
}

export interface IProposedExtensionAPI {
environment: {
/**
* An event that is emitted when execution details (for a resource) change. For instance, when interpreter configuration changes.
*/
readonly onDidChangeExecutionDetails: Event<Uri | undefined>;
/**
* Returns all the details the consumer needs to execute code within the selected environment,
* corresponding to the specified resource taking into account any workspace-specific settings
* for the workspace to which this resource belongs.
* @param {Resource} [resource] A resource for which the setting is asked for.
* * When no resource is provided, the setting scoped to the first workspace folder is returned.
* * If no folder is present, it returns the global setting.
* @returns {({ execCommand: string[] | undefined })}
*/
getExecutionDetails(
resource?: Resource,
): Promise<{
/**
* E.g of execution commands returned could be,
* * `['<path to the interpreter set in settings>']`
* * `['<path to the interpreter selected by the extension when setting is not set>']`
* * `['conda', 'run', 'python']` which is used to run from within Conda environments.
* or something similar for some other Python environments.
*
* @type {(string[] | undefined)} When return value is `undefined`, it means no interpreter is set.
* Otherwise, join the items returned using space to construct the full execution command.
*/
execCommand: string[] | undefined;
}>;
/**
* Returns the path to the python binary selected by the user or as in the settings.
* This is just the path to the python binary, this does not provide activation or any
* other activation command. The `resource` if provided will be used to determine the
* python binary in a multi-root scenario. If resource is `undefined` then the API
* returns what ever is set for the workspace.
* @param resource : Uri of a file or workspace
*/
getActiveEnvironmentPath(resource?: Resource): Promise<EnvPathType | undefined>;
/**
* Returns details for the given interpreter. Details such as absolute interpreter path,
* version, type (conda, pyenv, etc). Metadata such as `sysPrefix` can be found under
* metadata field.
* @param path : Full path to environment folder or interpreter whose details you need.
* @param options : [optional]
* * useCache : When true, cache is checked first for any data, returns even if there
* is partial data.
*/
getEnvironmentDetails(
path: string,
options?: EnvironmentDetailsOptions,
): Promise<EnvironmentDetails | undefined>;
/**
* Returns paths to environments that uniquely identifies an environment found by the extension
* at the time of calling. This API will *not* trigger a refresh. If a refresh is going on it
* will *not* wait for the refresh to finish. This will return what is known so far. To get
* complete list `await` on promise returned by `getRefreshPromise()`.
*
* Virtual environments lacking an interpreter are identified by environment folder paths,
* whereas other envs can be identified using interpreter path.
*/
getEnvironmentPaths(): Promise<EnvPathType[] | undefined>;
/**
* Sets the active environment path for the python extension for the resource. Configuration target
* will always be the workspace folder.
* @param path : Full path to environment folder or interpreter to set.
* @param resource : [optional] Uri of a file ro workspace to scope to a particular workspace
* folder.
*/
setActiveEnvironment(path: string, resource?: Resource): Promise<void>;
/**
* This API will re-trigger environment discovery. Extensions can wait on the returned
* promise to get the updated environment list. If there is a refresh already going on
* then it returns the promise for that refresh.
* @param options : [optional]
* * clearCache : When true, this will clear the cache before environment refresh
* is triggered.
*/
refreshEnvironment(): Promise<EnvPathType[] | undefined>;
/**
* Tracks discovery progress for current list of known environments, i.e when it starts, finishes or any other relevant
* stage. Note the progress for a particular query is currently not tracked or reported, this only indicates progress of
* the entire collection.
*/
readonly onRefreshProgress: Event<ProgressNotificationEvent>;
/**
* Returns a promise for the ongoing refresh. Returns `undefined` if there are no active
* refreshes going on.
*/
getRefreshPromise(options?: GetRefreshEnvironmentsOptions): Promise<void> | undefined;
/**
* This event is triggered when the known environment list changes, like when a environment
* is found, existing environment is removed, or some details changed on an environment.
*/
onDidEnvironmentsChanged: Event<EnvironmentsChangedParams[]>;
/**
* This event is triggered when the active environment changes.
*/
onDidActiveEnvironmentChanged: Event<ActiveEnvironmentChangedParams>;
};
}
69 changes: 66 additions & 3 deletions src/client/common/application/extensions.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
/* eslint-disable class-methods-use-this */
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

'use strict';

import { injectable } from 'inversify';
import { inject, injectable } from 'inversify';
import { Event, Extension, extensions } from 'vscode';
import * as stacktrace from 'stack-trace';
import * as path from 'path';
import { IExtensions } from '../types';
import { IFileSystem } from '../platform/types';
import { EXTENSION_ROOT_DIR } from '../constants';

/**
* Provides functions for tracking the list of extensions that VSCode has installed.
*/
@injectable()
export class Extensions implements IExtensions {
constructor(@inject(IFileSystem) private readonly fs: IFileSystem) {}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
public get all(): readonly Extension<any>[] {
return extensions.all;
}
Expand All @@ -17,7 +28,59 @@ export class Extensions implements IExtensions {
return extensions.onDidChange;
}

public getExtension(extensionId: any) {
public getExtension(extensionId: string): Extension<unknown> | undefined {
return extensions.getExtension(extensionId);
}

/**
* Code borrowed from:
* https://github.com/microsoft/vscode-jupyter/blob/67fe33d072f11d6443cf232a06bed0ac5e24682c/src/platform/common/application/extensions.node.ts
*/
public async determineExtensionFromCallStack(): Promise<{ extensionId: string; displayName: string }> {
const { stack } = new Error();
if (stack) {
const pythonExtRoot = path.join(EXTENSION_ROOT_DIR.toLowerCase(), path.sep);
const frames = stack
.split('\n')
.map((f) => {
const result = /\((.*)\)/.exec(f);
if (result) {
return result[1];
}
return undefined;
})
.filter((item) => item && !item.toLowerCase().startsWith(pythonExtRoot))
.filter((item) =>
this.all.some(
(ext) => item!.includes(ext.extensionUri.path) || item!.includes(ext.extensionUri.fsPath),
),
) as string[];
stacktrace.parse(new Error('Ex')).forEach((item) => {
const fileName = item.getFileName();
if (fileName && !fileName.toLowerCase().startsWith(pythonExtRoot)) {
frames.push(fileName);
}
});
for (const frame of frames) {
// This file is from a different extension. Try to find its `package.json`.
let dirName = path.dirname(frame);
let last = frame;
while (dirName && dirName.length < last.length) {
const possiblePackageJson = path.join(dirName, 'package.json');
if (await this.fs.pathExists(possiblePackageJson)) {
const text = await this.fs.readFile(possiblePackageJson);
try {
const json = JSON.parse(text);
return { extensionId: `${json.publisher}.${json.name}`, displayName: json.displayName };
} catch {
// If parse fails, then not an extension.
}
}
last = dirName;
dirName = path.dirname(dirName);
}
}
}
return { extensionId: 'unknown', displayName: 'unknown' };
}
}
5 changes: 5 additions & 0 deletions src/client/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,11 @@ export interface IExtensions {
* @return An extension or `undefined`.
*/
getExtension<T>(extensionId: string): Extension<T> | undefined;

/**
* Determines which extension called into our extension code based on call stacks.
*/
determineExtensionFromCallStack(): Promise<{ extensionId: string; displayName: string }>;
}

export const IBrowserService = Symbol('IBrowserService');
Expand Down
Loading