Skip to content

Commit f9150b2

Browse files
authored
Remove hardcoding of position of connection file in kernelspec… (#11299)
* Remove hardcoding of position of connection file * Use correct name for connection files * Use same types
1 parent 1198a2e commit f9150b2

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

src/client/datascience/jupyter/kernels/jupyterKernelSpec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33
'use strict';
44
import type { Kernel } from '@jupyterlab/services';
5+
import type { JSONObject } from '@phosphor/coreutils';
56
import * as path from 'path';
67
import { CancellationToken } from 'vscode';
78
import { createPromiseFromCancellation } from '../../../common/cancellation';
@@ -17,6 +18,8 @@ export class JupyterKernelSpec implements IJupyterKernelSpec {
1718
public specFile: string | undefined;
1819
public display_name: string;
1920
public argv: string[];
21+
public readonly env?: JSONObject;
22+
2023
// tslint:disable-next-line: no-any
2124
public metadata?: Record<string, any> & { interpreter?: Partial<PythonInterpreter> };
2225
constructor(specModel: Kernel.ISpecModel, file?: string) {
@@ -27,6 +30,8 @@ export class JupyterKernelSpec implements IJupyterKernelSpec {
2730
this.specFile = file;
2831
this.display_name = specModel.display_name;
2932
this.metadata = specModel.metadata;
33+
// tslint:disable-next-line: no-any
34+
this.env = specModel.env as any;
3035
}
3136
}
3237

src/client/datascience/kernel-launcher/kernelFinder.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ const kernelPaths = new Map([
2020
['kernel', path.join('share', 'jupyter', 'kernels')]
2121
]);
2222

23+
// https://jupyter-client.readthedocs.io/en/stable/kernels.html
24+
const connectionFilePlaceholder = '{connection_file}';
25+
26+
export function findIndexOfConnectionFile(kernelSpec: Readonly<IJupyterKernelSpec>): number {
27+
return kernelSpec.argv.indexOf(connectionFilePlaceholder);
28+
}
29+
2330
// This class searches for a kernel that matches the given kernel name.
2431
// First it seraches on a global persistent state, then on the installed python interpreters,
2532
// and finally on the default locations that jupyter installs kernels on.
@@ -212,7 +219,13 @@ export class KernelFinder implements IKernelFinder {
212219
path: this.activeInterpreter?.path!,
213220
display_name: this.activeInterpreter?.displayName ? this.activeInterpreter.displayName : 'Python 3',
214221
metadata: {},
215-
argv: [this.activeInterpreter?.path || 'python', '-m', 'ipykernel_launcher', '-f', '<connection_file>']
222+
argv: [
223+
this.activeInterpreter?.path || 'python',
224+
'-m',
225+
'ipykernel_launcher',
226+
'-f',
227+
connectionFilePlaceholder
228+
]
216229
};
217230

218231
this.cache.push(defaultSpec);

src/client/datascience/kernel-launcher/kernelLauncher.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { createDeferred, Deferred } from '../../common/utils/async';
1616
import * as localize from '../../common/utils/localize';
1717
import { noop } from '../../common/utils/misc';
1818
import { IJupyterKernelSpec } from '../types';
19+
import { findIndexOfConnectionFile } from './kernelFinder';
1920
import { IKernelConnection, IKernelFinder, IKernelLauncher, IKernelProcess } from './types';
2021

2122
// Launches and disposes a kernel process given a kernelspec and a resource or python interpreter.
@@ -54,15 +55,18 @@ class KernelProcess implements IKernelProcess {
5455

5556
public async launch(): Promise<void> {
5657
this.connectionFile = await this.file.createTemporaryFile('.json');
57-
5858
const args = [...this._kernelSpec.argv];
5959
await this.file.writeFile(this.connectionFile.filePath, JSON.stringify(this._connection), {
6060
encoding: 'utf-8',
6161
flag: 'w'
6262
});
6363

6464
// Inclide the conenction file in the arguments and remove the first argument which should be python
65-
args[4] = this.connectionFile.filePath;
65+
const indexOfConnectionFile = findIndexOfConnectionFile(this._kernelSpec);
66+
if (indexOfConnectionFile === -1) {
67+
throw new Error(`Connection file not found in kernelspec json args, ${args.join(' ')}`);
68+
}
69+
args[indexOfConnectionFile] = this.connectionFile.filePath;
6670
args.splice(0, 1);
6771

6872
const executionService = await this.executionFactory.create({

src/client/datascience/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,10 @@ export interface IJupyterKernelSpec {
404404
// tslint:disable-next-line: no-any
405405
readonly metadata?: Record<string, any> & { interpreter?: Partial<PythonInterpreter> };
406406
readonly argv: string[];
407+
/**
408+
* A dictionary of environment variables to set for the kernel.
409+
*/
410+
readonly env?: JSONObject;
407411
}
408412

409413
export const INotebookImporter = Symbol('INotebookImporter');

0 commit comments

Comments
 (0)