@@ -18,10 +18,13 @@ import (
1818 "context"
1919 "errors"
2020 "io/fs"
21+ "sort"
2122
2223 "github.com/bufbuild/buf/private/bufpkg/bufconfig"
2324 "github.com/bufbuild/buf/private/bufpkg/bufmodule"
2425 "github.com/bufbuild/buf/private/bufpkg/bufparse"
26+ "github.com/bufbuild/buf/private/bufpkg/bufplugin"
27+ "github.com/bufbuild/buf/private/pkg/slicesext"
2528 "github.com/bufbuild/buf/private/pkg/storage"
2629 "github.com/bufbuild/buf/private/pkg/syserror"
2730)
@@ -39,11 +42,13 @@ type WorkspaceDepManager interface {
3942 BufLockFileDigestType () bufmodule.DigestType
4043 // ExisingBufLockFileDepModuleKeys returns the ModuleKeys from the buf.lock file.
4144 ExistingBufLockFileDepModuleKeys (ctx context.Context ) ([]bufmodule.ModuleKey , error )
45+ // ExistingBufLockFileRemotePluginKeys returns the PluginKeys from the buf.lock file.
46+ ExistingBufLockFileRemotePluginKeys (ctx context.Context ) ([]bufplugin.PluginKey , error )
4247 // UpdateBufLockFile updates the lock file that backs the Workspace to contain exactly
43- // the given ModuleKeys.
48+ // the given ModuleKeys and PluginKeys .
4449 //
4550 // If a buf.lock does not exist, one will be created.
46- UpdateBufLockFile (ctx context.Context , depModuleKeys []bufmodule.ModuleKey ) error
51+ UpdateBufLockFile (ctx context.Context , depModuleKeys []bufmodule.ModuleKey , remotePluginKeys []bufplugin. PluginKey ) error
4752 // ConfiguredDepModuleRefs returns the configured dependencies of the Workspace as ModuleRefs.
4853 //
4954 // These come from buf.yaml files.
@@ -56,6 +61,16 @@ type WorkspaceDepManager interface {
5661 //
5762 // Sorted.
5863 ConfiguredDepModuleRefs (ctx context.Context ) ([]bufparse.Ref , error )
64+ // ConfiguredRemotePluginRefs returns the configured remote plugins of the Workspace as PluginRefs.
65+ //
66+ // These come from buf.yaml files.
67+ //
68+ // The PluginRefs in this list will be unique by FullName. If there are two PluginRefs
69+ // in the buf.yaml with the same FullName but different Refs, an error will be given
70+ // at workspace constructions.
71+ //
72+ // Sorted.
73+ ConfiguredRemotePluginRefs (ctx context.Context ) ([]bufparse.Ref , error )
5974
6075 isWorkspaceDepManager ()
6176}
@@ -110,14 +125,58 @@ func (w *workspaceDepManager) ConfiguredDepModuleRefs(ctx context.Context) ([]bu
110125 }
111126 case bufconfig .FileVersionV2 :
112127 if ! w .isV2 {
113- return nil , syserror .Newf ("buf.yaml at %q did had version %v but expected v12 " , w .targetSubDirPath , fileVersion )
128+ return nil , syserror .Newf ("buf.yaml at %q did had version %v but expected v2 " , w .targetSubDirPath , fileVersion )
114129 }
115130 default :
116131 return nil , syserror .Newf ("unknown FileVersion: %v" , fileVersion )
117132 }
118133 return bufYAMLFile .ConfiguredDepModuleRefs (), nil
119134}
120135
136+ func (w * workspaceDepManager ) ConfiguredRemotePluginRefs (ctx context.Context ) ([]bufparse.Ref , error ) {
137+ bufYAMLFile , err := bufconfig .GetBufYAMLFileForPrefix (ctx , w .bucket , w .targetSubDirPath )
138+ if err != nil {
139+ if ! errors .Is (err , fs .ErrNotExist ) {
140+ return nil , err
141+ }
142+ }
143+ if bufYAMLFile == nil {
144+ return nil , nil
145+ }
146+ switch fileVersion := bufYAMLFile .FileVersion (); fileVersion {
147+ case bufconfig .FileVersionV1Beta1 , bufconfig .FileVersionV1 :
148+ if w .isV2 {
149+ return nil , syserror .Newf ("buf.yaml at %q did had version %v but expected v1beta1, v1" , w .targetSubDirPath , fileVersion )
150+ }
151+ // Plugins are not supported in versions less than v2.
152+ return nil , nil
153+ case bufconfig .FileVersionV2 :
154+ if ! w .isV2 {
155+ return nil , syserror .Newf ("buf.yaml at %q did had version %v but expected v2" , w .targetSubDirPath , fileVersion )
156+ }
157+ default :
158+ return nil , syserror .Newf ("unknown FileVersion: %v" , fileVersion )
159+ }
160+ pluginRefs := slicesext .Filter (
161+ slicesext .Map (
162+ bufYAMLFile .PluginConfigs (),
163+ func (value bufconfig.PluginConfig ) bufparse.Ref {
164+ return value .PluginRef ()
165+ },
166+ ),
167+ func (value bufparse.Ref ) bool {
168+ return value != nil
169+ },
170+ )
171+ sort .Slice (
172+ pluginRefs ,
173+ func (i int , j int ) bool {
174+ return pluginRefs [i ].FullName ().String () < pluginRefs [j ].FullName ().String ()
175+ },
176+ )
177+ return pluginRefs , nil
178+ }
179+
121180func (w * workspaceDepManager ) BufLockFileDigestType () bufmodule.DigestType {
122181 if w .isV2 {
123182 return bufmodule .DigestTypeB5
@@ -136,11 +195,22 @@ func (w *workspaceDepManager) ExistingBufLockFileDepModuleKeys(ctx context.Conte
136195 return bufLockFile .DepModuleKeys (), nil
137196}
138197
139- func (w * workspaceDepManager ) UpdateBufLockFile (ctx context.Context , depModuleKeys []bufmodule.ModuleKey ) error {
198+ func (w * workspaceDepManager ) ExistingBufLockFileRemotePluginKeys (ctx context.Context ) ([]bufplugin.PluginKey , error ) {
199+ bufLockFile , err := bufconfig .GetBufLockFileForPrefix (ctx , w .bucket , w .targetSubDirPath )
200+ if err != nil {
201+ if errors .Is (err , fs .ErrNotExist ) {
202+ return nil , nil
203+ }
204+ return nil , err
205+ }
206+ return bufLockFile .RemotePluginKeys (), nil
207+ }
208+
209+ func (w * workspaceDepManager ) UpdateBufLockFile (ctx context.Context , depModuleKeys []bufmodule.ModuleKey , remotePluginKeys []bufplugin.PluginKey ) error {
140210 var bufLockFile bufconfig.BufLockFile
141211 var err error
142212 if w .isV2 {
143- bufLockFile , err = bufconfig .NewBufLockFile (bufconfig .FileVersionV2 , depModuleKeys )
213+ bufLockFile , err = bufconfig .NewBufLockFile (bufconfig .FileVersionV2 , depModuleKeys , remotePluginKeys )
144214 if err != nil {
145215 return err
146216 }
@@ -154,7 +224,10 @@ func (w *workspaceDepManager) UpdateBufLockFile(ctx context.Context, depModuleKe
154224 } else {
155225 fileVersion = existingBufYAMLFile .FileVersion ()
156226 }
157- bufLockFile , err = bufconfig .NewBufLockFile (fileVersion , depModuleKeys )
227+ if len (remotePluginKeys ) > 0 {
228+ return syserror .Newf ("remote plugins are not supported for v1 buf.yaml files" )
229+ }
230+ bufLockFile , err = bufconfig .NewBufLockFile (fileVersion , depModuleKeys , nil )
158231 if err != nil {
159232 return err
160233 }
0 commit comments