diff --git a/cosmos/dbt/graph.py b/cosmos/dbt/graph.py index c2551677e..6e4c96ba3 100644 --- a/cosmos/dbt/graph.py +++ b/cosmos/dbt/graph.py @@ -3,6 +3,7 @@ import itertools import json import os +import shutil import tempfile from dataclasses import dataclass, field from pathlib import Path @@ -185,7 +186,7 @@ def load( def run_dbt_ls(self, project_path: Path, tmp_dir: Path, env_vars: dict[str, str]) -> dict[str, DbtNode]: """Runs dbt ls command and returns the parsed nodes.""" - ls_command = [dbt_cmd, "ls", "--output", "json"] + ls_command = [self.dbt_cmd, "ls", "--output", "json"] if self.render_config.exclude: ls_command.extend(["--exclude", *self.render_config.exclude]) @@ -229,8 +230,8 @@ def load_via_dbt_ls(self) -> None: if not self.profile_config: raise CosmosLoadDbtException("Unable to load project via dbt ls without a profile config.") - if not self.profile_config: - raise CosmosLoadDbtException("Unable to load project via dbt ls without a profile config.") + if not shutil.which(self.dbt_cmd): + raise CosmosLoadDbtException(f"Unable to find the dbt executable: {self.dbt_cmd}") with tempfile.TemporaryDirectory() as tmpdir: logger.info( diff --git a/tests/dbt/test_graph.py b/tests/dbt/test_graph.py index de6e97c22..2da989c6c 100644 --- a/tests/dbt/test_graph.py +++ b/tests/dbt/test_graph.py @@ -5,7 +5,7 @@ import pytest -from cosmos.config import ExecutionConfig, ProfileConfig, ProjectConfig, RenderConfig +from cosmos.config import ExecutionConfig, ProfileConfig, ProjectConfig, RenderConfig, CosmosConfigException from cosmos.constants import DbtResourceType, ExecutionMode from cosmos.dbt.graph import ( CosmosLoadDbtException, @@ -349,6 +349,7 @@ def test_load_via_dbt_ls_with_invalid_dbt_path(): render_config = RenderConfig(dbt_project_path=DBT_PROJECTS_ROOT_DIR / DBT_PROJECT_NAME) with patch("pathlib.Path.exists", return_value=True): dbt_graph = DbtGraph( + dbt_cmd="/inexistent/dbt", project=project_config, execution_config=execution_config, render_config=render_config, @@ -358,10 +359,10 @@ def test_load_via_dbt_ls_with_invalid_dbt_path(): profiles_yml_filepath=Path(__file__).parent.parent / "sample/profiles.yml", ), ) - with pytest.raises(CosmosConfigException) as err_info: + with pytest.raises(CosmosLoadDbtException) as err_info: dbt_graph.load_via_dbt_ls() - expected = "Unable to find the dbt executable, attempted: and ." + expected = "Unable to find the dbt executable: /inexistent/dbt" assert err_info.value.args[0] == expected