pi-goimports
v0.2.1
Published
pi extension — run goimports on write/edit of *.go files and inject the diff into the tool result
Readme
pi-goimports
A pi extension that runs
goimports automatically after every write/edit to a *.go file, writes
the formatted result back, and appends a standard unified diff to the tool
result so the next turn sees the actual import/format changes. It also
appends a standing rule to the system prompt once per agent run telling the
model to write the code body first and let goimports manage resolvable
imports.
What it does
before_agent_start— appends a constant rule to the system prompt once per agent run (cache-safe): write the body first, let goimports add the imports it can resolve, only add non-resolvable imports manually after goimports has run. Also tells the model how to recover from a stale module cache index (rungomodindex), and notes that edits via thebashtool are not processed automatically.tool_resultforwrite/editon*.go— runsgoimports(no-w/-d), which prints the formatted result to stdout without touching the file; writes it back itself and appends a standard unified diff to the tool result for the model. No-op when goimports changes nothing. When it does make changes, a compact marker (◆ goimports <path> +N -M) is appended to the chat transcript right after the tool call, so the cue persists in scrollback. Expand (ctrl+o) to see the full diff.
Binary lookup (cached per session)
goimports is resolved once at session start — on PATH, or via go env
GOBIN/GOPATH if installed off-PATH. If it can't be found (or go is
missing), a warning is shown at startup with install steps; install it and
/reload to activate. The result is stable for the session so the
system-prompt rule doesn't flip mid-run.
Install
pi install npm:pi-goimports
# or pin a version
pi install npm:[email protected]
# alternative — from git
pi install git:github.com/huww98/pi-goimports
pi install git:github.com/huww98/[email protected]To try without installing: pi -e npm:pi-goimports.
Configuration
PI_GOIMPORTS_ARGS— extra args appended to thegoimportsinvocation (e.g.-local github.com/myorgfor local import grouping).
Requirements
goimportson PATH or in$GOBIN/GOPATH/bin(install withgo install golang.org/x/tools/cmd/goimports@latest).goon PATH (goimports shells out togofor env context).- Optional:
gomodindex— rebuilds the module-cache index goimports uses to resolve imports. Run it aftergo getif a newly-added import isn't being resolved (goimports never refreshes the index itself, see golang/go#80087). Without it goimports still works, by scanningGOMODCACHEdirectly (slower).go install golang.org/x/tools/internal/modindex/gomodindex@latest.
Development
npm install
npm test # (integration tests; requires goimports + go)
npm run typecheckLicense
MIT
