diff --git a/internal/mod/modresolve/resolve.go b/internal/mod/modresolve/resolve.go index 1faec6a82..bf37fbccc 100644 --- a/internal/mod/modresolve/resolve.go +++ b/internal/mod/modresolve/resolve.go @@ -155,8 +155,16 @@ func (r *registryConfig) init() error { // Shouldn't happen because default should apply. return fmt.Errorf("empty pathEncoding") } - if r.StripPrefix && r.PathEncoding != encPath { - return fmt.Errorf("cannot strip prefix unless using path encoding") + if r.StripPrefix { + if r.PathEncoding != encPath { + // TODO we could relax this to allow storing of naked tags + // when the module path matches exactly and hash tags + // otherwise. + return fmt.Errorf("cannot strip prefix unless using path encoding") + } + if r.repository == "" { + return fmt.Errorf("use of stripPrefix requires a non-empty repository within the registry") + } } return nil } @@ -388,6 +396,7 @@ func (r *resolver) ResolveToLocation(mpath, vers string) (Location, bool) { bestMatchReg := r.cfg.DefaultRegistry for pat, reg := range r.cfg.ModuleRegistries { if pat == mpath { + bestMatch = pat bestMatchReg = reg break } diff --git a/internal/mod/modresolve/resolve_test.go b/internal/mod/modresolve/resolve_test.go index 5bd15c226..70329be8f 100644 --- a/internal/mod/modresolve/resolve_test.go +++ b/internal/mod/modresolve/resolve_test.go @@ -381,6 +381,11 @@ moduleRegistries: { Repository: "repo/one/two/three", Tag: "v0.0.1", }, + "stripped.org/bar v0.0.1": { + Host: "r3.example", + Repository: "repo", + Tag: "v0.0.1", + }, }, }, { testName: "InvalidModulePath", @@ -428,6 +433,18 @@ moduleRegistries: { } `, err: `registry host "ok.com" is specified both as secure and insecure`, + }, { + testName: "StripPrefixWithNoRepo", + catchAllDefault: "c.example", + in: ` +moduleRegistries: { + "a.example/foo": { + registry: "foo.example" + stripPrefix: true + } +} +`, + err: `invalid registry configuration in "a.example/foo": use of stripPrefix requires a non-empty repository within the registry`, }} for _, tc := range testCases {