Skip to content

Commit 92661cc

Browse files
committed
feat(vscode): handling python errors well
[ci skip]
1 parent 461b2a2 commit 92661cc

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

sqlmesh/core/config/loader.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ def load_config_from_paths(
122122
validation_error_message(e, f"Invalid project config '{config_name}':")
123123
+ "\n\nVerify your config.py."
124124
)
125+
except ConfigError:
126+
# Re-raise ConfigError as is (it already has the proper location and message)
127+
raise
125128
else:
126129
raise ConfigError(
127130
f"Unsupported config file extension '{extension}' in config file '{path}'."
@@ -184,8 +187,19 @@ def load_config_from_python_module(
184187
module_path: Path,
185188
config_name: str = "config",
186189
) -> C:
187-
with sys_path(module_path.parent):
188-
config_module = import_python_file(module_path, module_path.parent)
190+
try:
191+
with sys_path(module_path.parent):
192+
config_module = import_python_file(module_path, module_path.parent)
193+
except SyntaxError as e:
194+
raise ConfigError(
195+
f"Python syntax error in config file: {e.msg} ({e.filename}, line {e.lineno})",
196+
location=module_path,
197+
)
198+
except Exception as e:
199+
raise ConfigError(
200+
f"Failed to load config file: {str(e)}",
201+
location=module_path,
202+
)
189203

190204
try:
191205
config_obj = getattr(config_module, config_name)

vscode/extension/tests/broken_project.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,43 @@ test.describe('Bad config.py/config.yaml file issues', () => {
358358
.first()
359359
.isVisible({ timeout: 5_000 })
360360
})
361+
362+
test('sushi example, bad config.py', async ({ page, sharedCodeServer }) => {
363+
const tempDir = await fs.mkdtemp(
364+
path.join(os.tmpdir(), 'vscode-test-tcloud-'),
365+
)
366+
await fs.copy(SUSHI_SOURCE_PATH, tempDir)
367+
await createPythonInterpreterSettingsSpecifier(tempDir)
368+
369+
const configPyPath = path.join(tempDir, 'config.py')
370+
// Write an invalid Python to config.py
371+
await fs.writeFile(configPyPath, 'invalid_python_code = [1, 2, 3')
372+
373+
await page.goto(
374+
`http://127.0.1:${sharedCodeServer.codeServerPort}/?folder=${tempDir}`,
375+
)
376+
await page.waitForLoadState('networkidle')
377+
378+
// Open customers.sql model
379+
await page
380+
.getByRole('treeitem', { name: 'models', exact: true })
381+
.locator('a')
382+
.click()
383+
await page
384+
.getByRole('treeitem', { name: 'customers.sql', exact: true })
385+
.locator('a')
386+
.click()
387+
388+
// Expect the error to appear
389+
await page.waitForSelector('text=Error creating context')
390+
391+
// Open the problems view
392+
await runCommand(page, 'View: Focus Problems')
393+
394+
// Assert that the error is present in the problems view
395+
await page
396+
.getByText('Python syntax error in config file:', { exact: true })
397+
.first()
398+
.isVisible({ timeout: 5_000 })
399+
})
361400
})

0 commit comments

Comments
 (0)