Skip to content

Commit

Permalink
fix(gofmt): fix the issue where the gofmt stderr is not handled
Browse files Browse the repository at this point in the history
  • Loading branch information
wwcchh0123 committed Jun 14, 2024
1 parent 6e2f76e commit 7a89a79
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions internal/linters/go/gofmt/gofmt.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gofmt

import (
"bytes"
"errors"
"os/exec"
"regexp"
"strconv"
Expand Down Expand Up @@ -52,7 +54,7 @@ func gofmtHandler(log *xlog.Logger, a linters.Agent) error {
type Gofmt struct {
dir string
gofmt string
execute func(dir, command string, args ...string) ([]byte, error)
execute func(dir, command string, args ...string) ([]byte, []byte, error)
}

func NewgofmtExecutor(dir string) (linters.Linter, error) {
Expand All @@ -64,24 +66,38 @@ func NewgofmtExecutor(dir string) (linters.Linter, error) {
return &Gofmt{
dir: dir,
gofmt: g,
execute: func(dir, command string, args ...string) ([]byte, error) {
execute: func(dir, command string, args ...string) ([]byte, []byte, error) {

Check warning on line 69 in internal/linters/go/gofmt/gofmt.go

View check run for this annotation

Codecov / codecov/patch

internal/linters/go/gofmt/gofmt.go#L69

Added line #L69 was not covered by tests
c := exec.Command(command, args...)
c.Dir = dir
log.Printf("final command: %v \n", c)
return c.Output()
if c.Stdout != nil {
return nil, nil, errors.New("exec: Stdout already set")

Check warning on line 74 in internal/linters/go/gofmt/gofmt.go

View check run for this annotation

Codecov / codecov/patch

internal/linters/go/gofmt/gofmt.go#L73-L74

Added lines #L73 - L74 were not covered by tests
}
if c.Stderr != nil {
return nil, nil, errors.New("exec: Stderr already set")

Check warning on line 77 in internal/linters/go/gofmt/gofmt.go

View check run for this annotation

Codecov / codecov/patch

internal/linters/go/gofmt/gofmt.go#L76-L77

Added lines #L76 - L77 were not covered by tests
}
var stdoutBuffer bytes.Buffer
var stderrBuffer bytes.Buffer
c.Stdout = &stdoutBuffer
c.Stderr = &stderrBuffer
err := c.Run()
return stdoutBuffer.Bytes(), stderrBuffer.Bytes(), err

Check warning on line 84 in internal/linters/go/gofmt/gofmt.go

View check run for this annotation

Codecov / codecov/patch

internal/linters/go/gofmt/gofmt.go#L79-L84

Added lines #L79 - L84 were not covered by tests
},
}, nil
}

func (g *Gofmt) Run(log *xlog.Logger, args ...string) ([]byte, error) {
b, err := g.execute(g.dir, g.gofmt, args...)
stdoutput, stderr, err := g.execute(g.dir, g.gofmt, args...)

Check warning on line 90 in internal/linters/go/gofmt/gofmt.go

View check run for this annotation

Codecov / codecov/patch

internal/linters/go/gofmt/gofmt.go#L90

Added line #L90 was not covered by tests
if err != nil {
log.Errorf("gofmt run with status: %v, mark and continue", err)
return b, err
if stderr != nil {
log.Errorf("gofmt run cause stderr: %s, mark and continue", stderr)

Check warning on line 94 in internal/linters/go/gofmt/gofmt.go

View check run for this annotation

Codecov / codecov/patch

internal/linters/go/gofmt/gofmt.go#L93-L94

Added lines #L93 - L94 were not covered by tests
}
return stdoutput, err

Check warning on line 96 in internal/linters/go/gofmt/gofmt.go

View check run for this annotation

Codecov / codecov/patch

internal/linters/go/gofmt/gofmt.go#L96

Added line #L96 was not covered by tests
} else {
log.Infof("gofmt running succeeded")
}
return b, nil
return stdoutput, nil

Check warning on line 100 in internal/linters/go/gofmt/gofmt.go

View check run for this annotation

Codecov / codecov/patch

internal/linters/go/gofmt/gofmt.go#L100

Added line #L100 was not covered by tests
}

func (g *Gofmt) Parse(log *xlog.Logger, output []byte) (map[string][]linters.LinterOutput, error) {
Expand Down

0 comments on commit 7a89a79

Please sign in to comment.