-
-
Notifications
You must be signed in to change notification settings - Fork 669
fix: build tag parsing. #1413
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: build tag parsing. #1413
Conversation
|
Please could you fix the lint warnings? |
| trackSuppressions bool | ||
| concurrency int | ||
| analyzerSet *analyzers.AnalyzerSet | ||
| mu sync.Mutex |
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.
Why was required to remove this mutex? How is related to this change?
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.
This mutex was only used when mutating the config (refer: https://github.com/securego/gosec/pull/1053/files). But the mutation of the config causes potential issues for the next worker.
Given the config was passed in via a pointer to func (gosec *Analyzer) load(pkgPath string, conf *packages.Config), the previous code lines mutate the top level config, potentially removing the build flags for the next worker:
gosec.mu.Lock()
conf.BuildFlags = nil
defer gosec.mu.Unlock()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.
Can you please run the make build-race and post the result? Thanks
| // step 2/2: pass in cli encoded build flags to build correctly. | ||
| conf := &packages.Config{ | ||
| Mode: LoadMode, | ||
| BuildFlags: CLIBuildTags(buildTags), |
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.
Ins't this the only fix required to get properly the build flags propagated? Why's the reason for the extended refactoring?
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.
Couple of reasons:
- The default builder wants build tags in the form
[]string{"tag1", "tag2"} packages.Loadwants cli build flags in the form of[]string{"-tags=tag1,tag2", "-ldflags=\"-s -w\""}- Modifying the top level config here by nil'ing out the build flags here could remove build tags for the next worker.
The change was made to stop top level config mutation (passed in as a pointer). By defining the config locally the mutex isn't required.
In the PR description I mentioned a possible refactor: moving the config creation back and making load() take in both buildTags []string and the fixed conf *packages.Config which could reduce code churn.
However, passing a shared conf *packages.Config again risks future contributors accidentally mutating it, so preferred creating the config where it's required.
Other option is to deep-copy the config before calling gosec.load().
Happy to refine things as required! 😄
| type Option func(conf *packages.Config) | ||
|
|
||
| // WithBuildTags enables injecting build tags into the package config on build. | ||
| func WithBuildTags(tags []string) Option { |
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.
To not break the API for existing tests, I decided to use variadic options to inject expected build tags under test.
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1413 +/- ##
==========================================
- Coverage 68.49% 64.66% -3.84%
==========================================
Files 75 76 +1
Lines 4384 4590 +206
==========================================
- Hits 3003 2968 -35
- Misses 1233 1487 +254
+ Partials 148 135 -13 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Aims to fix #1412
Couple of thoughts:
load()take in bothbuildTagsand a*package.Configso it's only processed and in memory once.SampleCodeBuildTag=>SampleCodeCompilationFailand addedshould not panic if a file can not compileas a test case