Skip to content

Commit

Permalink
Only check directories in workspace targeting fallback logic (#3184)
Browse files Browse the repository at this point in the history
  • Loading branch information
doriable committed Jul 24, 2024
1 parent 8be3d05 commit 9b236ca
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- Fix the git input parameter `ref` to align with the `git` notion of a ref. This allows for the use
of branch names, tag names, and commit hashes.
- Fix unexpected `buf build` errors with absolute path directory inputs without workspace and/or
module configurations (e.g. `buf.yaml`, `buf.work.yaml`) and proto file paths set to the `--path` flag.

## [v1.35.0] - 2024-07-22

Expand Down
8 changes: 8 additions & 0 deletions private/buf/bufworkspace/workspace_targeting.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,14 @@ func checkForControllingWorkspaceOrV1Module(
ignoreWorkspaceCheck bool,
) (buftarget.ControllingWorkspace, error) {
path = normalpath.Normalize(path)
// We attempt to check that the provided target path is not a file by checking the extension.
// Any valid proto file provided as a target would have the .proto extension, so we treat
// any path given without as a directory.
// This could be a file without an extension, in which case an error would be returned
// to the user when we attempt to check for a controlling workspace.
if normalpath.Ext(path) != "" {
path = normalpath.Dir(path)
}
// Keep track of any v1 module found along the way. If we find a v1 or v2 workspace, we
// return that over the v1 module, but we return this as the fallback.
var fallbackV1Module buftarget.ControllingWorkspace
Expand Down
28 changes: 28 additions & 0 deletions private/buf/cmd/buf/buf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,34 @@ func TestSuccessProfile1(t *testing.T) {
testRunStdoutProfile(t, nil, 0, ``, "build", filepath.Join("testdata", "success"))
}

func TestSuccessDir(t *testing.T) {
t.Parallel()
testRunStdout(t, nil, 0, ``, "build", filepath.Join("testdata", "successnobufyaml"))
testRunStdout(
t,
nil,
0,
``,
"build",
filepath.Join("testdata", "successnobufyaml"),
"--path",
filepath.Join("testdata", "successnobufyaml", "buf", "buf.proto"),
)
wd, err := osext.Getwd()
require.NoError(t, err)
testRunStdout(t, nil, 0, ``, "build", filepath.Join(wd, "testdata", "successnobufyaml"))
testRunStdout(
t,
nil,
0,
``,
"build",
filepath.Join(wd, "testdata", "successnobufyaml"),
"--path",
filepath.Join(wd, "testdata", "successnobufyaml", "buf", "buf.proto"),
)
}

func TestFail1(t *testing.T) {
t.Parallel()
testRunStdout(
Expand Down
10 changes: 10 additions & 0 deletions private/buf/cmd/buf/testdata/successnobufyaml/buf/buf.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto3";

package buf;

import "google/protobuf/descriptor.proto";

message Foo {
int64 one = 1;
google.protobuf.DescriptorProto two = 2;
}

0 comments on commit 9b236ca

Please sign in to comment.