Skip to content

Commit 0daecb6

Browse files
committed
Fix kernel registration on windows
1 parent bb69fa5 commit 0daecb6

File tree

5 files changed

+40
-10
lines changed

5 files changed

+40
-10
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Change Log
22

3+
### 1.1.0
4+
5+
- Fix kernel registration on Windows.
36
## [1.0.0]
47

58
- Initial release

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ This extension will automatically install the necessary dependencies required to
2424

2525
## Release Notes
2626

27+
### 1.1.0
28+
29+
- Fix kernel registration on Windows.
2730
### 1.0.0
2831

29-
Initial release
32+
- Initial release

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"publisher": "donjayamanne",
44
"displayName": "TypeScript Notebooks",
55
"description": "TypeScript with Jupyter Notebooks",
6-
"version": "1.0.0",
6+
"version": "1.1.0",
77
"engines": {
88
"vscode": "^1.53.0"
99
},

src/extension.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ export async function activate(context: ExtensionContext) {
1818
installKernelSpec(),
1919
registerWithJupyter(),
2020
optIntoNativeNotebooks(),
21-
configureEditor(),
22-
installKernelSpec(),
21+
configureEditor()
2322
]).catch(noop);
2423
registerCommands(context, outputChannel);
2524
}

src/kernel.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ import * as path from "path";
22
import { homedir } from "os";
33
import * as fs from "fs-extra";
44
import { logMessage } from "./logger";
5+
import { format } from "util";
56

67
const winJupyterPath = path.join("AppData", "Roaming", "jupyter", "kernels");
78
const linuxJupyterPath = path.join(".local", "share", "jupyter", "kernels");
89
const macJupyterPath = path.join("Library", "Jupyter", "kernels");
9-
10-
function getKernelsDir(platform: string = process.platform) {
11-
if (/^win/.test(platform)) {
10+
const isWindows = /^win/.test(process.platform);
11+
function getKernelsDir() {
12+
if (isWindows) {
1213
return winJupyterPath;
13-
} else if (/^darwin/.test(platform)) {
14+
} else if (/^darwin/.test(process.platform)) {
1415
return macJupyterPath;
15-
} else if (/^linux/.test(platform)) {
16+
} else if (/^linux/.test(process.platform)) {
1617
return linuxJupyterPath;
1718
} else {
1819
throw new Error("Unable to determine the OS");
@@ -30,19 +31,43 @@ async function ensureKernelsDirectory() {
3031
await fs.ensureDir(kernelsDir);
3132
}
3233

34+
const tslabExecutable = isWindows ? "tslab.cmd" : "tslab";
35+
3336
const kernelSpec = {
34-
argv: ["tslab", "kernel", "--config-path", "{connection_file}"],
37+
argv: [tslabExecutable, "kernel", "--config-path", "{connection_file}"],
3538
// eslint-disable-next-line @typescript-eslint/naming-convention
3639
display_name: "TypeScript",
3740
language: "typescript",
3841
};
3942
const kernelSpecName = "typescript";
4043

44+
export async function updateKernelSpec(kernelSpecFile: string) {
45+
if (!isWindows) {
46+
return;
47+
}
48+
try {
49+
const contents: typeof kernelSpec = await fs.readJSON(kernelSpecFile, {
50+
encoding: "utf8",
51+
});
52+
if (contents.argv[0] === tslabExecutable) {
53+
return;
54+
}
55+
await fs.writeFile(kernelSpecFile, JSON.stringify(kernelSpec), {
56+
flag: "w",
57+
});
58+
logMessage(`Existing KernelSpec updated ${kernelSpecFile}`);
59+
} catch (ex) {
60+
logMessage(`Failed to update kernel.json ${format(ex)}`);
61+
}
62+
return;
63+
}
64+
4165
export async function installKernelSpec() {
4266
await ensureKernelsDirectory();
4367
const kernelSpecDirectory = path.join(kernelsDir, kernelSpecName);
4468
const kernelSpecFile = path.join(kernelSpecDirectory, "kernel.json");
4569
if (await fs.pathExists(kernelSpecFile)) {
70+
updateKernelSpec(kernelSpecFile);
4671
logMessage(`KernelSpec already exists ${kernelSpecFile}`);
4772
return;
4873
}

0 commit comments

Comments
 (0)