Skip to content

Add context-aware shell helper package (fork of mage/sh)#1

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/copy-magefile-mage-sh-with-context
Draft

Add context-aware shell helper package (fork of mage/sh)#1
Copilot wants to merge 2 commits intomainfrom
copilot/copy-magefile-mage-sh-with-context

Conversation

Copy link
Copy Markdown

Copilot AI commented May 7, 2026

Summary

This PR creates a context-aware fork of github.com/magefile/mage/sh as a standalone Go package at github.com/magefile/shx.

What changed

Every exported function that runs a command now accepts a context.Context as its first parameter. The context is passed through to exec.CommandContext, giving callers proper control over timeouts, cancellation, and deadlines.

Key design decisions

  • context.Context on all command functionsRun, RunV, RunWith, RunWithV, Output, OutputWith, Exec all take ctx as their first parameter. The closures returned by RunCmd and OutCmd also accept a context.
  • No mage/mg dependency — The mg.Verbose() check is inlined (reads MAGEFILE_VERBOSE env var directly) and mg.Fatalf() is replaced with a local fatalError type that implements the same ExitStatus() int interface. This makes shx a zero-dependency package.
  • File helpers unchangedRm and Copy don't execute commands, so they don't need a context parameter.
  • New testTestContextCancellation verifies that a cancelled context properly terminates a running command.

Files

File Description
cmd.go Core command-running functions with context.Context support
helpers.go File helpers (Rm, Copy) — unchanged from original
cmd_test.go Tests for command functions, adapted for context
helpers_test.go Tests for file helpers
testmain_test.go Test binary helper infrastructure
go.mod Go module definition
README.md Package documentation with usage examples

Testing

All 17 tests pass:

ok  	github.com/magefile/shx	0.011s

Copilot AI and others added 2 commits May 7, 2026 21:23
Copy all exported functions from github.com/magefile/mage/sh and add
context.Context as the first parameter to every command-running function.
The context is passed through to exec.CommandContext for cancellation
and timeout support.

Remove the dependency on github.com/magefile/mage/mg by inlining the
verbose check and fatal error type.

Agent-Logs-Url: https://github.com/magefile/shx/sessions/f0fda061-9670-4a20-a708-2ab74aa13234

Co-authored-by: natefinch <3185864+natefinch@users.noreply.github.com>
Agent-Logs-Url: https://github.com/magefile/shx/sessions/f0fda061-9670-4a20-a708-2ab74aa13234

Co-authored-by: natefinch <3185864+natefinch@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants