Skip to content

openRepoShape command: workdir() sets its EXIT trap inside a $(...) subshell, so the temp dir is gone before setup.sh lands in it — every fetching run refuses (--doctor, the scaffold, curl | bash --install) #74

Closed

Description

@brettheap

Symptom

Every run of the installed openRepoShape command that has to FETCH setup.sh refuses, on a machine where both gh api and curl fetch the file fine by hand:

/home/brett/.local/bin/openRepoShape: line 76: /tmp/tmp.OiKY19hk4w/setup.sh: No such file or directory
/home/brett/.local/bin/openRepoShape: line 82: /tmp/tmp.OiKY19hk4w/setup.sh: No such file or directory

REFUSED: could not fetch setup.sh from opensoft/openRepoShape at main. Both ways failed:
    gh api repos/opensoft/openRepoShape/contents/setup.sh?ref=main
    curl -fsSL https://raw.githubusercontent.com/opensoft/openRepoShape/main/setup.sh

The two No such file or directory lines are the redirect targets >"$dest" inside fetch_from_repo: the directory the shim made a moment earlier is already gone. The refusal text then blames the network, which is wrong.

Cause

workdir() creates the temp directory and sets trap 'rm -rf -- "$WORKDIR"' EXIT. Every caller invokes it as $(workdir) — a command-substitution SUBSHELL. The trap is therefore set in the subshell and fires the instant the substitution returns, deleting the directory before the parent shell writes into it. The parent's WORKDIR also stays empty, so the guard [ -z "$WORKDIR" ] never short-circuits and a second $(workdir) makes (and immediately deletes) a second directory.

Six-line reproduction under bash 5.2.21:

set -euo pipefail
WORKDIR=""
workdir() { if [ -z "$WORKDIR" ]; then WORKDIR="$(mktemp -d)"; trap 'rm -rf -- "$WORKDIR"' EXIT; fi; printf '%s' "$WORKDIR"; }
D="$(workdir)/setup.sh"
[ -d "$(dirname "$D")" ] && echo "dir exists" || echo "dir GONE"    # -> dir GONE
echo "parent WORKDIR='$WORKDIR'"                                       # -> ''

git blame puts workdir() at a48d1f3 (#37), the commit that introduced the command, so the command has never worked from a fetch.

Scope

Every path that reaches $(workdir):

  • openRepoShape <Project> --org <org> — the command's whole purpose.
  • openRepoShape --doctor.
  • curl … | bash -s -- --install and its gh api twin — the README's documented no-checkout install, because install_self fetches into $(workdir) when there is no file to copy from.

Unaffected: ./openRepoShape --install run from a file (copies $SELF, no workdir), and any run with $OPENREPOSHAPE_SETUP_SH set (the offline test path, which skips the fetch).

Why the suite did not catch it

tests/test_openreposhape_command.py sets $OPENREPOSHAPE_SETUP_SH on every run, by design, so workdir() is never called. The module docstring says the fetch cannot be exercised offline. It can, partly: fetch_from_repo is gh api then curl, and neither can be pointed at a directory — but $OPENREPOSHAPE_REPO already accepts a filesystem path (shape_clone_url passes /* through), so the fix can be exercised by a test that unsets $OPENREPOSHAPE_SETUP_SH and makes the fetch land in a workdir that still exists when the fetched bytes are used — or, at minimum, by a test that runs workdir twice in the shim's own text and asserts one directory, still present, after the substitution. Whichever is chosen, the test must FAIL on today's openRepoShape and pass on the fixed one.

Fix shape

Make the temp directory and set the trap ONCE in the main shell, and have callers read $WORKDIR instead of capturing the function's stdout. Nothing else in the shim needs to move.

Workaround until then

OPENREPOSHAPE_SETUP_SH=/path/to/openRepoShape/setup.sh openRepoShape --doctor

Activity

  1. brettheap commented on Sep 9, 2026

    @brettheap
    ContributorAuthor

    CLAIMED — lane xfactory-2 (xFactory-2), session 47c80b53-25d0-407b-8e19-432e9316bf61, 2026-09-09T00:18Z, for the fix to openRepoShape's workdir() (this issue) — branch fix/shim-workdir-subshell-trap.

    Reads before claiming:

    1. Comments on this issue: none (this lane opened it moments ago).
    2. gh pr list --repo opensoft/openRepoShape --state all --search "workdir": no results. --search "EXIT trap": only setup.sh is a shim over setup-project.py: the flow exists once (#50) #60 (merged; the setup.sh shim, unrelated).
    3. git ls-remote --heads origin | grep -i -E 'workdir|trap|doctor|fetch|shim': only feat/setup-self-bootstrap, the branch openRepoShape command + the worked example (Northwind starts Atlas) + setup.sh clones where you stand #37 merged from — the one this bug arrived on. No live branch on this object.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions