Repository navigation
fix(repo): refuse a --cache-root, --cache or $DOKIMI_REPO_CACHE that is not a usable directory with exit 2 #891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
antonwolfy
wants to merge
6
commits into
intel:main
Choose a base branch
from
antonwolfy:fix/cache-path-usable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+360
−15
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
626cf63
fix(repo): refuse a --cache-root or --cache that is not a usable dire…
antonwolfy 18360fd
fix(repo): refuse a $DOKIMI_REPO_CACHE that is not a usable directory…
antonwolfy 8ccfe5a
fix(repo): cover the unset --cache-root branches of the cache path ch…
antonwolfy e50dbc7
Merge main into fix/cache-path-usable
antonwolfy fbbd87d
fix(repo): shorten the _cache_dir docstring so repo.py stays under th…
antonwolfy ba159aa
fix(repo): check the resolved cache paths before the image build
vchamarthi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
dokimi/changelog.d/20261117_150000_antonwolfy_cache-paths-usable.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ### Fixed | ||
|
|
||
| - **`eval check` and `repo run` refuse a `--cache-root` or a `--cache NAME=PATH` that is not a usable directory with exit 2 naming the flag and the path** — a regular file, a path below one, or a `--cache-root` holding a regular file named after a cache used to end in `✗ [Errno 17] File exists` or in `✗ [Errno 20] Not a directory`, and exit 1 after the image build; the paths are now checked before the export and the build. An existing read-only directory still passes. |
3 changes: 3 additions & 0 deletions
3
dokimi/changelog.d/20261117_150100_antonwolfy_repo-cache-env-usable.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ### Fixed | ||
|
|
||
| - **`repo` commands refuse a `$DOKIMI_REPO_CACHE` that is not a usable directory with exit 2 and a message naming the variable and the path** — with no `--cache-dir`, a variable naming a regular file (or a path below one) used to end in `✗ [Errno 17] File exists` and exit 1 at the clone; it is now checked before any clone, as `--cache-dir` is. A `--cache-dir` that is given wins, and the variable is not looked at then. A bad variable next to a local-directory source is refused too, where it used to be ignored. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # Copyright (C) 2026 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """`usable_cache_dir` names the setting it was given in each of its messages (#826, #827).""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
|
|
||
| import pytest | ||
|
|
||
| from dokimi.commands.cache_dir import CACHE_USE, RETENTION_USE, usable_cache_dir | ||
| from dokimi.utils import InputError | ||
|
|
||
|
|
||
| def _message(raw, **kwargs) -> str: | ||
| with pytest.raises(InputError) as exc: | ||
| usable_cache_dir(raw, **kwargs) | ||
| return str(exc.value) | ||
|
|
||
|
|
||
| def test_the_default_label_is_cache_dir_in_a_file_and_a_file_above_it(tmp_path): | ||
| blocker = tmp_path / "file" | ||
| blocker.write_text("x", encoding="utf-8") | ||
| assert _message(str(blocker)) == f"--cache-dir {blocker} is not a directory; pass a directory {RETENTION_USE}" | ||
| child = blocker / "c" | ||
| assert _message(str(child)) == (f"--cache-dir {child} has {blocker} above it, which is not a directory; " | ||
| f"pass a directory {RETENTION_USE}") | ||
|
|
||
|
|
||
| @pytest.mark.skipif(os.name != "posix" or os.geteuid() == 0, reason="needs permission bits that bind the user") | ||
| def test_a_custom_label_replaces_the_flag_in_the_not_writable_message(tmp_path): | ||
| locked = tmp_path / "locked" | ||
| locked.mkdir() | ||
| locked.chmod(0o500) | ||
| try: | ||
| assert _message(str(locked / "new"), label="--cache-root", use=CACHE_USE) == ( | ||
| f"--cache-root {locked / 'new'}: {locked} is not writable; pass a directory {CACHE_USE}") | ||
| finally: | ||
| locked.chmod(0o700) | ||
|
|
||
|
|
||
| def test_a_custom_label_replaces_the_flag_in_the_not_a_directory_messages(tmp_path): | ||
| blocker = tmp_path / "file" | ||
| blocker.write_text("x", encoding="utf-8") | ||
| assert _message(str(blocker), use=CACHE_USE, label="--cache build-cache") == ( | ||
| f"--cache build-cache {blocker} is not a directory; pass a directory {CACHE_USE}") | ||
| assert "--cache-dir" not in _message(str(blocker / "c"), label="--cache-root") | ||
|
|
||
|
|
||
| def test_a_custom_label_does_not_change_what_passes(tmp_path): | ||
| assert usable_cache_dir(None, label="--cache-root") is None | ||
| assert usable_cache_dir("", label="--cache-root") is None | ||
| assert usable_cache_dir(str(tmp_path / "new"), label="--cache-root") == tmp_path / "new" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| # Copyright (C) 2026 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """`$DOKIMI_REPO_CACHE` is checked like `--cache-dir` before any clone (#827): a bad value exits 2 naming the variable.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import subprocess | ||
|
|
||
| import pytest | ||
|
|
||
| from dokimi import cli | ||
| from dokimi.commands.repo import _cache_dir | ||
| from tests.cli_refusal import refusal | ||
|
|
||
| CLOSING = "pass a directory the repository clones can be cached in" | ||
|
|
||
|
|
||
| def _argv(monkeypatch, *argv): | ||
| monkeypatch.setattr("sys.argv", ["dokimi", *argv]) | ||
|
|
||
|
|
||
| def _source(tmp_path) -> str: | ||
| return f"file://{tmp_path}/nothere@HEAD" | ||
|
|
||
|
|
||
| def _commands(tmp_path): | ||
| return { | ||
| "fetch": ["repo", "fetch", _source(tmp_path)], | ||
| "scan": ["repo", "scan", _source(tmp_path)], | ||
| "reach": ["repo", "reach", _source(tmp_path), "--model", "m"], | ||
| "check": ["eval", "check", _source(tmp_path)], | ||
| } | ||
|
|
||
|
|
||
| def _file(tmp_path): | ||
| path = tmp_path / "cachefile" | ||
| path.write_text("x", encoding="utf-8") | ||
| return path | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("command", ["fetch", "scan", "reach", "check"]) | ||
| def test_repo_command_refuses_a_cache_variable_that_is_a_regular_file(command, tmp_path, monkeypatch, capsys): | ||
| """#827: it was `✗ [Errno 17] File exists: '<file>'`, exit 1, with no mention of the variable.""" | ||
| cache = _file(tmp_path) | ||
| monkeypatch.setenv("DOKIMI_REPO_CACHE", str(cache)) | ||
| _argv(monkeypatch, *_commands(tmp_path)[command]) | ||
| with refusal() as exc: | ||
| cli.main() | ||
| err = capsys.readouterr().err | ||
| assert exc.code == 2 | ||
| assert f"$DOKIMI_REPO_CACHE {cache} is not a directory; {CLOSING}" in err | ||
| assert "File exists" not in err and cache.read_text(encoding="utf-8") == "x" | ||
|
|
||
|
|
||
| def test_repo_fetch_refuses_a_cache_variable_below_a_regular_file_and_makes_nothing(tmp_path, monkeypatch, capsys): | ||
| blocker = _file(tmp_path) | ||
| monkeypatch.setenv("DOKIMI_REPO_CACHE", str(blocker / "cache")) | ||
| _argv(monkeypatch, *_commands(tmp_path)["fetch"]) | ||
| with refusal() as exc: | ||
| cli.main() | ||
| err = capsys.readouterr().err | ||
| assert exc.code == 2 | ||
| assert f"$DOKIMI_REPO_CACHE {blocker / 'cache'} has {blocker} above it, which is not a directory; {CLOSING}" in err | ||
| assert sorted(p.name for p in tmp_path.iterdir()) == ["cachefile"] | ||
|
|
||
|
|
||
| def test_a_cache_dir_flag_wins_over_a_bad_variable(tmp_path, monkeypatch, capsys): | ||
| src = tmp_path / "src" | ||
| src.mkdir() | ||
| (src / "a.txt").write_text("a", encoding="utf-8") | ||
| git = ["git", "-C", str(src), "-c", "user.name=t", "-c", "user.email=t@example.com"] | ||
| subprocess.run([*git, "init", "-q"], check=True) | ||
| subprocess.run([*git, "add", "-A"], check=True) | ||
| subprocess.run([*git, "commit", "-qm", "init"], check=True) | ||
| sha = subprocess.run([*git, "rev-parse", "HEAD"], check=True, capture_output=True, text=True).stdout.strip() | ||
| cache = tmp_path / "cache" | ||
| cache.mkdir() | ||
| monkeypatch.setenv("DOKIMI_REPO_CACHE", str(_file(tmp_path))) | ||
| _argv(monkeypatch, "repo", "fetch", f"file://{src}@{sha}", "--cache-dir", str(cache)) | ||
| cli.main() | ||
| assert capsys.readouterr().out.startswith(f"{sha}\t") | ||
| assert any(cache.iterdir()) # cloned into the flag's directory | ||
|
|
||
|
|
||
| def test_a_bad_cache_dir_flag_is_still_named_when_the_variable_is_set(tmp_path, monkeypatch, capsys): | ||
| bad = _file(tmp_path) | ||
| monkeypatch.setenv("DOKIMI_REPO_CACHE", str(tmp_path)) | ||
| _argv(monkeypatch, *_commands(tmp_path)["fetch"], "--cache-dir", str(bad)) | ||
| with refusal() as exc: | ||
| cli.main() | ||
| err = capsys.readouterr().err | ||
| assert exc.code == 2 and f"--cache-dir {bad} is not a directory" in err and "$DOKIMI_REPO_CACHE" not in err | ||
|
|
||
|
|
||
| def test_cache_dir_returns_a_good_variable_and_an_empty_one_counts_as_unset(tmp_path, monkeypatch): | ||
| args = argparse.Namespace(cache_dir=None) | ||
| monkeypatch.setenv("DOKIMI_REPO_CACHE", "") | ||
| assert _cache_dir(args) is None | ||
| monkeypatch.setenv("DOKIMI_REPO_CACHE", str(tmp_path)) | ||
| assert _cache_dir(args) == tmp_path | ||
| assert _cache_dir(argparse.Namespace(cache_dir="")) == tmp_path | ||
| monkeypatch.setenv("DOKIMI_REPO_CACHE", str(tmp_path / "new" / "deeper")) | ||
| assert _cache_dir(args) == tmp_path / "new" / "deeper" | ||
| assert not (tmp_path / "new").exists() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Must-fix — Impact: unusable cache input can bypass the labeled usage-error handler, producing exit 1 or a traceback with exit 5 instead of exit 2. Trigger: on supported Python 3.11/3.12, create a self-referencing symlink (
ln -s loop /tmp/loop) and pass--cache-root /tmp/loopor--cache build-cache=/tmp/loop;Path.resolve()raises RuntimeError. An inaccessible ancestor can also raise OSError during resolution. Evidence: all three new resolve() calls execute outside usable_cache_dir's exception handling; _errors() catches OSError as exit 1 but excludes RuntimeError, which reaches the CLI's traceback/exit-5 handler. Catch resolution failures (OSError and RuntimeError) and convert them to InputError naming the relevant flag and path, covering root, override and derived-child paths with regression tests. Workaround: supply an accessible, non-looping path. Severity justification: medium, because a concrete class of invalid user inputs violates the promised exit-code and diagnostic contract without data loss.