Skip to content

Commit 41246e9

Browse files
authored
fix(internal/gapicgen): exec Stdout already set (#4509)
This error was occuring because stdout was being set by some calling locations which violates Command.Output usage. This refactors code to never set stdout. I also added some more debug info of logging the command output which can be useful in the case of failures.
1 parent d8ec92b commit 41246e9

File tree

3 files changed

+22
-31
lines changed

3 files changed

+22
-31
lines changed

internal/gapicgen/execv/command.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,17 @@ type CmdWrapper struct {
3333
// The commands stdout/stderr default to os.Stdout/os.Stderr respectfully.
3434
func Command(name string, arg ...string) *CmdWrapper {
3535
c := &CmdWrapper{exec.Command(name, arg...)}
36-
c.Stdout = os.Stdout
3736
c.Stderr = os.Stderr
3837
c.Stdin = os.Stdin
3938
return &CmdWrapper{exec.Command(name, arg...)}
4039
}
4140

4241
// Run a command.
4342
func (c *CmdWrapper) Run() error {
44-
_, err := c.Output()
43+
b, err := c.Output()
44+
if len(b) > 0 {
45+
log.Printf("Command Output: %s", b)
46+
}
4547
return err
4648
}
4749

internal/gapicgen/git/git.go

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package git
1717
import (
1818
"bytes"
1919
"fmt"
20-
"io"
2120
"log"
2221
"os"
2322
"os/exec"
@@ -87,17 +86,16 @@ func ParseChangeInfo(googleapisDir string, hashes []string, gapicPkgs map[string
8786
var changes []*ChangeInfo
8887
for _, hash := range hashes {
8988
// Get commit title and body
90-
rawBody := bytes.NewBuffer(nil)
9189
c := execv.Command("git", "show", "--pretty=format:%s~~%b", "-s", hash)
92-
c.Stdout = rawBody
9390
c.Dir = googleapisDir
94-
if err := c.Run(); err != nil {
91+
b, err := c.Output()
92+
if err != nil {
9593
return nil, err
9694
}
9795

98-
ss := strings.Split(rawBody.String(), "~~")
96+
ss := strings.Split(string(b), "~~")
9997
if len(ss) != 2 {
100-
return nil, fmt.Errorf("expected two segments for commit, got %d: %q", len(ss), rawBody.String())
98+
return nil, fmt.Errorf("expected two segments for commit, got %d: %s", len(ss), b)
10199
}
102100
title, body := strings.TrimSpace(ss[0]), strings.TrimSpace(ss[1])
103101

@@ -154,46 +152,38 @@ func CommitsSinceHash(gitDir, hash string, inclusive bool) ([]string, error) {
154152
commitRange = fmt.Sprintf("%s..", hash)
155153
}
156154

157-
out := bytes.NewBuffer(nil)
158155
c := execv.Command("git", "rev-list", commitRange)
159-
c.Stdout = out
160156
c.Dir = gitDir
161-
if err := c.Run(); err != nil {
157+
b, err := c.Output()
158+
if err != nil {
162159
return nil, err
163160
}
164-
return strings.Split(strings.TrimSpace(out.String()), "\n"), nil
161+
return strings.Split(strings.TrimSpace(string(b)), "\n"), nil
165162
}
166163

167164
// UpdateFilesSinceHash returns a listed of files updated since the provided
168165
// hash for the given gitDir.
169166
func UpdateFilesSinceHash(gitDir, hash string) ([]string, error) {
170-
out := bytes.NewBuffer(nil)
171167
// The provided diff-filter flags restricts to files that have been:
172168
// - (A) Added
173169
// - (C) Copied
174170
// - (M) Modified
175171
// - (R) Renamed
176172
c := execv.Command("git", "diff-tree", "--no-commit-id", "--name-only", "--diff-filter=ACMR", "-r", fmt.Sprintf("%s..HEAD", hash))
177-
c.Stdout = out
178173
c.Dir = gitDir
179-
if err := c.Run(); err != nil {
174+
b, err := c.Output()
175+
if err != nil {
180176
return nil, err
181177
}
182-
return strings.Split(out.String(), "\n"), nil
178+
return strings.Split(string(b), "\n"), nil
183179
}
184180

185181
// HasChanges reports whether the given directory has uncommitted git changes.
186182
func HasChanges(dir string) (bool, error) {
187-
// Write command output to both os.Stderr and local, so that we can check
188-
// whether there are modified files.
189-
inmem := &bytes.Buffer{}
190-
w := io.MultiWriter(os.Stderr, inmem)
191-
192183
c := execv.Command("bash", "-c", "git status --short")
193184
c.Dir = dir
194-
c.Stdout = w
195-
err := c.Run()
196-
return inmem.Len() > 0, err
185+
b, err := c.Output()
186+
return len(b) > 0, err
197187
}
198188

199189
// DeepClone clones a repository in the given directory.
@@ -249,12 +239,11 @@ func FileDiff(dir, filename string) (string, error) {
249239
// filesChanged returns a list of files changed in a commit for the provdied
250240
// hash in the given gitDir.
251241
func filesChanged(gitDir, hash string) ([]string, error) {
252-
out := bytes.NewBuffer(nil)
253242
c := execv.Command("git", "show", "--pretty=format:", "--name-only", hash)
254-
c.Stdout = out
255243
c.Dir = gitDir
256-
if err := c.Run(); err != nil {
244+
b, err := c.Output()
245+
if err != nil {
257246
return nil, err
258247
}
259-
return strings.Split(out.String(), "\n"), nil
248+
return strings.Split(string(b), "\n"), nil
260249
}

internal/gapicgen/tools.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ package gapicgen
2020
import (
2121
"fmt"
2222
"os"
23-
"os/exec"
23+
24+
"cloud.google.com/go/internal/gapicgen/execv"
2425
)
2526

2627
// VerifyAllToolsExist ensures that all required tools exist on the system.
2728
func VerifyAllToolsExist(toolsNeeded []string) error {
2829
for _, t := range toolsNeeded {
29-
c := exec.Command("which", t)
30-
c.Stdout = os.Stdout
30+
c := execv.Command("which", t)
3131
c.Stderr = os.Stderr
3232
if c.Run() != nil {
3333
return fmt.Errorf("%s does not appear to be installed. please install it. all tools needed: %v", t, toolsNeeded)

0 commit comments

Comments
 (0)