diff --git a/dvc/output/local.py b/dvc/output/local.py index fd2c8b2860..8e5ee4b280 100644 --- a/dvc/output/local.py +++ b/dvc/output/local.py @@ -5,6 +5,7 @@ from dvc.exceptions import DvcException from dvc.istextfile import istextfile from dvc.output.base import BaseOutput +from dvc.scm.tree import is_working_tree from dvc.utils import relpath from dvc.utils.fs import path_isin @@ -22,6 +23,8 @@ def __init__(self, stage, path, *args, **kwargs): path = relpath(path, stage.wdir) super().__init__(stage, path, *args, **kwargs) + if self.is_in_repo and self.repo and is_working_tree(self.repo.tree): + self.tree = self.repo.tree def _parse_path(self, tree, path): parsed = urlparse(path) diff --git a/tests/func/test_ignore.py b/tests/func/test_ignore.py index 7fb1e73b8d..41533ac389 100644 --- a/tests/func/test_ignore.py +++ b/tests/func/test_ignore.py @@ -364,3 +364,28 @@ def test_pattern_trie_tree(tmp_dir, dvc): ) == ignore_pattern_bottom ) + + +def test_ignore_in_added_dir(tmp_dir, dvc): + tmp_dir.gen( + { + "dir": { + "sub": { + "ignored": {"content": "ignored content"}, + "not_ignored": "not ignored content", + } + }, + ".dvcignore": "**/ignored", + } + ) + dvc.tree.__dict__.pop("dvcignore", None) + + ignored_path = tmp_dir / "dir" / "sub" / "ignored" + assert not dvc.tree.exists(PathInfo(ignored_path)) + assert ignored_path.exists() + + dvc.add("dir") + shutil.rmtree(ignored_path) + dvc.checkout() + + assert not ignored_path.exists()