diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ce50ce4bf..0144967487 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/private/buf/bufworkspace/workspace_targeting.go b/private/buf/bufworkspace/workspace_targeting.go index db2abf4d8b..969568450e 100644 --- a/private/buf/bufworkspace/workspace_targeting.go +++ b/private/buf/bufworkspace/workspace_targeting.go @@ -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 diff --git a/private/buf/cmd/buf/buf_test.go b/private/buf/cmd/buf/buf_test.go index 7f17c8c723..6e224ba18b 100644 --- a/private/buf/cmd/buf/buf_test.go +++ b/private/buf/cmd/buf/buf_test.go @@ -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( diff --git a/private/buf/cmd/buf/testdata/successnobufyaml/buf/buf.proto b/private/buf/cmd/buf/testdata/successnobufyaml/buf/buf.proto new file mode 100644 index 0000000000..b5d46d68be --- /dev/null +++ b/private/buf/cmd/buf/testdata/successnobufyaml/buf/buf.proto @@ -0,0 +1,10 @@ +syntax = "proto3"; + +package buf; + +import "google/protobuf/descriptor.proto"; + +message Foo { + int64 one = 1; + google.protobuf.DescriptorProto two = 2; +}