Skip to content

Ensures development mode ids are calculated from development path #97

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 4 commits into from
Dec 18, 2024
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
4 changes: 3 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,9 @@ export interface EnvironmentManager {
readonly displayName?: string;

/**
* The preferred package manager ID for the environment manager.
* The preferred package manager ID for the environment manager. This is a combination
* of publisher id, extension id, and {@link EnvironmentManager.name package manager name}.
* `<publisher-id>.<extension-id>:<package-manager-name>`
*
* @example
* 'ms-python.python:pip'
Expand Down
4 changes: 4 additions & 0 deletions src/common/extension.apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ import { Extension, extensions } from 'vscode';
export function getExtension<T = any>(extensionId: string): Extension<T> | undefined {
return extensions.getExtension(extensionId);
}

export function allExtensions(): readonly Extension<any>[] {
return extensions.all;
}
24 changes: 0 additions & 24 deletions src/common/extensions.apis.ts

This file was deleted.

53 changes: 53 additions & 0 deletions src/common/utils/frameUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { ENVS_EXTENSION_ID, PYTHON_EXTENSION_ID } from '../constants';
import { parseStack } from '../errors/utils';
import { allExtensions, getExtension } from '../extension.apis';

interface FrameData {
filePath: string;
functionName: string;
}

function getFrameData(): FrameData[] {
const frames = parseStack(new Error());
return frames.map((frame) => ({
filePath: frame.getFileName(),
functionName: frame.getFunctionName(),
}));
}

export function getCallingExtension(): string {
const pythonExts = [ENVS_EXTENSION_ID, PYTHON_EXTENSION_ID];

const extensions = allExtensions();
const otherExts = extensions.filter((ext) => !pythonExts.includes(ext.id));
const frames = getFrameData().filter((frame) => !!frame.filePath);

for (const frame of frames) {
const filename = frame.filePath;
if (filename) {
const ext = otherExts.find((ext) => filename.includes(ext.id));
if (ext) {
return ext.id;
}
}
}

// `ms-python.vscode-python-envs` extension in Development mode
const candidates = frames.filter((frame) => otherExts.some((s) => frame.filePath.includes(s.extensionPath)));
const envsExtPath = getExtension(ENVS_EXTENSION_ID)?.extensionPath;
if (!envsExtPath) {
throw new Error('Something went wrong with feature registration');
}

if (candidates.length === 0 && frames.every((frame) => frame.filePath.startsWith(envsExtPath))) {
return PYTHON_EXTENSION_ID;
}

// 3rd party extension in Development mode
const candidateExt = otherExts.find((ext) => candidates[0].filePath.includes(ext.extensionPath));
if (candidateExt) {
return candidateExt.id;
}

throw new Error('Unable to determine calling extension id, registration failed');
}
2 changes: 1 addition & 1 deletion src/features/envManagers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
PythonProjectManager,
PythonProjectSettings,
} from '../internal.api';
import { getCallingExtension } from '../common/extensions.apis';
import { getCallingExtension } from '../common/utils/frameUtils';
import {
EnvironmentManagerAlreadyRegisteredError,
PackageManagerAlreadyRegisteredError,
Expand Down
Loading