Skip to content

[builder] Add --go-build-flags cli flag #12589

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

Closed
wants to merge 9 commits into from

Conversation

braydonk
Copy link
Contributor

@braydonk braydonk commented Mar 9, 2025

Description

This PR adds the --go-build-flags cli flag which will allow the user to pass additional flags for the go build command used to compile the collector.

While doing the refactor, I also made it so that the default binary name now adds the .exe extension when building for Windows.

Link to tracking issue

Fixes #12588
Fixes #12591

Testing

New unit tests were added to verify the build command args based on the config. A new CompileAndGenerate test case was added as well.

Documentation

A section has been added to the README.

This PR adds the `--go-build-flags` cli flag which will allow the user
to pass additional flags for the `go build` command used to compile the
collector.
@braydonk braydonk requested a review from a team as a code owner March 9, 2025 16:13
@braydonk braydonk requested a review from codeboten March 9, 2025 16:13
@@ -113,6 +113,7 @@ var replaceModules = []string{
func newTestConfig(tb testing.TB) *Config {
cfg, err := NewDefaultConfig()
require.NoError(tb, err)
cfg.Distribution.Name = "test_distribution"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, the integration tests are running with a blank name field, which means the args end up being build -o 'ldflags=-s -w' .... So it's building a binary with the name 'ldflags=-s -w' instead of applying the ldflags. It didn't break any tests but is probably not intentional.

braydonk added 3 commits March 9, 2025 12:20
I had removed these because I didn't understand what they did at first,
and forgot to add them back once I realized why they were there.
@braydonk braydonk marked this pull request as draft March 9, 2025 17:00
Copy link

codecov bot commented Mar 9, 2025

Codecov Report

Attention: Patch coverage is 92.30769% with 3 lines in your changes missing coverage. Please review.

Project coverage is 92.21%. Comparing base (7d3e03e) to head (4a52b1e).
Report is 190 commits behind head on main.

Files with missing lines Patch % Lines
cmd/builder/internal/builder/config.go 90.90% 2 Missing and 1 partial ⚠️

❌ Your patch status has failed because the patch coverage (92.30%) is below the target coverage (95.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #12589      +/-   ##
==========================================
+ Coverage   92.18%   92.21%   +0.02%     
==========================================
  Files         469      469              
  Lines       25394    25408      +14     
==========================================
+ Hits        23409    23429      +20     
+ Misses       1574     1570       -4     
+ Partials      411      409       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@braydonk braydonk marked this pull request as ready for review March 10, 2025 01:38
gcflags := ""
executableName := c.Distribution.Name
if runtime.GOOS == "windows" {
executableName += ".exe"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codecov seems to think these aren't covered, but I think they are covered by TestGetGoBuildArgs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the reason you're getting this warning is because the body of the if statement isn't being run in codecov because it's not running in windows. I think this finding should probably be ignored because the test TestGetGoBuildArgs explicitly tests correctly on windows systems

gcflags := ""
executableName := c.Distribution.Name
if runtime.GOOS == "windows" {
executableName += ".exe"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the reason you're getting this warning is because the body of the if statement isn't being run in codecov because it's not running in windows. I think this finding should probably be ignored because the test TestGetGoBuildArgs explicitly tests correctly on windows systems

Copy link
Contributor

This PR was marked stale due to lack of activity. It will be closed in 14 days.

@github-actions github-actions bot added the Stale label Mar 25, 2025
@mattsains
Copy link
Contributor

mattsains commented Mar 25, 2025

@open-telemetry/collector-approvers I think this needs review

@github-actions github-actions bot removed the Stale label Mar 26, 2025
Copy link
Contributor

github-actions bot commented Apr 9, 2025

This PR was marked stale due to lack of activity. It will be closed in 14 days.

@github-actions github-actions bot added the Stale label Apr 9, 2025
```console
ocb --config=config.yaml --go-build-flags="-p 32 -buildvcs=false"
```
If you provide any flags in `--go-build-flags` that are already configured by the builder automatically, **they will be overridden**:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this flexible enough to remove any option passed by default? For example, what if I don't want to pass -trimpath? Could I do this by overriding it with --go-build-flags?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so we used to be able to pass the binary name on the command line as well as a lot of other flags
#11576
But that was deprecated in favor of manifest.yaml config values
I am thinking we should try to keep things in the config/manifest file to the maximum extent possible, and adding command line flags should be an exception that requires justification why it can't be in the yaml


You can provide additional flags to `go build` with the builder flag `--go-build-flags`:
```console
ocb --config=config.yaml --go-build-flags="-p 32 -buildvcs=false"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any advantage of this being a CLI flag vs being in config.yaml?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I would agree with that approach, except for that we already have --ldflags on the command line
#11996
My question is more if we take this approach (--go-build-flags) if we should deprecate the --ldflags option since --ldflags could be passed with this argument, right?
I would vote moving both this and ldflags to the manifest.yaml in general, though.

@github-actions github-actions bot removed the Stale label Apr 10, 2025
Copy link
Contributor

@jackgopack4 jackgopack4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not convinced that we should be adding more CLI arguments to ocb, especially when we have recently removed several recently (#11576)
I think there is an argument to be made that this and the ld/gc flags should be moved to a config.yaml section


You can provide additional flags to `go build` with the builder flag `--go-build-flags`:
```console
ocb --config=config.yaml --go-build-flags="-p 32 -buildvcs=false"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I would agree with that approach, except for that we already have --ldflags on the command line
#11996
My question is more if we take this approach (--go-build-flags) if we should deprecate the --ldflags option since --ldflags could be passed with this argument, right?
I would vote moving both this and ldflags to the manifest.yaml in general, though.

```console
ocb --config=config.yaml --go-build-flags="-p 32 -buildvcs=false"
```
If you provide any flags in `--go-build-flags` that are already configured by the builder automatically, **they will be overridden**:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so we used to be able to pass the binary name on the command line as well as a lot of other flags
#11576
But that was deprecated in favor of manifest.yaml config values
I am thinking we should try to keep things in the config/manifest file to the maximum extent possible, and adding command line flags should be an exception that requires justification why it can't be in the yaml

Copy link
Contributor

This PR was marked stale due to lack of activity. It will be closed in 14 days.

@github-actions github-actions bot added the Stale label Apr 25, 2025
@braydonk
Copy link
Contributor Author

I'm just going to close this I think. The reason I wanted to add this is similar to the reason I opened #11453, which is that there are ways I want to be able to build the Collector from ocb that can't be known at config time. However, the reason that issue was dismissed probably applies here too. I am generally opposed to this unilateral move to config over CLI flags, but not willing to fight for it. So at this point I just --skip-compilation and use Go directly.

#12591 which this PR addressed still should be fixed.

@braydonk braydonk closed this Apr 25, 2025
@DesAWSume
Copy link

Looks like this has been merged, could we have an update README or a instruction to build a exe/msi file will work on Windows?

@braydonk
Copy link
Contributor Author

@DesAWSume This wasn't merged. The instructions that exist on the README work the same on Windows, the only difference is you will have to rename the resulting binary to have a .exe extension.

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

Successfully merging this pull request may close these issues.

[builder] Produces binary name with no .exe extension on Windows [builder] Allow passing additional Go build args to ocb
5 participants