Skip to content

Commit 3ab48e5

Browse files
authored
fix(vscode): make work better with venvs (#4482)
1 parent 9d86f89 commit 3ab48e5

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

vscode/extension/src/utilities/common/python.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { commands, Disposable, Event, EventEmitter, Uri } from 'vscode'
55
import { traceError, traceLog } from './log'
66
import { PythonExtension, ResolvedEnvironment } from '@vscode/python-extension'
77
import path from 'path'
8-
import { IS_WINDOWS } from '../isWindows'
98

109
export interface IInterpreterDetails {
1110
path?: string[]
@@ -39,15 +38,16 @@ export async function initializePython(
3938
api.environments.onDidChangeActiveEnvironmentPath(async e => {
4039
const environment = await api.environments.resolveEnvironment(e.path)
4140
const isVirtualEnv = environment?.environment !== undefined
42-
const binPath = isVirtualEnv
43-
? environment?.environment?.folderUri.fsPath
41+
// Get the directory of the Python executable for virtual environments
42+
const pythonDir = environment?.executable.uri
43+
? path.dirname(environment.executable.uri.fsPath)
4444
: undefined
4545

4646
onDidChangePythonInterpreterEvent.fire({
4747
path: [e.path],
4848
resource: e.resource?.uri,
4949
isVirtualEnvironment: isVirtualEnv,
50-
binPath,
50+
binPath: isVirtualEnv ? pythonDir : undefined,
5151
})
5252
}),
5353
)
@@ -76,17 +76,16 @@ export async function getInterpreterDetails(
7676
)
7777
if (environment?.executable.uri && checkVersion(environment)) {
7878
const isVirtualEnv = environment.environment !== undefined
79-
const binPath = isVirtualEnv
80-
? environment.environment?.folderUri.fsPath
81-
: undefined
79+
// Get the directory of the Python executable
80+
const pythonDir = path.dirname(environment?.executable.uri.fsPath)
8281

8382
return {
8483
path: [environment?.executable.uri.fsPath],
8584
resource,
8685
isVirtualEnvironment: isVirtualEnv,
87-
binPath: binPath
88-
? path.join(binPath, IS_WINDOWS ? 'Scripts' : 'bin')
89-
: undefined,
86+
// For virtual environments, we need to point directly to the bin directory
87+
// rather than constructing it from the environment folder
88+
binPath: isVirtualEnv ? pythonDir : undefined,
9089
}
9190
}
9291
return { path: undefined, resource }

vscode/extension/src/utilities/exec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export async function execAsync(
1313
options: ExecOptions & { signal?: AbortSignal } = {},
1414
): Promise<ExecResult> {
1515
const fullCmd = `${command} ${args.join(' ')}`
16-
traceInfo(`Executing command: ${fullCmd}`)
16+
traceInfo(`Executing command: ${fullCmd} in ${options.cwd}`)
1717

1818
try {
1919
const result = await execAsyncCore(command, args, options)

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ export const isSqlmeshEnterpriseInstalled = async (): Promise<
8282
if (isErr(tcloudBin)) {
8383
return tcloudBin
8484
}
85-
const called = await execAsync(tcloudBin.value, ['is_sqlmesh_installed'])
85+
const projectRoot = await getProjectRoot()
86+
const called = await execAsync(tcloudBin.value, ['is_sqlmesh_installed'], {
87+
cwd: projectRoot.uri.fsPath,
88+
})
8689
if (called.exitCode !== 0) {
8790
return err({
8891
type: 'generic',
@@ -232,7 +235,7 @@ export const sqlmeshExec = async (): Promise<
232235
workspacePath,
233236
env: {
234237
PYTHONPATH: interpreterDetails.path?.[0],
235-
VIRTUAL_ENV: path.dirname(interpreterDetails.binPath!),
238+
VIRTUAL_ENV: path.dirname(path.dirname(interpreterDetails.binPath!)), // binPath now points to bin dir
236239
PATH: interpreterDetails.binPath!,
237240
},
238241
args: [],
@@ -349,8 +352,8 @@ export const sqlmeshLspExec = async (): Promise<
349352
workspacePath,
350353
env: {
351354
PYTHONPATH: interpreterDetails.path?.[0],
352-
VIRTUAL_ENV: path.dirname(interpreterDetails.binPath!),
353-
PATH: path.join(path.dirname(interpreterDetails.binPath!), 'bin'),
355+
VIRTUAL_ENV: path.dirname(path.dirname(interpreterDetails.binPath!)), // binPath now points to bin dir
356+
PATH: interpreterDetails.binPath!, // binPath already points to the bin directory
354357
},
355358
args: [],
356359
})

0 commit comments

Comments
 (0)