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 authored and CarlJi committed Jun 17, 2024
1 parent 25e42d0 commit 0bc3810
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) {
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")
}
if c.Stderr != nil {
return nil, nil, errors.New("exec: Stderr already set")
}
var stdoutBuffer bytes.Buffer
var stderrBuffer bytes.Buffer
c.Stdout = &stdoutBuffer
c.Stderr = &stderrBuffer
err := c.Run()
return stdoutBuffer.Bytes(), stderrBuffer.Bytes(), err
},
}, 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...)
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)
}
return stdoutput, err
} else {
log.Infof("gofmt running succeeded")
}
return b, nil
return stdoutput, nil
}

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

0 comments on commit 0bc3810

Please sign in to comment.