Skip to content

Commit 44ed517

Browse files
committed
cmd/go/internal/load: make check for path in import error more robust
When producing an ImportPathError from ImportErrorf, we check to see whether the error string contains the path for the error. The issue is that we were checking for the exact path string when sometimes the string is quoted when the error is constructed, and the escaping in the quote may not match the path string. Check for both the path string, and the quoted path string. Fixes #68737 Change-Id: I01bf4e495056e929570bc11bc1f2000ce6d2802b Reviewed-on: https://go-review.googlesource.com/c/go/+/603475 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Sam Thanawalla <[email protected]>
1 parent b623422 commit 44ed517

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/cmd/go/internal/load/pkg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ type importError struct {
539539

540540
func ImportErrorf(path, format string, args ...any) ImportPathError {
541541
err := &importError{importPath: path, err: fmt.Errorf(format, args...)}
542-
if errStr := err.Error(); !strings.Contains(errStr, path) {
542+
if errStr := err.Error(); !strings.Contains(errStr, path) && !strings.Contains(errStr, strconv.Quote(path)) {
543543
panic(fmt.Sprintf("path %q not in error %q", path, errStr))
544544
}
545545
return err
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Issue #68737: Don't panic if the import path string doesn't appear
2+
# in the import error. The string may not appear because it may be
3+
# escaped when quoted as part of the error message.
4+
5+
! go run '' # Quote contains 0x01 byte
6+
! stderr panic
7+
stderr 'malformed import path "\\x01": invalid char ''\\x01'''

0 commit comments

Comments
 (0)