Skip to content

Conversation

@Syedowais312
Copy link

@Syedowais312 Syedowais312 commented Dec 21, 2025

Problem

When running an invalid CLI command (for example otelcorecol foo), Cobra already prints a user-facing error message.

However, the builder-generated main.go also logs the same error using log.Fatalf, which results in duplicate error output.

Solution

This change updates the builder template to:

  • remove log.Fatalf from main()
  • exit with a non-zero status using os.Exit(1)
  • allow Cobra to fully own CLI error output

Result

Before

Error: unknown command "foo" for "otelcorecol"
Run 'otelcorecol --help' for usage.
collector server run finished with error: unknown command "foo" for "otelcorecol"

After

Error: unknown command "foo" for "otelcorecol"
Run 'otelcorecol --help' for usage.

Notes

  • Change is limited to cmd/builder/internal/builder/templates/main.go.tmpl
  • No behavior change for successful execution paths
  • Exit code behavior remains unchanged

Closes: #14302

@Syedowais312 Syedowais312 requested a review from a team as a code owner December 21, 2025 10:29
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Dec 21, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: Syedowais312 / name: syed owais (bd3991a)

Copy link
Contributor

@axw axw left a comment

Choose a reason for hiding this comment

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

LGTM, I suggest adding a comment to make it clear that we're intentionally not logging the error though.

@axw
Copy link
Contributor

axw commented Jan 5, 2026

@Syedowais312 can you please add a changelog entry? It's a small change, but still worthy of an enhancement changelog IMO.

@Syedowais312
Copy link
Author

@axw I’ve added a changelog entry and a comment in main.go.tmpl explaining the intentional omission of error logging. let me know if any further changes are needed.

@Syedowais312 Syedowais312 requested a review from axw January 5, 2026 16:18
CHANGELOG.md Outdated
- `cmd/mdatagen`: Add lint/ordering validation for metadata.yaml (#13781)
- `pdata/xpdata`: Refactor JSON marshaling and unmarshaling to use `pcommon.Value` instead of `AnyValue`. (#13837)
- `pkg/exporterhelper`: Expose `MergeCtx` in exporterhelper's queue batch settings` (#13742)
- `cmd/builder`: Avoid duplicate CLI error logging in generated collector binaries by relying on cobra's error handling. (#14317)
Copy link
Contributor

Choose a reason for hiding this comment

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

@Syedowais312 Syedowais312 force-pushed the fix/ocb-duplicate-cli-error-log branch from 18b3e17 to 70644ec Compare January 6, 2026 08:16
@Syedowais312
Copy link
Author

@axw Thanks for the pointer ,fixed the chloggen entry by removing empty optional fields, validated with make chlog-validate, and updated the PR. Please let me know if anything else is needed.

@Syedowais312 Syedowais312 requested a review from axw January 6, 2026 08:18
@codecov
Copy link

codecov bot commented Jan 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.10%. Comparing base (c216239) to head (bd3991a).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #14317   +/-   ##
=======================================
  Coverage   92.10%   92.10%           
=======================================
  Files         668      668           
  Lines       41379    41379           
=======================================
  Hits        38114    38114           
  Misses       2227     2227           
  Partials     1038     1038           

☔ 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.

Copy link
Contributor

@axw axw left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @Syedowais312. CI is failing because the generated code in otelcorecol will need changing.

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.

3 participants