Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
`ignore: [!foo/bar.proto]`. Previously they parsed as empty strings, now they rejected
with an error.
- Fix lint comment ignores on proto2 `group` fields being ignored.
- Update the LSP to support named generation templates such as `buf.go.gen.yaml` and
`buf.gen.go.yaml`.
- Improve the `buf curl` error message for methods that accept a single request message.
- Deduplicate remote input fetches within a single command invocation, so that multiple
`inputs` in a `buf.gen.yaml` that resolve to the same archive, git repository, or image
Expand Down
25 changes: 20 additions & 5 deletions private/buf/buflsp/buf_gen_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"log/slog"
"os"
"path/filepath"
"strings"
"sync"

"buf.build/go/standard/xos/xexec"
Expand All @@ -38,9 +39,13 @@ const CommandRunGenerate = "buf.generate.run"
// plugins in a buf.gen.yaml file and publish informational diagnostics for any that are outdated.
const CommandCheckPluginUpdates = "buf.generate.checkPluginUpdates"

// isBufGenYAMLURI reports whether uri refers to a buf.gen.yaml file.
// isBufGenYAMLURI reports whether uri refers to a buf.gen.yaml file. Named
// templates such as buf.go.gen.yaml and buf.gen.go.yaml are also matched, since
// they are commonly passed to buf generate with --template.
func isBufGenYAMLURI(uri protocol.URI) bool {
return filepath.Base(uri.Filename()) == bufconfig.DefaultBufGenYAMLFileName
name := filepath.Base(uri.Filename())
return (strings.HasPrefix(name, "buf.") && strings.HasSuffix(name, ".gen.yaml")) ||
(strings.HasPrefix(name, "buf.gen.") && strings.HasSuffix(name, ".yaml"))
}

// bufGenYAMLManager tracks open buf.gen.yaml files in the LSP session.
Expand Down Expand Up @@ -213,8 +218,9 @@ func (m *bufGenYAMLManager) GetCodeLenses(uri protocol.URI) []protocol.CodeLens
return lenses
}

// ExecuteRunGenerate runs buf generate in the directory containing the given
// buf.gen.yaml URI. Results are reported to the user via ShowMessage.
// ExecuteRunGenerate runs buf generate with the given buf.gen.yaml URI as the
// template, from the directory containing it. Results are reported to the user
// via ShowMessage.
func (m *bufGenYAMLManager) ExecuteRunGenerate(ctx context.Context, uri protocol.URI) error {
dirPath := filepath.Dir(uri.Filename())
executable, err := os.Executable()
Expand All @@ -225,7 +231,7 @@ func (m *bufGenYAMLManager) ExecuteRunGenerate(ctx context.Context, uri protocol
msg := "buf generate completed successfully"
var outBuf bytes.Buffer
if err := xexec.Run(ctx, executable,
xexec.WithArgs("generate"),
xexec.WithArgs(bufGenerateArgs(uri)...),
xexec.WithDir(dirPath),
xexec.WithStdout(&outBuf),
xexec.WithStderr(&outBuf),
Expand All @@ -240,6 +246,15 @@ func (m *bufGenYAMLManager) ExecuteRunGenerate(ctx context.Context, uri protocol
return nil
}

// bufGenerateArgs returns the buf generate arguments for the template at uri.
func bufGenerateArgs(uri protocol.URI) []string {
name := filepath.Base(uri.Filename())
if name == bufconfig.DefaultBufGenYAMLFileName {
return []string{"generate"}
}
return []string{"generate", "--template", name}
}

// ExecuteCheckPluginUpdates queries the BSR for the latest version of each
// versioned remote plugin in the buf.gen.yaml file and publishes an
// informational diagnostic on any plugin line where a newer version is
Expand Down
8 changes: 8 additions & 0 deletions private/buf/buflsp/buf_gen_yaml_lsp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,14 @@ func TestBufGenYAMLCodeLens(t *testing.T) {
wantRunLensLine: 0,
wantCheckLensLine: 1, // plugins: key is on line 1
},
{
name: "named_template",
fixture: "testdata/buf_gen_yaml/named_template/buf.go.gen.yaml",
wantCount: 2,
wantTitles: []string{"Run buf generate", "Check for plugin updates"},
wantRunLensLine: 0,
wantCheckLensLine: 1,
},
}

for _, tc := range tests {
Expand Down
29 changes: 29 additions & 0 deletions private/buf/buflsp/buf_yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,18 @@ func TestIsBufGenYAMLURI(t *testing.T) {
expected bool
}{
{"file:///home/user/project/buf.gen.yaml", true},
{"file:///home/user/project/buf.go.gen.yaml", true},
{"file:///home/user/project/buf.connect-go.gen.yaml", true},
{"file:///home/user/project/buf.gen.go.yaml", true},
{"file:///home/user/project/buf.gen-private.yaml", false},
{"file:///home/user/project/buf-go.gen.yaml", false},
{"file:///home/user/project/bufgo.gen.yaml", false},
{"file:///home/user/project/buf.generated.yaml", false},
{"file:///home/user/project/buf.yaml", false},
{"file:///home/user/project/buf.work.yaml", false},
{"file:///home/user/project/buf.policy.yaml", false},
{"file:///home/user/project/buf.gen.yaml.bak", false},
{"file:///home/user/project/mybuf.gen.yaml", false},
{"file:///home/user/project/foo.proto", false},
}

Expand All @@ -146,6 +156,25 @@ func TestIsBufGenYAMLURI(t *testing.T) {
}
}

func TestBufGenerateArgs(t *testing.T) {
t.Parallel()

tests := []struct {
uri protocol.URI
expected []string
}{
{"file:///home/user/project/buf.gen.yaml", []string{"generate"}},
{"file:///home/user/project/buf.go.gen.yaml", []string{"generate", "--template", "buf.go.gen.yaml"}},
}

for _, tt := range tests {
t.Run(string(tt.uri), func(t *testing.T) {
t.Parallel()
assert.Equal(t, tt.expected, bufGenerateArgs(tt.uri))
})
}
}

func TestIsBufPolicyYAMLURI(t *testing.T) {
t.Parallel()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: v2
plugins:
- remote: buf.build/protocolbuffers/go
out: gen/go
- remote: buf.build/bufbuild/es:v2.2.2
out: gen/es
- local: protoc-gen-custom
out: gen/custom
inputs:
- module: buf.build/acme/petapis
- directory: proto
Loading