From 1d8b2e5fa6b9a3737ef11e7b9bb52ca43f46cee4 Mon Sep 17 00:00:00 2001 From: David de la Iglesia Castro Date: Thu, 30 Mar 2023 13:30:03 +0200 Subject: [PATCH] exp run: Propagate `.dvc/config.local` in `TempDirExecutor`. Closes #7254 --- dvc/repo/experiments/executor/local.py | 16 ++++++++++++---- tests/func/experiments/test_experiments.py | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/dvc/repo/experiments/executor/local.py b/dvc/repo/experiments/executor/local.py index 07eb4ab82..3bb0d47fb 100644 --- a/dvc/repo/experiments/executor/local.py +++ b/dvc/repo/experiments/executor/local.py @@ -4,6 +4,7 @@ from contextlib import ExitStack from tempfile import mkdtemp from typing import TYPE_CHECKING, Optional, Union +from configobj import ConfigObj from funcy import retry from shortuuid import uuid @@ -134,22 +135,29 @@ class TempDirExecutor(BaseLocalExecutor): merge_rev = self.scm.get_ref(EXEC_MERGE) self.scm.stash.apply(merge_rev) + self._update_config(repo.config.read("local")) - def _config(self, cache_dir): + def _update_config(self, update): local_config = os.path.join( self.root_dir, self.dvc_dir, "config.local", ) logger.debug("Writing experiments local config '%s'", local_config) - with open(local_config, "w", encoding="utf-8") as fobj: - fobj.write(f"[cache]\n dir = {cache_dir}") + if os.path.exists(local_config): + conf_obj = ConfigObj(local_config) + conf_obj.merge(update) + else: + conf_obj = ConfigObj(update) + if conf_obj: + with open(local_config, "wb") as fobj: + conf_obj.write(fobj) def init_cache( self, repo: "Repo", rev: str, run_cache: bool = True # noqa: ARG002 ): """Initialize DVC cache.""" - self._config(repo.cache.repo.path) + self._update_config({"cache": {"dir": repo.cache.repo.path}}) def cleanup(self, infofile: Optional[str] = None): super().cleanup(infofile) diff --git a/tests/func/experiments/test_experiments.py b/tests/func/experiments/test_experiments.py index 2cb499269..f6e9d5b5e 100644 --- a/tests/func/experiments/test_experiments.py +++ b/tests/func/experiments/test_experiments.py @@ -5,6 +5,7 @@ import stat from textwrap import dedent import pytest +from configobj import ConfigObj from funcy import first from dvc.dvcfile import PROJECT_FILE @@ -664,3 +665,21 @@ def test_experiment_no_commit(tmp_dir): repo.experiments.ls() finally: repo.close() + + +def test_local_config_is_propagated_to_tmp(tmp_dir, scm, dvc): + with dvc.config.edit("local") as conf: + conf["cache"]["type"] = "hardlink" + + stage = dvc.stage.add( + cmd="cat .dvc/config.local > file", name="foo", outs_no_cache=["file"] + ) + scm.add_commit(["dvc.yaml"], message="add dvc.yaml") + + results = dvc.experiments.run(stage.addressing, tmp_dir=True) + exp = first(results) + fs = scm.get_fs(exp) + + with fs.open("file") as fobj: + conf_obj = ConfigObj(fobj) + assert conf_obj["cache"]["type"] == "hardlink"