Skip to content

Commit 111ea75

Browse files
committed
feat: make vscode extension work with enterprise
[ci skip]
1 parent 2d87813 commit 111ea75

2 files changed

Lines changed: 177 additions & 60 deletions

File tree

vscode/extension/src/lsp/lsp.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@ export class LSPClient implements Disposable {
3636
options: {
3737
cwd: workspacePath,
3838
},
39+
args: sqlmesh.value.args,
3940
},
4041
debug: {
4142
command: sqlmesh.value.bin,
4243
transport: TransportKind.stdio,
4344
options: {
4445
cwd: workspacePath,
45-
}
46+
},
47+
args: sqlmesh.value.args,
4648
}
4749
}
4850
let clientOptions: LanguageClientOptions = {
Lines changed: 174 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,197 @@
11
import path from "path"
22
import { traceLog, traceVerbose } from "../common/log"
33
import { getInterpreterDetails } from "../common/python"
4-
import { Result, ok } from "../functional/result"
4+
import { Result, err, isErr, ok } from "../functional/result"
55
import { getProjectRoot } from "../common/utilities"
66

77
export type sqlmesh_exec = {
8-
workspacePath: string;
9-
bin: string;
10-
env: Record<string, string | undefined>;
8+
workspacePath: string;
9+
bin: string;
10+
env: Record<string, string | undefined>;
11+
args: string[];
12+
};
13+
14+
/**
15+
* Check if tcloud is installed in the current Python environment.
16+
*
17+
* @returns A Result indicating whether tcloud is installed.
18+
*/
19+
export const is_tcloud_installed = async (): Promise<
20+
Result<boolean, string>
21+
> => {
22+
const interpreterDetails = await getInterpreterDetails()
23+
if (!interpreterDetails.path) {
24+
return err("No Python interpreter found")
25+
}
26+
traceVerbose(
27+
`Using interpreter from Python extension: ${interpreterDetails.path.join(
28+
" "
29+
)}`
30+
)
31+
32+
const pythonPath = interpreterDetails.path[0]
33+
const checkScript = `
34+
import sys
35+
if sys.version_info >= (3, 12):
36+
from importlib import metadata
37+
else:
38+
import importlib_metadata as metadata
39+
40+
try:
41+
metadata.version('tcloud')
42+
print("true")
43+
except metadata.PackageNotFoundError:
44+
print("false")
45+
`
46+
try {
47+
const { execFile } = require("child_process")
48+
const { promisify } = require("util")
49+
const execFileAsync = promisify(execFile)
50+
51+
traceVerbose(`Checking tcloud installation with script: ${checkScript}`)
52+
const { stdout } = await execFileAsync(pythonPath, ["-c", checkScript])
53+
traceVerbose(`tcloud installation check result: ${stdout.trim()}`)
54+
return ok(stdout.trim() === "true")
55+
} catch (error) {
56+
return err(`Failed to check tcloud installation: ${error}`)
57+
}
58+
}
59+
60+
/**
61+
* Get the tcloud executable for the current Python environment.
62+
*
63+
* @returns The tcloud executable for the current Python environment.
64+
*/
65+
export const get_tcloud_bin = async (): Promise<Result<string, string>> => {
66+
const interpreterDetails = await getInterpreterDetails()
67+
if (!interpreterDetails.path) {
68+
return err("No Python interpreter found")
69+
}
70+
const pythonPath = interpreterDetails.path[0]
71+
const binPath = path.join(path.dirname(pythonPath), "tcloud")
72+
return ok(binPath)
1173
}
1274

1375
/**
1476
* Get the sqlmesh executable for the current workspace.
15-
*
77+
*
1678
* @returns The sqlmesh executable for the current workspace.
1779
*/
1880
export const sqlmesh_exec = async (): Promise<Result<sqlmesh_exec, string>> => {
19-
const projectRoot = await getProjectRoot()
20-
const workspacePath = projectRoot.uri.fsPath
21-
const interpreterDetails = await getInterpreterDetails()
22-
traceLog(`Interpreter details: ${JSON.stringify(interpreterDetails)}`)
23-
if (interpreterDetails.path) {
24-
traceVerbose(`Using interpreter from Python extension: ${interpreterDetails.path.join(' ')}`)
81+
const projectRoot = await getProjectRoot()
82+
const workspacePath = projectRoot.uri.fsPath
83+
const interpreterDetails = await getInterpreterDetails()
84+
traceLog(`Interpreter details: ${JSON.stringify(interpreterDetails)}`)
85+
if (interpreterDetails.path) {
86+
traceVerbose(
87+
`Using interpreter from Python extension: ${interpreterDetails.path.join(
88+
" "
89+
)}`
90+
)
91+
}
92+
if (interpreterDetails.isVirtualEnvironment) {
93+
traceLog("Using virtual environment")
94+
const tcloudInstalled = await is_tcloud_installed()
95+
if (isErr(tcloudInstalled)) {
96+
return tcloudInstalled
2597
}
26-
if (interpreterDetails.isVirtualEnvironment) {
27-
traceLog('Using virtual environment')
28-
const binPath = path.join(interpreterDetails.binPath!, 'sqlmesh')
29-
traceLog(`Bin path: ${binPath}`)
30-
return ok({
31-
bin: binPath,
32-
workspacePath,
33-
env: {
34-
PYTHONPATH: interpreterDetails.path?.[0],
35-
VIRTUAL_ENV: path.dirname(interpreterDetails.binPath!),
36-
PATH: path.join(path.dirname(interpreterDetails.binPath!), 'bin')
37-
}
38-
})
39-
} else {
40-
return ok({
41-
bin: 'sqlmesh',
42-
workspacePath,
43-
env: {},
44-
})
98+
if (tcloudInstalled.value) {
99+
const tcloudBin = await get_tcloud_bin()
100+
if (isErr(tcloudBin)) {
101+
return tcloudBin
102+
}
103+
return ok({
104+
bin: `${tcloudBin.value} sqlmesh`,
105+
workspacePath,
106+
env: {
107+
PYTHONPATH: interpreterDetails.path?.[0],
108+
VIRTUAL_ENV: path.dirname(interpreterDetails.binPath!),
109+
PATH: interpreterDetails.binPath!,
110+
},
111+
args:[],
112+
})
45113
}
114+
const binPath = path.join(interpreterDetails.binPath!, "sqlmesh")
115+
traceLog(`Bin path: ${binPath}`)
116+
return ok({
117+
bin: binPath,
118+
workspacePath,
119+
env: {
120+
PYTHONPATH: interpreterDetails.path?.[0],
121+
VIRTUAL_ENV: path.dirname(interpreterDetails.binPath!),
122+
PATH: interpreterDetails.binPath!,
123+
},
124+
args: [],
125+
})
126+
} else {
127+
return ok({
128+
bin: "sqlmesh",
129+
workspacePath,
130+
env: {},
131+
args: [],
132+
})
133+
}
46134
}
47135

48136
/**
49137
* Get the sqlmesh_lsp executable for the current workspace.
50-
*
138+
*
51139
* @returns The sqlmesh_lsp executable for the current workspace.
52140
*/
53-
export const sqlmesh_lsp_exec = async (): Promise<Result<sqlmesh_exec, string>> => {
54-
const projectRoot = await getProjectRoot()
55-
const workspacePath = projectRoot.uri.fsPath
56-
const interpreterDetails = await getInterpreterDetails()
57-
traceLog(`Interpreter details: ${JSON.stringify(interpreterDetails)}`)
58-
if (interpreterDetails.path) {
59-
traceVerbose(`Using interpreter from Python extension: ${interpreterDetails.path.join(' ')}`)
60-
}
61-
if (interpreterDetails.isVirtualEnvironment) {
62-
traceLog('Using virtual environment')
63-
const binPath = path.join(interpreterDetails.binPath!, 'sqlmesh_lsp')
64-
traceLog(`Bin path: ${binPath}`)
65-
return ok({
66-
bin: binPath,
67-
workspacePath,
68-
env: {
69-
PYTHONPATH: interpreterDetails.path?.[0],
70-
VIRTUAL_ENV: path.dirname(interpreterDetails.binPath!),
71-
PATH: path.join(path.dirname(interpreterDetails.binPath!), 'bin')
72-
}
73-
})
74-
} else {
75-
return ok({
76-
bin: 'sqlmesh_lsp',
77-
workspacePath,
78-
env: {},
79-
})
141+
export const sqlmesh_lsp_exec = async (): Promise<
142+
Result<sqlmesh_exec, string>
143+
> => {
144+
const projectRoot = await getProjectRoot()
145+
const workspacePath = projectRoot.uri.fsPath
146+
const interpreterDetails = await getInterpreterDetails()
147+
traceLog(`Interpreter details: ${JSON.stringify(interpreterDetails)}`)
148+
if (interpreterDetails.path) {
149+
traceVerbose(
150+
`Using interpreter from Python extension: ${interpreterDetails.path.join(
151+
" "
152+
)}`
153+
)
154+
}
155+
if (interpreterDetails.isVirtualEnvironment) {
156+
traceLog("Using virtual environment")
157+
const tcloudInstalled = await is_tcloud_installed()
158+
if (isErr(tcloudInstalled)) {
159+
return tcloudInstalled
80160
}
81-
161+
// if (tcloudInstalled.value) {
162+
// const tcloudBin = await get_tcloud_bin()
163+
// if (isErr(tcloudBin)) {
164+
// return tcloudBin
165+
// }
166+
// return ok({
167+
// bin: tcloudBin.value,
168+
// workspacePath,
169+
// env: {
170+
// PYTHONPATH: interpreterDetails.path?.[0],
171+
// VIRTUAL_ENV: path.dirname(interpreterDetails.binPath!),
172+
// PATH: interpreterDetails.binPath!,
173+
// },
174+
// args: ['sqlmesh', 'sqlmesh_lsp'],
175+
// })
176+
// }
177+
const binPath = path.join(interpreterDetails.binPath!, "sqlmesh_lsp")
178+
traceLog(`Bin path: ${binPath}`)
179+
return ok({
180+
bin: binPath,
181+
workspacePath,
182+
env: {
183+
PYTHONPATH: interpreterDetails.path?.[0],
184+
VIRTUAL_ENV: path.dirname(interpreterDetails.binPath!),
185+
PATH: path.join(path.dirname(interpreterDetails.binPath!), "bin"),
186+
},
187+
args: [],
188+
})
189+
} else {
190+
return ok({
191+
bin: "sqlmesh_lsp",
192+
workspacePath,
193+
env: {},
194+
args: [],
195+
})
196+
}
82197
}

0 commit comments

Comments
 (0)