Skip to content

Commit 784c5cb

Browse files
committed
feat(vscode): handling extra vars from tcloud
1 parent 7dd52c5 commit 784c5cb

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

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

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export interface SqlmeshExecInfo {
2525
* 1. Check if the project has a tcloud.yaml file in the project root. If it does, we assume it's a Tcloud project.
2626
* 2. Check if the project has tcloud installed in the Python environment.
2727
*
28-
* @returns A Result indicating whether tcloud is installed.
28+
* @returns A Result indicating whether tcloud is installed
2929
*/
3030
export const isTcloudProject = async (): Promise<Result<boolean, string>> => {
3131
const projectRoot = await getProjectRoot()
@@ -49,6 +49,47 @@ export const isTcloudProject = async (): Promise<Result<boolean, string>> => {
4949
return ok(isTcloudInstalled.value)
5050
}
5151

52+
const environmentVariablesSchema = z.record(z.string())
53+
54+
type EnvironmentVariables = z.infer<typeof environmentVariablesSchema>
55+
56+
export const getTCLoudEnvironmentVariables = async (): Promise<Result<EnvironmentVariables, ErrorType>> => {
57+
const projectRoot = await getProjectRoot()
58+
const resolvedPath = resolveProjectPath(projectRoot)
59+
if (isErr(resolvedPath)) {
60+
return err(
61+
{
62+
type: "generic",
63+
message: resolvedPath.error
64+
}
65+
)
66+
}
67+
const bin = await getTcloudBin()
68+
if (isErr(bin)) {
69+
return bin
70+
}
71+
const returned = await execAsync(bin.value, ['print_environment_variables'],
72+
{
73+
cwd: resolvedPath.value
74+
}
75+
)
76+
if (returned.exitCode !== 0) {
77+
return err({
78+
type: 'generic',
79+
message: `Failed to get tcloud environment variables: ${returned.stderr}`,
80+
})
81+
}
82+
const parsed = JSON.parse(returned.stdout)
83+
const validated = environmentVariablesSchema.safeParse(parsed)
84+
if (!validated.success) {
85+
return err({
86+
type: 'generic',
87+
message: `Failed to validate tcloud environment variables: ${validated.error.message}`,
88+
})
89+
}
90+
return ok(validated.data)
91+
}
92+
5293
/**
5394
* Get the tcloud executable for the current Python environment.
5495
*
@@ -382,13 +423,22 @@ export const sqlmeshLspExec = async (): Promise<
382423
type: 'sqlmesh_lsp_not_found',
383424
})
384425
}
426+
let additionalEnv: Record<string, string> = {}
427+
const gotten = await getTCLoudEnvironmentVariables()
428+
// TODO: Remove this try catch when we are confident that the tcloud command is always available.
429+
if (isErr(gotten)) {
430+
traceLog(`Failed to get tcloud environment variables: ${JSON.stringify(gotten.error)}`)
431+
} else {
432+
additionalEnv = gotten.value
433+
}
385434
return ok({
386435
bin: binPath,
387436
workspacePath,
388437
env: {
389438
PYTHONPATH: interpreterDetails.path?.[0],
390439
VIRTUAL_ENV: path.dirname(path.dirname(interpreterDetails.binPath!)), // binPath now points to bin dir
391440
PATH: interpreterDetails.binPath!, // binPath already points to the bin directory
441+
...additionalEnv,
392442
},
393443
args: [],
394444
})

0 commit comments

Comments
 (0)