Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions plugins/go-format/.claude-plugin/plugin.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"$schema": "https://json.schemastore.org/claude-code-plugin-manifest.json",
"name": "go-format",
"version": "0.3.7",
"description": "Auto-fix Go formatting and import management on edit via goimports \u2014 runs unconditionally (no consumer-config gate), skipping generated files.",
"version": "0.3.8",
"description": "Auto-fix Go formatting and import management on edit via goimports runs unconditionally (no consumer-config gate), skipping generated files.",
"author": {
"name": "Melodic Software",
"email": "info@melodicsoftware.com"
Expand Down
7 changes: 7 additions & 0 deletions plugins/go-format/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
All notable changes to the `go-format` plugin are documented here. Format follows
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/); this plugin uses semantic versioning.

## [0.3.8]

### Added

- **Content-mutation disclosure on goimports rewrites (#1596).** When goimports changes a file,
the hook emits a `systemMessage` naming the path.

## [0.3.7]

### Changed
Expand Down
10 changes: 10 additions & 0 deletions plugins/go-format/hooks/go-format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ fi
GOIMPORTS_ARGS=(-w -l)
[[ -n "$LOCAL_PREFIX" ]] && GOIMPORTS_ARGS+=(-local "$LOCAL_PREFIX")

_go_before=""
if _go_before=$(mktemp 2>/dev/null); then
cp "$FILE" "$_go_before" 2>/dev/null || _go_before=""
fi
Comment thread
kyle-sexton marked this conversation as resolved.
# -w writes the fix in place; -l (combined with -w) lists the changed
# filename on stdout, which this hook doesn't need (a successful autofix
# carries no advisory noise, same posture as a successful ruff/typos fix
Expand All @@ -240,6 +244,12 @@ STDERR=$("$GOIMPORTS_BIN" "${GOIMPORTS_ARGS[@]}" -- "$FILE" 2>&1 >/dev/null)
RC=$?

if [[ $RC -eq 0 ]]; then
if [[ -n "$_go_before" ]]; then
if ! cmp -s "$_go_before" "$FILE" 2>/dev/null; then
hook::emit_system_message "go-format: reformatted $(basename "$FILE") via goimports (imports and layout only)."
fi
rm -f "$_go_before"
Comment on lines 227 to +251

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggestion — error-handling: $_go_before snapshot leaks on the non-success paths.

The snapshot is only removed inside the RC -eq 0 branch (rm -f "$_go_before" at line 251). On the two other exits from this hook — the syntax-error branch (RC -eq 2) and the generic tool-break branch below it — $_go_before is left behind in the temp directory. Since every *.go edit that goimports can't parse cleanly will repeat this on every save, the leak is not a one-off.

For comparison, the sibling ruff-format hook doesn't have this gap because its cleanup runs unconditionally after the fix/format passes (both of which are || true, so there's no failure branch to skip past):

if [[ -n "$_ruff_before" ]]; then
  if ! cmp -s "$_ruff_before" "$FILE" 2>/dev/null; then
    hook::emit_system_message "..."
  fi
  rm -f "$_ruff_before"
fi

Here in go-format.sh, goimports has a real failure mode with its own exit path, so the rm -f needs to run on all of them — e.g. trap 'rm -f "$_go_before"' EXIT right after the snapshot is created (covers RC -eq 2, the generic-failure branch, and any future early exit), rather than duplicating the rm -f in each branch.

(Independently flagged by Codex and the security-review lane on this PR as a hygiene issue; reporting here as well since unbounded temp-file accumulation on an error path is squarely an error-handling/correctness concern for this lane.)

fi
# Clean, or fixed silently (formatting/import changes carry no advisory
# noise — same posture as a successful ruff/typos autofix pass).
emit_tel "ok" '[]'
Expand Down