Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions vscode/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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`."
}
}
},
Expand Down
29 changes: 28 additions & 1 deletion vscode/extension/src/utilities/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,48 @@ import { traceVerbose, traceInfo } from './common/log'

export interface SqlmeshConfiguration {
projectPath: string
lspEntryPoint: string
}

/**
* Get the SQLMesh configuration from VS Code settings.
*
* @returns The SQLMesh configuration
*/
export function getSqlmeshConfiguration(): SqlmeshConfiguration {
function getSqlmeshConfiguration(): SqlmeshConfiguration {
const config = workspace.getConfiguration('sqlmesh')
const projectPath = config.get<string>('projectPath', '')
const lspEntryPoint = config.get<string>('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(' ')
Comment thread
benfdking marked this conversation as resolved.
Outdated
const entrypoint = parts[0]
const args = parts.slice(1)
if (args.length === 0) {
return { entrypoint, args: [] }
}
Comment thread
benfdking marked this conversation as resolved.
Outdated
return { entrypoint, args }
}

/**
* Validate and resolve the project path from configuration.
* If no project path is configured, use the workspace folder.
Expand Down
30 changes: 21 additions & 9 deletions vscode/extension/src/utilities/sqlmesh/sqlmesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -413,15 +413,7 @@ export const ensureSqlmeshLspDependenciesInstalled = async (): Promise<
export const sqlmeshLspExec = async (): Promise<
Result<SqlmeshExecInfo, ErrorType>
> => {
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({
Expand All @@ -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) {
Expand Down
213 changes: 213 additions & 0 deletions vscode/extension/tests/configuration.spec.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
// 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 ({
Comment thread
benfdking marked this conversation as resolved.
Outdated
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
`)
})
})