Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add buftarget package #2799

Merged
merged 32 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
410127a
Add first draft of buftarget
doriable Feb 3, 2024
33572a5
Plumb `buftarget` through to `buffetch/internal`
doriable Feb 15, 2024
b3daed0
WIP
doriable Feb 16, 2024
82e1a76
fix
bufdev Feb 28, 2024
8e35869
Clean up ProtoFileRef handling
doriable Feb 28, 2024
84d11c9
Add better fallback logic for workspace targeting
doriable Feb 29, 2024
5cd61f8
Fix all private/buf/cmd/buf integration tests
doriable Mar 1, 2024
95d6877
Fix issue with input paths outside of the current working dir context
doriable Mar 1, 2024
8046151
Fix the last of the tests
doriable Mar 4, 2024
859e018
Fix lint
doriable Mar 4, 2024
36c73b1
Rename InputPath to InputDir for bucket targeting for clarity
doriable Mar 4, 2024
5d6a67b
Have slightly nicer fallback check for workspace targeting
doriable Mar 4, 2024
e1f62ce
Fix some windows tests
doriable Mar 4, 2024
4121d87
Fix ci
doriable Mar 4, 2024
e0498d0
Fix windows test
doriable Mar 4, 2024
7680c73
Clean-up some extraneous changes
doriable Mar 5, 2024
aa283a7
Remove extraneous workspace configuration
doriable Mar 5, 2024
d0ed762
Merge remote-tracking branch 'origin/bufmod' into BSR-3326-target-paths
doriable Mar 5, 2024
e2ef952
Merge branch 'bufmod' into BSR-3326-target-paths
bufdev Mar 5, 2024
80b7c2a
TODO.txt problem solved
bufdev Mar 5, 2024
0c6af1e
s/InputDir/InputDirPath/g
bufdev Mar 5, 2024
b9e80d0
Godoc
bufdev Mar 5, 2024
adc6d29
Cleanups
bufdev Mar 5, 2024
ebd0d76
Add godocs for ControllingWorkspace
doriable Mar 5, 2024
d9bdb94
Fallback logic should only check target paths if input is not a proto…
doriable Mar 5, 2024
93681d8
Better error message
bufdev Mar 5, 2024
62102b9
Add test case for proto file ref with no workspace or module
doriable Mar 5, 2024
16f05a6
commit
bufdev Mar 5, 2024
517c389
Merge remote-tracking branch 'origin/BSR-3326-target-paths' into BSR-…
doriable Mar 5, 2024
6965ae1
Error message cleanup
bufdev Mar 5, 2024
7dcc76e
error messages
bufdev Mar 6, 2024
b7792f0
Test fixes
bufdev Mar 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,3 @@ NOTE: We are not allowing cross-workspace finding for include_package_files=true

- Changed protoc-gen-buf-lint and protoc-gen-buf-breaking configuration, deprecated old configurations
- Need to manually test protoc-gen-buf-breaking

$ bothbuf build google/ads/admob/v1/admob_api.proto
OLD
OLD STDOUT:
OLD EXIT CODE: 100
OLD STDERR:
google/ads/admob/v1/admob_api.proto:19:8:stat google/ads/admob/v1/admob_resources.proto: file does not exist
NEW
NEW STDOUT:
NEW EXIT CODE: 100
NEW STDERR:
/Users/pedge/git/googleapis/google/ads/admob/v1/admob_api.proto:19:8:stat google/ads/admob/v1/admob_resources.proto: file does not exist
35 changes: 7 additions & 28 deletions private/buf/bufctl/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ func (c *controller) getWorkspaceForProtoFileRef(
// TODO FUTURE: Feed flag names through to here.
return nil, fmt.Errorf("--exclude-path is not valid for use with .proto file references")
}
readBucketCloser, err := c.buffetchReader.GetSourceReadBucketCloser(
readBucketCloser, bucketTargeting, err := c.buffetchReader.GetSourceReadBucketCloser(
ctx,
c.container,
protoFileRef,
Expand All @@ -804,19 +804,9 @@ func (c *controller) getWorkspaceForProtoFileRef(
defer func() {
retErr = multierr.Append(retErr, readBucketCloser.Close())
}()
// The ProtoFilePath is still relative to the input bucket, not the bucket
// retrieved from buffetch. Treat the path just as we do with targetPaths
// and externalPaths in withPathsForBucketExtender.
protoFilePath, err := readBucketCloser.PathForExternalPath(protoFileRef.ProtoFilePath())
if err != nil {
return nil, err
}
options := []bufworkspace.WorkspaceBucketOption{
bufworkspace.WithTargetSubDirPath(
readBucketCloser.SubDirPath(),
),
bufworkspace.WithProtoFileTargetPath(
protoFilePath,
protoFileRef.ProtoFilePath(),
protoFileRef.IncludePackageFiles(),
),
bufworkspace.WithConfigOverride(
Expand All @@ -832,6 +822,7 @@ func (c *controller) getWorkspaceForProtoFileRef(
return c.workspaceProvider.GetWorkspaceForBucket(
ctx,
readBucketCloser,
bucketTargeting,
options...,
)
}
Expand All @@ -841,7 +832,7 @@ func (c *controller) getWorkspaceForSourceRef(
sourceRef buffetch.SourceRef,
functionOptions *functionOptions,
) (_ bufworkspace.Workspace, retErr error) {
readBucketCloser, err := c.buffetchReader.GetSourceReadBucketCloser(
readBucketCloser, bucketTargeting, err := c.buffetchReader.GetSourceReadBucketCloser(
ctx,
c.container,
sourceRef,
Expand All @@ -853,18 +844,7 @@ func (c *controller) getWorkspaceForSourceRef(
defer func() {
retErr = multierr.Append(retErr, readBucketCloser.Close())
}()
functionOptions, err = functionOptions.withPathsForBucketExtender(readBucketCloser)
if err != nil {
return nil, err
}
options := []bufworkspace.WorkspaceBucketOption{
bufworkspace.WithTargetSubDirPath(
readBucketCloser.SubDirPath(),
),
bufworkspace.WithTargetPaths(
functionOptions.targetPaths,
functionOptions.targetExcludePaths,
),
bufworkspace.WithConfigOverride(
functionOptions.configOverride,
),
Expand All @@ -878,6 +858,7 @@ func (c *controller) getWorkspaceForSourceRef(
return c.workspaceProvider.GetWorkspaceForBucket(
ctx,
readBucketCloser,
bucketTargeting,
options...,
)
}
Expand All @@ -887,7 +868,7 @@ func (c *controller) getWorkspaceDepManagerForDirRef(
dirRef buffetch.DirRef,
functionOptions *functionOptions,
) (_ bufworkspace.WorkspaceDepManager, retErr error) {
readWriteBucket, err := c.buffetchReader.GetDirReadWriteBucket(
readWriteBucket, bucketTargeting, err := c.buffetchReader.GetDirReadWriteBucket(
ctx,
c.container,
dirRef,
Expand All @@ -901,9 +882,7 @@ func (c *controller) getWorkspaceDepManagerForDirRef(
return c.workspaceDepManagerProvider.GetWorkspaceDepManager(
ctx,
readWriteBucket,
bufworkspace.WithTargetSubDirPath(
readWriteBucket.SubDirPath(),
),
bucketTargeting,
)
}

Expand Down
38 changes: 15 additions & 23 deletions private/buf/bufctl/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

package bufctl

import "github.com/bufbuild/buf/private/buf/buffetch"
import (
"github.com/bufbuild/buf/private/buf/buffetch"
)

type ControllerOption func(*controller)

Expand Down Expand Up @@ -144,28 +146,6 @@ func newFunctionOptions(controller *controller) *functionOptions {
}
}

func (f *functionOptions) withPathsForBucketExtender(
bucketExtender buffetch.BucketExtender,
) (*functionOptions, error) {
deref := *f
c := &deref
for i, inputTargetPath := range c.targetPaths {
targetPath, err := bucketExtender.PathForExternalPath(inputTargetPath)
if err != nil {
return nil, err
}
c.targetPaths[i] = targetPath
}
for i, inputTargetExcludePath := range c.targetExcludePaths {
targetExcludePath, err := bucketExtender.PathForExternalPath(inputTargetExcludePath)
if err != nil {
return nil, err
}
c.targetExcludePaths[i] = targetExcludePath
}
return c, nil
}

func (f *functionOptions) getGetReadBucketCloserOptions() []buffetch.GetReadBucketCloserOption {
var getReadBucketCloserOptions []buffetch.GetReadBucketCloserOption
if f.copyToInMemory {
Expand All @@ -174,6 +154,18 @@ func (f *functionOptions) getGetReadBucketCloserOptions() []buffetch.GetReadBuck
buffetch.GetReadBucketCloserWithCopyToInMemory(),
)
}
if len(f.targetPaths) > 0 {
getReadBucketCloserOptions = append(
getReadBucketCloserOptions,
buffetch.GetReadBucketCloserWithTargetPaths(f.targetPaths),
)
}
if len(f.targetExcludePaths) > 0 {
getReadBucketCloserOptions = append(
getReadBucketCloserOptions,
buffetch.GetReadBucketCloserWithTargetExcludePaths(f.targetExcludePaths),
)
}
if f.configOverride != "" {
// If we have a config override, we do not search for buf.yamls or buf.work.yamls,
// instead acting as if the config override was the only configuration file available.
Expand Down
47 changes: 42 additions & 5 deletions private/buf/buffetch/buffetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net/http"

"github.com/bufbuild/buf/private/buf/buffetch/internal"
"github.com/bufbuild/buf/private/buf/buftarget"
"github.com/bufbuild/buf/private/bufpkg/bufconfig"
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"github.com/bufbuild/buf/private/pkg/app"
Expand Down Expand Up @@ -321,7 +322,7 @@ type SourceReader interface {
container app.EnvStdinContainer,
sourceRef SourceRef,
options ...GetReadBucketCloserOption,
) (ReadBucketCloser, error)
) (ReadBucketCloser, buftarget.BucketTargeting, error)
}

// GetReadBucketCloserOption is an option for a GetSourceReadBucketCloser call.
Expand All @@ -345,6 +346,22 @@ func GetReadBucketCloserWithNoSearch() GetReadBucketCloserOption {
}
}

// GetReadBucketCloserWithTargetPaths sets the targets paths for bucket targeting information
// returned with the bucket.
func GetReadBucketCloserWithTargetPaths(targetPaths []string) GetReadBucketCloserOption {
return func(getReadBucketCloserOptions *getReadBucketCloserOptions) {
getReadBucketCloserOptions.targetPaths = targetPaths
}
}

// GetReadBucketCloserWithTargetExcludePaths sets the target exclude paths for bucket targeting
// information returned with the bucket.
func GetReadBucketCloserWithTargetExcludePaths(targetExcludePaths []string) GetReadBucketCloserOption {
return func(getReadBucketCloserOptions *getReadBucketCloserOptions) {
getReadBucketCloserOptions.targetExcludePaths = targetExcludePaths
}
}

// DirReader is a dir reader.
type DirReader interface {
// GetDirReadWriteBucket gets the dir bucket.
Expand All @@ -353,7 +370,7 @@ type DirReader interface {
container app.EnvStdinContainer,
dirRef DirRef,
options ...GetReadWriteBucketOption,
) (ReadWriteBucket, error)
) (ReadWriteBucket, buftarget.BucketTargeting, error)
}

// GetReadWriteBucketOption is an option for a GetDirReadWriteBucket call.
Expand All @@ -369,6 +386,22 @@ func GetReadWriteBucketWithNoSearch() GetReadWriteBucketOption {
}
}

// GetReadWriteBucketWithTargetPaths sets the target paths for the bucket targeting information
// returned with the bucket.
func GetReadWriteBucketWithTargetPaths(targetPaths []string) GetReadWriteBucketOption {
return func(getReadWriteBucketOptions *getReadWriteBucketOptions) {
getReadWriteBucketOptions.targetPaths = targetPaths
}
}

// GetReadWriteBucketWithTargetExcludePaths sets the target exclude paths for the bucket
// targeting information returned with the bucket.
func GetReadWriteBucketWithTargetExcludePaths(targetExcludePaths []string) GetReadWriteBucketOption {
return func(getReadWriteBucketOptions *getReadWriteBucketOptions) {
getReadWriteBucketOptions.targetExcludePaths = targetExcludePaths
}
}

// ModuleFetcher is a module fetcher.
type ModuleFetcher interface {
// GetModuleKey gets the ModuleKey.
Expand Down Expand Up @@ -544,16 +577,20 @@ func GetInputConfigForString(
}

type getReadBucketCloserOptions struct {
noSearch bool
copyToInMemory bool
noSearch bool
copyToInMemory bool
targetPaths []string
targetExcludePaths []string
}

func newGetReadBucketCloserOptions() *getReadBucketCloserOptions {
return &getReadBucketCloserOptions{}
}

type getReadWriteBucketOptions struct {
noSearch bool
noSearch bool
targetPaths []string
targetExcludePaths []string
}

func newGetReadWriteBucketOptions() *getReadWriteBucketOptions {
Expand Down
63 changes: 28 additions & 35 deletions private/buf/buffetch/internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/http"
"strconv"

"github.com/bufbuild/buf/private/buf/buftarget"
"github.com/bufbuild/buf/private/bufpkg/bufconfig"
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"github.com/bufbuild/buf/private/pkg/app"
Expand Down Expand Up @@ -183,7 +184,7 @@ type ProtoFileRef interface {
BucketRef
// Path is the normalized path to the file reference.
Path() string
// IncludePackageFiles says to include the same package files TODO update comment
// IncludePackageFiles says to include the files from the same package files
IncludePackageFiles() bool
FileScheme() FileScheme
protoFileRef()
Expand Down Expand Up @@ -411,12 +412,6 @@ type BucketExtender interface {
// the directory that contained this terminate file, and the subDirPath will be the sub-direftory of
// the actual asset relative to the terminate file.
SubDirPath() string

// PathForExternalPath takes a path external to the asset and converts it to
// a path that is relative to the asset.
//
// The returned path will be normalized and validated.
PathForExternalPath(externalPath string) (string, error)
}

// ReadBucketCloser is a bucket returned from GetReadBucketCloser.
Expand All @@ -443,23 +438,19 @@ type Reader interface {
options ...GetFileOption,
) (io.ReadCloser, error)
// GetReadBucketCloser gets the bucket.
//
// If done for a ProtoFileRef, the Bucket will be for the enclosing module or workspace via
// terminateFileNames or protoFileTerminateFileNames. No filtering of the Bucket is performed, this is
// the responsibility of the caller.
GetReadBucketCloser(
ctx context.Context,
container app.EnvStdinContainer,
bucketRef BucketRef,
options ...GetReadBucketCloserOption,
) (ReadBucketCloser, error)
) (ReadBucketCloser, buftarget.BucketTargeting, error)
// GetReadWriteBucket gets the bucket.
GetReadWriteBucket(
ctx context.Context,
container app.EnvStdinContainer,
dirRef DirRef,
options ...GetReadWriteBucketOption,
) (ReadWriteBucket, error)
) (ReadWriteBucket, buftarget.BucketTargeting, error)
// GetModuleKey gets the ModuleKey.
GetModuleKey(
ctx context.Context,
Expand Down Expand Up @@ -829,21 +820,23 @@ func WithGetReadBucketCloserCopyToInMemory() GetReadBucketCloserOption {
// See bufconfig.TerminateAtControllingWorkspace, which is the only thing that uses this.
// This is used by both non-ProtoFileRefs to find the controlling workspace, AND ProtoFileRefs
// to find the controlling workspace of an enclosing module or workspace.
func WithGetReadBucketCloserTerminateFunc(terminateFunc TerminateFunc) GetReadBucketCloserOption {
func WithGetReadBucketCloserTerminateFunc(terminateFunc buftarget.TerminateFunc) GetReadBucketCloserOption {
return func(getReadBucketCloserOptions *getReadBucketCloserOptions) {
getReadBucketCloserOptions.terminateFunc = terminateFunc
}
}

// WithGetReadBucketCloserProtoFileTerminateFunc is like WithGetReadBucketCloserTerminateFunc, but determines
// where to stop searching for the enclosing module or workspace when given a ProtoFileRef.
//
// See bufconfig.TerminateAtEnclosingModuleOrWorkspaceForProtoFileRef, which is the only thing that uses this.
// This finds the enclosing module or workspace.
// This is only used for ProtoFileRefs.
func WithGetReadBucketCloserProtoFileTerminateFunc(protoFileTerminateFunc TerminateFunc) GetReadBucketCloserOption {
// WithGetReadBucketCloserTargetPaths sets the target paths for the bucket targeting information.
func WithGetReadBucketCloserTargetPaths(targetPaths []string) GetReadBucketCloserOption {
return func(getReadBucketCloserOptions *getReadBucketCloserOptions) {
getReadBucketCloserOptions.protoFileTerminateFunc = protoFileTerminateFunc
getReadBucketCloserOptions.targetPaths = targetPaths
}
}

// WithGetReadBucketCloserTargetExcludePaths sets the target exclude paths for the bucket targeting information.
func WithGetReadBucketCloserTargetExcludePaths(targetExcludePaths []string) GetReadBucketCloserOption {
return func(getReadBucketCloserOptions *getReadBucketCloserOptions) {
getReadBucketCloserOptions.targetExcludePaths = targetExcludePaths
}
}

Expand All @@ -857,24 +850,24 @@ type GetReadWriteBucketOption func(*getReadWriteBucketOptions)
// See bufconfig.TerminateAtControllingWorkspace, which is the only thing that uses this.
// This is used by both non-ProtoFileRefs to find the controlling workspace, AND ProtoFileRefs
// to find the controlling workspace of an enclosing module or workspace.
func WithGetReadWriteBucketTerminateFunc(terminateFunc TerminateFunc) GetReadWriteBucketOption {
func WithGetReadWriteBucketTerminateFunc(terminateFunc buftarget.TerminateFunc) GetReadWriteBucketOption {
return func(getReadWriteBucketOptions *getReadWriteBucketOptions) {
getReadWriteBucketOptions.terminateFunc = terminateFunc
}
}

// TerminateFunc is a termination function.
//
// This function should return true if the search should terminate at the prefix, that is
// the prefix should be where the bucket is mapped onto.
//
// The original subDirPath is also given to the TerminateFunc.
type TerminateFunc func(
ctx context.Context,
bucket storage.ReadBucket,
prefix string,
originalSubDirPath string,
) (terminate bool, err error)
// WithGetReadWriteBucketTargetPaths sets the target paths for the bucket targeting information.
func WithGetReadWriteBucketTargetPaths(targetPaths []string) GetReadWriteBucketOption {
return func(getReadWriteBucketOptions *getReadWriteBucketOptions) {
getReadWriteBucketOptions.targetPaths = targetPaths
}
}

func WithGetReadWriteBucketTargetExcludePaths(targetExcludePaths []string) GetReadWriteBucketOption {
return func(getReadWriteBucketOptions *getReadWriteBucketOptions) {
getReadWriteBucketOptions.targetExcludePaths = targetExcludePaths
}
}

// PutFileOption is a PutFile option.
type PutFileOption func(*putFileOptions)
Expand Down
Loading
Loading