diff --git a/dvc/stage/__init__.py b/dvc/stage/__init__.py index 866203001..85615c43f 100644 --- a/dvc/stage/__init__.py +++ b/dvc/stage/__init__.py @@ -3,7 +3,7 @@ import os import string from collections import defaultdict -from funcy import project +from funcy import cached_property, project import dvc.dependency as dependency import dvc.prompt as prompt @@ -127,6 +127,8 @@ class Stage(params.StageParams): @path.setter def path(self, path): self._path = path + self.__dict__.pop("path_in_repo", None) + self.__dict__.pop("relpath", None) @property def dvcfile(self): @@ -172,11 +174,11 @@ class Stage(params.StageParams): and self.path_in_repo == other.path_in_repo ) - @property + @cached_property def path_in_repo(self): return relpath(self.path, self.repo.root_dir) - @property + @cached_property def relpath(self): return relpath(self.path) diff --git a/tests/func/test_stage.py b/tests/func/test_stage.py index f2c00113b..aabe42f07 100644 --- a/tests/func/test_stage.py +++ b/tests/func/test_stage.py @@ -220,6 +220,10 @@ def test_stage_strings_representation(tmp_dir, dvc, run_copy): folder = tmp_dir / "dir" folder.mkdir() with folder.chdir(): + # `Stage` caches `relpath` results, forcing it to reset + stage1.path = stage1.path + stage2.path = stage2.path + rel_path = os.path.relpath(stage1.path) assert stage1.addressing == rel_path assert repr(stage1) == f"Stage: '{rel_path}'"