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

Identification of caller on remote servers #15293

Merged
merged 3 commits into from
Mar 6, 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
120 changes: 120 additions & 0 deletions src/platform/common/application/extension.node.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { assert } from 'chai';
import { anything, when } from 'ts-mockito';
import { Disposable, EventEmitter, Uri } from 'vscode';
import { mockedVSCodeNamespaces, resetVSCodeMocks } from '../../../test/vscode-mock';
import { DisposableStore } from '../utils/lifecycle';
import { Extensions } from './extensions.node';
import { EOL } from 'os';

const stack1 = [
'Error: ',
' at Extensions.determineExtensionFromCallStack (/Users/username/Development/vsc/vscode-jupyter/src/platform/common/application/extensions.node.ts:18:26)',
' at JupyterKernelServiceFactory.getService (/Users/username/Development/vsc/vscode-jupyter/src/standalone/api/unstable/kernelApi.ts:43:38)',
' at getKernelService (/Users/username/Development/vsc/vscode-jupyter/src/standalone/api/unstable/index.ts:92:33)',
' at Object.getKernelService (/Users/username/Development/vsc/vscode-jupyter/src/standalone/api/index.ts:43:33)',
' at activateFeature (/Users/username/.vscode-insiders/extensions/ms-toolsai.vscode-jupyter-powertoys-0.1.0/out/main.js:13466:56)',
' at activate (/Users/username/.vscode-insiders/extensions/ms-toolsai.vscode-jupyter-powertoys-0.1.0/out/main.js:13479:9)',
' at activate (/Users/username/.vscode-insiders/extensions/ms-toolsai.vscode-jupyter-powertoys-0.1.0/out/main.js:14160:5)',
' at u.n (/Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:140:6255)',
' at u.m (/Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:140:6218)',
' at u.l (/Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/out/vs/workbench/api/node/extensionHostProcess.js:140:5675)'
];
const stack2 = [
'Error:',
' at kg.determineExtensionFromCallStack (/storage/username/.vscode-insiders/extensions/ms-toolsai.jupyter-2024.3.0/dist/extension.node.js:203:3695)',
' at F9 (/storage/username/.vscode-insiders/extensions/ms-toolsai.jupyter-2024.3.0/dist/extension.node.js:198:24742)',
' at Object.getKernelService (/storage/username/.vscode-insiders/extensions/ms-toolsai.jupyter-2024.3.0/dist/extension.node.js:198:26312)',
' at activateFeature (/storage/username/.vscode-insiders/extensions/ms-toolsai.vscode-jupyter-powertoys-0.1.0/out/main.js:335:56)',
' at process.processTicksAndRejections (node:internal/process/task_queues:95:5)',
' at async activate (/storage/username/.vscode-insiders/extensions/ms-toolsai.vscode-jupyter-powertoys-0.1.0/out/main.js:343:9)',
' at async activate (/storage/username/.vscode-insiders/extensions/ms-toolsai.vscode-jupyter-powertoys-0.1.0/out/main.js:13995:5)',
' at async u.n (/storage/username/.vscode-insiders/cli/servers/Insiders-bb171489789c9a49e985a4a2c8694138d70d42c1/server/out/vs/workbench/api/node/extensionHostProcess.js:140:6255)',
' at async u.m (/storage/username/.vscode-insiders/cli/servers/Insiders-bb171489789c9a49e985a4a2c8694138d70d42c1/server/out/vs/workbench/api/node/extensionHostProcess.js:140:6218)',
' at async u.l (/storage/username/.vscode-insiders/cli/servers/Insiders-bb171489789c9a49e985a4a2c8694138d70d42c1/server/out/vs/workbench/api/node/extensionHostProcess.js:140:5675)'
];
const extensions1 = [
{
id: 'vscode.bat',
packageJSON: { displayName: 'vscode.bat' },
extensionUri: Uri.file('/Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/extensions/bat')
},
{
id: 'donjayamanne.kusto',
packageJSON: { displayName: 'donjayamanne.kusto' },
extensionUri: Uri.file('/Users/username/.vscode-insiders/extensions/donjayamanne.kusto-0.4.4')
},
{
id: 'ms-python.python',
packageJSON: { displayName: 'ms-python.python' },
extensionUri: Uri.file('/Users/username/.vscode-insiders/extensions/ms-python.python-2024.3.10640539')
},
{
id: 'ms-toolsai.vscode-jupyter-powertoys',
packageJSON: { displayName: 'ms-toolsai.vscode-jupyter-powertoys' },
extensionUri: Uri.file('/Users/username/.vscode-insiders/extensions/ms-toolsai.vscode-jupyter-powertoys-0.1.0')
},
{
id: 'ms-toolsai.jupyter',
packageJSON: { displayName: 'ms-toolsai.jupyter' },
extensionUri: Uri.file('/Users/username/Development/vsc/vscode-jupyter')
}
];
const extensions2 = [
{
id: 'vscode.bat',
packageJSON: { displayName: 'vscode.bat', version: '1.0.0' },
extensionUri: Uri.file('/Applications/Visual Studio Code - Insiders.app/Contents/Resources/app/extensions/bat')
},
{
id: 'donjayamanne.kusto',
packageJSON: { displayName: 'donjayamanne.kusto', version: '0.4.4' },
extensionUri: Uri.file('/Users/username/.vscode-insiders/extensions/donjayamanne.kusto-0.4.4')
},
{
id: 'ms-python.python',
packageJSON: { displayName: 'ms-python.python', version: '2024.3.10640539' },
extensionUri: Uri.file('/storage/username/.vscode-server-insiders/extensions/ms-python.python-2024.3.10640539')
},
{
id: 'ms-toolsai.vscode-jupyter-powertoys',
packageJSON: { displayName: 'ms-toolsai.vscode-jupyter-powertoys', version: '0.1.0' },
extensionUri: Uri.file(
'/storage/username/.vscode-server-insiders/extensions/ms-toolsai.vscode-jupyter-powertoys-0.1.0'
)
},
{
id: 'ms-toolsai.jupyter',
packageJSON: { displayName: 'ms-toolsai.jupyter', version: '2024.3.0' },
extensionUri: Uri.file('/storage/username/.vscode-server-insiders/extensions/ms-toolsai.jupyter-2024.3.0')
}
];

suite(`Interpreter Service`, () => {
const disposables = new DisposableStore();
setup(() => {
when(mockedVSCodeNamespaces.extensions.onDidChange).thenReturn(disposables.add(new EventEmitter<void>()).event);
disposables.add(new Disposable(() => resetVSCodeMocks()));
});
teardown(() => disposables.clear());
test('Identify from callstack', () => {
when(mockedVSCodeNamespaces.extensions.all).thenReturn(extensions1 as any);
when(mockedVSCodeNamespaces.extensions.getExtension(anything())).thenCall(function (id: string) {
return extensions1.find((e) => e.id === id);
});
const { displayName, extensionId } = new Extensions([]).determineExtensionFromCallStack(stack1.join(EOL));
assert.strictEqual(extensionId, 'ms-toolsai.vscode-jupyter-powertoys');
assert.strictEqual(displayName, 'ms-toolsai.vscode-jupyter-powertoys');
});
test('Identify from callstack on remote server', () => {
when(mockedVSCodeNamespaces.extensions.all).thenReturn(extensions2 as any);
when(mockedVSCodeNamespaces.extensions.getExtension(anything())).thenCall(function (id: string) {
return extensions2.find((e) => e.id === id);
});
const { displayName, extensionId } = new Extensions([]).determineExtensionFromCallStack(stack2.join(EOL));
assert.strictEqual(extensionId, 'ms-toolsai.vscode-jupyter-powertoys');
assert.strictEqual(displayName, 'ms-toolsai.vscode-jupyter-powertoys');
});
});
78 changes: 67 additions & 11 deletions src/platform/common/application/extensions.node.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { injectable } from 'inversify';
import { extensions } from 'vscode';
import { IExtensions } from '../types';
import { inject, injectable } from 'inversify';
import { extensions, type Extension } from 'vscode';
import { IDisposableRegistry, IExtensions } from '../types';
import { DataScience } from '../utils/localize';
import { parseStack } from '../../errors';
import { JVSC_EXTENSION_ID, unknownExtensionId } from '../constants';
Expand All @@ -14,31 +14,55 @@ import { traceError } from '../../logging';
*/
@injectable()
export class Extensions implements IExtensions {
private _extensions: readonly Extension<unknown>[] = [];
private get extensions() {
return this._extensions;
}
constructor(@inject(IDisposableRegistry) disposables: IDisposableRegistry) {
disposables.push(extensions.onDidChange(() => (this._extensions = extensions.all)));
this._extensions = extensions.all;
}
public determineExtensionFromCallStack(stack?: string): { extensionId: string; displayName: string } {
stack = stack || new Error().stack;
try {
const jupyterExtRoot = extensions.getExtension(JVSC_EXTENSION_ID)!.extensionUri.toString().toLowerCase();
let frames: string[] = [];
if (stack) {
const jupyterExtRoot = extensions
.getExtension(JVSC_EXTENSION_ID)!
.extensionUri.toString()
.toLowerCase();
const frames = stack
frames = stack
.split('\n')
.map((f) => {
const result = /\((.*)\)/.exec(f);
if (result) {
return result[1];
const filenameWithPositions = result ? result[1] : undefined;
try {
const filename = /\((.*)\:\d*\:\d*\)/.exec(f);
if (!filename) {
return filenameWithPositions;
}
if (!filenameWithPositions) {
return filename[1];
}
if (filenameWithPositions.startsWith(filename[1])) {
return filename[1];
}
} catch {
//
}
return filenameWithPositions;
})
.filter((item) => item && !item.toLowerCase().startsWith(jupyterExtRoot)) as string[];
const folderParts = jupyterExtRoot.split(/[\\/]/);
const indexOfJupyterExtFolder = folderParts.findIndex((item) => item.startsWith(JVSC_EXTENSION_ID));
const extensionFolderName =
indexOfJupyterExtFolder === -1 ? undefined : folderParts[indexOfJupyterExtFolder - 1];

parseStack(new Error('Ex')).forEach((item) => {
const fileName = item.getFileName();
if (fileName && !fileName.toLowerCase().startsWith(jupyterExtRoot)) {
frames.push(fileName);
}
});
for (const frame of frames) {
const matchingExt = extensions.all.find(
const matchingExt = this.extensions.find(
(ext) =>
ext.id !== JVSC_EXTENSION_ID &&
(frame.toLowerCase().startsWith(ext.extensionUri.fsPath.toLowerCase()) ||
Expand All @@ -48,7 +72,39 @@ export class Extensions implements IExtensions {
return { extensionId: matchingExt.id, displayName: matchingExt.packageJSON.displayName };
}
}
// We're just after the extensions folder.
let extensionPathFromFrames = frames.find((frame) => frame.includes(JVSC_EXTENSION_ID));
if (extensionPathFromFrames) {
extensionPathFromFrames = extensionPathFromFrames.substring(
0,
extensionPathFromFrames.indexOf(JVSC_EXTENSION_ID) - 1
);
}

if (!extensionFolderName || !extensionPathFromFrames) {
return { extensionId: unknownExtensionId, displayName: DataScience.unknownPackage };
}
// Possible Jupyter extension root is ~/.vscode-server-insiders/extensions/ms-toolsai.jupyter-2024.3.0
// But call stack has paths such as ~/.vscode-insiders/extensions/ms-toolsai.vscode-jupyter-powertoys-0.1.0/out/main.js
for (const frame of frames.filter((f) => {
return f.startsWith(extensionPathFromFrames!) && !f.includes(JVSC_EXTENSION_ID);
})) {
let extensionIdInFrame = frame
.substring(extensionPathFromFrames.length)
.substring(1)
.split(/[\\/]/)[0];
if (extensionIdInFrame.includes('-')) {
extensionIdInFrame = extensionIdInFrame.substring(0, extensionIdInFrame.lastIndexOf('-'));
}
const matchingExt = this.extensions.find((ext) => ext.id === extensionIdInFrame);
if (matchingExt) {
return { extensionId: matchingExt.id, displayName: matchingExt.packageJSON.displayName };
}
}
}
traceError(`Unable to determine the caller of the extension API for trace stack`, stack);
traceError(`Jupyter Root`, jupyterExtRoot);
traceError(`Frames`, frames);
return { extensionId: unknownExtensionId, displayName: DataScience.unknownPackage };
} catch (ex) {
traceError(`Unable to determine the caller of the extension API for trace stack.`, stack);
Expand Down
Loading