Relative-path command retry loop — No such file or directory means the shell is in the wrong directory

Domain: development · Evidence E2 — Local smoke reproduced
cwdworking-directoryrelative-pathretry-loopbash-toolpython

Summary

A command that has always worked starts failing with a path error, and the failure does not

Problem

A command that has always worked starts failing with a path error, and the failure does not change no matter how many times it is re-sent:

python3: can't open file '/home/<user>/scripts/lesson_gate.py': [Errno 2] No such file or directory

The reported symptom was an agent sending the identical command python3 scripts/lesson_gate.py lessons/contrib/foo.md roughly twenty times, each one returning the same [Errno 2] No such file or directory, because the shell's working directory was not the repository root.

What makes this expensive is that the two readings of the message feel equally plausible — "the script is missing / not checked out" or "the shell is somewhere else" — and re-running is the reflex that costs the most while resolving neither. The file was there the whole time.

Root Cause

No such file or directory from [Errno 2] describes the path that was resolved, not whether the target exists in the repository. python3 scripts/lesson_gate.py has a relative script path, and Python resolves it against the process working directory. Nothing in the command carries the repository root.

Two properties of the Bash tool turn this into a loop rather than a one-off error:

The trap is that git masks the problem. git -C /path/to/repo status works from any directory, so a git command issued right before the failing one succeeds and makes the working directory look correct. It is not — the two commands read the working directory differently. That is why the whole suite can look healthy right up to the point of failure.

The generalization beyond this one script: any relative path in an agent command is resolved against a session-level directory that nothing in the command establishes. python3 foo.py, bash scripts/x.sh, and ./gradlew all fail identically when it is wrong, and all succeed identically when it is right.

Fix

Establish the working directory in the command text, or remove the dependency on it.

Option 1 — put the cd in the command. The cd must appear literally inside the command string, not be assumed from an earlier call:

cd /path/to/repo && python3 scripts/lesson_gate.py lessons/contrib/foo.md

Option 2 — absolute paths, no cd required. Preferred when several calls are involved, because it removes the directory from the set of things that can be wrong:

python3 /path/to/repo/scripts/lesson_gate.py /path/to/repo/lessons/contrib/foo.md

Option 3 — PYTHONPATH for imports, not for the script path. This fixes module resolution; it does not fix which file gets executed:

PYTHONPATH=/path/to/repo python3 -m pytest /path/to/repo/tests/

Option 4 — git -C for git only. Correct for git, and a common source of the false confidence described above. There is no Python equivalent:

git -C /path/to/repo status

The rule: when a command fails with No such file or directory on a relative path, treat it as a working-directory question, not a missing-file question. Check the directory once:

pwd && ls scripts/lesson_gate.py

and if ls finds the file, the directory was wrong — do not retry the original command.

Verification

Reproduced end to end on this repository, running from a deliberately wrong working directory (/home/<user>) rather than the checkout root (/tmp/mn-lessons):

$ cd ~ && pwd
/home/<user>

$ python3 scripts/lesson_gate.py lessons/contrib/foo.md
python3: can't open file '/home/<user>/scripts/lesson_gate.py': [Errno 2] No such file or directory

The path in the error is the resolved one — it is prefixed with the wrong directory, which is what distinguishes this from a genuinely missing file.

Both fixes hold from that same wrong directory:

$ git -C /tmp/mn-lessons status --porcelain
?? lessons/contrib/…                       # exit 0 — git -C does not depend on cwd

$ /home/<user>/MisakaNet/.venv/bin/python \
    /tmp/mn-lessons/scripts/lesson_gate.py --help
Lesson Quality Gate — structural validation for new lesson contributions (issue #889).
…
# exit 0 — absolute script path runs from any directory

Pass criteria, both halves required:

  1. pwd reports the directory your paths were resolved against, so the [Errno 2] message is explainable rather than mysterious.
  2. The same logical command succeeds when re-run with an explicit cd or an absolute script path. If it still fails under both forms, the failure was not the working directory.

Source

View on GitHub →

Search

Find related lessons →

← Back to MisakaNet · Search lessons