diff --git a/.changes/unreleased/Fixes-20250219-224310.yaml b/.changes/unreleased/Fixes-20250219-224310.yaml new file mode 100644 index 00000000000..ca7091f6d5e --- /dev/null +++ b/.changes/unreleased/Fixes-20250219-224310.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Models defined in .yml files have weird paths +time: 2025-02-19T22:43:10.5172941+10:30 +custom: + Author: joshuanits + Issue: "11321" diff --git a/core/dbt/contracts/graph/nodes.py b/core/dbt/contracts/graph/nodes.py index df1f6185df7..58bff93b57e 100644 --- a/core/dbt/contracts/graph/nodes.py +++ b/core/dbt/contracts/graph/nodes.py @@ -255,6 +255,11 @@ def get_target_write_path( if os.path.basename(self.path) == os.path.basename(self.original_file_path): # One-to-one relationship of nodes to files. path = self.original_file_path + elif os.path.dirname(self.path) == os.path.basename(self.original_file_path): + parent_dirname = os.path.dirname(self.original_file_path) + dirname = os.path.dirname(self.path).replace(".", "_") + basename = os.path.basename(self.path) + path = os.path.join(parent_dirname, dirname, basename) else: # Many-to-one relationship of nodes to files. path = os.path.join(self.original_file_path, self.path) diff --git a/tests/unit/graph/test_nodes.py b/tests/unit/graph/test_nodes.py index a0e0a8d7e56..2769cf33684 100644 --- a/tests/unit/graph/test_nodes.py +++ b/tests/unit/graph/test_nodes.py @@ -410,6 +410,22 @@ def parsed_node(self) -> ParsedNode: database=None, ) + @pytest.fixture(scope="class") + def parsed_yml_node(self) -> ParsedNode: + return ParsedNode( + resource_type=NodeType.Model, + unique_id="model.test_package.test_name", + name="test_name", + package_name="test_package", + schema="test_schema", + alias="test_alias", + fqn=["models", "test_name"], + original_file_path="folder/test_original_file_path.yml", + checksum=FileHash.from_contents("checksum"), + path="test_original_file_path.yml/test_path.sql", + database=None, + ) + def test_get_target_write_path(self, parsed_node): write_path = parsed_node.get_target_write_path("target_path", "subdirectory") assert ( @@ -423,3 +439,10 @@ def test_get_target_write_path_split(self, parsed_node): write_path == "target_path/subdirectory/test_package/test_original_file_path/test_path/test_path_split.sql" ) + + def test_get_target_write_path_for_yml_node(self, parsed_yml_node): + write_path = parsed_yml_node.get_target_write_path("target_path", "subdirectory") + assert ( + write_path + == "target_path/subdirectory/test_package/folder/test_original_file_path_yml/test_path.sql" + )