@@ -17,6 +17,7 @@ package bufremotepluginconfig
1717import (
1818 "errors"
1919 "fmt"
20+ "slices"
2021 "strings"
2122
2223 "buf.build/go/spdx"
@@ -60,7 +61,7 @@ func newConfig(externalConfig ExternalConfig, options []ConfigOption) (*Config,
6061 dependencies = append (dependencies , reference )
6162 }
6263 }
63- registryConfig , err := newRegistryConfig (externalConfig .Registry )
64+ registryConfig , err := newRegistryConfig (externalConfig .Registry , dependencies , opts . overrideRemote )
6465 if err != nil {
6566 return nil , err
6667 }
@@ -87,7 +88,11 @@ func newConfig(externalConfig ExternalConfig, options []ConfigOption) (*Config,
8788 }, nil
8889}
8990
90- func newRegistryConfig (externalRegistryConfig ExternalRegistryConfig ) (* RegistryConfig , error ) {
91+ func newRegistryConfig (
92+ externalRegistryConfig ExternalRegistryConfig ,
93+ pluginDependencies []bufremotepluginref.PluginReference ,
94+ overrideRemote string ,
95+ ) (* RegistryConfig , error ) {
9196 var (
9297 isGoEmpty = externalRegistryConfig .Go == nil
9398 isNPMEmpty = externalRegistryConfig .NPM == nil
@@ -125,7 +130,7 @@ func newRegistryConfig(externalRegistryConfig ExternalRegistryConfig) (*Registry
125130 options := OptionsSliceToPluginOptions (externalRegistryConfig .Opts )
126131 switch {
127132 case ! isGoEmpty :
128- goRegistryConfig , err := newGoRegistryConfig (externalRegistryConfig .Go )
133+ goRegistryConfig , err := newGoRegistryConfig (externalRegistryConfig .Go , pluginDependencies , overrideRemote )
129134 if err != nil {
130135 return nil , err
131136 }
@@ -242,14 +247,18 @@ func newNPMRegistryConfig(externalNPMRegistryConfig *ExternalNPMRegistryConfig)
242247 }, nil
243248}
244249
245- func newGoRegistryConfig (externalGoRegistryConfig * ExternalGoRegistryConfig ) (* GoRegistryConfig , error ) {
250+ func newGoRegistryConfig (
251+ externalGoRegistryConfig * ExternalGoRegistryConfig ,
252+ pluginDependencies []bufremotepluginref.PluginReference ,
253+ overrideRemote string ,
254+ ) (* GoRegistryConfig , error ) {
246255 if externalGoRegistryConfig == nil {
247256 return nil , nil
248257 }
249258 if externalGoRegistryConfig .MinVersion != "" && ! modfile .GoVersionRE .MatchString (externalGoRegistryConfig .MinVersion ) {
250259 return nil , fmt .Errorf ("the go minimum version %q must be a valid semantic version in the form of <major>.<minor>" , externalGoRegistryConfig .MinVersion )
251260 }
252- var dependencies []* GoRegistryDependencyConfig
261+ var runtimeDependencies []* GoRegistryDependencyConfig
253262 for _ , dep := range externalGoRegistryConfig .Deps {
254263 if dep .Module == "" {
255264 return nil , errors .New ("go runtime dependency requires a non-empty module name" )
@@ -260,17 +269,37 @@ func newGoRegistryConfig(externalGoRegistryConfig *ExternalGoRegistryConfig) (*G
260269 if ! semver .IsValid (dep .Version ) {
261270 return nil , fmt .Errorf ("go runtime dependency %s:%s does not have a valid semantic version" , dep .Module , dep .Version )
262271 }
263- dependencies = append (
264- dependencies ,
272+ runtimeDependencies = append (
273+ runtimeDependencies ,
265274 & GoRegistryDependencyConfig {
266275 Module : dep .Module ,
267276 Version : dep .Version ,
268277 },
269278 )
270279 }
280+ var basePlugin bufremotepluginref.PluginIdentity
281+ if externalGoRegistryConfig .BasePlugin != "" {
282+ var err error
283+ basePlugin , err = pluginIdentityForStringWithOverrideRemote (externalGoRegistryConfig .BasePlugin , overrideRemote )
284+ if err != nil {
285+ return nil , fmt .Errorf ("failed to parse base plugin: %w" , err )
286+ }
287+ // Validate the base plugin is included as one of the plugin dependencies when both are
288+ // specified. This ensures there's exactly one base type and it has a known dependency to
289+ // generate imports correctly and build a correct Go mod file.
290+ if len (pluginDependencies ) > 0 {
291+ ok := slices .ContainsFunc (pluginDependencies , func (ref bufremotepluginref.PluginReference ) bool {
292+ return ref .IdentityString () == basePlugin .IdentityString ()
293+ })
294+ if ! ok {
295+ return nil , fmt .Errorf ("base plugin %q not found in plugin dependencies" , externalGoRegistryConfig .BasePlugin )
296+ }
297+ }
298+ }
271299 return & GoRegistryConfig {
272300 MinVersion : externalGoRegistryConfig .MinVersion ,
273- Deps : dependencies ,
301+ Deps : runtimeDependencies ,
302+ BasePlugin : basePlugin ,
274303 }, nil
275304}
276305
0 commit comments