Skip to content

Commit

Permalink
fix race condition on magefile
Browse files Browse the repository at this point in the history
Fix a race condition when calling `go-licence-detector`. The original
code waited for a goroutine to write to `go-licence-detector`'s stdin
before starting the command, so the mage target was hanging.

This commit fixes it, adds logging (when verbose set) to the command
run as well as improves the error message.
  • Loading branch information
belimawr committed Sep 7, 2022
1 parent 5ca0ae1 commit 2d3a9e5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- Remove username/password for fleet-server authentication. {pull-beats}[29458]

==== Bugfixes
- Fix race condition on `mage notice` {pull}[1108]
- Fix rename *ConfigChange to *PolicyChange to align on changes in the UI. {pull-beats}[20779]
- Thread safe sorted set {pull-beats}[21290]
- Copy Action store on upgrade {pull-beats}[21298]
Expand Down
41 changes: 37 additions & 4 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package main

import (
"bytes"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -111,8 +112,22 @@ func Notice() error {
return errors.Wrap(err, "failed running go list, please fix the issues reported")
}
fmt.Println(">> fmt - go run")
cmd := exec.Command("go", "run", "go.elastic.co/go-licence-detector", "-includeIndirect", "-rules", "dev-tools/notice/rules.json", "-overrides", "dev-tools/notice/overrides.json", "-noticeTemplate", "dev-tools/notice/NOTICE.txt.tmpl",
"-noticeOut", "NOTICE.txt", "-depsOut", "\"\"")
goLicenceDetectorCMD := []string{"go",
"run",
"go.elastic.co/go-licence-detector",
"-includeIndirect",
"-rules",
"dev-tools/notice/rules.json",
"-overrides",
"dev-tools/notice/overrides.json",
"-noticeTemplate",
"dev-tools/notice/NOTICE.txt.tmpl",
"-noticeOut",
"NOTICE.txt",
"-depsOut",
"\"\""}
printCMD(goLicenceDetectorCMD)
cmd := exec.Command(goLicenceDetectorCMD[0], goLicenceDetectorCMD[1:]...)
stdin, err := cmd.StdinPipe()
if err != nil {
return errors.Wrap(err, "failed running go run, please fix the issues reported")
Expand All @@ -126,10 +141,10 @@ func Notice() error {
fmt.Println(err)
}
}()
out, err := cmd.CombinedOutput()
wg.Wait()
_, err = cmd.CombinedOutput()
if err != nil {
return errors.Wrap(err, "failed combined output, please fix the issues reported")
return fmt.Errorf("calling go-licence-detector returned an error: '%w'. Its output is: '%s'", err, string(out))
}
return nil
}
Expand Down Expand Up @@ -925,3 +940,21 @@ func majorMinor() string {
}
return ""
}

// printCMD prints the command in the same format than when
// using the functions from the `sh` package. It also respects
// the mage verbose flag
func printCMD(cmd []string) {
if !mg.Verbose() {
return
}

buff := &bytes.Buffer{}

fmt.Fprintf(buff, "exec: %s", cmd[0])
for _, arg := range cmd[1:] {
fmt.Fprintf(buff, " %q", arg)
}

fmt.Println(buff.String())
}

0 comments on commit 2d3a9e5

Please sign in to comment.