@@ -16,6 +16,7 @@ import { StopWatch } from '../../../common/utils/stopWatch';
1616import { IInterpreterService , PythonInterpreter } from '../../../interpreter/contracts' ;
1717import { IEventNamePropertyMapping , sendTelemetryEvent } from '../../../telemetry' ;
1818import { KnownNotebookLanguages , Telemetry } from '../../constants' ;
19+ import { IKernelFinder } from '../../kernel-launcher/types' ;
1920import { reportAction } from '../../progress/decorator' ;
2021import { ReportableAction } from '../../progress/types' ;
2122import { IJupyterKernelSpec , IJupyterSessionManager , IKernelDependencyService } from '../../types' ;
@@ -57,7 +58,8 @@ export class KernelSelector {
5758 @inject ( IApplicationShell ) private readonly applicationShell : IApplicationShell ,
5859 @inject ( KernelService ) private readonly kernelService : KernelService ,
5960 @inject ( IInterpreterService ) private readonly interpreterService : IInterpreterService ,
60- @inject ( IKernelDependencyService ) private readonly kernelDepdencyService : IKernelDependencyService
61+ @inject ( IKernelDependencyService ) private readonly kernelDepdencyService : IKernelDependencyService ,
62+ @inject ( IKernelFinder ) private readonly kernelFinder : IKernelFinder
6163 ) { }
6264
6365 /**
@@ -160,6 +162,7 @@ export class KernelSelector {
160162 @reportAction ( ReportableAction . KernelsGetKernelForLocalConnection )
161163 public async getKernelForLocalConnection (
162164 resource : Resource ,
165+ type : 'raw' | 'jupyter' | 'noConnection' ,
163166 sessionManager ?: IJupyterSessionManager ,
164167 notebookMetadata ?: nbformat . INotebookMetadata ,
165168 disableUI ?: boolean ,
@@ -171,68 +174,28 @@ export class KernelSelector {
171174 interpreterFound : false ,
172175 promptedToSelect : false
173176 } ;
174- // When this method is called, we know we've started a local jupyter server.
177+ // When this method is called, we know we've started a local jupyter server or are connecting raw
175178 // Lets pre-warm the list of local kernels.
176179 this . selectionProvider
177- . getKernelSelectionsForLocalSession ( resource , 'jupyter' , sessionManager , cancelToken )
180+ . getKernelSelectionsForLocalSession ( resource , type , sessionManager , cancelToken )
178181 . ignoreErrors ( ) ;
179182
180183 let selection : KernelSpecInterpreter = { } ;
181- if ( notebookMetadata ?. kernelspec ) {
182- selection . kernelSpec = await this . kernelService . findMatchingKernelSpec (
183- notebookMetadata ?. kernelspec ,
184+
185+ if ( type === 'jupyter' ) {
186+ selection = await this . getKernelForLocalJupyterConnection (
187+ resource ,
188+ stopWatch ,
189+ telemetryProps ,
184190 sessionManager ,
191+ notebookMetadata ,
192+ disableUI ,
185193 cancelToken
186194 ) ;
187- if ( selection . kernelSpec ) {
188- selection . interpreter = await this . kernelService . findMatchingInterpreter (
189- selection . kernelSpec ,
190- cancelToken
191- ) ;
192- sendTelemetryEvent ( Telemetry . UseExistingKernel ) ;
193-
194- // Make sure we update the environment in the kernel before using it
195- await this . kernelService . updateKernelEnvironment (
196- selection . interpreter ,
197- selection . kernelSpec ,
198- cancelToken
199- ) ;
200- } else {
201- // No kernel info, hence prmopt to use current interpreter as a kernel.
202- const activeInterpreter = await this . interpreterService . getActiveInterpreter ( resource ) ;
203- if ( activeInterpreter ) {
204- selection = await this . useInterpreterAsKernel (
205- resource ,
206- activeInterpreter ,
207- 'jupyter' ,
208- notebookMetadata . kernelspec . display_name ,
209- sessionManager ,
210- disableUI ,
211- cancelToken
212- ) ;
213- } else {
214- telemetryProps . promptedToSelect = true ;
215- selection = await this . selectLocalKernel (
216- resource ,
217- 'jupyter' ,
218- stopWatch ,
219- sessionManager ,
220- cancelToken
221- ) ;
222- }
223- }
224- } else {
225- // No kernel info, hence use current interpreter as a kernel.
226- const activeInterpreter = await this . interpreterService . getActiveInterpreter ( resource ) ;
227- if ( activeInterpreter ) {
228- selection . interpreter = activeInterpreter ;
229- selection . kernelSpec = await this . kernelService . searchAndRegisterKernel (
230- activeInterpreter ,
231- disableUI ,
232- cancelToken
233- ) ;
234- }
195+ } else if ( type === 'raw' ) {
196+ selection = await this . getKernelForLocalRawConnection ( resource , notebookMetadata , cancelToken ) ;
235197 }
198+
236199 // If still not found, log an error (this seems possible for some people, so use the default)
237200 if ( ! selection . kernelSpec ) {
238201 traceError ( 'Jupyter Kernel Spec not found for a local connection' ) ;
@@ -313,6 +276,99 @@ export class KernelSelector {
313276 interpreter : interpreter
314277 } ;
315278 }
279+
280+ // Get our kernelspec and matching interpreter for a connection to a local jupyter server
281+ private async getKernelForLocalJupyterConnection (
282+ resource : Resource ,
283+ stopWatch : StopWatch ,
284+ telemetryProps : IEventNamePropertyMapping [ Telemetry . FindKernelForLocalConnection ] ,
285+ sessionManager ?: IJupyterSessionManager ,
286+ notebookMetadata ?: nbformat . INotebookMetadata ,
287+ disableUI ?: boolean ,
288+ cancelToken ?: CancellationToken
289+ ) : Promise < KernelSpecInterpreter > {
290+ let selection : KernelSpecInterpreter = { } ;
291+ if ( notebookMetadata ?. kernelspec ) {
292+ selection . kernelSpec = await this . kernelService . findMatchingKernelSpec (
293+ notebookMetadata ?. kernelspec ,
294+ sessionManager ,
295+ cancelToken
296+ ) ;
297+ if ( selection . kernelSpec ) {
298+ selection . interpreter = await this . kernelService . findMatchingInterpreter (
299+ selection . kernelSpec ,
300+ cancelToken
301+ ) ;
302+ sendTelemetryEvent ( Telemetry . UseExistingKernel ) ;
303+
304+ // Make sure we update the environment in the kernel before using it
305+ await this . kernelService . updateKernelEnvironment (
306+ selection . interpreter ,
307+ selection . kernelSpec ,
308+ cancelToken
309+ ) ;
310+ } else {
311+ // No kernel info, hence prmopt to use current interpreter as a kernel.
312+ const activeInterpreter = await this . interpreterService . getActiveInterpreter ( resource ) ;
313+ if ( activeInterpreter ) {
314+ selection = await this . useInterpreterAsKernel (
315+ resource ,
316+ activeInterpreter ,
317+ 'jupyter' ,
318+ notebookMetadata . kernelspec . display_name ,
319+ sessionManager ,
320+ disableUI ,
321+ cancelToken
322+ ) ;
323+ } else {
324+ telemetryProps . promptedToSelect = true ;
325+ selection = await this . selectLocalKernel (
326+ resource ,
327+ 'jupyter' ,
328+ stopWatch ,
329+ sessionManager ,
330+ cancelToken
331+ ) ;
332+ }
333+ }
334+ } else {
335+ // No kernel info, hence use current interpreter as a kernel.
336+ const activeInterpreter = await this . interpreterService . getActiveInterpreter ( resource ) ;
337+ if ( activeInterpreter ) {
338+ selection . interpreter = activeInterpreter ;
339+ selection . kernelSpec = await this . kernelService . searchAndRegisterKernel (
340+ activeInterpreter ,
341+ disableUI ,
342+ cancelToken
343+ ) ;
344+ }
345+ }
346+
347+ return selection ;
348+ }
349+
350+ // Get our kernelspec and interpreter for a local raw connection
351+ private async getKernelForLocalRawConnection (
352+ resource : Resource ,
353+ notebookMetadata ?: nbformat . INotebookMetadata ,
354+ cancelToken ?: CancellationToken
355+ ) : Promise < KernelSpecInterpreter > {
356+ const selection : KernelSpecInterpreter = { } ;
357+
358+ // First use our kernel finder to locate a kernelspec on disk
359+ selection . kernelSpec = await this . kernelFinder . findKernelSpec (
360+ resource ,
361+ notebookMetadata ?. kernelspec ?. name ,
362+ cancelToken
363+ ) ;
364+
365+ if ( selection . kernelSpec ) {
366+ // Locate the interpreter that matches our kernelspec
367+ selection . interpreter = await this . kernelService . findMatchingInterpreter ( selection . kernelSpec , cancelToken ) ;
368+ }
369+ return selection ;
370+ }
371+
316372 private async selectKernel (
317373 resource : Resource ,
318374 type : 'raw' | 'jupyter' | 'noConnection' ,
0 commit comments