Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20251105-180255.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Fixes metricflow not being able to find the profiles.yml in the home directory
time: 2025-11-05T18:02:55.20941+02:00
custom:
Author: pekapa
Issue: https://github.com/dbt-labs/metricflow/issues/1719
5 changes: 4 additions & 1 deletion dbt-metricflow/dbt_metricflow/cli/cli_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ def setup(
Returns: None
"""
cwd = pathlib.Path.cwd()
home = pathlib.Path.home()

dbt_profiles_dir_default_path = cwd if (cwd / "profiles.yml").exists() else home / ".dbt"

dbt_profiles_dir_env_var = os.environ.get(CLIConfiguration.DBT_PROFILES_DIR_ENV_VAR_NAME)
dbt_profiles_dir_from_env = (
Expand All @@ -75,7 +78,7 @@ def setup(
pathlib.Path(dbt_project_dir_env_var) if dbt_project_dir_env_var is not None else None
)

dbt_profiles_path = dbt_profiles_path or dbt_profiles_dir_from_env or cwd
dbt_profiles_path = dbt_profiles_path or dbt_profiles_dir_from_env or dbt_profiles_dir_default_path
dbt_project_path = dbt_project_path or dbt_project_dir_from_env or cwd
dbt_project_yaml_path = dbt_project_path / "dbt_project.yml"
if not dbt_project_yaml_path.exists():
Expand Down
68 changes: 64 additions & 4 deletions tests_metricflow/cli/test_isolated_command_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
from metricflow_semantics.toolkit.mf_logging.pretty_print import PrettyFormatDictOption, mf_pformat_dict

from dbt_metricflow.cli.cli_configuration import CLIConfiguration
from tests_metricflow.cli.cli_test_helpers import (
create_tutorial_project_files,
run_dbt_build,
)
from tests_metricflow.cli.cli_test_helpers import create_tutorial_project_files, run_dbt_build
from tests_metricflow.cli.isolated_cli_command_interface import IsolatedCliCommandEnum
from tests_metricflow.cli.isolated_cli_command_runner import IsolatedCliCommandRunner
from tests_metricflow.snapshot_utils import assert_str_snapshot_equal
Expand Down Expand Up @@ -156,3 +153,66 @@ def test_environment_variables(
snapshot_str=result.output,
expectation_description="A table showing the `transactions` metric.",
)


@pytest.mark.slow
def test_home_profiles_dir(
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
) -> None:
"""Tests running an MF CLI command with the profiles file in the home directory."""
with tempfile.TemporaryDirectory() as tmp_directory:
tmp_directory_path = Path(tmp_directory)
dbt_project_path = create_tutorial_project_files(tmp_directory_path)

# Move the `profiles.yml` to a the home directory to isolate the two variables.
dbt_profiles_path = Path.home() / ".dbt"
if not dbt_profiles_path.exists():
os.mkdir(dbt_profiles_path)

if (dbt_profiles_path / "profiles.yml").exists():
# Backup the existing profiles.yml file.
shutil.move(
src=dbt_profiles_path / "profiles.yml",
dst=dbt_profiles_path / "profiles.yml.backup",
)

shutil.move(
src=dbt_project_path / "profiles.yml",
dst=dbt_profiles_path / "profiles.yml",
)

cli_runner = IsolatedCliCommandRunner(
dbt_project_path=dbt_project_path,
# No need to specify the profiles path since it's in the home directory.
)
with cli_runner.running_context():
run_dbt_build(cli_runner)
result = cli_runner.run_command(
command_enum=IsolatedCliCommandEnum.MF_QUERY,
command_args=[
"--metrics",
"transactions",
],
)

if (dbt_profiles_path / "profiles.yml.backup").exists():
# Restore the original profiles.yml file.
# We need to do this before the result is checked
# to avoid raising an exception and failing to restore the original file.
shutil.move(
src=dbt_profiles_path / "profiles.yml.backup",
dst=dbt_profiles_path / "profiles.yml",
)
else:
# No backup so let's cleanup the profiles.yml file.
os.remove(dbt_profiles_path / "profiles.yml")

result.raise_exception_on_failure()
assert_str_snapshot_equal(
request=request,
mf_test_configuration=mf_test_configuration,
snapshot_id="result",
snapshot_str=result.output,
expectation_description="A table showing the `transactions` metric.",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
test_name: test_home_profiles_dir
test_filename: test_isolated_command_runner.py
docstring:
Tests running an MF CLI command with the profiles file in the home directory.
expectation_description:
A table showing the `transactions` metric.
---
transactions
--------------
50