Skip to content

Commit c23d799

Browse files
author
David Kutugata
authored
fixed path that is saved in the cache (#11525)
changed the name of the cache file catch exception if what is saved in cache is not a string avoid search if the kernel name is the default
1 parent 6133159 commit c23d799

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const kernelPaths = new Map([
2929
['macJupyterPath', path.join('Library', 'Jupyter', 'kernels')],
3030
['kernel', path.join('share', 'jupyter', 'kernels')]
3131
]);
32+
const cacheFile = 'kernelSpecPathCache.json';
33+
const defaultSpecName = 'python_defaultSpec_';
3234

3335
// https://jupyter-client.readthedocs.io/en/stable/kernels.html
3436
const connectionFilePlaceholder = '{connection_file}';
@@ -68,7 +70,7 @@ export class KernelFinder implements IKernelFinder {
6870
this.cache = await this.readCache();
6971
let foundKernel: IJupyterKernelSpec | undefined;
7072

71-
if (kernelName) {
73+
if (kernelName && !kernelName.includes(defaultSpecName)) {
7274
let kernelSpec = await this.searchCache(kernelName);
7375

7476
if (kernelSpec) {
@@ -192,8 +194,13 @@ export class KernelFinder implements IKernelFinder {
192194
private async getKernelSpecFromDisk(paths: string[], kernelName: string): Promise<IJupyterKernelSpec | undefined> {
193195
const promises = paths.map((kernelPath) => this.file.search('**/kernel.json', kernelPath));
194196
const searchResults = await Promise.all(promises);
195-
searchResults.forEach((result) => {
196-
this.cache.push(...result);
197+
searchResults.forEach((result, i) => {
198+
result.forEach((res) => {
199+
const specPath = path.join(paths[i], res);
200+
if (!this.cache.includes(specPath)) {
201+
this.cache.push(specPath);
202+
}
203+
});
197204
});
198205

199206
return this.searchCache(kernelName);
@@ -207,7 +214,7 @@ export class KernelFinder implements IKernelFinder {
207214
// This creates a default kernel spec. When launched, 'python' argument will map to using the interpreter
208215
// associated with the current resource for launching.
209216
const defaultSpec: Kernel.ISpecModel = {
210-
name: `python_defaultSpec_${Date.now()}`,
217+
name: defaultSpecName + Date.now().toString(),
211218
language: 'python',
212219
display_name: this.activeInterpreter?.displayName ? this.activeInterpreter.displayName : 'Python 3',
213220
metadata: {},
@@ -221,7 +228,7 @@ export class KernelFinder implements IKernelFinder {
221228
private async readCache(): Promise<string[]> {
222229
try {
223230
return JSON.parse(
224-
await this.file.readFile(path.join(this.context.globalStoragePath, 'kernelSpecCache.json'))
231+
await this.file.readFile(path.join(this.context.globalStoragePath, cacheFile))
225232
) as string[];
226233
} catch {
227234
traceInfo('No kernelSpec cache found.');
@@ -230,18 +237,24 @@ export class KernelFinder implements IKernelFinder {
230237
}
231238

232239
private async writeCache(cache: string[]) {
233-
await this.file.writeFile(
234-
path.join(this.context.globalStoragePath, 'kernelSpecCache.json'),
235-
JSON.stringify(cache)
236-
);
240+
await this.file.writeFile(path.join(this.context.globalStoragePath, cacheFile), JSON.stringify(cache));
237241
}
238242

239243
private async searchCache(kernelName: string): Promise<IJupyterKernelSpec | undefined> {
240-
const kernelJsonFile = this.cache.find((kernelPath) => path.basename(path.dirname(kernelPath)) === kernelName);
244+
const kernelJsonFile = this.cache.find((kernelPath) => {
245+
try {
246+
return path.basename(path.dirname(kernelPath)) === kernelName;
247+
} catch (e) {
248+
traceInfo('KernelSpec path in cache is not a string.', e);
249+
return false;
250+
}
251+
});
241252

242253
if (kernelJsonFile) {
243254
const kernelJson = JSON.parse(await this.file.readFile(kernelJsonFile));
244-
return new JupyterKernelSpec(kernelJson, kernelJsonFile);
255+
const spec = new JupyterKernelSpec(kernelJson, kernelJsonFile);
256+
spec.name = kernelName;
257+
return spec;
245258
}
246259

247260
return undefined;

src/test/datascience/kernelFinder.unit.test.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ suite('Kernel Finder', () => {
3535
const interpreters: PythonInterpreter[] = [];
3636
let resource: Resource;
3737
const kernelName = 'testKernel';
38+
const cacheFile = 'kernelSpecPathCache.json';
3839
const kernel: JupyterKernelSpec = {
3940
name: 'testKernel',
4041
language: 'python',
@@ -43,7 +44,7 @@ suite('Kernel Finder', () => {
4344
metadata: {},
4445
env: {},
4546
argv: ['<python path>', '-m', 'ipykernel_launcher', '-f', '{connection_file}'],
46-
specFile: path.join('kernels', kernelName, 'kernel.json')
47+
specFile: path.join('1', 'share', 'jupyter', 'kernels', kernelName, 'kernel.json')
4748
};
4849

4950
function setupFileSystem() {
@@ -55,9 +56,9 @@ suite('Kernel Finder', () => {
5556
.setup((fs) => fs.search(typemoq.It.isAnyString(), typemoq.It.isAnyString()))
5657
.returns(() =>
5758
Promise.resolve([
58-
path.join('kernels', kernel.name, 'kernel.json'),
59-
path.join('kernels', 'kernelA', 'kernel.json'),
60-
path.join('kernels', 'kernelB', 'kernel.json')
59+
path.join(kernel.name, 'kernel.json'),
60+
path.join('kernelA', 'kernel.json'),
61+
path.join('kernelB', 'kernel.json')
6162
])
6263
);
6364
}
@@ -127,7 +128,7 @@ suite('Kernel Finder', () => {
127128
fileSystem
128129
.setup((fs) => fs.readFile(typemoq.It.isAnyString()))
129130
.returns((param: string) => {
130-
if (param.includes('kernelSpecCache.json')) {
131+
if (param.includes(cacheFile)) {
131132
return Promise.resolve(`["${kernel.name}"]`);
132133
}
133134
return Promise.resolve(JSON.stringify(kernel));
@@ -142,7 +143,7 @@ suite('Kernel Finder', () => {
142143
fileSystem
143144
.setup((fs) => fs.readFile(typemoq.It.isAnyString()))
144145
.returns((pathParam: string) => {
145-
if (pathParam.includes('kernelSpecCache.json')) {
146+
if (pathParam.includes(cacheFile)) {
146147
return Promise.resolve('[]');
147148
}
148149
return Promise.resolve(JSON.stringify(kernel));
@@ -160,7 +161,7 @@ suite('Kernel Finder', () => {
160161
fileSystem
161162
.setup((fs) => fs.readFile(typemoq.It.isAnyString()))
162163
.returns((pathParam: string) => {
163-
if (pathParam.includes('kernelSpecCache.json')) {
164+
if (pathParam.includes(cacheFile)) {
164165
return Promise.resolve('[]');
165166
}
166167
return Promise.resolve(JSON.stringify(kernel));
@@ -178,7 +179,7 @@ suite('Kernel Finder', () => {
178179
fileSystem
179180
.setup((fs) => fs.readFile(typemoq.It.isAnyString()))
180181
.returns((pathParam: string) => {
181-
if (pathParam.includes('kernelSpecCache.json')) {
182+
if (pathParam.includes(cacheFile)) {
182183
return Promise.resolve('[]');
183184
}
184185
return Promise.resolve(JSON.stringify(kernel));
@@ -193,7 +194,7 @@ suite('Kernel Finder', () => {
193194
fileSystem
194195
.setup((fs) => fs.readFile(typemoq.It.isAnyString()))
195196
.returns((pathParam: string) => {
196-
if (pathParam.includes('kernelSpecCache.json')) {
197+
if (pathParam.includes(cacheFile)) {
197198
return Promise.resolve('[]');
198199
}
199200
return Promise.resolve('{}');
@@ -209,7 +210,7 @@ suite('Kernel Finder', () => {
209210
fileSystem
210211
.setup((fs) => fs.readFile(typemoq.It.isAnyString()))
211212
.returns((pathParam: string) => {
212-
if (pathParam.includes('kernelSpecCache.json')) {
213+
if (pathParam.includes(cacheFile)) {
213214
return Promise.resolve('[]');
214215
}
215216
return Promise.resolve('{}');
@@ -224,7 +225,7 @@ suite('Kernel Finder', () => {
224225
fileSystem
225226
.setup((fs) => fs.readFile(typemoq.It.isAnyString()))
226227
.returns((pathParam: string) => {
227-
if (pathParam.includes('kernelSpecCache.json')) {
228+
if (pathParam.includes(cacheFile)) {
228229
return Promise.resolve(`["${spec.path}"]`);
229230
}
230231
return Promise.resolve(JSON.stringify(spec));
@@ -244,7 +245,7 @@ suite('Kernel Finder', () => {
244245
fileSystem
245246
.setup((fs) => fs.readFile(typemoq.It.isAnyString()))
246247
.returns((pathParam: string) => {
247-
if (pathParam.includes('kernelSpecCache.json')) {
248+
if (pathParam.includes(cacheFile)) {
248249
return Promise.resolve('[]');
249250
} else if (pathParam.includes('kernelA')) {
250251
const specA = {
@@ -267,7 +268,7 @@ suite('Kernel Finder', () => {
267268
fileSystem
268269
.setup((fs) => fs.readFile(typemoq.It.isAnyString()))
269270
.returns((pathParam: string) => {
270-
if (pathParam.includes('kernelSpecCache.json')) {
271+
if (pathParam.includes(cacheFile)) {
271272
return Promise.resolve(
272273
JSON.stringify([
273274
path.join('kernels', kernel.name, 'kernel.json'),

0 commit comments

Comments
 (0)