Skip to content

fix: use %w instead of %s for error wrapping in fmt.Errorf calls#1222

Open
AdeshDeshmukh wants to merge 1 commit into
kitops-ml:mainfrom
AdeshDeshmukh:fix/error-wrapping-percent-w
Open

fix: use %w instead of %s for error wrapping in fmt.Errorf calls#1222
AdeshDeshmukh wants to merge 1 commit into
kitops-ml:mainfrom
AdeshDeshmukh:fix/error-wrapping-percent-w

Conversation

@AdeshDeshmukh

Copy link
Copy Markdown

Description

This PR fixes 11 instances across 8 files where fmt.Errorf was using %s to format an error argument instead of %w. While the error messages read just fine either way, using %s silently breaks the error chain — callers using errors.Is() or errors.As() cannot unwrap through these errors to inspect the underlying cause.

This is a Go best practice that the rest of the codebase already follows correctly (many adjacent lines use %w properly). These were likely copy-paste oversights.

What was changed

Every fix is the same pattern: %s%w for the error argument in a fmt.Errorf call.

Package File Lines Context
pkg/lib/filesystem/unpack core.go, skill.go, util.go 4 Wrapping getStoreForRef, GetManifest, and NewLocalRepo errors
pkg/lib/repo/local migration.go 2 Wrapping os.RemoveAll errors during storage migration
pkg/cmd/remove remove.go 2 Wrapping removeModelRef and oras.Resolve errors
pkg/cmd/tag tag.go 1 Wrapping oras.Resolve error
pkg/cmd/kitimport util.go 1 Wrapping os.WriteFile error
pkg/cmd/dev opts.go 1 Wrapping findAvailablePort error

Additional cleanup

In pkg/lib/filesystem/unpack/util.go, the format string also had a stray trailing \n ("failed to read local storage: %s\n"), which was removed as part of the fix. Trailing newlines are unusual in Go error messages and can cause formatting issues in logs.

Verification

  • go vet ./... — ✅ passes
  • go build ./... — ✅ compiles
  • go test ./... — ✅ 63 tests pass across 6 affected packages
  • DCO sign-off included

Why this matters

Consider a caller trying to inspect the error from removeModel():

err := removeModel(ctx, opts)
if errors.Is(err, errdef.ErrNotFound) {
    // Handle not-found case
}

Before this fix, the %s verb stringifies the underlying error, so errors.Is() never matches — even though the codebase explicitly checks err == errdef.ErrNotFound in the same functions. After the fix, the error chain is preserved and unwrapping works as intended.

Checklist

  • Changes are minimal and targeted — each fix is a single character change
  • go vet reports no issues
  • go build succeeds
  • Existing tests pass (63 tests across 6 packages)
  • DCO sign-off included

Copilot AI review requested due to automatic review settings June 23, 2026 07:37

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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 8 out of 8 changed files in this pull request and generated 1 comment.

Comment thread pkg/cmd/dev/opts.go Outdated
availPort, err := findAvailablePort()
if err != nil {
return fmt.Errorf("invalid arguments: %s", err)
return fmt.Errorf("invalid arguments: %w", err)
…ss the codebase

Changes %s to %w in 11 fmt.Errorf calls where the argument is an error
type, enabling proper error chain unwrapping via errors.Is()/errors.As().
This is a Go best practice that ensures callers can inspect the wrapped
error rather than receiving a stringified version.

Includes cleanup of a stray trailing \n in one error format string.

Affected packages:
  pkg/lib/filesystem/unpack/   (4 occurrences in core.go, skill.go, util.go)
  pkg/lib/repo/local/          (2 occurrences in migration.go)
  pkg/cmd/remove/              (2 occurrences in remove.go)
  pkg/cmd/tag/                 (1 occurrence in tag.go)
  pkg/cmd/kitimport/           (1 occurrence in util.go)
  pkg/cmd/dev/                 (1 occurrence in opts.go)

Signed-off-by: Adesh Deshmukh <adeshkd123@gmail.com>
@AdeshDeshmukh AdeshDeshmukh force-pushed the fix/error-wrapping-percent-w branch from 6e98e92 to 4c60582 Compare June 25, 2026 14:39
@AdeshDeshmukh

Copy link
Copy Markdown
Author

Agreed that message was misleading. Updated in the latest push.

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.

2 participants