From 711da789c29b4ea24e064fbf3dc87eb28f3bc6fa Mon Sep 17 00:00:00 2001 From: chillaranand Date: Wed, 7 Jan 2026 20:20:16 +0530 Subject: [PATCH] fix(data): resolve relative paths from repo root instead of cwd --- dvc/api/data.py | 3 ++- tests/unit/test_api.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/dvc/api/data.py b/dvc/api/data.py index fb2824fb86..b8ba577967 100644 --- a/dvc/api/data.py +++ b/dvc/api/data.py @@ -291,7 +291,8 @@ def _open( fs_path = path else: fs = DVCFileSystem(repo=_repo, subrepos=True) - fs_path = fs.from_os_path(path) + rel_path = fs.from_os_path(path) + fs_path = fs.join(fs.root_marker, rel_path) try: with fs.open(fs_path, mode=mode, encoding=encoding) as fobj: diff --git a/tests/unit/test_api.py b/tests/unit/test_api.py index f5a8e7344e..09e06b1014 100644 --- a/tests/unit/test_api.py +++ b/tests/unit/test_api.py @@ -23,3 +23,14 @@ def test_open_rev_raises_error_on_wrong_mode(tmp_dir, dvc): ): with api.open("foo", mode="w"): pass + + +def test_api_read_from_subdir_with_repo_arg(tmp_dir, dvc): + """Ensure relative paths are resolved from repo root, not cwd.""" + + tmp_dir.dvc_gen({"data": {"data.xml": "contents"}}) + subdir = tmp_dir / "src" + subdir.mkdir() + + with subdir.chdir(): + assert api.read("data/data.xml", repo=str(tmp_dir)) == "contents"