@@ -21,6 +21,41 @@ export interface SqlmeshExecInfo {
2121 args : string [ ]
2222}
2323
24+ /**
25+ * Gets the current SQLMesh environment variables that would be used for execution.
26+ * This is useful for debugging and understanding the environment configuration.
27+ *
28+ * @returns A Result containing the environment variables or an error
29+ */
30+ export async function getSqlmeshEnvironment ( ) : Promise < Result < Record < string , string > , string > > {
31+ const interpreterDetails = await getInterpreterDetails ( )
32+ const envVariables = await getPythonEnvVariables ( )
33+ if ( isErr ( envVariables ) ) {
34+ return err ( envVariables . error )
35+ }
36+
37+ const binPath = interpreterDetails . binPath
38+ const virtualEnvPath = binPath && interpreterDetails . isVirtualEnvironment
39+ ? path . dirname ( path . dirname ( binPath ) ) // binPath points to bin dir in venv
40+ : binPath ? path . dirname ( binPath ) : undefined
41+
42+ const env : Record < string , string > = {
43+ ...process . env ,
44+ ...envVariables . value ,
45+ PYTHONPATH : interpreterDetails . path ?. [ 0 ] ?? '' ,
46+ }
47+
48+ if ( virtualEnvPath ) {
49+ env [ 'VIRTUAL_ENV' ] = virtualEnvPath
50+ }
51+
52+ if ( binPath ) {
53+ env [ 'PATH' ] = `${ binPath } ${ path . delimiter } ${ process . env . PATH || '' } `
54+ }
55+
56+ return ok ( env )
57+ }
58+
2459/**
2560 * Returns true if the current project is a Tcloud project. To detect this we,
2661 * 1. Check if the project has a tcloud.yaml file in the project root. If it does, we assume it's a Tcloud project.
@@ -68,23 +103,17 @@ export const getTcloudBin = async (): Promise<Result<SqlmeshExecInfo, ErrorType>
68103 if ( ! fs . existsSync ( binPath ) ) {
69104 return err ( { type : 'tcloud_bin_not_found' } )
70105 }
71- const envVariables = await getPythonEnvVariables ( )
72- if ( isErr ( envVariables ) ) {
106+ const env = await getSqlmeshEnvironment ( )
107+ if ( isErr ( env ) ) {
73108 return err ( {
74109 type : 'generic' ,
75- message : envVariables . error ,
110+ message : env . error ,
76111 } )
77112 }
78113 return ok ( {
79114 bin : binPath ,
80115 workspacePath : interpreterDetails . resource ?. fsPath ?? '' ,
81- env : {
82- ...process . env ,
83- ...envVariables . value ,
84- PYTHONPATH : interpreterDetails . path [ 0 ] ,
85- VIRTUAL_ENV : path . dirname ( interpreterDetails . binPath ! ) ,
86- PATH : `${ interpreterDetails . binPath ! } ${ path . delimiter } ${ process . env . PATH || '' } ` ,
87- } ,
116+ env : env . value ,
88117 args : [ ] ,
89118 } )
90119}
@@ -300,16 +329,17 @@ export const sqlmeshExec = async (): Promise<
300329 }
301330 const binPath = path . join ( interpreterDetails . binPath ! , sqlmesh )
302331 traceLog ( `Bin path: ${ binPath } ` )
332+ const env = await getSqlmeshEnvironment ( )
333+ if ( isErr ( env ) ) {
334+ return err ( {
335+ type : 'generic' ,
336+ message : env . error ,
337+ } )
338+ }
303339 return ok ( {
304340 bin : binPath ,
305341 workspacePath,
306- env : {
307- ...process . env ,
308- ...envVariables . value ,
309- PYTHONPATH : interpreterDetails . path ?. [ 0 ] ,
310- VIRTUAL_ENV : path . dirname ( path . dirname ( interpreterDetails . binPath ! ) ) , // binPath now points to bin dir
311- PATH : `${ interpreterDetails . binPath ! } ${ path . delimiter } ${ process . env . PATH || '' } ` ,
312- } ,
342+ env : env . value ,
313343 args : [ ] ,
314344 } )
315345 } else {
@@ -319,13 +349,17 @@ export const sqlmeshExec = async (): Promise<
319349 type : 'sqlmesh_not_found' ,
320350 } )
321351 }
352+ const env = await getSqlmeshEnvironment ( )
353+ if ( isErr ( env ) ) {
354+ return err ( {
355+ type : 'generic' ,
356+ message : env . error ,
357+ } )
358+ }
322359 return ok ( {
323360 bin : sqlmesh ,
324361 workspacePath,
325- env : {
326- ...process . env ,
327- ...envVariables . value ,
328- } ,
362+ env : env . value ,
329363 args : [ ] ,
330364 } )
331365 }
@@ -455,19 +489,27 @@ export const sqlmeshLspExec = async (): Promise<
455489 if ( isErr ( ensuredDependencies ) ) {
456490 return ensuredDependencies
457491 }
492+ const env = await getSqlmeshEnvironment ( )
493+ if ( isErr ( env ) ) {
494+ return err ( {
495+ type : 'generic' ,
496+ message : env . error ,
497+ } )
498+ }
458499 return ok ( {
459500 bin : binPath ,
460501 workspacePath,
461- env : {
462- ...process . env ,
463- ...envVariables . value ,
464- PYTHONPATH : interpreterDetails . path ?. [ 0 ] ,
465- VIRTUAL_ENV : path . dirname ( path . dirname ( interpreterDetails . binPath ! ) ) , // binPath now points to bin dir
466- PATH : `${ interpreterDetails . binPath ! } ${ path . delimiter } ${ process . env . PATH || '' } ` , // binPath already points to the bin directory
467- } ,
502+ env : env . value ,
468503 args : [ ] ,
469504 } )
470505 } else {
506+ const env = await getSqlmeshEnvironment ( )
507+ if ( isErr ( env ) ) {
508+ return err ( {
509+ type : 'generic' ,
510+ message : env . error ,
511+ } )
512+ }
471513 const exists = await doesExecutableExist ( sqlmeshLSP )
472514 if ( ! exists ) {
473515 return err ( {
@@ -477,10 +519,7 @@ export const sqlmeshLspExec = async (): Promise<
477519 return ok ( {
478520 bin : sqlmeshLSP ,
479521 workspacePath,
480- env : {
481- ...process . env ,
482- ...envVariables . value ,
483- } ,
522+ env : env . value ,
484523 args : [ ] ,
485524 } )
486525 }
0 commit comments