|
| 1 | +# Repo-root-absolute dep path shorthand on write |
| 2 | + |
| 3 | +## Problem |
| 4 | + |
| 5 | +When a `.dvc` file depends on artifacts outside its own directory, DVX writes |
| 6 | +the dep path via `os.path.relpath()`, producing `../`-laden strings: |
| 7 | + |
| 8 | +```yaml |
| 9 | +# www/public/njsp/csvs.dvc |
| 10 | +deps: |
| 11 | + ../../../njsp/data/crashes.parquet: e9799868d9114846a7052100454cde51 |
| 12 | +``` |
| 13 | +
|
| 14 | +These `../../../` prefixes are hard to scan at a glance, change meaning based |
| 15 | +on which `.dvc` file they live in, and get noisier the deeper the `.dvc` file |
| 16 | +is nested. |
| 17 | + |
| 18 | +A shorter form is already supported on **read** (`src/dvx/run/dvc_files.py` |
| 19 | +lines 108-126): a leading `/` in a dep path is treated as repo-root-absolute, |
| 20 | +so the above is equivalent to: |
| 21 | + |
| 22 | +```yaml |
| 23 | +deps: |
| 24 | + /njsp/data/crashes.parquet: e9799868d9114846a7052100454cde51 |
| 25 | +``` |
| 26 | + |
| 27 | +But `_relativize_dep_paths` (same file, L129-153) is purely `os.path.relpath` |
| 28 | +based, so DVX writes back the `../` form and hand-edited `/`-shorthand paths |
| 29 | +revert on the next `dvx run` that rewrites the file. |
| 30 | + |
| 31 | +## Proposal |
| 32 | + |
| 33 | +In `_relativize_dep_paths`, prefer the repo-root-absolute shorthand for any |
| 34 | +dep whose relpath from `dvc_dir` would contain `..`. |
| 35 | + |
| 36 | +### Write rule |
| 37 | + |
| 38 | +For each `(dep_path, hash)`: |
| 39 | + |
| 40 | +- If `dep_path` is inside `dvc_dir` (i.e. `relpath(dep_path, dvc_dir)` has no |
| 41 | + `..`): write as-is relative (current behavior). |
| 42 | + - e.g. `njsp/data/refresh.dvc` with dep `njsp/data/FAUQStats2024.xml` |
| 43 | + → writes as `FAUQStats2024.xml` (dvc-dir-relative, no `/` prefix). |
| 44 | +- If `dep_path` is outside `dvc_dir` (relpath would start with `../`): write |
| 45 | + as `/<dep_path>` (repo-root-absolute). |
| 46 | + - e.g. `www/public/njsp/csvs.dvc` with dep `njsp/data/crashes.parquet` |
| 47 | + → writes as `/njsp/data/crashes.parquet` (not `../../../njsp/data/...`). |
| 48 | + |
| 49 | +### Rationale |
| 50 | + |
| 51 | +- Humans scanning `.dvc` files in deeply-nested directories don't have to |
| 52 | + count `../` segments to locate the dep. |
| 53 | +- Moving a `.dvc` file doesn't invalidate dep paths that point outside its |
| 54 | + directory — `/path` is stable, `../../path` is not. |
| 55 | +- No migration needed for older files: reads continue to accept both forms. |
| 56 | +- The `/` prefix is visually distinct from the bare `foo/bar` form used for |
| 57 | + same-directory deps, making it easy to tell at a glance whether a dep |
| 58 | + lives inside or outside the stage's own dir. |
| 59 | + |
| 60 | +### Edge cases |
| 61 | + |
| 62 | +- `dvc_dir == "."` (stage file is at repo root): paths are already |
| 63 | + repo-root-relative; don't add `/` prefix. Leave as-is. |
| 64 | +- `dvc_dir` absolute (unusual; current code preserves this): don't modify. |
| 65 | +- Windows: `os.path.relpath` may raise `ValueError` across drives; current |
| 66 | + code preserves the raw path in that case. The new rule doesn't apply |
| 67 | + there (no meaningful "repo root"). |
| 68 | + |
| 69 | +## Opt-out |
| 70 | + |
| 71 | +Add a `run.path_style` config key in `.dvx/config.yml` (or `dvx.yml`): |
| 72 | + |
| 73 | +```yaml |
| 74 | +run: |
| 75 | + path_style: repo_root # default: prefer / shorthand for out-of-dir deps |
| 76 | + # path_style: relative # old behavior: always os.path.relpath |
| 77 | +``` |
| 78 | + |
| 79 | +Only needed if there's demand for the old behavior (e.g. to minimize diff |
| 80 | +churn on initial rollout). Probably unnecessary. |
| 81 | + |
| 82 | +## Migration |
| 83 | + |
| 84 | +No forced migration. Over time, as DVX rewrites each `.dvc` file in a given |
| 85 | +repo, deps get normalized to the new shorthand. Downstream projects that |
| 86 | +prefer an immediate bulk conversion can do it with a short script (find |
| 87 | +every `.dvc` file, re-resolve each dep to repo-root-relative via the existing |
| 88 | +`_resolve_dep_paths`, then write back via the new `_relativize_dep_paths`). |
| 89 | + |
| 90 | +## Test plan |
| 91 | + |
| 92 | +- New `test_dvc_files` cases covering: |
| 93 | + - Dep inside `dvc_dir`: writes dvc-dir-relative (no leading `/`). |
| 94 | + - Dep outside `dvc_dir`: writes `/repo-root-relative`. |
| 95 | + - `dvc_dir == "."`: writes repo-root-relative (no leading `/`). |
| 96 | + - Roundtrip: resolve(relativize(deps)) == resolve(original_deps) for any |
| 97 | + supported write form. |
| 98 | +- Existing tests in `tests/test_run_dvc_files.py` for the read path should |
| 99 | + keep passing unchanged. |
| 100 | + |
| 101 | +## Out of scope |
| 102 | + |
| 103 | +- Changing the resolve (read) path. The leading-`/` form is already accepted |
| 104 | + and that doesn't need to change. |
| 105 | +- Converting `outs` paths — those live within `dvc_dir` by convention. |
| 106 | +- Source project: `hccs/crashes` will adopt this shorthand via its own spec |
| 107 | + (see `specs/dvx-dep-path-shorthand.md` there) once the DVX change lands. |
0 commit comments