-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(go): merge nested flags into string for ldflags for Go binaries #8368
base: main
Are you sure you want to change the base?
fix(go): merge nested flags into string for ldflags for Go binaries #8368
Conversation
cd33139
to
06bb746
Compare
allFlags, err := shlex.Split(flags) | ||
if err != nil { | ||
return nil, xerrors.Errorf("unable to split flags: %w", err) | ||
} | ||
|
||
for i, flag := range allFlags { | ||
ss := strings.Split(flag, "=") | ||
ss[0] = strings.ReplaceAll(ss[0], "h", "") // remove `h` from flags to avoid help flag error. | ||
allFlags[i] = strings.Join(ss, "=") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed logic in 76f6a3c as suggested @knqyf263 (see #8365 (comment))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found some time to think about it and got an idea.
#8368 (comment)
06bb746
to
76f6a3c
Compare
I just remember we already use go-shellwords. trivy/pkg/flag/report_flags.go Line 237 in cb0b3a9
And I think we can disable the built-in help by adding diff --git a/pkg/dependency/parser/golang/binary/parse.go b/pkg/dependency/parser/golang/binary/parse.go
index f6e4abe8e..68d3b4708 100644
--- a/pkg/dependency/parser/golang/binary/parse.go
+++ b/pkg/dependency/parser/golang/binary/parse.go
@@ -9,7 +9,7 @@ import (
"sort"
"strings"
- "github.com/google/shlex"
+ "github.com/mattn/go-shellwords"
"github.com/samber/lo"
"github.com/spf13/pflag"
"golang.org/x/mod/semver"
@@ -160,18 +160,7 @@ func (p *Parser) ldFlags(settings []debug.BuildSetting) []string {
// splitLDFlags separates flags with spaces.
// If a flag contains nested flags - the nested flags will be stored as a single element
func splitLDFlags(flags string) ([]string, error) {
- allFlags, err := shlex.Split(flags)
- if err != nil {
- return nil, xerrors.Errorf("unable to split flags: %w", err)
- }
-
- for i, flag := range allFlags {
- ss := strings.Split(flag, "=")
- ss[0] = strings.ReplaceAll(ss[0], "h", "") // remove `h` from flags to avoid help flag error.
- allFlags[i] = strings.Join(ss, "=")
- }
-
- return allFlags, nil
+ return shellwords.Parse(flags)
}
// ParseLDFlags attempts to parse the binary's version from any `-ldflags` passed to `go build` at build time.
@@ -187,6 +176,7 @@ func (p *Parser) ParseLDFlags(name string, flags []string) string {
// to handle that edge case.
var x map[string]string
fset.StringToStringVarP(&x, "", "X", nil, "")
+ fset.BoolP("help", "h", false, "just to disable the built-in help flag")
if err := fset.Parse(flags); err != nil {
p.logger.Error("Could not parse -ldflags found in build info", log.Err(err))
return "" |
Description
See #8365
Related issues
Checklist