11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT License.
33
4- import { inject , injectable } from 'inversify' ;
4+ import { inject , injectable , optional } from 'inversify' ;
55import { logger } from '../../../platform/logging' ;
66import { getDisplayPath } from '../../../platform/common/platform/fs-paths' ;
77import { IConfigurationService , Resource , type ReadWrite } from '../../../platform/common/types' ;
@@ -18,6 +18,7 @@ import { IJupyterKernelSpec } from '../../types';
1818import { CancellationToken , Uri } from 'vscode' ;
1919import { PYTHON_LANGUAGE } from '../../../platform/common/constants' ;
2020import { trackKernelResourceInformation } from '../../telemetry/helper' ;
21+ import { SqlIntegrationEnvironmentVariablesProvider } from '../../../notebooks/deepnote/integrations/sqlIntegrationEnvironmentVariablesProvider' ;
2122
2223/**
2324 * Class used to fetch environment variables for a kernel.
@@ -30,8 +31,17 @@ export class KernelEnvironmentVariablesService {
3031 @inject ( IEnvironmentVariablesService ) private readonly envVarsService : IEnvironmentVariablesService ,
3132 @inject ( ICustomEnvironmentVariablesProvider )
3233 private readonly customEnvVars : ICustomEnvironmentVariablesProvider ,
33- @inject ( IConfigurationService ) private readonly configService : IConfigurationService
34- ) { }
34+ @inject ( IConfigurationService ) private readonly configService : IConfigurationService ,
35+ @inject ( SqlIntegrationEnvironmentVariablesProvider )
36+ @optional ( )
37+ private readonly sqlIntegrationEnvVars ?: SqlIntegrationEnvironmentVariablesProvider
38+ ) {
39+ logger . info (
40+ `KernelEnvironmentVariablesService: Constructor called, sqlIntegrationEnvVars is ${
41+ sqlIntegrationEnvVars ? 'AVAILABLE' : 'UNDEFINED'
42+ } `
43+ ) ;
44+ }
3545 /**
3646 * Generates the environment variables for the kernel.
3747 *
@@ -51,6 +61,11 @@ export class KernelEnvironmentVariablesService {
5161 kernelSpec : IJupyterKernelSpec ,
5262 token ?: CancellationToken
5363 ) {
64+ logger . info (
65+ `KernelEnvVarsService.getEnvironmentVariables: Called for resource ${
66+ resource ?. toString ( ) || 'undefined'
67+ } , sqlIntegrationEnvVars is ${ this . sqlIntegrationEnvVars ? 'AVAILABLE' : 'UNDEFINED' } `
68+ ) ;
5469 let kernelEnv =
5570 kernelSpec . env && Object . keys ( kernelSpec . env ) . length > 0
5671 ? ( Object . assign ( { } , kernelSpec . env ) as ReadWrite < NodeJS . ProcessEnv > )
@@ -68,7 +83,7 @@ export class KernelEnvironmentVariablesService {
6883 if ( token ?. isCancellationRequested ) {
6984 return ;
7085 }
71- let [ customEnvVars , interpreterEnv ] = await Promise . all ( [
86+ let [ customEnvVars , interpreterEnv , sqlIntegrationEnvVars ] = await Promise . all ( [
7287 this . customEnvVars
7388 . getCustomEnvironmentVariables ( resource , isPythonKernel ? 'RunPythonCode' : 'RunNonPythonCode' , token )
7489 . catch ( noop ) ,
@@ -82,6 +97,24 @@ export class KernelEnvironmentVariablesService {
8297 ) ;
8398 return undefined ;
8499 } )
100+ : undefined ,
101+ this . sqlIntegrationEnvVars
102+ ? this . sqlIntegrationEnvVars
103+ . getEnvironmentVariables ( resource , token )
104+ . then ( ( vars ) => {
105+ if ( vars && Object . keys ( vars ) . length > 0 ) {
106+ logger . info (
107+ `KernelEnvVarsService: Got ${
108+ Object . keys ( vars ) . length
109+ } SQL integration env vars: ${ Object . keys ( vars ) . join ( ', ' ) } `
110+ ) ;
111+ }
112+ return vars ;
113+ } )
114+ . catch < undefined > ( ( ex ) => {
115+ logger . error ( 'Failed to get SQL integration env variables for Kernel' , ex ) ;
116+ return undefined ;
117+ } )
85118 : undefined
86119 ] ) ;
87120 if ( token ?. isCancellationRequested ) {
@@ -123,6 +156,16 @@ export class KernelEnvironmentVariablesService {
123156
124157 Object . assign ( mergedVars , interpreterEnv , kernelEnv ) ; // kernels vars win over interpreter.
125158
159+ // Merge SQL integration environment variables
160+ if ( sqlIntegrationEnvVars ) {
161+ logger . info (
162+ `KernelEnvVarsService: Merging ${
163+ Object . keys ( sqlIntegrationEnvVars ) . length
164+ } SQL integration env vars into kernel env`
165+ ) ;
166+ Object . assign ( mergedVars , sqlIntegrationEnvVars ) ;
167+ }
168+
126169 // If user asks us to, set PYTHONNOUSERSITE
127170 // For more details see here https://github.com/microsoft/vscode-jupyter/issues/8553#issuecomment-997144591
128171 // https://docs.python.org/3/library/site.html#site.ENABLE_USER_SITE
@@ -138,6 +181,16 @@ export class KernelEnvironmentVariablesService {
138181 // We can support this, however since this has not been requested, lets not do it.'
139182 this . envVarsService . mergeVariables ( kernelEnv , mergedVars ) ; // kernels vars win over interpreter.
140183 this . envVarsService . mergeVariables ( customEnvVars , mergedVars ) ; // custom vars win over all.
184+
185+ // Merge SQL integration environment variables
186+ if ( sqlIntegrationEnvVars ) {
187+ logger . info (
188+ `KernelEnvVarsService: Merging ${
189+ Object . keys ( sqlIntegrationEnvVars ) . length
190+ } SQL integration env vars into kernel env (non-python)`
191+ ) ;
192+ this . envVarsService . mergeVariables ( sqlIntegrationEnvVars , mergedVars ) ;
193+ }
141194 }
142195
143196 // env variables in kernelSpecs can contain variables that need to be substituted
0 commit comments