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
2 changes: 1 addition & 1 deletion cmd/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func runAudit(cmd *cobra.Command, dir string) error {
return err
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
ctx, cancel := context.WithTimeout(cmd.Context(), 10*time.Minute)
defer cancel()

// Fingerprint for caching — best-effort; empty string means no caching.
Expand Down
4 changes: 2 additions & 2 deletions cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func runRestore(cmd *cobra.Command, dir string, localMode bool, maxTokens int) e

if graph == nil {
opts.LocalMode = true
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
ctx, cancel := context.WithTimeout(cmd.Context(), 30*time.Second)
defer cancel()
var err error
graph, err = restore.BuildProjectGraph(ctx, rootDir, projectName)
Expand Down Expand Up @@ -135,7 +135,7 @@ func restoreViaAPI(cmd *cobra.Command, cfg *config.Config, rootDir, projectName

client := api.New(cfg)

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
ctx, cancel := context.WithTimeout(cmd.Context(), 10*time.Minute)
defer cancel()

fmt.Fprintln(cmd.ErrOrStderr(), "Analyzing repository…")
Expand Down
4 changes: 2 additions & 2 deletions cmd/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func runShare(cmd *cobra.Command, dir string) error {
return err
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
ctx, cancel := context.WithTimeout(cmd.Context(), 10*time.Minute)
defer cancel()

fp, _ := cache.RepoFingerprint(rootDir)
Expand All @@ -76,7 +76,7 @@ func runShare(cmd *cobra.Command, dir string) error {

// Upload and get public URL.
client := api.New(cfg)
uploadCtx, uploadCancel := context.WithTimeout(context.Background(), 2*time.Minute)
uploadCtx, uploadCancel := context.WithTimeout(ctx, 2*time.Minute)
defer uploadCancel()

fmt.Fprintln(cmd.ErrOrStderr(), "Uploading report…")
Expand Down
12 changes: 10 additions & 2 deletions internal/compact/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ func CompactSource(src []byte, lang Language) ([]byte, error) {
// compactGo strips non-directive comments via the Go AST, shortens local
// identifiers, and removes blank lines.
func compactGo(src []byte) ([]byte, error) {
// //go:embed directives must immediately precede their variable declaration
// and cannot be relocated. Return the source unchanged so that embed
// semantics and the "embed" import are preserved.
if bytes.Contains(src, []byte("//go:embed")) {
return src, nil
}

fset := token.NewFileSet()
// Parsing without parser.ParseComments drops all comments from the AST.
f, err := parser.ParseFile(fset, "", src, 0)
Expand Down Expand Up @@ -137,14 +144,15 @@ func compactGo(src []byte) ([]byte, error) {
}

// scanGoDirectives extracts //go:build, // +build and //go:generate lines.
// //go:embed is intentionally excluded: it must remain adjacent to its variable
// declaration and cannot be moved to the file top (handled by early return in compactGo).
func scanGoDirectives(src []byte) []byte {
var out []byte
for _, line := range bytes.Split(src, []byte("\n")) {
text := strings.TrimSpace(string(line))
if strings.HasPrefix(text, "//go:build") ||
strings.HasPrefix(text, "// +build") ||
strings.HasPrefix(text, "//go:generate") ||
strings.HasPrefix(text, "//go:embed") {
strings.HasPrefix(text, "//go:generate") {
out = append(out, bytes.TrimRight(line, " \t")...)
out = append(out, '\n')
}
Expand Down
20 changes: 20 additions & 0 deletions internal/compact/handler_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package compact

import (
"bytes"
"go/parser"
"go/token"
"os"
Expand Down Expand Up @@ -113,6 +114,25 @@ func Foo() {}
}
}

func TestCompactGoPreservesEmbedFiles(t *testing.T) {
// //go:embed must stay adjacent to its var declaration and cannot be moved
// to the file top. Files containing it should be returned unchanged.
src := []byte(`package foo

import _ "embed"

//go:embed hello.txt
var hello string
`)
got, err := CompactSource(src, Go)
if err != nil {
t.Fatalf("CompactSource error: %v", err)
}
if !bytes.Equal(got, src) {
t.Errorf("expected source unchanged for //go:embed file, got:\n%s", got)
}
}

func TestCompactGoReducesSize(t *testing.T) {
src := []byte(`// Package math provides basic math utilities.
// It is intentionally simple.
Expand Down
Loading