Skip to content

Commit f5ebfbb

Browse files
benfdkingCopilot
andauthored
feat(vscode): print env into terminal (#4789)
Co-authored-by: Copilot <[email protected]>
1 parent 50a219c commit f5ebfbb

File tree

5 files changed

+120
-48
lines changed

5 files changed

+120
-48
lines changed

vscode/extension/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@
7373
"title": "SQLMesh: Restart Servers",
7474
"description": "SQLMesh"
7575
},
76+
{
77+
"command": "sqlmesh.printEnvironment",
78+
"title": "SQLMesh: Print Environment Variables",
79+
"description": "SQLMesh"
80+
},
7681
{
7782
"command": "sqlmesh.signin",
7883
"title": "SQLMesh: Sign in to Tobiko Cloud",
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as vscode from 'vscode'
2+
import { getSqlmeshEnvironment } from '../utilities/sqlmesh/sqlmesh'
3+
import { isErr } from '@bus/result'
4+
import { IS_WINDOWS } from '../utilities/isWindows'
5+
6+
export function printEnvironment() {
7+
return async () => {
8+
const envResult = await getSqlmeshEnvironment()
9+
10+
if (isErr(envResult)) {
11+
await vscode.window.showErrorMessage(envResult.error)
12+
return
13+
}
14+
15+
const env = envResult.value
16+
17+
// Create a new terminal with the SQLMesh environment
18+
const terminal = vscode.window.createTerminal({
19+
name: 'SQLMesh Environment',
20+
env: env,
21+
})
22+
23+
// Show the terminal
24+
terminal.show()
25+
26+
// Run the appropriate command to display environment variables
27+
if (IS_WINDOWS) {
28+
// On Windows, use 'set' command
29+
terminal.sendText('set')
30+
} else {
31+
// On Unix-like systems, use 'env' command
32+
terminal.sendText('env | sort')
33+
}
34+
35+
// Show a notification
36+
vscode.window.showInformationMessage(
37+
'SQLMesh environment variables displayed in terminal',
38+
)
39+
}
40+
}

vscode/extension/src/extension.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { signIn } from './commands/signin'
1414
import { signInSpecifyFlow } from './commands/signinSpecifyFlow'
1515
import { renderModel, reRenderModelForSourceFile } from './commands/renderModel'
1616
import { stop } from './commands/stop'
17+
import { printEnvironment } from './commands/printEnvironment'
1718
import { isErr } from '@bus/result'
1819
import { handleError } from './utilities/errors'
1920
import { selector, completionProvider } from './completion/completion'
@@ -161,6 +162,7 @@ export async function activate(context: vscode.ExtensionContext) {
161162
await restart()
162163
}),
163164
registerCommand(`sqlmesh.stop`, stop(lspClient)),
165+
registerCommand(`sqlmesh.printEnvironment`, printEnvironment()),
164166
)
165167

166168
const result = await lspClient.start()

vscode/extension/src/lsp/lsp.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,7 @@ export class LSPClient implements Disposable {
8181
transport: TransportKind.stdio,
8282
options: {
8383
cwd: workspacePath,
84-
// TODO: This is a temporary fix to avoid the issue with the LSP server
85-
// crashing when the number of workers is too high. This is a workaround
86-
// to avoid the issue. Once fixed, we should remove the whole env block.
87-
env: {
88-
MAX_FORK_WORKERS: '1',
89-
...process.env,
90-
...sqlmesh.value.env,
91-
},
84+
env: sqlmesh.value.env,
9285
},
9386
args: sqlmesh.value.args,
9487
},
@@ -97,14 +90,7 @@ export class LSPClient implements Disposable {
9790
transport: TransportKind.stdio,
9891
options: {
9992
cwd: workspacePath,
100-
env: {
101-
// TODO: This is a temporary fix to avoid the issue with the LSP server
102-
// crashing when the number of workers is too high. This is a workaround
103-
// to avoid the issue. Once fixed, we should remove the whole env block.
104-
MAX_FORK_WORKERS: '1',
105-
...process.env,
106-
...sqlmesh.value.env,
107-
},
93+
env: sqlmesh.value.env,
10894
},
10995
args: sqlmesh.value.args,
11096
},

vscode/extension/src/utilities/sqlmesh/sqlmesh.ts

Lines changed: 71 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)