diff --git a/dvc/repo/metrics/show.py b/dvc/repo/metrics/show.py index 956e2a160e..cdd787f005 100644 --- a/dvc/repo/metrics/show.py +++ b/dvc/repo/metrics/show.py @@ -60,19 +60,19 @@ def _read_metrics(repo, metrics, rev): if not tree.exists(metric): continue - with tree.open(metric, "r") as fobj: - try: + try: + with tree.open(metric, "r") as fobj: # NOTE this also supports JSON val = yaml.safe_load(fobj) - except yaml.YAMLError: - logger.debug( - "failed to read '%s' on '%s'", metric, rev, exc_info=True - ) - continue + except (FileNotFoundError, yaml.YAMLError): + logger.debug( + "failed to read '%s' on '%s'", metric, rev, exc_info=True + ) + continue - val = _extract_metrics(val) - if val: - res[str(metric)] = val + val = _extract_metrics(val) + if val: + res[str(metric)] = val return res diff --git a/tests/func/metrics/test_show.py b/tests/func/metrics/test_show.py index edbce4c7ae..69414b4e96 100644 --- a/tests/func/metrics/test_show.py +++ b/tests/func/metrics/test_show.py @@ -4,6 +4,7 @@ from dvc.repo import Repo from dvc.repo.metrics.show import NoMetricsError +from dvc.utils.fs import remove def test_show_empty(dvc): @@ -93,3 +94,19 @@ def test_show_subrepo_with_preexisting_tags(tmp_dir, scm): "workspace": {expected_path: {"foo": 1}}, "v1": {expected_path: {"foo": 1}}, } + + +def test_missing_cache(tmp_dir, dvc, run_copy_metrics): + tmp_dir.gen("metrics_t.yaml", "1.1") + run_copy_metrics( + "metrics_t.yaml", "metrics.yaml", metrics=["metrics.yaml"], + ) + + # This one should be skipped + stage = run_copy_metrics( + "metrics_t.yaml", "metrics2.yaml", metrics=["metrics2.yaml"], + ) + remove(stage.outs[0].fspath) + remove(stage.outs[0].cache_path) + + assert dvc.metrics.show() == {"": {"metrics.yaml": 1.1}}