Skip to content

modernize Go practices - error wrapping, dead code removal, and depre… - #4613

Merged
liamfallon merged 1 commit into
kptdev:mainfrom
Nordix:modernize-go-practices
Jul 2, 2026
Merged

modernize Go practices - error wrapping, dead code removal, and depre…#4613
liamfallon merged 1 commit into
kptdev:mainfrom
Nordix:modernize-go-practices

Conversation

@aravindtga

Copy link
Copy Markdown
Contributor

Description

  • What changed:

    • Replaced %s/%v/%q and err.Error() patterns in fmt.Errorf calls with proper %w error wrapping (15 sites across 7 files)
    • Removed dead code: unused findResourceGroupInv function, its test TestKptMigrate_findResourceGroupInv, and the rgInvObj test fixture
    • Replaced os.Setenv/defer os.Setenv with t.Setenv in tests for automatic cleanup
    • Replaced http.DefaultClient (no timeout) with a custom http.Client with 60s timeout
    • Upgraded gotest.tools import from deprecated v1 (gotest.tools) to gotest.tools/v3 and removed the stale v1 dependency
    • Replaced interface{} with any in doc comment
    • Updated copyright years to include 2026 on all modified files per project guidelines
  • Why it's needed:

    • Using %s/%v instead of %w breaks the error chain, making errors.Is/errors.As unable to match wrapped errors
    • Dead code (findResourceGroupInv) is unreachable and triggers linter warnings
    • gotest.tools v1 is unmaintained; v3 is actively maintained and was already an indirect dependency
    • http.DefaultClient has no timeout, risking indefinite hangs on network issues
    • os.Setenv in tests can leak environment changes if the test panics or fails before deferred cleanup runs
    • Copyright headers must reflect all years of modification
  • How it works:

    • Mechanical replacements with no behavioural changes beyond enabling proper error unwrapping and removing unused code paths

Related Issue(s)

  • N/A (code hygiene)

Type of Change

  • Bug fix
  • New feature
  • Enhancement
  • Refactor
  • Documentation
  • Tests
  • Other: ________

Checklist

  • Code follows project style guidelines
  • Self-reviewed changes
  • Tests added/updated
  • Documentation added/updated
  • All tests and gating checks pass

AI Disclosure

  • I have used AI in the creation of this PR.

If so, please describe how:
- Kiro to audit the codebase for outdated Go patterns, identify quick wins, and apply the mechanical fixes.

@netlify

netlify Bot commented Jul 2, 2026

Copy link
Copy Markdown

Deploy Preview for kptdocs ready!

Name Link
🔨 Latest commit 0f39c90
🔍 Latest deploy log https://app.netlify.com/projects/kptdocs/deploys/6a4647455f63230008a6093c
😎 Deploy Preview https://deploy-preview-4613--kptdocs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR modernizes several Go patterns across the codebase to improve error traceability (via proper wrapping), reduce maintenance burden (by removing dead code), and improve test hygiene and operational safety (env cleanup and HTTP timeouts).

Changes:

  • Replaced string-formatting error construction with %w wrapping in multiple packages to preserve error chains for errors.Is/errors.As.
  • Removed unreachable migration helper code (findResourceGroupInv) along with its test/fixture.
  • Improved reliability by using t.Setenv in tests and replacing http.DefaultClient with a timeout-configured http.Client.

Reviewed changes

Copilot reviewed 13 out of 14 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
thirdparty/cmdconfig/commands/cmdeval/cmdeval.go Wraps underlying errors with %w to preserve error chains when reading/mutating Kptfiles.
release/formula/main.go Uses a custom http.Client with a 60s timeout instead of http.DefaultClient.
pkg/lib/util/cmdutil/cmdutil.go Wraps MkdirAll failures with %w to preserve the original error.
pkg/lib/util/attribution/attribution_test.go Replaces os.Setenv + deferred cleanup with t.Setenv for safer test isolation.
pkg/lib/update/update.go Wraps merge-comment processing errors with %w for better downstream classification.
pkg/lib/kptops/render_executor.go Uses %w for KRM validation errors to keep unwrapping working.
pkg/fn/runtime/runner.go Uses %w for input/output KRM validation errors to keep unwrapping working.
internal/testutil/testutil.go Updates gotest.tools import to gotest.tools/v3 (non-deprecated module path).
go.mod Switches direct dependency from deprecated gotest.tools v1 to gotest.tools/v3.
go.sum Removes stale v1 gotest.tools sums and retains v3 sums.
commands/live/migrate/migratecmd.go Removes dead/unreachable findResourceGroupInv helper and its now-unused import.
commands/live/migrate/migratecmd_test.go Removes dead test + fixture tied to the removed migration helper.
api/kptfile/v1/validation.go Wraps KRM validation errors with %w to preserve error chains with path context.
api/fnresult/v1/types.go Wraps YAML parsing errors with %w and updates a doc comment to use any.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread api/fnresult/v1/types.go Outdated
…cated APIs

Signed-off-by: Aravindhan Ayyanathan <aravindhan.a@est.tech>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 13 out of 14 changed files in this pull request and generated no new comments.

@aravindtga
aravindtga marked this pull request as ready for review July 2, 2026 11:15
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. go Pull requests that update Go code labels Jul 2, 2026
@aravindtga
aravindtga requested a review from a team July 2, 2026 11:15
@dosubot dosubot Bot added the lgtm label Jul 2, 2026
@liamfallon
liamfallon merged commit 26d67a8 into kptdev:main Jul 2, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go Pull requests that update Go code lgtm size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants