From ff79a94e4e5acbf42bd0c94d74e54b1faba8e4e5 Mon Sep 17 00:00:00 2001 From: Zhenyu Gao Date: Fri, 15 Oct 2021 01:16:35 -0700 Subject: [PATCH] catch KeyError exception while walking dvc folder 1. add try-catch to catch KeyError exception while walking dvc folder 2. Fix #6695 --- dvc/fs/dvc.py | 23 +++++++++++++---------- tests/func/test_ls.py | 12 ++++++++++++ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/dvc/fs/dvc.py b/dvc/fs/dvc.py index 90199d1d2d..dabe92873e 100644 --- a/dvc/fs/dvc.py +++ b/dvc/fs/dvc.py @@ -166,16 +166,19 @@ def _walk(self, root, trie, topdown=True, **kwargs): self._add_dir(trie, out, **kwargs) root_len = len(root.parts) - for key, out in trie.iteritems(prefix=root.parts): # noqa: B301 - if key == root.parts: - continue - - name = key[root_len] - if len(key) > root_len + 1 or (out and out.is_dir_checksum): - dirs.add(name) - continue - - files.append(name) + try: + for key, out in trie.iteritems(prefix=root.parts): # noqa: B301 + if key == root.parts: + continue + + name = key[root_len] + if len(key) > root_len + 1 or (out and out.is_dir_checksum): + dirs.add(name) + continue + + files.append(name) + except KeyError: + pass assert topdown dirs = list(dirs) diff --git a/tests/func/test_ls.py b/tests/func/test_ls.py index 1b716e9571..e8fb15e58f 100644 --- a/tests/func/test_ls.py +++ b/tests/func/test_ls.py @@ -117,6 +117,18 @@ def test_ls_repo_dvc_only_recursive(tmp_dir, dvc, scm): ) +def test_ls_repo_with_new_path_dir(tmp_dir, dvc, scm): + tmp_dir.scm_gen(FS_STRUCTURE, commit="init") + tmp_dir.dvc_gen({"mysub": {}}, commit="dvc") + tmp_dir.gen({"mysub/sub": {"foo": "content"}}) + + files = Repo.ls(os.fspath(tmp_dir), path="mysub/sub") + match_files( + files, + ((("foo",), False),), + ) + + def test_ls_repo_with_path_dir(tmp_dir, dvc, scm): tmp_dir.scm_gen(FS_STRUCTURE, commit="init") tmp_dir.dvc_gen(DVC_STRUCTURE, commit="dvc")