From 64e977448e3504307050a70b1c89ca9bca82017c Mon Sep 17 00:00:00 2001 From: Ben King <9087625+benfdking@users.noreply.github.com> Date: Wed, 30 Jul 2025 17:49:43 +0100 Subject: [PATCH 1/2] feat(vscode): allow the lsp loaded to be specified - allow the extension to specify the lsp script entrypoint --- vscode/extension/package.json | 5 + vscode/extension/src/utilities/config.ts | 29 ++- .../src/utilities/sqlmesh/sqlmesh.ts | 30 ++- vscode/extension/tests/configuration.spec.ts | 213 ++++++++++++++++++ 4 files changed, 267 insertions(+), 10 deletions(-) create mode 100644 vscode/extension/tests/configuration.spec.ts diff --git a/vscode/extension/package.json b/vscode/extension/package.json index a0d853a0a9..f96d27c45e 100644 --- a/vscode/extension/package.json +++ b/vscode/extension/package.json @@ -36,6 +36,11 @@ "type": "string", "default": "", "markdownDescription": "The path to the SQLMesh project. If not set, the extension will try to find the project root automatically. If set, the extension will use the project root as the workspace path, e.g. it will run `sqlmesh` and `sqlmesh_lsp` in the project root. The path can be absolute `/Users/sqlmesh_user/sqlmesh_project/sushi` or relative `./project_folder/sushi` to the workspace root." + }, + "sqlmesh.lspEntrypoint": { + "type": "string", + "default": "", + "markdownDescription": "The entry point for the SQLMesh LSP server. If not set the extension looks for the default lsp. If set, the extension will use the entry point as the LSP path, The path can be absolute `/Users/sqlmesh_user/sqlmesh_project/sushi/sqlmesh_lsp` or relative `./project_folder/sushi/sqlmesh_lsp` to the workspace root. It can also have arguments, e.g. `./project_folder/sushi/sqlmesh_lsp --port 5000`." } } }, diff --git a/vscode/extension/src/utilities/config.ts b/vscode/extension/src/utilities/config.ts index e77a39ce55..d5be576f27 100644 --- a/vscode/extension/src/utilities/config.ts +++ b/vscode/extension/src/utilities/config.ts @@ -6,6 +6,7 @@ import { traceVerbose, traceInfo } from './common/log' export interface SqlmeshConfiguration { projectPath: string + lspEntryPoint: string } /** @@ -13,14 +14,40 @@ export interface SqlmeshConfiguration { * * @returns The SQLMesh configuration */ -export function getSqlmeshConfiguration(): SqlmeshConfiguration { +function getSqlmeshConfiguration(): SqlmeshConfiguration { const config = workspace.getConfiguration('sqlmesh') const projectPath = config.get('projectPath', '') + const lspEntryPoint = config.get('lspEntrypoint', '') return { projectPath, + lspEntryPoint, } } +/** + * Get the SQLMesh LSP entry point from VS Code settings. undefined if not set + * it's expected to be a string in the format "command arg1 arg2 ...". + */ +export function getSqlmeshLspEntryPoint(): + | { + entrypoint: string + args: string[] + } + | undefined { + const config = getSqlmeshConfiguration() + if (config.lspEntryPoint === '') { + return undefined + } + // Split the entry point into command and arguments + const parts = config.lspEntryPoint.split(' ') + const entrypoint = parts[0] + const args = parts.slice(1) + if (args.length === 0) { + return { entrypoint, args: [] } + } + return { entrypoint, args } +} + /** * Validate and resolve the project path from configuration. * If no project path is configured, use the workspace folder. diff --git a/vscode/extension/src/utilities/sqlmesh/sqlmesh.ts b/vscode/extension/src/utilities/sqlmesh/sqlmesh.ts index d95017a2ca..104869192b 100644 --- a/vscode/extension/src/utilities/sqlmesh/sqlmesh.ts +++ b/vscode/extension/src/utilities/sqlmesh/sqlmesh.ts @@ -11,7 +11,7 @@ import { execAsync } from '../exec' import z from 'zod' import { ProgressLocation, window } from 'vscode' import { IS_WINDOWS } from '../isWindows' -import { resolveProjectPath } from '../config' +import { getSqlmeshLspEntryPoint, resolveProjectPath } from '../config' import { isSemVerGreaterThanOrEqual } from '../semver' export interface SqlmeshExecInfo { @@ -413,15 +413,7 @@ export const ensureSqlmeshLspDependenciesInstalled = async (): Promise< export const sqlmeshLspExec = async (): Promise< Result > => { - const sqlmeshLSP = IS_WINDOWS ? 'sqlmesh_lsp.exe' : 'sqlmesh_lsp' const projectRoot = await getProjectRoot() - const envVariables = await getPythonEnvVariables() - if (isErr(envVariables)) { - return err({ - type: 'generic', - message: envVariables.error, - }) - } const resolvedPath = resolveProjectPath(projectRoot) if (isErr(resolvedPath)) { return err({ @@ -430,6 +422,26 @@ export const sqlmeshLspExec = async (): Promise< }) } const workspacePath = resolvedPath.value + + const configuredLSPExec = getSqlmeshLspEntryPoint() + if (configuredLSPExec) { + traceLog(`Using configured SQLMesh LSP entry point: ${configuredLSPExec.entrypoint} ${configuredLSPExec.args.join(' ')}`) + return ok({ + bin: configuredLSPExec.entrypoint, + workspacePath, + env: process.env, + args: configuredLSPExec.args, + }) + } + const sqlmeshLSP = IS_WINDOWS ? 'sqlmesh_lsp.exe' : 'sqlmesh_lsp' + const envVariables = await getPythonEnvVariables() + if (isErr(envVariables)) { + return err({ + type: 'generic', + message: envVariables.error, + }) + } + const interpreterDetails = await getInterpreterDetails() traceLog(`Interpreter details: ${JSON.stringify(interpreterDetails)}`) if (interpreterDetails.path) { diff --git a/vscode/extension/tests/configuration.spec.ts b/vscode/extension/tests/configuration.spec.ts new file mode 100644 index 0000000000..95ddca7cc9 --- /dev/null +++ b/vscode/extension/tests/configuration.spec.ts @@ -0,0 +1,213 @@ +import { test, expect } from './fixtures' +import { + createVirtualEnvironment, + openServerPage, + pipInstall, + REPO_ROOT, + SUSHI_SOURCE_PATH, + waitForLoadedSQLMesh, +} from './utils' +import path from 'path' +import fs from 'fs-extra' + +async function setupPythonEnvironment(tempDir: string): Promise { + // Create a temporary directory for the virtual environment + const venvDir = path.join(tempDir, '.venv') + fs.mkdirSync(venvDir, { recursive: true }) + + // Create virtual environment + const pythonDetails = await createVirtualEnvironment(venvDir) + + // Install sqlmesh from the local repository with LSP support + const customMaterializations = path.join( + REPO_ROOT, + 'examples', + 'custom_materializations', + ) + const sqlmeshWithExtras = `${REPO_ROOT}[lsp,bigquery]` + await pipInstall(pythonDetails, [sqlmeshWithExtras, customMaterializations]) +} + +/** + * Creates an entrypoint file used to test the LSP configuration. + * + * The entrypoint file is a bash script that simply calls out to the + */ +const createEntrypointFile = ( + tempDir: string, + entrypointFileName: string, + bitToStripFromArgs = '', +): { + entrypointFile: string + fileWhereStoredInputs: string +} => { + const entrypointFile = path.join(tempDir, entrypointFileName) + const fileWhereStoredInputs = path.join(tempDir, 'inputs.txt') + const sqlmeshLSPFile = path.join(tempDir, '.venv/bin/sqlmesh_lsp') + + // Create the entrypoint file + fs.writeFileSync( + entrypointFile, + `#!/bin/bash +echo "$@" > ${fileWhereStoredInputs} +# Strip bitToStripFromArgs from the beginning of the args if it matches +if [[ "$1" == "${bitToStripFromArgs}" ]]; then + shift +fi +# Call the sqlmesh_lsp with the remaining arguments +${sqlmeshLSPFile} "$@"`, + { mode: 0o755 }, // Make it executable + ) + + return { + entrypointFile, + fileWhereStoredInputs, + } +} + +test.describe('Test LSP Entrypoint configuration', () => { + test('specify single entrypoint relalative path', async ({ + page, + sharedCodeServer, + tempDir, + }) => { + await fs.copy(SUSHI_SOURCE_PATH, tempDir) + + await setupPythonEnvironment(tempDir) + + const { fileWhereStoredInputs } = createEntrypointFile( + tempDir, + 'entrypoint.sh', + ) + + const settings = { + 'sqlmesh.lspEntrypoint': './entrypoint.sh', + } + // Write the settings to the settings.json file + const settingsPath = path.join(tempDir, '.vscode', 'settings.json') + fs.mkdirSync(path.dirname(settingsPath), { recursive: true }) + fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2)) + + await openServerPage(page, tempDir, sharedCodeServer) + + // Wait for the models folder to be visible + await page.waitForSelector('text=models') + + // Click on the models folder, excluding external_models + await page + .getByRole('treeitem', { name: 'models', exact: true }) + .locator('a') + .click() + + // Open the customer_revenue_lifetime model + await page + .getByRole('treeitem', { name: 'customers.sql', exact: true }) + .locator('a') + .click() + + await waitForLoadedSQLMesh(page) + + // Check that the output file exists and contains the entrypoint script arguments + expect(fs.existsSync(fileWhereStoredInputs)).toBe(true) + expect(fs.readFileSync(fileWhereStoredInputs, 'utf8')).toBe(`--stdio +`) + }) + + test('specify one entrypoint absolute path', async ({ + page, + sharedCodeServer, + tempDir, + }) => { + await fs.copy(SUSHI_SOURCE_PATH, tempDir) + + await setupPythonEnvironment(tempDir) + + const { entrypointFile, fileWhereStoredInputs } = createEntrypointFile( + tempDir, + 'entrypoint.sh', + ) + // Assert that the entrypoint file is an absolute path + expect(path.isAbsolute(entrypointFile)).toBe(true) + + const settings = { + 'sqlmesh.lspEntrypoint': `${entrypointFile}`, + } + // Write the settings to the settings.json file + const settingsPath = path.join(tempDir, '.vscode', 'settings.json') + fs.mkdirSync(path.dirname(settingsPath), { recursive: true }) + fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2)) + + await openServerPage(page, tempDir, sharedCodeServer) + + // Wait for the models folder to be visible + await page.waitForSelector('text=models') + + // Click on the models folder, excluding external_models + await page + .getByRole('treeitem', { name: 'models', exact: true }) + .locator('a') + .click() + + // Open the customer_revenue_lifetime model + await page + .getByRole('treeitem', { name: 'customers.sql', exact: true }) + .locator('a') + .click() + + await waitForLoadedSQLMesh(page) + + // Check that the output file exists and contains the entrypoint script arguments + expect(fs.existsSync(fileWhereStoredInputs)).toBe(true) + expect(fs.readFileSync(fileWhereStoredInputs, 'utf8')).toBe(`--stdio +`) + }) + + test('specify entrypoint with arguments', async ({ + page, + sharedCodeServer, + tempDir, + }) => { + await fs.copy(SUSHI_SOURCE_PATH, tempDir) + + await setupPythonEnvironment(tempDir) + + const { fileWhereStoredInputs } = createEntrypointFile( + tempDir, + 'entrypoint.sh', + '--argToIgnore', + ) + + const settings = { + 'sqlmesh.lspEntrypoint': './entrypoint.sh --argToIgnore', + } + // Write the settings to the settings.json file + const settingsPath = path.join(tempDir, '.vscode', 'settings.json') + fs.mkdirSync(path.dirname(settingsPath), { recursive: true }) + fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2)) + + await openServerPage(page, tempDir, sharedCodeServer) + + // Wait for the models folder to be visible + await page.waitForSelector('text=models') + + // Click on the models folder, excluding external_models + await page + .getByRole('treeitem', { name: 'models', exact: true }) + .locator('a') + .click() + + // Open the customer_revenue_lifetime model + await page + .getByRole('treeitem', { name: 'customers.sql', exact: true }) + .locator('a') + .click() + + await waitForLoadedSQLMesh(page) + + // Check that the output file exists and contains the entrypoint script arguments + expect(fs.existsSync(fileWhereStoredInputs)).toBe(true) + expect(fs.readFileSync(fileWhereStoredInputs, 'utf8')) + .toBe(`--argToIgnore --stdio +`) + }) +}) From eca0cce6b030e3403cd255fab507fa47581e5872 Mon Sep 17 00:00:00 2001 From: Ben King <9087625+benfdking@users.noreply.github.com> Date: Wed, 30 Jul 2025 20:57:22 +0100 Subject: [PATCH 2/2] addressing comments --- pnpm-lock.yaml | 17 +++++++++++++++++ vscode/extension/package.json | 2 ++ vscode/extension/src/utilities/config.ts | 18 +++++++++++++----- vscode/extension/tests/configuration.spec.ts | 2 +- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3d0a5449f2..2e39e21fbd 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -26,12 +26,18 @@ importers: '@types/fs-extra': specifier: ^11.0.4 version: 11.0.4 + '@types/shell-quote': + specifier: ^1.7.5 + version: 1.7.5 '@vscode/python-extension': specifier: ^1.0.5 version: 1.0.5 fs-extra: specifier: ^11.3.0 version: 11.3.0 + shell-quote: + specifier: ^1.8.3 + version: 1.8.3 vscode-jsonrpc: specifier: ^8.2.1 version: 8.2.1 @@ -2378,6 +2384,9 @@ packages: '@types/sarif@2.1.7': resolution: {integrity: sha512-kRz0VEkJqWLf1LLVN4pT1cg1Z9wAuvI6L97V3m2f5B76Tg8d413ddvLBPTEHAZJlnn4XSvu0FkZtViCQGVyrXQ==} + '@types/shell-quote@1.7.5': + resolution: {integrity: sha512-+UE8GAGRPbJVQDdxi16dgadcBfQ+KG2vgZhV1+3A1XmHbmwcdwhCUwIdy+d3pAGrbvgRoVSjeI9vOWyq376Yzw==} + '@types/unist@2.0.11': resolution: {integrity: sha512-CmBKiL6NNo/OqgmMn95Fk9Whlp2mtvIv+KNpQKN2F4SjvrEesubTRWGYSg+BnWZOnlCaSTU1sMpsBOzgbYhnsA==} @@ -5296,6 +5305,10 @@ packages: resolution: {integrity: sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==} engines: {node: '>=8'} + shell-quote@1.8.3: + resolution: {integrity: sha512-ObmnIF4hXNg1BqhnHmgbDETF8dLPCggZWBjkQfhZpbszZnYur5DUljTcCHii5LC3J5E0yeO/1LIMyH+UvHQgyw==} + engines: {node: '>= 0.4'} + should-equal@2.0.0: resolution: {integrity: sha512-ZP36TMrK9euEuWQYBig9W55WPC7uo37qzAEmbjHz4gfyuXrEUgF8cUvQVO+w+d3OMfPvSRQJ22lSm8MQJ43LTA==} @@ -8563,6 +8576,8 @@ snapshots: '@types/sarif@2.1.7': {} + '@types/shell-quote@1.7.5': {} + '@types/unist@2.0.11': {} '@types/unist@3.0.3': {} @@ -11972,6 +11987,8 @@ snapshots: shebang-regex@3.0.0: {} + shell-quote@1.8.3: {} + should-equal@2.0.0: dependencies: should-type: 1.4.0 diff --git a/vscode/extension/package.json b/vscode/extension/package.json index f96d27c45e..a745645413 100644 --- a/vscode/extension/package.json +++ b/vscode/extension/package.json @@ -141,8 +141,10 @@ "dependencies": { "@duckdb/node-api": "1.3.2-alpha.25", "@types/fs-extra": "^11.0.4", + "@types/shell-quote": "^1.7.5", "@vscode/python-extension": "^1.0.5", "fs-extra": "^11.3.0", + "shell-quote": "^1.8.3", "vscode-jsonrpc": "^8.2.1", "vscode-languageclient": "^9.0.1", "zod": "^3.25.76" diff --git a/vscode/extension/src/utilities/config.ts b/vscode/extension/src/utilities/config.ts index d5be576f27..c8edcd13ce 100644 --- a/vscode/extension/src/utilities/config.ts +++ b/vscode/extension/src/utilities/config.ts @@ -3,6 +3,8 @@ import path from 'path' import fs from 'fs' import { Result, err, ok } from '@bus/result' import { traceVerbose, traceInfo } from './common/log' +import { parse } from 'shell-quote' +import { z } from 'zod' export interface SqlmeshConfiguration { projectPath: string @@ -24,6 +26,8 @@ function getSqlmeshConfiguration(): SqlmeshConfiguration { } } +const stringsArray = z.array(z.string()) + /** * Get the SQLMesh LSP entry point from VS Code settings. undefined if not set * it's expected to be a string in the format "command arg1 arg2 ...". @@ -39,12 +43,16 @@ export function getSqlmeshLspEntryPoint(): return undefined } // Split the entry point into command and arguments - const parts = config.lspEntryPoint.split(' ') - const entrypoint = parts[0] - const args = parts.slice(1) - if (args.length === 0) { - return { entrypoint, args: [] } + const parts = parse(config.lspEntryPoint) + const parsed = stringsArray.safeParse(parts) + if (!parsed.success) { + throw new Error( + `Invalid lspEntrypoint configuration: ${config.lspEntryPoint}. Expected a + string in the format "command arg1 arg2 ...".`, + ) } + const entrypoint = parsed.data[0] + const args = parsed.data.slice(1) return { entrypoint, args } } diff --git a/vscode/extension/tests/configuration.spec.ts b/vscode/extension/tests/configuration.spec.ts index 95ddca7cc9..6f187d5274 100644 --- a/vscode/extension/tests/configuration.spec.ts +++ b/vscode/extension/tests/configuration.spec.ts @@ -66,7 +66,7 @@ ${sqlmeshLSPFile} "$@"`, } test.describe('Test LSP Entrypoint configuration', () => { - test('specify single entrypoint relalative path', async ({ + test('specify single entrypoint relative path', async ({ page, sharedCodeServer, tempDir,