Skip to content

Commit

Permalink
Use NPM package to pull in types for Python Ext (#14007)
Browse files Browse the repository at this point in the history
* Use NPM package to pull in types for Python Ext

* Work around

* fix formatting
  • Loading branch information
DonJayamanne authored Aug 1, 2023
1 parent 438c5b7 commit dda80e4
Show file tree
Hide file tree
Showing 16 changed files with 101 additions and 335 deletions.
26 changes: 26 additions & 0 deletions build/ci/postInstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,31 @@ function verifyMomentIsOnlyUsedByJupyterLabCoreUtils() {
throw new Error(`Moment is being used by other packages (${otherPackagesUsingMoment.join(', ')}).`);
}
}

/**
* Work around for https://github.com/microsoft/vscode-python/issues/21720
*
* The npm package @vscode/python-extension has a peer dependency on @types/vscode.
* However Jupyter extensions uses @vscode-dts with the cli arg '-f' instead of @types/vscode.
* The webpack builds when running the command `npm list --production --parseable --depth=99999 --loglevel=error`
* Work around is to remove the peerDepencency on @types/vscode.
*/
function fixPythonExtensionNpmPackageJson() {
const jsonFilePath = path.join(
__dirname,
'..',
'..',
'node_modules',
'@vscode',
'python-extension',
'package.json'
);
const packageJson = JSON.parse(fs.readFileSync(jsonFilePath));
if (packageJson.peerDependencies && packageJson.peerDependencies['@types/vscode']) {
delete packageJson.peerDependencies['@types/vscode'];
fs.writeFileSync(jsonFilePath, JSON.stringify(packageJson, undefined, 4));
}
}
async function downloadZmqBinaries() {
if (common.getBundleConfiguration() === common.bundleConfiguration.web) {
// No need to download zmq binaries for web.
Expand All @@ -254,6 +279,7 @@ fixVariableNameInKernelDefaultJs();
removeUnnecessaryLoggingFromKernelDefault();
updateJSDomTypeDefinition();
fixStripComments();
fixPythonExtensionNpmPackageJson();
verifyMomentIsOnlyUsedByJupyterLabCoreUtils();
downloadZmqBinaries()
.then(() => process.exit(0))
Expand Down
31 changes: 31 additions & 0 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2018,8 +2018,8 @@
"clean": "gulp clean",
"updateBuildNumber": "gulp updateBuildNumber",
"webpack": "webpack",
"download-api": "vscode-dts dev",
"postdownload-api": "vscode-dts main",
"download-api": "vscode-dts dev -f",
"postdownload-api": "vscode-dts main -f",
"generateTelemetry": "gulp generateTelemetry",
"openInBrowser": "vscode-test-web --extensionDevelopmentPath=. ./src/test/datascience",
"startJupyterServer": "node build/preDebugWebTest.js",
Expand All @@ -2044,6 +2044,7 @@
"@nteract/messaging": "^7.0.0",
"@vscode/extension-telemetry": "^0.7.7",
"@vscode/jupyter-lsp-middleware": "^0.2.50",
"@vscode/python-extension": "^1.0.2",
"ajv-keywords": "^3.5.2",
"ansi-to-html": "^0.6.7",
"bootstrap": "^4.3.1",
Expand Down
4 changes: 2 additions & 2 deletions src/extension.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ import {
isCI,
isTestExecution,
JUPYTER_OUTPUT_CHANNEL,
PythonExtension,
PythonExtensionId,
STANDARD_OUTPUT_CHANNEL
} from './platform/common/constants';
import { getDisplayPath } from './platform/common/platform/fs-paths';
Expand Down Expand Up @@ -283,7 +283,7 @@ function addOutputChannel(context: IExtensionContext, serviceManager: IServiceMa
// Log env info.
standardOutputChannel.appendLine(`${env.appName} (${version}, ${env.remoteName}, ${env.appHost})`);
standardOutputChannel.appendLine(`Jupyter Extension Version: ${context.extension.packageJSON['version']}.`);
const pythonExtension = extensions.getExtension(PythonExtension);
const pythonExtension = extensions.getExtension(PythonExtensionId);
if (pythonExtension) {
standardOutputChannel.appendLine(`Python Extension Version: ${pythonExtension.packageJSON['version']}.`);
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/extension.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ import {
isCI,
isTestExecution,
JUPYTER_OUTPUT_CHANNEL,
PythonExtension,
PythonExtensionId,
STANDARD_OUTPUT_CHANNEL
} from './platform/common/constants';
import { getJupyterOutputChannel } from './standalone/devTools/jupyterOutputChannel';
Expand Down Expand Up @@ -257,7 +257,7 @@ function addOutputChannel(context: IExtensionContext, serviceManager: IServiceMa
// Log env info.
standardOutputChannel.appendLine(`${env.appName} (${version}, ${env.remoteName}, ${env.appHost})`);
standardOutputChannel.appendLine(`Jupyter Extension Version: ${context.extension.packageJSON['version']}.`);
const pythonExtension = extensions.getExtension(PythonExtension);
const pythonExtension = extensions.getExtension(PythonExtensionId);
if (pythonExtension) {
standardOutputChannel.appendLine(`Python Extension Version: ${pythonExtension.packageJSON['version']}.`);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ import { JupyterPaths } from '../../../kernels/raw/finder/jupyterPaths.node';
import { IPythonApiProvider, IPythonExtensionChecker } from '../../../platform/api/types';
import { pythonEnvToJupyterEnv } from '../../../platform/api/pythonApi';
import { createInterpreterKernelSpec, getKernelId } from '../../../kernels/helpers';
import { Environment } from '../../../platform/api/pythonApiTypes';
import { noop } from '../../../platform/common/utils/misc';
import { IExtensionSyncActivationService } from '../../../platform/activation/types';
import { IInterpreterService } from '../../../platform/interpreter/contracts';
import { KernelFinder } from '../../../kernels/kernelFinder';
import { Environment } from '@vscode/python-extension';

// Provides the UI to select a Kernel Source for a given notebook document
@injectable()
Expand Down
26 changes: 13 additions & 13 deletions src/platform/api/pythonApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as localize from '../common/utils/localize';
import { injectable, inject } from 'inversify';
import { sendTelemetryEvent } from '../../telemetry';
import { IWorkspaceService, IApplicationShell, ICommandManager } from '../common/application/types';
import { isCI, PythonExtension, Telemetry } from '../common/constants';
import { isCI, PythonExtensionId, Telemetry } from '../common/constants';
import { IExtensions, IDisposableRegistry, Resource, IExtensionContext } from '../common/types';
import { createDeferred, sleep } from '../common/utils/async';
import { traceError, traceInfo, traceInfoIfCI, traceVerbose, traceWarning } from '../logging';
Expand All @@ -20,12 +20,12 @@ import { areInterpreterPathsSame, getInterpreterHash } from '../pythonEnvironmen
import { EnvironmentType, PythonEnvironment } from '../pythonEnvironments/info';
import { areObjectsWithUrisTheSame, isUri, noop } from '../common/utils/misc';
import { StopWatch } from '../common/utils/stopWatch';
import { Environment, KnownEnvironmentTools, ProposedExtensionAPI, ResolvedEnvironment } from './pythonApiTypes';
import { PromiseMonitor } from '../common/utils/promises';
import { PythonExtensionActicationFailedError } from '../errors/pythonExtActivationFailedError';
import { PythonExtensionApiNotExportedError } from '../errors/pythonExtApiNotExportedError';
import { getOSType, OSType } from '../common/utils/platform';
import { SemVer } from 'semver';
import { ResolvedEnvironment, KnownEnvironmentTools, Environment, PythonExtension } from '@vscode/python-extension';

export function deserializePythonEnvironment(
pythonVersion: Partial<PythonEnvironment_PythonApi> | undefined,
Expand Down Expand Up @@ -254,9 +254,9 @@ export class OldPythonApiProvider implements IPythonApiProvider {
this.init().catch(noop);
return this.api.promise;
}
public async getNewApi(): Promise<ProposedExtensionAPI | undefined> {
public async getNewApi(): Promise<PythonExtension | undefined> {
await this.init();
const extension = this.extensions.getExtension<ProposedExtensionAPI>(PythonExtension);
const extension = this.extensions.getExtension<PythonExtension>(PythonExtensionId);
if (extension?.packageJSON?.version) {
this._pythonExtensionVersion = new SemVer(extension?.packageJSON?.version);
}
Expand All @@ -276,7 +276,7 @@ export class OldPythonApiProvider implements IPythonApiProvider {
if (this.initialized) {
return;
}
const pythonExtension = this.extensions.getExtension<{ jupyter: { registerHooks(): void } }>(PythonExtension);
const pythonExtension = this.extensions.getExtension<{ jupyter: { registerHooks(): void } }>(PythonExtensionId);
if (!pythonExtension) {
await this.extensionChecker.showPythonExtensionInstallRequiredPrompt();
} else {
Expand All @@ -288,7 +288,7 @@ export class OldPythonApiProvider implements IPythonApiProvider {
if (this.hooksRegistered) {
return;
}
const pythonExtension = this.extensions.getExtension<{ jupyter: { registerHooks(): void } }>(PythonExtension);
const pythonExtension = this.extensions.getExtension<{ jupyter: { registerHooks(): void } }>(PythonExtensionId);
if (!pythonExtension) {
return;
}
Expand Down Expand Up @@ -347,15 +347,15 @@ export class PythonExtensionChecker implements IPythonExtensionChecker {
}

public get isPythonExtensionInstalled() {
return this.extensions.getExtension(PythonExtension) !== undefined;
return this.extensions.getExtension(PythonExtensionId) !== undefined;
}
public get isPythonExtensionActive() {
return this.extensions.getExtension(PythonExtension)?.isActive === true;
return this.extensions.getExtension(PythonExtensionId)?.isActive === true;
}

// Directly install the python extension instead of just showing the extension open page
public async directlyInstallPythonExtension(): Promise<void> {
return this.commandManager.executeCommand('workbench.extensions.installExtension', PythonExtension, {
return this.commandManager.executeCommand('workbench.extensions.installExtension', PythonExtensionId, {
context: { skipWalkthrough: true }
});
}
Expand Down Expand Up @@ -386,7 +386,7 @@ export class PythonExtensionChecker implements IPythonExtensionChecker {
}
private async installPythonExtension() {
// Have the user install python
this.commandManager.executeCommand('extension.open', PythonExtension).then(noop, noop);
this.commandManager.executeCommand('extension.open', PythonExtensionId).then(noop, noop);
}

private async extensionsChangeHandler(): Promise<void> {
Expand Down Expand Up @@ -445,8 +445,8 @@ export class InterpreterService implements IInterpreterService {
public onDidEnvironmentVariablesChange = this._onDidEnvironmentVariablesChange.event;
private eventHandlerAdded?: boolean;
private interpreterListCachePromise: Promise<PythonEnvironment[]> | undefined = undefined;
private apiPromise: Promise<ProposedExtensionAPI | undefined> | undefined;
private api?: ProposedExtensionAPI;
private apiPromise: Promise<PythonExtension | undefined> | undefined;
private api?: PythonExtension;
private _status: 'refreshing' | 'idle' = 'idle';
public get status() {
return this._status;
Expand Down Expand Up @@ -772,7 +772,7 @@ export class InterpreterService implements IInterpreterService {
}
this.pendingInterpretersChangeEventTriggers.clear();
}
private async getApi(): Promise<ProposedExtensionAPI | undefined> {
private async getApi(): Promise<PythonExtension | undefined> {
if (!this.extensionChecker.isPythonExtensionInstalled) {
return;
}
Expand Down
12 changes: 6 additions & 6 deletions src/platform/api/pythonApi.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
ActiveEnvironmentPathChangeEvent,
EnvironmentsChangeEvent,
EnvironmentVariablesChangeEvent,
ProposedExtensionAPI
} from './pythonApiTypes';
PythonExtension
} from '@vscode/python-extension';
import { IPythonApiProvider, IPythonExtensionChecker } from './types';

suite(`Interpreter Service`, () => {
Expand All @@ -32,8 +32,8 @@ suite(`Interpreter Service`, () => {
let onDidChangeActiveEnvironmentPath: EventEmitter<ActiveEnvironmentPathChangeEvent>;
let onDidChangeEnvironments: EventEmitter<EnvironmentsChangeEvent>;
let onDidEnvironmentVariablesChange: EventEmitter<EnvironmentVariablesChangeEvent>;
let newPythonApi: ProposedExtensionAPI;
let environments: ProposedExtensionAPI['environments'];
let newPythonApi: PythonExtension;
let environments: PythonExtension['environments'];
setup(() => {
interpreterService = mock<IInterpreterService>();
apiProvider = mock<IPythonApiProvider>();
Expand All @@ -51,8 +51,8 @@ suite(`Interpreter Service`, () => {
disposables.push(onDidChangeEnvironments);
disposables.push(onDidEnvironmentVariablesChange);

newPythonApi = mock<ProposedExtensionAPI>();
environments = mock<ProposedExtensionAPI['environments']>();
newPythonApi = mock<PythonExtension>();
environments = mock<PythonExtension['environments']>();
when(newPythonApi.environments).thenReturn(instance(environments));
when(environments.onDidChangeActiveEnvironmentPath).thenReturn(onDidChangeActiveEnvironmentPath.event);
when(environments.onDidChangeEnvironments).thenReturn(onDidChangeEnvironments.event);
Expand Down
Loading

0 comments on commit dda80e4

Please sign in to comment.