diff --git a/CHANGELOG.md b/CHANGELOG.md index 965d28fec8..46736592ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,9 @@ ## [Unreleased] -- No changes yet. +- Fix archive and git inputs so that `--path` and `--exclude-path` paths are relative to + the `#subdir` rather than the root of the input. This fixes an unintended behavior change + that was introduced in `v1.32.0`. ## [v1.32.0] - 2024-05-16 diff --git a/data/template/buf.go.gen.yaml b/data/template/buf.go.gen.yaml index b63d77f4d0..ec1d8f1721 100644 --- a/data/template/buf.go.gen.yaml +++ b/data/template/buf.go.gen.yaml @@ -23,9 +23,9 @@ inputs: tag: v27.0-rc1 subdir: src paths: - - src/google/protobuf/cpp_features.proto + - google/protobuf/cpp_features.proto - git_repo: https://github.com/protocolbuffers/protobuf.git tag: v27.0-rc1 subdir: java/core/src/main/resources paths: - - java/core/src/main/resources/google/protobuf/java_features.proto + - google/protobuf/java_features.proto diff --git a/private/buf/buffetch/internal/reader.go b/private/buf/buffetch/internal/reader.go index d403e94e9e..3457658d06 100644 --- a/private/buf/buffetch/internal/reader.go +++ b/private/buf/buffetch/internal/reader.go @@ -573,6 +573,15 @@ func getReadBucketCloserForBucket( if err := validatePaths(inputSubDirPath, targetPaths, targetExcludePaths); err != nil { return nil, nil, err } + // For archive and git refs, target paths and target exclude paths are expected to be + // mapped to the inputSubDirPath rather than the execution context. + // This affects buftarget when checking and mapping paths against the controlling + // workspace, so we need to ensure that all paths are properly mapped. + targetPaths, targetExcludePaths = mapTargetPathsAndTargetExcludePathsForArchiveAndGitRefs( + inputSubDirPath, + targetPaths, + targetExcludePaths, + ) bucketTargeting, err := buftarget.NewBucketTargeting( ctx, logger, @@ -904,6 +913,29 @@ func validatePaths( return nil } +func mapTargetPathsAndTargetExcludePathsForArchiveAndGitRefs( + inputSubDirPath string, + targetPaths []string, + targetExcludePaths []string, +) ([]string, []string) { + // No need to remap + if inputSubDirPath == "." { + return targetPaths, targetExcludePaths + } + return slicesext.Map( + targetPaths, + func(targetPath string) string { + return normalpath.Join(inputSubDirPath, targetPath) + }, + ), + slicesext.Map( + targetExcludePaths, + func(targetExcludePath string) string { + return normalpath.Join(inputSubDirPath, targetExcludePath) + }, + ) +} + type getFileOptions struct { keepFileCompression bool } diff --git a/private/buf/cmd/buf/workspace_test.go b/private/buf/cmd/buf/workspace_test.go index 2d5c4f9b73..87a88f0af0 100644 --- a/private/buf/cmd/buf/workspace_test.go +++ b/private/buf/cmd/buf/workspace_test.go @@ -291,7 +291,7 @@ func TestWorkspaceArchiveDir(t *testing.T) { "lint", filepath.Join(zipDir, "archive.zip#subdir=proto"), "--path", - filepath.Join("proto", "rpc.proto"), + filepath.Join("rpc.proto"), ) } } @@ -342,7 +342,7 @@ func TestWorkspaceNestedArchive(t *testing.T) { "lint", filepath.Join(zipDir, "archive.zip#subdir=proto/internal"), "--path", - filepath.Join("proto", "internal", "internal.proto"), + filepath.Join("internal.proto"), ) } } diff --git a/private/buf/cmd/buf/workspace_unix_test.go b/private/buf/cmd/buf/workspace_unix_test.go index 8cdfa31200..aedf998624 100644 --- a/private/buf/cmd/buf/workspace_unix_test.go +++ b/private/buf/cmd/buf/workspace_unix_test.go @@ -160,7 +160,7 @@ func TestWorkspaceGit(t *testing.T) { "build", "../../../../.git#ref=HEAD,subdir=private/buf/cmd/buf/testdata/workspace/success/dir/proto", "--path", - filepath.Join("private", "buf", "cmd", "buf", "testdata", "workspace", "success", "dir", "proto", "rpc.proto"), + filepath.Join("rpc.proto"), ) testRunStdout( t, @@ -188,7 +188,7 @@ func TestWorkspaceGit(t *testing.T) { "lint", "../../../../.git#ref=HEAD,subdir=private/buf/cmd/buf/testdata/workspace/success/dir/proto", "--path", - filepath.Join("private", "buf", "cmd", "buf", "testdata", "workspace", "success", "dir", "proto", "rpc.proto"), + filepath.Join("rpc.proto"), ) testRunStdout( t, @@ -206,7 +206,7 @@ func TestWorkspaceGit(t *testing.T) { "build", "../../../../.git#ref=HEAD,subdir=private/buf/cmd/buf/testdata/workspace/success/v2/dir/proto", "--path", - filepath.Join("private", "buf", "cmd", "buf", "testdata", "workspace", "success", "v2", "dir", "proto", "rpc.proto"), + filepath.Join("rpc.proto"), ) testRunStdout( t, @@ -234,6 +234,6 @@ func TestWorkspaceGit(t *testing.T) { "lint", "../../../../.git#ref=HEAD,subdir=private/buf/cmd/buf/testdata/workspace/success/v2/dir/proto", "--path", - filepath.Join("private", "buf", "cmd", "buf", "testdata", "workspace", "success", "v2", "dir", "proto", "rpc.proto"), + filepath.Join("rpc.proto"), ) }