From 4b8e791bafcd908f99bd60cfe7de8207a3e61cc7 Mon Sep 17 00:00:00 2001 From: Edward McFarlane <3036610+emcfarlane@users.noreply.github.com> Date: Wed, 24 Jan 2024 14:36:09 -0500 Subject: [PATCH] Fix InvalidRemoteErr panic (#2728) On an unknown remote ModuleIdentity may be nil. Use the fallback logic to select the correct dependency and derive the related module identity. Fixes #2725 A dial error for an invalid host will now correctly return the error formatted with a prompt to check the registry: ``` Failure: dial tcp: lookup private.registry: no such host. Are you sure "private.registry" (derived from module name "private.registry/org/dependency") is a Buf Schema Registry? ``` --- .../buf/cmd/buf/command/mod/modprune/modprune.go | 13 +++++++++---- .../buf/cmd/buf/command/mod/modupdate/modupdate.go | 13 +++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/private/buf/cmd/buf/command/mod/modprune/modprune.go b/private/buf/cmd/buf/command/mod/modprune/modprune.go index 000b0a8eb4..3375527647 100644 --- a/private/buf/cmd/buf/command/mod/modprune/modprune.go +++ b/private/buf/cmd/buf/command/mod/modprune/modprune.go @@ -93,9 +93,13 @@ func run( } var dependencyModulePins []bufmoduleref.ModulePin if len(requestReferences) > 0 { - var remote string + var ( + remote string + moduleIdentityString string + ) if config.ModuleIdentity != nil && config.ModuleIdentity.Remote() != "" { remote = config.ModuleIdentity.Remote() + moduleIdentityString = config.ModuleIdentity.IdentityString() } else { // At this point we know there's at least one dependency. If it's an unnamed module, select // the right remote from the list of dependencies. @@ -104,10 +108,11 @@ func run( return fmt.Errorf(`File %q has invalid "deps" references`, existingConfigFilePath) } remote = selectedRef.Remote() + moduleIdentityString = selectedRef.IdentityString() container.Logger().Debug(fmt.Sprintf( `File %q does not specify the "name" field. Based on the dependency %q, it appears that you are using a BSR instance at %q. Did you mean to specify "name: %s/..." within %q?`, existingConfigFilePath, - selectedRef.IdentityString(), + moduleIdentityString, remote, remote, existingConfigFilePath, @@ -125,8 +130,8 @@ func run( }), ) if err != nil { - if remote != bufconnect.DefaultRemote { - return bufcli.NewInvalidRemoteError(err, remote, config.ModuleIdentity.IdentityString()) + if !connect.IsWireError(err) && remote != bufconnect.DefaultRemote { + return bufcli.NewInvalidRemoteError(err, remote, moduleIdentityString) } return err } diff --git a/private/buf/cmd/buf/command/mod/modupdate/modupdate.go b/private/buf/cmd/buf/command/mod/modupdate/modupdate.go index 0848c0ba89..a1c3efe023 100644 --- a/private/buf/cmd/buf/command/mod/modupdate/modupdate.go +++ b/private/buf/cmd/buf/command/mod/modupdate/modupdate.go @@ -215,9 +215,13 @@ func getDependencies( if len(moduleConfig.Build.DependencyModuleReferences) == 0 { return nil, nil } - var remote string + var ( + remote string + moduleIdentityString string + ) if moduleConfig.ModuleIdentity != nil && moduleConfig.ModuleIdentity.Remote() != "" { remote = moduleConfig.ModuleIdentity.Remote() + moduleIdentityString = moduleConfig.ModuleIdentity.IdentityString() } else { // At this point we know there's at least one dependency. If it's an unnamed module, select // the right remote from the list of dependencies. @@ -226,10 +230,11 @@ func getDependencies( return nil, fmt.Errorf(`File %q has invalid "deps" references`, existingConfigFilePath) } remote = selectedRef.Remote() + moduleIdentityString = selectedRef.IdentityString() container.Logger().Debug(fmt.Sprintf( `File %q does not specify the "name" field. Based on the dependency %q, it appears that you are using a BSR instance at %q. Did you mean to specify "name: %s/..." within %q?`, existingConfigFilePath, - selectedRef.IdentityString(), + moduleIdentityString, remote, remote, existingConfigFilePath, @@ -268,8 +273,8 @@ func getDependencies( }), ) if err != nil { - if remote != bufconnect.DefaultRemote { - return nil, bufcli.NewInvalidRemoteError(err, remote, moduleConfig.ModuleIdentity.IdentityString()) + if !connect.IsWireError(err) && remote != bufconnect.DefaultRemote { + return nil, bufcli.NewInvalidRemoteError(err, remote, moduleIdentityString) } return nil, err }