diff --git a/dvc/repo/experiments/run.py b/dvc/repo/experiments/run.py index 5975a375d1..42e5ab9100 100644 --- a/dvc/repo/experiments/run.py +++ b/dvc/repo/experiments/run.py @@ -11,7 +11,7 @@ @locked -def run( # noqa: C901 +def run( # noqa: C901, PLR0912 repo, targets: Optional[Iterable[str]] = None, params: Optional[Iterable[str]] = None, @@ -36,6 +36,18 @@ def run( # noqa: C901 from dvc.utils.hydra import to_hydra_overrides path_overrides = to_path_overrides(params) + + if tmp_dir or queue: + untracked = repo.scm.untracked_files() + for path in path_overrides: + if path in untracked: + logger.debug( + "'%s' is currently untracked but will be modified by DVC. " + "Adding it to git.", + path, + ) + repo.scm.add([path]) + hydra_sweep = any( x.is_sweep_override() for param_file in path_overrides diff --git a/tests/func/experiments/test_queue.py b/tests/func/experiments/test_queue.py index 3ee9b43df2..eee07ecc10 100644 --- a/tests/func/experiments/test_queue.py +++ b/tests/func/experiments/test_queue.py @@ -92,3 +92,19 @@ def test_queue_remove_done( assert set(celery_queue.clear(success=True)) == set(success_tasks[:2]) assert celery_queue.status() == [] + + +def test_queue_doesnt_remove_untracked_params_file(tmp_dir, dvc, scm): + """Regression test for https://github.com/iterative/dvc/issues/7842""" + tmp_dir.gen("params.yaml", "foo: 1") + stage = dvc.run(cmd="echo ${foo}", params=["foo"], name="echo-foo") + scm.add( + [ + "dvc.yaml", + "dvc.lock", + ".gitignore", + ] + ) + scm.commit("init") + dvc.experiments.run(stage.addressing, params=["foo=2"], queue=True) + assert (tmp_dir / "params.yaml").exists()