From 0c4c2642d891cd44a36befadd493db4a5284fbba Mon Sep 17 00:00:00 2001 From: Victor Skvortsov Date: Fri, 6 Nov 2026 11:40:25 +0500 Subject: [PATCH] Fix dstack apply -f for configurations outside the current directory Co-authored-by: Charlie Freeman <243496084+cf3901646@users.noreply.github.com> --- .../cli/services/configurators/__init__.py | 20 ++++++- .../cli/services/configurators/base.py | 4 +- .../configurators/test_configurators.py | 60 +++++++++++++++++++ 3 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 src/tests/_internal/cli/services/configurators/test_configurators.py diff --git a/src/dstack/_internal/cli/services/configurators/__init__.py b/src/dstack/_internal/cli/services/configurators/__init__.py index cf0d7f08f6..8dd0e89691 100644 --- a/src/dstack/_internal/cli/services/configurators/__init__.py +++ b/src/dstack/_internal/cli/services/configurators/__init__.py @@ -64,6 +64,20 @@ def get_run_configurator_class(configurator_type: str) -> Type[BaseRunConfigurat def load_apply_configuration( configuration_file: Optional[str], ) -> Tuple[str, AnyApplyConfiguration]: + """ + Loads and parses the configuration for `dstack apply` and `dstack delete`. + + Args: + configuration_file: The `-f` argument as passed by the user: a path to the configuration + file, absolute or relative to the current directory, or `-` to read from stdin. + If `None`, `.dstack.yml` or `.dstack.yaml` in the current directory is used. + + Returns: + The configuration path and the parsed configuration. The configuration path is relative + to the current directory if the file is under it, and absolute otherwise; `-` for stdin. + It's shown to the user and sent to the server as part of the spec. Its parent directory + is the base for relative paths in the configuration, such as `repos` and `files`. + """ if configuration_file is None: configuration_path = Path.cwd() / ".dstack.yml" if not configuration_path.exists(): @@ -85,4 +99,8 @@ def load_apply_configuration( raise ConfigurationError(f"Failed to load configuration from {configuration_path}") if isinstance(configuration_path, int): return APPLY_STDIN_NAME, conf - return str(configuration_path.absolute().relative_to(Path.cwd())), conf + absolute_path = configuration_path.absolute() + cwd = Path.cwd() + if absolute_path.is_relative_to(cwd): + return str(absolute_path.relative_to(cwd)), conf + return str(absolute_path), conf diff --git a/src/dstack/_internal/cli/services/configurators/base.py b/src/dstack/_internal/cli/services/configurators/base.py index f380d9fe0e..fb54c165d3 100644 --- a/src/dstack/_internal/cli/services/configurators/base.py +++ b/src/dstack/_internal/cli/services/configurators/base.py @@ -38,7 +38,7 @@ def apply_configuration( Args: conf: The apply configuration. - configuration_path: The path to the configuration file. + configuration_path: The configuration path as returned by `load_apply_configuration`. command_args: The args parsed by `dstack apply`. configurator_args: The known args parsed by `cls.get_parser()`. """ @@ -56,7 +56,7 @@ def delete_configuration( Args: conf: The apply configuration. - configuration_path: The path to the configuration file. + configuration_path: The configuration path as returned by `load_apply_configuration`. command_args: The args parsed by `dstack delete`. """ pass diff --git a/src/tests/_internal/cli/services/configurators/test_configurators.py b/src/tests/_internal/cli/services/configurators/test_configurators.py new file mode 100644 index 0000000000..5530908889 --- /dev/null +++ b/src/tests/_internal/cli/services/configurators/test_configurators.py @@ -0,0 +1,60 @@ +from pathlib import Path + +import pytest + +from dstack._internal.cli.services.configurators import load_apply_configuration +from dstack._internal.core.errors import ConfigurationError +from dstack._internal.core.models.configurations import TaskConfiguration + +CONFIGURATION_YAML = "type: task\ncommands:\n - echo hi\n" + + +class TestLoadApplyConfiguration: + @pytest.fixture + def cwd(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path: + cwd = tmp_path / "cwd" + cwd.mkdir() + monkeypatch.chdir(cwd) + return cwd + + def test_returns_relative_path_for_configuration_under_cwd(self, cwd: Path): + _write_configuration(cwd / "conf" / "task.dstack.yml") + + path, conf = load_apply_configuration(str(cwd / "conf" / "task.dstack.yml")) + + assert path == str(Path("conf") / "task.dstack.yml") + assert isinstance(conf, TaskConfiguration) + + def test_returns_absolute_path_for_configuration_outside_cwd(self, cwd: Path): + configuration_path = _write_configuration(cwd.parent / "conf" / "task.dstack.yml") + + path, conf = load_apply_configuration(str(configuration_path)) + + assert path == str(configuration_path) + assert isinstance(conf, TaskConfiguration) + + def test_keeps_parent_segments_of_relative_path(self, cwd: Path): + _write_configuration(cwd.parent / "conf" / "task.dstack.yml") + configuration_file = str(Path("..") / "conf" / "task.dstack.yml") + + path, _ = load_apply_configuration(configuration_file) + + assert path == configuration_file + + def test_loads_default_configuration_from_cwd(self, cwd: Path): + _write_configuration(cwd / ".dstack.yml") + + path, conf = load_apply_configuration(None) + + assert path == ".dstack.yml" + assert isinstance(conf, TaskConfiguration) + + def test_raises_if_configuration_does_not_exist(self, cwd: Path): + with pytest.raises(ConfigurationError): + load_apply_configuration(str(cwd / "missing.dstack.yml")) + + +def _write_configuration(path: Path) -> Path: + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(CONFIGURATION_YAML) + return path