Skip to content

fix(compact): preserve Go files with //go:embed directives unchanged#88

Merged
greynewell merged 2 commits intomainfrom
fix-compact-go-embed
Apr 9, 2026
Merged

fix(compact): preserve Go files with //go:embed directives unchanged#88
greynewell merged 2 commits intomainfrom
fix-compact-go-embed

Conversation

@greynewell
Copy link
Copy Markdown
Contributor

@greynewell greynewell commented Apr 8, 2026

Summary

  • compactGo was including //go:embed in the list of directives recovered after stripping comments
  • Unlike //go:build/// +build (which belong at the file top), //go:embed must immediately precede its variable declaration
  • Moving it to the top of the file detaches it from the variable, breaking compilation (the var gets its zero value and the _ "embed" import becomes unused)
  • Fix: return source unchanged when //go:embed is present; remove it from scanGoDirectives
  • Added regression test TestCompactGoPreservesEmbedFiles

Test plan

  • go test ./internal/compact/... passes including new test
  • go build ./... passes
  • Manual: supermodel compact on a Go file with //go:embed returns it unchanged

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved command lifecycle management for audit, restore, and share operations by properly linking timeouts and cancellation to command context.
  • New Features

    • Added detection and preservation of source files containing embedded resource declarations during compaction to prevent breaking affected code.

greynewell and others added 2 commits April 8, 2026 17:39
context.Background() ignores Cobra's command context, meaning Ctrl+C
had no effect on in-flight API calls in audit, share, and restore.
Derive all timeouts from cmd.Context() so cancellation propagates.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
//go:embed must immediately precede its variable declaration; moving it
to the top of the file (as scanGoDirectives was doing) detaches it from
the variable, causing compilation failures at embed time.

Fix: return the source unchanged when //go:embed is present, and remove
//go:embed from scanGoDirectives (only //go:build, // +build, and
//go:generate belong at the file top). Add a test to prevent regression.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 8, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 260c1575-6730-4e8d-9a52-96357f2c1b90

📥 Commits

Reviewing files that changed from the base of the PR and between d9478a9 and d8a7faf.

📒 Files selected for processing (5)
  • cmd/audit.go
  • cmd/restore.go
  • cmd/share.go
  • internal/compact/handler.go
  • internal/compact/handler_test.go

Walkthrough

This PR makes two main changes: (1) Updates context handling in three CLI commands (audit, restore, share) to use the Cobra command's lifecycle context instead of standalone background contexts, affecting when operations are canceled, and (2) Modifies Go source compaction to detect and preserve //go:embed directives and their source code unchanged.

Changes

Cohort / File(s) Summary
CLI Context Lifecycle Updates
cmd/audit.go, cmd/restore.go, cmd/share.go
Changed context creation from context.Background() to cmd.Context(), tying command execution timeouts and cancellations to the parent command's lifecycle rather than independent background contexts. Affects audit analysis, restore operations, and upload flows.
Go Embed Preservation
internal/compact/handler.go
Updated compactGo to detect //go:embed presence and return source unchanged; updated scanGoDirectives to exclude embed directives from compiler directive extraction, preserving only build tags and generate directives.
Embed Preservation Test
internal/compact/handler_test.go
Added TestCompactGoPreservesEmbedFiles to verify CompactSource returns input unchanged when //go:embed directives are present.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • jonathanpopham

Poem

🔄 Commands now dance with their parent's breath,
Contexts flow like rivers, not static threads—
While embedded Go files rest undisturbed,
Their directives preserved, their bytes unblurred. ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main fix: preserving Go files with //go:embed directives unchanged during compaction, which is the primary objective of this PR.
Description check ✅ Passed The description covers all required sections: What (the fix), Why (the problem with moving //go:embed), and Test plan with specific commands.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-compact-go-embed

Comment @coderabbitai help to get the list of available commands and usage tips.

@greynewell greynewell merged commit b8d16ff into main Apr 9, 2026
7 checks passed
@greynewell greynewell deleted the fix-compact-go-embed branch April 9, 2026 17:03
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.

1 participant