diff --git a/.changes/unreleased/Fixes-20250116-192616.yaml b/.changes/unreleased/Fixes-20250116-192616.yaml new file mode 100644 index 00000000000..9913e2473a4 --- /dev/null +++ b/.changes/unreleased/Fixes-20250116-192616.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Allow copying asset when dbt docs command is run outside the dbt project +time: 2025-01-16T19:26:16.160976423Z +custom: + Author: cedric-orange + Issue: "9308" diff --git a/core/dbt/task/docs/generate.py b/core/dbt/task/docs/generate.py index 0befb9d349b..2de9399d5b5 100644 --- a/core/dbt/task/docs/generate.py +++ b/core/dbt/task/docs/generate.py @@ -238,8 +238,10 @@ def run(self) -> CatalogArtifact: if os.path.exists(to_asset_path): shutil.rmtree(to_asset_path) - if os.path.exists(asset_path): - shutil.copytree(asset_path, to_asset_path) + from_asset_path = os.path.join(self.config.project_root, asset_path) + + if os.path.exists(from_asset_path): + shutil.copytree(from_asset_path, to_asset_path) if self.manifest is None: raise DbtInternalError("self.manifest was None in run!") diff --git a/tests/functional/adapter/basic/test_docs_generate.py b/tests/functional/adapter/basic/test_docs_generate.py index c6c070bf2a3..bf6384a5103 100644 --- a/tests/functional/adapter/basic/test_docs_generate.py +++ b/tests/functional/adapter/basic/test_docs_generate.py @@ -453,9 +453,30 @@ def test_run_and_generate(self, project, expected_catalog): verify_catalog(project, expected_catalog, start_time) # Check that assets have been copied to the target directory for use in the docs html page - assert os.path.exists(os.path.join("", "target", "assets")) - assert os.path.exists(os.path.join("", "target", "assets", "lorem-ipsum.txt")) - assert not os.path.exists(os.path.join("", "target", "non-existent-assets")) + assert os.path.exists(os.path.join(project.project_root, "target", "assets")) + assert os.path.exists( + os.path.join(project.project_root, "target", "assets", "lorem-ipsum.txt") + ) + assert not os.path.exists( + os.path.join(project.project_root, "target", "non-existent-assets") + ) + + # Test generic "docs generate" command + def test_locally_run_and_generate(self, project, expected_catalog): + os.chdir( + project.profiles_dir + ) # Change to random directory to test that assets doc generation works with project-dir + start_time = run_and_generate(project) + verify_catalog(project, expected_catalog, start_time) + + # Check that assets have been copied to the target directory for use in the docs html page + assert os.path.exists(os.path.join(project.project_root, "target", "assets")) + assert os.path.exists( + os.path.join(project.project_root, "target", "assets", "lorem-ipsum.txt") + ) + assert not os.path.exists( + os.path.join(project.project_root, "target", "non-existent-assets") + ) class TestDocsGenerate(BaseDocsGenerate):