Skip to content

Commit 02ce5c7

Browse files
committed
Plugin update command
1 parent 4b7a1e8 commit 02ce5c7

File tree

22 files changed

+1169
-23
lines changed

22 files changed

+1169
-23
lines changed

private/buf/bufcli/cache.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ var (
103103
//
104104
// Normalized.
105105
v3CacheModuleLockRelDirPath = normalpath.Join("v3", "modulelocks")
106+
// v3CachePluginRelDirPath is the relative path to the files cache directory in its newest iteration.
107+
//
108+
// Normalized.
109+
v3CachePluginRelDirPath = normalpath.Join("v3", "plugins")
106110
// v3CacheWasmRuntimeRelDirPath is the relative path to the Wasm runtime cache directory in its newest iteration.
107111
// This directory is used to store the Wasm runtime cache. This is an implementation specific cache and opaque outside of the runtime.
108112
//
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2020-2024 Buf Technologies, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package bufcli
16+
17+
import (
18+
"github.com/bufbuild/buf/private/bufpkg/bufplugin"
19+
"github.com/bufbuild/buf/private/bufpkg/bufplugin/bufpluginapi"
20+
"github.com/bufbuild/buf/private/bufpkg/bufregistryapi/bufregistryapiplugin"
21+
"github.com/bufbuild/buf/private/pkg/app/appext"
22+
)
23+
24+
// NewPluginKeyProvider returns a new PluginKeyProvider.
25+
func NewPluginKeyProvider(container appext.Container) (bufplugin.PluginKeyProvider, error) {
26+
clientConfig, err := NewConnectClientConfig(container)
27+
if err != nil {
28+
return nil, err
29+
}
30+
return bufpluginapi.NewPluginKeyProvider(
31+
container.Logger(),
32+
bufregistryapiplugin.NewClientProvider(
33+
clientConfig,
34+
),
35+
), nil
36+
}

private/buf/bufmigrate/migrator.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ func (m *migrator) buildBufYAMLAndBufLockFiles(
400400
bufLock, err = bufconfig.NewBufLockFile(
401401
bufconfig.FileVersionV2,
402402
resolvedLockEntries,
403+
nil,
403404
)
404405
if err != nil {
405406
return nil, nil, err
@@ -444,6 +445,7 @@ func (m *migrator) buildBufYAMLAndBufLockFiles(
444445
bufLock, err = bufconfig.NewBufLockFile(
445446
bufconfig.FileVersionV2,
446447
resolvedDepModuleKeys,
448+
nil, // Plugins are not supported in v1.
447449
)
448450
if err != nil {
449451
return nil, nil, err

private/buf/bufworkspace/workspace_dep_manager.go

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
121180
func (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
}

private/buf/cmd/buf/buf.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import (
6363
"github.com/bufbuild/buf/private/buf/cmd/buf/command/mod/modlslintrules"
6464
"github.com/bufbuild/buf/private/buf/cmd/buf/command/mod/modopen"
6565
"github.com/bufbuild/buf/private/buf/cmd/buf/command/plugin/pluginpush"
66+
"github.com/bufbuild/buf/private/buf/cmd/buf/command/plugin/pluginupdate"
6667
"github.com/bufbuild/buf/private/buf/cmd/buf/command/push"
6768
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/module/modulecommit/modulecommitaddlabel"
6869
"github.com/bufbuild/buf/private/buf/cmd/buf/command/registry/module/modulecommit/modulecommitinfo"
@@ -177,6 +178,7 @@ func NewRootCommand(name string) *appcmd.Command {
177178
Short: "Work with plugins",
178179
SubCommands: []*appcmd.Command{
179180
pluginpush.NewCommand("push", builder),
181+
pluginupdate.NewCommand("update", builder),
180182
},
181183
},
182184
{

private/buf/cmd/buf/command/dep/depupdate/depupdate.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ func NewCommand(
4545
flags := newFlags()
4646
return &appcmd.Command{
4747
Use: name + " <directory>",
48-
Short: "Update pinned dependencies in a buf.lock",
49-
Long: `Fetch the latest digests for the specified references in buf.yaml,
48+
Short: "Update pinned module dependencies in a buf.lock",
49+
Long: `Fetch the latest digests for the specified module references in buf.yaml,
5050
and write them and their transitive dependencies to buf.lock.
5151
5252
The first argument is the directory of the local module to update.
@@ -139,6 +139,10 @@ func run(
139139
logger.Warn(fmt.Sprintf("No configured dependencies were found to update in %q.", dirPath))
140140
return nil
141141
}
142+
existingRemotePluginKeys, err := workspaceDepManager.ExistingBufLockFileRemotePluginKeys(ctx)
143+
if err != nil {
144+
return err
145+
}
142146

143147
// We're about to edit the buf.lock file on disk. If we have a subsequent error,
144148
// attempt to revert the buf.lock file.
@@ -148,11 +152,11 @@ func run(
148152
// overlay the new buf.lock file in a union bucket.
149153
defer func() {
150154
if retErr != nil {
151-
retErr = errors.Join(retErr, workspaceDepManager.UpdateBufLockFile(ctx, existingDepModuleKeys))
155+
retErr = errors.Join(retErr, workspaceDepManager.UpdateBufLockFile(ctx, existingDepModuleKeys, existingRemotePluginKeys))
152156
}
153157
}()
154158
// Edit the buf.lock file with the unpruned dependencies.
155-
if err := workspaceDepManager.UpdateBufLockFile(ctx, configuredDepModuleKeys); err != nil {
159+
if err := workspaceDepManager.UpdateBufLockFile(ctx, configuredDepModuleKeys, existingRemotePluginKeys); err != nil {
156160
return err
157161
}
158162
workspace, err := controller.GetWorkspace(ctx, dirPath, bufctl.WithIgnoreAndDisallowV1BufWorkYAMLs())

private/buf/cmd/buf/command/dep/internal/internal.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,11 @@ func Prune(
106106
if err := validateModuleKeysContains(bufYAMLBasedDepModuleKeys, depModuleKeys); err != nil {
107107
return err
108108
}
109-
return workspaceDepManager.UpdateBufLockFile(ctx, depModuleKeys)
109+
existingRemotePluginKeys, err := workspaceDepManager.ExistingBufLockFileRemotePluginKeys(ctx)
110+
if err != nil {
111+
return err
112+
}
113+
return workspaceDepManager.UpdateBufLockFile(ctx, depModuleKeys, existingRemotePluginKeys)
110114
}
111115

112116
// LogUnusedConfiugredDepsForWorkspace takes a workspace and logs the unused configured

0 commit comments

Comments
 (0)