Skip to content

Commit cb8a394

Browse files
CopilotpelikhanCopilot
authored
Fix Go code quality issues in cmd/generate package: resource leaks, variable shadowing, and error handling (#82)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com> Co-authored-by: Peli de Halleux <pelikhan@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent d21cd6c commit cb8a394

3 files changed

Lines changed: 21 additions & 12 deletions

File tree

cmd/generate/context.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (h *generateCommandHandler) CreateContextFromPrompt() (*PromptPexContext, e
2626
}
2727

2828
runID := fmt.Sprintf("run_%d", time.Now().Unix())
29-
context := &PromptPexContext{
29+
promptContext := &PromptPexContext{
3030
// Unique identifier for the run
3131
RunID: runID,
3232
// The prompt content and metadata
@@ -50,21 +50,21 @@ func (h *generateCommandHandler) CreateContextFromPrompt() (*PromptPexContext, e
5050
} else {
5151
sessionInfo = fmt.Sprintf("reloading session file at %s", *h.sessionFile)
5252
// Check if prompt hashes match
53-
if existingContext.PromptHash != context.PromptHash {
53+
if existingContext.PromptHash != promptContext.PromptHash {
5454
return nil, fmt.Errorf("prompt changed unable to reuse session file")
5555
}
5656

5757
// Merge existing context data
5858
if existingContext != nil {
59-
context = mergeContexts(existingContext, context)
59+
promptContext = mergeContexts(existingContext, promptContext)
6060
}
6161
}
6262
}
6363

64-
h.WriteToParagraph(RenderMessagesToString(context.Prompt.Messages))
64+
h.WriteToParagraph(RenderMessagesToString(promptContext.Prompt.Messages))
6565
h.WriteEndBox(sessionInfo)
6666

67-
return context, nil
67+
return promptContext, nil
6868
}
6969

7070
// loadContextFromFile loads a PromptPexContext from a JSON file

cmd/generate/generate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ func NewGenerateCommand(cfg *command.Config) *cobra.Command {
7676
sessionFile: util.Ptr(sessionFile),
7777
}
7878

79-
// Create context
80-
context, err := handler.CreateContextFromPrompt()
79+
// Create prompt context
80+
promptContext, err := handler.CreateContextFromPrompt()
8181
if err != nil {
8282
return fmt.Errorf("failed to create context: %w", err)
8383
}
8484

8585
// Run the PromptPex pipeline
86-
if err := handler.RunTestGenerationPipeline(context); err != nil {
86+
if err := handler.RunTestGenerationPipeline(promptContext); err != nil {
8787
// Disable usage help for pipeline failures
8888
cmd.SilenceUsage = true
8989
return fmt.Errorf("pipeline failed: %w", err)

cmd/generate/llm.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@ func (h *generateCommandHandler) callModelWithRetry(step string, req azuremodels
2828
for attempt := 0; attempt <= maxRetries; attempt++ {
2929
sp := spinner.New(spinner.CharSets[14], 100*time.Millisecond, spinner.WithWriter(h.cfg.ErrOut))
3030
sp.Start()
31-
//nolint:gocritic,revive // TODO
32-
defer sp.Stop()
3331

3432
resp, err := h.client.GetChatCompletionStream(ctx, req, h.org)
3533
if err != nil {
34+
sp.Stop()
3635
var rateLimitErr *azuremodels.RateLimitError
3736
if errors.As(err, &rateLimitErr) {
3837
if attempt < maxRetries {
@@ -53,8 +52,6 @@ func (h *generateCommandHandler) callModelWithRetry(step string, req azuremodels
5352
return "", err
5453
}
5554
reader := resp.Reader
56-
//nolint:gocritic,revive // TODO
57-
defer reader.Close()
5855

5956
var content strings.Builder
6057
for {
@@ -63,6 +60,11 @@ func (h *generateCommandHandler) callModelWithRetry(step string, req azuremodels
6360
if errors.Is(err, context.Canceled) || strings.Contains(err.Error(), "EOF") {
6461
break
6562
}
63+
if closeErr := reader.Close(); closeErr != nil {
64+
// Log close error but don't override the original error
65+
fmt.Fprintf(h.cfg.ErrOut, "Warning: failed to close reader: %v\n", closeErr)
66+
}
67+
sp.Stop()
6668
return "", err
6769
}
6870
for _, choice := range completion.Choices {
@@ -75,6 +77,13 @@ func (h *generateCommandHandler) callModelWithRetry(step string, req azuremodels
7577
}
7678
}
7779

80+
// Properly close reader and stop spinner before returning success
81+
err = reader.Close()
82+
sp.Stop()
83+
if err != nil {
84+
return "", fmt.Errorf("failed to close reader: %w", err)
85+
}
86+
7887
res := strings.TrimSpace(content.String())
7988
h.LogLLMResponse(res)
8089
return res, nil

0 commit comments

Comments
 (0)