Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

DmitriyLewen
Copy link
Contributor

Description

See #8365

Related issues

Checklist

  • I've read the guidelines for contributing to this repository.
  • I've followed the conventions in the PR title.
  • I've added tests that prove my fix is effective or that my feature works.
  • I've updated the documentation with the relevant information (if needed).
  • I've added usage information (if the PR introduces new options)
  • I've included a "before" and "after" example to the description (if the PR is a user interface change).

@DmitriyLewen DmitriyLewen self-assigned this Feb 7, 2025
@DmitriyLewen DmitriyLewen marked this pull request as ready for review February 7, 2025 11:32
@DmitriyLewen DmitriyLewen force-pushed the fix/gobinary-nested-flags branch from cd33139 to 06bb746 Compare February 10, 2025 05:57
Comment on lines +163 to +172
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, "=")
}
Copy link
Contributor Author

@DmitriyLewen DmitriyLewen Feb 10, 2025

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))

Copy link
Collaborator

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)

@knqyf263
Copy link
Collaborator

I just remember we already use go-shellwords.

outputPluginArgs, err = shellwords.Parse(arg)

And I think we can disable the built-in help by adding -h. How about the following change? It is simpler.

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 ""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug(gobinary): Trivy fails when ldflags contain nested flags
2 participants