fix: make vet accept %w in Errorf, document Wrap(nil) contract#32
Merged
Conversation
Wrap and Wrapf always return a non-nil error even for a nil input, matching fmt.Errorf with a nil %w operand (and unlike pkg/errors). This is deliberate: gowrapper's Errorf-to-Wrap rewrite is only semantics-preserving because both sides agree on nil. Document the contract in godoc and README, and add TestWrapNil so it cannot change silently.
Errorf falls back to fmt.Errorf when the format contains %w, but vet's printf analyzer flagged callers with 'does not support error-wrapping directive %w'. The analyzer classifies a wrapper by the calls that forward (format, a...): the fmt.Sprintf forward on the traced path downgraded Errorf from errorf-like to printf-like. Move the Sprintf call behind a non-variadic helper so the only printf forward left is fmt.Errorf. No behavior change; 'go test ./...' now passes with vet enabled.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Two related hardening changes around the library's core contracts:
Document and pin the
Wrap(nil)contract.Wrap/Wrapfalways return a non-nil error, even for a nil input — matchingfmt.Errorfwith a nil%woperand, and unlikepkg/errors. This was deliberate but undocumented. Now stated in godoc and README (with the guardedif err != nilidiom), and pinned byTestWrapNilso it cannot change silently. Changing it was considered and rejected: it would silently alter control flow in every downstream module, andcmd/gowrapper'sErrorf→Wraprewrite is only semantics-preserving because both sides agree on nil.Fix the vet false positive on
Errorfwith%w.Errorfgenuinely supports%wby falling back tofmt.Errorf(asserted byTestFormatError), but newer toolchains failedgo test ./...witherrors.Errorf does not support error-wrapping directive %w. Vet classifies printf wrappers by the calls that forward(format, a...); thefmt.Sprintfforward on the traced path downgradedErrorffrom errorf-like to printf-like. Moving the Sprintf call behind a non-variadic helper leavesfmt.Errorfas the only forward, so vet now classifiesErrorfcorrectly. No behavior change, and the same false positive disappears for downstream users writingerrors.Errorf("...: %w", err).Test plan
go vet ./...— clean (previously failed onformat_test.go:488)go test ./...— passes with vet enabled, including newTestWrapNil