From 3d522b10885a21f2c07344748e36bbfdcad01990 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Fri, 9 Aug 2019 17:31:23 -0400 Subject: [PATCH] cmd/go/internal/module: in VersionError, do not wrap an existing ModuleError VersionError wraps the given error in a ModuleError struct. If the given error is already a ModuleError for the same path and version, we now return it directly instead of wrapping. This makes it safer to call VersionError if we don't know whether a given error is already wrapped. Updates #30748 Change-Id: I41b23f6c3ead0ec382e848696da51f478da1ad35 Reviewed-on: https://go-review.googlesource.com/c/go/+/189781 Run-TryBot: Bryan C. Mills TryBot-Result: Gobot Gobot Reviewed-by: Jay Conrod --- src/cmd/go/internal/module/module.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cmd/go/internal/module/module.go b/src/cmd/go/internal/module/module.go index a1a3ccfe8ac4b..3d1ad2762875b 100644 --- a/src/cmd/go/internal/module/module.go +++ b/src/cmd/go/internal/module/module.go @@ -50,8 +50,13 @@ type ModuleError struct { Err error } -// VersionError returns a ModuleError derived from a Version and error. +// VersionError returns a ModuleError derived from a Version and error, +// or err itself if it is already such an error. func VersionError(v Version, err error) error { + var mErr *ModuleError + if errors.As(err, &mErr) && mErr.Path == v.Path && mErr.Version == v.Version { + return err + } return &ModuleError{ Path: v.Path, Version: v.Version,