Skip to content

openRepoShape: workdir() set its EXIT trap inside a $(...) subshell, so the temp dir was gone before setup.sh landed in it — every fetching run refused (#74) - #75

Merged
brettheap merged 1 commit into
mainfrom
fix/shim-workdir-subshell-trap
Sep 9, 2026
Merged

brettheap merged 1 commit into
mainfrom
fix/shim-workdir-subshell-trap

Conversation

@brettheap

@brettheap brettheap commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Lane: xfactory-2 (xFactory-2)
Claim: #74 (comment) (2026-09-09T00:18Z, on Brett Heap's "fix it")

Closes #74.

workdir() made the temporary directory and set trap 'rm -rf -- "$WORKDIR"' EXIT, and every caller read it as $(workdir) — a command substitution, which is a SUBSHELL. The trap was therefore the subshell's, fired the instant the substitution closed, and removed the directory before fetch_from_repo wrote a byte into it; the parent's $WORKDIR stayed empty as well. Every run that has to fetch setup.sh — the scaffold itself, --doctor, and the README's curl … | bash -s -- --install (which fetches its own bytes when run from stdin) — died with line 76: /tmp/tmp.XXXX/setup.sh: No such file or directory and then the misleading REFUSED: could not fetch setup.sh …, on a machine where gh api and curl both fetch the file fine by hand. git blame puts the function at a48d1f3 (#37), so the command has never worked from a fetch. Unaffected: ./openRepoShape --install from a file, and any run with $OPENREPOSHAPE_SETUP_SH set — which is every run the suite made, which is how it got through.

The fix: workdir takes no output. It makes the directory and sets the trap in the main shell, and its two callers read $WORKDIR. Three lines moved, one printf gone, one comment saying why; usage text, fetch order and argument handling are byte-identical.

Two tests pin it, both offline: a fake gh first on $PATH answers the one API call the command makes (contents/setup.sh with a stub that prints where it ran from; contents/openRepoShape with this checkout's own bytes), and a shadow curl refuses so no test can fall through to the network. The first runs --doctor with $OPENREPOSHAPE_SETUP_SH unset and asserts the fetched script RAN and that its directory is gone AFTERWARDS — the trap fires at the end, in the right shell. The second is the documented one-line install, bash -s -- --install from stdin into a temporary $OPENREPOSHAPE_BIN_DIR. Both fail on main's openRepoShape with exactly the reported No such file or directory, and pass here. run_cmd's environment-building is factored into command_env(…, setup_sh=) so the fetch path can be reached without disturbing the twenty existing calls; the module docstring no longer claims the fetch cannot be exercised.

Verified by hand as well: bash ./openRepoShape --doctor now self-bootstraps from upstream main and prints the preflight table ending this machine is ready. Full suite: 622 passed, 1 skipped, and one pre-existing failure unrelated to this change (test_naming_policy.py::test_cli_reports_an_unread_link_as_a_warning_and_still_classifies, which fails identically on main on this workstation because sibling checkouts of openXdox and openDox sit beside this one and the validator resolves the link from disk).

🤖 Generated with Claude Code

Summary by Sourcery

Keep fetched command payloads available through execution by moving temporary-directory cleanup into the main shell and cover both fetching callers with offline tests.

Bug Fixes:

  • Fix temporary workdir cleanup so fetched setup.sh and self-install payloads remain available until command completion.
  • Enable offline coverage of the setup.sh fetching and stdin-based installation paths, preventing regressions in temporary-directory lifetime handling.

Enhancements:

  • Refactor test environment construction to support exercising both configured and fetched setup.sh execution paths.

Tests:

  • Add offline tests covering fetched setup.sh execution and documented stdin installation behavior.

…so the temp dir was gone before setup.sh landed in it — every fetching run refused (#74)

`workdir()` printed its path and every caller read it as `$(workdir)` — a
command substitution, which is a subshell. The `trap 'rm -rf …' EXIT` was
therefore the SUBSHELL's trap and fired the instant the substitution closed:
the directory was deleted before `fetch_from_repo` could write into it, and
`$WORKDIR` in the main shell was still empty. Every run without
`$OPENREPOSHAPE_SETUP_SH` — which is every real run — died with `line 76:
/tmp/tmp.XXXX/setup.sh: No such file or directory` and then the misleading
`REFUSED: could not fetch setup.sh …`, and `curl … | bash -s -- --install`
failed the same way, fetching itself into the same vanished directory.

A trap only ever fires in the shell that set it, so `workdir()` now makes the
directory and sets the trap in the main shell and prints nothing; the two
callers say `workdir` and then read `$WORKDIR`. Nothing else moves — the
usage text, the fetch order and the argument handling are untouched.

Two tests pin it, both offline: a fake `gh` first on `$PATH` answers the one
API call the command makes (a stub `setup.sh` that prints where it ran from,
and this checkout's own bytes for `openRepoShape`), with `curl` shadowed by a
script that refuses so nothing can reach the network. One runs `--doctor`
with `$OPENREPOSHAPE_SETUP_SH` unset and asserts the fetched script ran, was
handed `--doctor`, and that its directory is gone AFTERWARDS rather than
during; the other runs `bash -s -- --install` from stdin and asserts the
installed copy is executable and byte-identical to this file. Both fail
against the old shim with the refusal above.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Copilot AI lite review requested due to automatic review settings September 9, 2026 00:28
@sourcery-ai

sourcery-ai Bot commented Sep 9, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Fixes temporary-directory lifetime by moving workdir setup and its EXIT trap out of command substitution, restoring setup.sh fetching for --doctor and stdin-based installation. Adds offline tests that exercise both paths, verify execution and installation, and confirm cleanup occurs only after the command completes.

File-Level Changes

Change Details Files
Keep the temporary work directory and its EXIT cleanup trap in the parent shell instead of creating them inside command substitution.
  • Changed workdir() to set the global WORKDIR without printing it.
  • Updated both callers to consume WORKDIR directly, preserving fetch order, usage text, and argument handling.
  • Ensured cleanup occurs after setup.sh is fetched and executed rather than before the fetch begins.
openRepoShape
Add offline regression coverage for both fetching workflows that previously failed.
  • Added controllable environment construction so tests can unset OPENREPOSHAPE_SETUP_SH.
  • Added fake gh responses for setup.sh and openRepoShape, plus a refusing curl to prevent network access.
  • Verified --doctor executes the fetched script from a live temporary directory and that cleanup happens afterward.
  • Verified the documented stdin install fetches, installs, and preserves the expected executable bytes and mode.
tests/test_openreposhape_command.py

Assessment against linked issues

Issue Objective Addressed Explanation
#74 Fix workdir() so the temporary directory and EXIT trap are created in the main shell rather than inside a command-substitution subshell, keeping the directory available while setup.sh is fetched and used. ✅
#74 Update all callers that previously used $(workdir) to use the persistent WORKDIR value, so fetching works for the scaffold command, --doctor, and stdin-based --install flows. ✅
#74 Add offline regression coverage proving that fetched setup.sh and self-install payloads can be written to and executed from the live work directory, while the directory is removed only after command completion. ✅

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@brettheap

Copy link
Copy Markdown
Contributor Author

@codex review

@sonarqubecloud

sonarqubecloud Bot commented Sep 9, 2026

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Needs a human reviewer. This enables the previously broken fetching and installation paths, causing fetched scripts to execute and potentially write an installed command or other bounded local state. Reverting stops future runs but does not automatically remove artifacts or other effects from runs that already succeeded.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

workdir() can now return non-zero when $WORKDIR is already set (due to the if compound command’s exit status), which can unexpectedly terminate the script under set -e.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Fixes a real execution bug in the openRepoShape front-door script where workdir()’s EXIT trap was being set inside a command-substitution subshell, causing the temporary directory to be deleted before fetched payloads could be written and executed; adds offline tests to prevent regressions on the fetch path.

Changes:

  • Make workdir() set up the temp directory + EXIT cleanup trap in the main shell and have callers read $WORKDIR instead of $(workdir).
  • Refactor test environment construction (command_env(...)) to allow exercising the fetch path offline.
  • Add two offline tests that validate fetched setup.sh execution and stdin-based self-install behavior without permitting network fallback.
File summaries
File Description
openRepoShape Adjusts workdir() usage so fetched scripts land in a live temp dir and cleanup happens at process exit.
tests/test_openreposhape_command.py Adds offline fetch-path coverage via a fake gh + refusing curl, and factors env setup to reach that path safely.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread openRepoShape
Comment on lines 67 to 71
workdir() {
if [ -z "$WORKDIR" ]; then
WORKDIR="$(mktemp -d)"
trap 'rm -rf -- "$WORKDIR"' EXIT
fi
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 2600f4c5ae

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@brettheap

Copy link
Copy Markdown
Contributor Author

On Copilot's "workdir() can now return non-zero when $WORKDIR is already set … under set -e": it cannot. An if compound command whose condition tests false and which has no else exits 0 — bash manual, Conditional Constructs: "The return status is the exit status of the last command executed, or zero if no condition tested true"; POSIX says the same of if. Checked directly, under the shim's own set -euo pipefail:

bash -euo pipefail -c 'WORKDIR=""; workdir() { if [ -z "$WORKDIR" ]; then WORKDIR="$(mktemp -d)"; trap "rm -rf -- \"$WORKDIR\"" EXIT; fi; }
  workdir; first=$WORKDIR; workdir
  echo "second call returned $? under set -e; WORKDIR unchanged: $([ "$first" = "$WORKDIR" ] && echo yes || echo no); still alive"'
# second call returned 0 under set -e; WORKDIR unchanged: yes; still alive

And in this script workdir is never called twice in one run anyway: install_self is followed by exit 0, and the main path calls it once. No change made for this finding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants