Skip to content

Commit 7eccb31

Browse files
nkwork9999claude
andcommitted
fix(dlt): surface underlying error and hint when attaching to pipeline fails
When `sqlmesh init -t dlt --dlt-path <path>` could not attach to the pipeline, the underlying dlt error was swallowed and replaced with an uninformative message. `--dlt-path` points to dlt's pipelines working directory (by default ~/.dlt/pipelines), not the directory containing the pipeline scripts, which is a common source of confusion. Now the error includes the original dlt exception, the directory that was searched, and a hint to omit `--dlt-path` when the pipeline exists in the default working directory. Also clarifies the `--dlt-path` help text and docstring accordingly. Fixes #5660. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Signed-off-by: nk <nkwork1234@gmail.com>
1 parent b44fdf6 commit 7eccb31

4 files changed

Lines changed: 28 additions & 7 deletions

File tree

docs/reference/cli.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ Options:
279279
empty.
280280
--dlt-pipeline TEXT DLT pipeline for which to generate a SQLMesh project.
281281
Use alongside template: dlt
282-
--dlt-path TEXT The directory where the DLT pipeline resides. Use
282+
--dlt-path TEXT The DLT pipelines working directory, where DLT stores
283+
pipeline state (by default ~/.dlt/pipelines). Use
283284
alongside template: dlt
284285
--help Show this message and exit.
285286
```

sqlmesh/cli/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def cli(
169169
@click.option(
170170
"--dlt-path",
171171
type=str,
172-
help="The directory where the DLT pipeline resides. Use alongside template: dlt",
172+
help="The DLT pipelines working directory, where DLT stores pipeline state (by default ~/.dlt/pipelines). Use alongside template: dlt",
173173
)
174174
@click.pass_context
175175
@error_handler
@@ -1155,7 +1155,7 @@ def table_name(
11551155
@click.option(
11561156
"--dlt-path",
11571157
type=str,
1158-
help="The directory where the DLT pipeline resides.",
1158+
help="The DLT pipelines working directory, where DLT stores pipeline state (by default ~/.dlt/pipelines).",
11591159
)
11601160
@click.pass_context
11611161
@error_handler

sqlmesh/integrations/dlt.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ def generate_dlt_models_and_settings(
2222
pipeline_name: The name of the DLT pipeline to attach to.
2323
dialect: The SQL dialect to use for generating SQLMesh models.
2424
tables: A list of table names to include.
25-
dlt_path: The path to the directory containing the DLT pipelines.
25+
dlt_path: The path to the DLT pipelines working directory, where DLT stores
26+
pipeline state (by default ~/.dlt/pipelines).
2627
2728
Returns:
2829
A tuple containing a set of the SQLMesh model definitions, the connection config and the start date.
@@ -34,8 +35,21 @@ def generate_dlt_models_and_settings(
3435

3536
try:
3637
pipeline = dlt.attach(pipeline_name=pipeline_name, pipelines_dir=dlt_path or "")
37-
except CannotRestorePipelineException:
38-
raise click.ClickException(f"Could not attach to pipeline {pipeline_name}")
38+
except CannotRestorePipelineException as e:
39+
from pathlib import Path
40+
from dlt.common.pipeline import get_dlt_pipelines_dir
41+
42+
searched_dir = dlt_path or get_dlt_pipelines_dir()
43+
msg = f"Could not attach to pipeline {pipeline_name}.\nSearched in: {searched_dir}\n{e}"
44+
if dlt_path and (Path(get_dlt_pipelines_dir()) / pipeline_name).exists():
45+
msg += (
46+
f"\nHint: A pipeline named '{pipeline_name}' exists in the default pipelines "
47+
f"working directory '{get_dlt_pipelines_dir()}'. Note that --dlt-path must "
48+
"point to the directory where DLT stores pipeline working state (by default "
49+
"~/.dlt/pipelines), not the directory containing your pipeline scripts. "
50+
"Try omitting --dlt-path."
51+
)
52+
raise click.ClickException(msg)
3953

4054
schema = pipeline.default_schema
4155
dataset = pipeline.dataset_name

tests/cli/test_cli.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ def test_dlt_pipeline(runner, tmp_path):
995995
exec(file.read())
996996

997997
# This should fail since it won't be able to locate the pipeline in this path
998-
with pytest.raises(ClickException, match=r".*Could not attach to pipeline*"):
998+
with pytest.raises(ClickException, match=r".*Could not attach to pipeline*") as excinfo:
999999
init_example_project(
10001000
tmp_path,
10011001
"duckdb",
@@ -1004,6 +1004,12 @@ def test_dlt_pipeline(runner, tmp_path):
10041004
dlt_path="./dlt2/pipelines",
10051005
)
10061006

1007+
# The error should surface where the pipeline was searched for and, since the
1008+
# pipeline exists in the default working directory, a hint about --dlt-path
1009+
error_message = str(excinfo.value)
1010+
assert "Searched in: ./dlt2/pipelines" in error_message
1011+
assert "Try omitting --dlt-path" in error_message
1012+
10071013
# By setting the pipelines path where the pipeline directory is located, it should work
10081014
dlt_path = get_dlt_pipelines_dir()
10091015
init_example_project(

0 commit comments

Comments
 (0)