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 a0d853a0a9..a745645413 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`." } } }, @@ -136,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 e77a39ce55..c8edcd13ce 100644 --- a/vscode/extension/src/utilities/config.ts +++ b/vscode/extension/src/utilities/config.ts @@ -3,9 +3,12 @@ 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 + lspEntryPoint: string } /** @@ -13,14 +16,46 @@ 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, } } +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 ...". + */ +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 = 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 } +} + /** * 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..6f187d5274 --- /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 relative 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 +`) + }) +})