Skip to content

Commit

Permalink
Add gazelle support for v2
Browse files Browse the repository at this point in the history
  • Loading branch information
srikrsna-buf committed May 22, 2024
1 parent 598ac93 commit 98bebcd
Show file tree
Hide file tree
Showing 106 changed files with 855 additions and 74 deletions.
30 changes: 18 additions & 12 deletions gazelle/buf/buf.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ const (

const lang = "buf"

var (
stringToBreakingMode = map[string]BreakingMode{
"module": BreakingModeModule,
"package": BreakingModePackage,
}
)
var stringToBreakingMode = map[string]BreakingMode{
"module": BreakingModeModule,
"package": BreakingModePackage,
}

// BreakingMode is the generation strategy for buf_breaking_test
//
Expand Down Expand Up @@ -80,25 +78,33 @@ type Config struct {
ModuleRoot bool
// BufConfigFile is for the nearest buf.yaml
BufConfigFile label.Label
// GeneratePushRule controls if buf_push should be generated.
// Path to the nearest module root.
//
// Always false for now.
GeneratePushRule bool
// Only applies to v2.
ModuleConfig *ModuleConfig
}

// BufModule is the parsed buf.yaml. It currently only supports version, name, and build
// top-level attributes.
type BufModule struct {
Version string `json:"version,omitempty" yaml:"version,omitempty"`
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Build BuildConfig `json:"build,omitempty" yaml:"build,omitempty"`
Version string `json:"version,omitempty" yaml:"version,omitempty"`
// V1
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Build BuildConfig `json:"build,omitempty" yaml:"build,omitempty"`
// V2
Modules []ModuleConfig `json:"modules,omitempty" yaml:"modules,omitempty"`
}

// BuildConfig is the build section of the buf.yaml
type BuildConfig struct {
Excludes []string `json:"excludes,omitempty" yaml:"excludes,omitempty"`
}

type ModuleConfig struct {
Path string `json:"path,omitempty" yaml:"path,omitempty"`
Excludes []string `json:"excludes,omitempty" yaml:"excludes,omitempty"`
}

// GetConfigForGazelleConfig extracts a Config from gazelle config.
// It will return `nil` if one is not found.
//
Expand Down
1 change: 1 addition & 0 deletions gazelle/buf/buf_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ type bufLock struct {
Owner string `yaml:"owner,omitempty" json:"owner,omitempty"`
Repository string `yaml:"repository,omitempty" json:"repository,omitempty"`
Commit string `yaml:"commit,omitempty" json:"commit,omitempty"`
Name string `json:"name,omitempty" yaml:"name,omitempty"`
} `yaml:"deps,omitempty" json:"deps,omitempty"`
}
18 changes: 11 additions & 7 deletions gazelle/buf/buf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,47 +32,51 @@ import (
func TestLint(t *testing.T) {
t.Parallel()
testRunGazelle(t, "lint")
testRunGazelle(t, "v2/lint")
}

func TestBreaking(t *testing.T) {
t.Parallel()
testRunGazelle(t, "breaking_module")
testRunGazelle(t, "breaking_package")
testRunGazelle(t, "breaking_package_to_module")
testRunGazelle(t, "v2/breaking_module")
testRunGazelle(t, "v2/breaking_package")
testRunGazelle(t, "v2/breaking_package_to_module")
}

func TestExcludes(t *testing.T) {
t.Parallel()
testRunGazelle(t, "excludes_module")
testRunGazelle(t, "excludes_package")
testRunGazelle(t, "v2/excludes_module")
testRunGazelle(t, "v2/excludes_package")
}

func TestWorkspaces(t *testing.T) {
t.Parallel()
testRunGazelle(t, "workspace")
testRunGazelle(t, "v2/workspace")
}

func TestCrossResolve(t *testing.T) {
t.Parallel()
testRunGazelle(t, "cross_resolve")
}

func TestPush(t *testing.T) {
// Skipping this test until we release the `buf_push` rule
t.SkipNow()
t.Parallel()
testRunGazelle(t, "push")
testRunGazelle(t, "v2/cross_resolve")
}

func TestMerge(t *testing.T) {
t.Parallel()
testRunGazelle(t, "merge")
testRunGazelle(t, "v2/merge")
}

func TestImportResolve(t *testing.T) {
t.Parallel()
testRunGazelle(t, "imports", "update-repos", "--from_file=buf.work.yaml", "-to_macro=buf_deps.bzl%buf_deps", "-prune")
testRunGazelle(t, "imports_toolchain_name", "update-repos", "--from_file=buf.work.yaml", "-to_macro=buf_deps.bzl%buf_deps", "-prune")
testRunGazelle(t, "v2/imports", "update-repos", "--from_file=buf.yaml", "-to_macro=buf_deps.bzl%buf_deps", "-prune")
testRunGazelle(t, "v2/imports_toolchain_name", "update-repos", "--from_file=buf.yaml", "-to_macro=buf_deps.bzl%buf_deps", "-prune")
}

func testRunGazelle(t *testing.T, name string, gazelleArgs ...string) {
Expand Down
20 changes: 19 additions & 1 deletion gazelle/buf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,16 @@ func loadConfig(gazelleConfig *config.Config, packageRelativePath string, file *
if err != nil {
log.Print("error trying to load default config", err)
}
if bufModule != nil {
if config.Module != nil && config.Module.Version == "v2" {
for _, module := range config.Module.Modules {
if module.Path == packageRelativePath {
config.ModuleRoot = true
config.BufConfigFile = label.New("", "", "buf.yaml") // v2 will always have a buf.yaml at the roor.
config.ModuleConfig = &module
break
}
}
} else if bufModule != nil {
config.Module = bufModule
config.ModuleRoot = true
config.BufConfigFile = label.New("", packageRelativePath, bufConfigFile)
Expand Down Expand Up @@ -172,11 +181,20 @@ func isWithinExcludes(config *Config, path string) bool {
if config.Module == nil {
return false
}
// v1
for _, exclude := range config.Module.Build.Excludes {
if strings.Contains(path, exclude) {
return true
}
}
// v2, all paths are relative to the root to `buf.yaml`, so no need to filter out the module.
for _, module := range config.Module.Modules {
for _, exclude := range module.Excludes {
if strings.HasPrefix(path, exclude) {
return true
}
}
}
return false
}

Expand Down
23 changes: 6 additions & 17 deletions gazelle/buf/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ func (*bufLang) GenerateRules(args language.GenerateArgs) language.GenerateResul
result.Imports = append(result.Imports, struct{}{})
}
if config.ModuleRoot {
protoImportPaths := getProtoImportPaths(config, args.Dir)
if config.GeneratePushRule && config.Module.Name != "" {
pushRule := generatePushRule()
result.Gen = append(result.Gen, pushRule)
result.Imports = append(result.Imports, protoImportPaths)
}
if config.BreakingImageTarget != "" && config.BreakingMode == BreakingModeModule {
breakingRule := generateBreakingRule(config, "buf")
result.Gen = append(result.Gen, breakingRule)
Expand All @@ -73,10 +67,6 @@ func (*bufLang) GenerateRules(args language.GenerateArgs) language.GenerateResul
result.Empty = append(result.Empty, generateEmptyRule(rule))
continue
}
if rule.Kind() == pushRuleKind {
result.Empty = append(result.Empty, generateEmptyRule(rule))
continue
}
// proto_library targets are mapped one to one for lint and breaking rules in package mode
if rule.Kind() == lintRuleKind || rule.Kind() == breakingRuleKind {
if shouldRemoveSingleTargetBufRule(
Expand All @@ -96,6 +86,9 @@ func generateLintRule(config *Config, target string) *rule.Rule {
if config.Module != nil {
r.SetAttr("config", config.BufConfigFile.String())
}
if config.ModuleConfig != nil {
r.SetAttr("module", config.ModuleConfig.Path)
}
return r
}

Expand All @@ -113,13 +106,9 @@ func generateBreakingRule(config *Config, target string) *rule.Rule {
if config.Module != nil {
r.SetAttr("config", config.BufConfigFile.String())
}
return r
}

func generatePushRule() *rule.Rule {
r := rule.NewRule(pushRuleKind, "buf_push")
r.SetAttr("config", "buf.yaml")
r.SetAttr("lock", "buf.lock")
if config.ModuleConfig != nil {
r.SetAttr("module", config.ModuleConfig.Path)
}
return r
}

Expand Down
16 changes: 0 additions & 16 deletions gazelle/buf/kinds.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const (
lintRuleKind = "buf_lint_test"
breakingRuleKind = "buf_breaking_test"
dependenciesRepoRuleKind = "buf_dependencies"
pushRuleKind = "buf_push"
)

var bufKinds = map[string]rule.KindInfo{
Expand Down Expand Up @@ -56,20 +55,6 @@ var bufKinds = map[string]rule.KindInfo{
"modules": true,
},
},
pushRuleKind: {
MatchAttrs: []string{"targets"},
NonEmptyAttrs: map[string]bool{
"targets": true,
},
MergeableAttrs: map[string]bool{
"targets": true,
"config": true,
"lock": true,
},
ResolveAttrs: map[string]bool{
"targets": true,
},
},
}

var bufLoads = []rule.LoadInfo{
Expand All @@ -79,7 +64,6 @@ var bufLoads = []rule.LoadInfo{
lintRuleKind,
breakingRuleKind,
dependenciesRepoRuleKind,
pushRuleKind,
},
},
}
Expand Down
6 changes: 2 additions & 4 deletions gazelle/buf/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ func (l *bufLang) Resolve(
if config.BreakingMode != BreakingModeModule {
return
}
fallthrough
case pushRuleKind:
resolveProtoTargetsForRule(
gazelleConfig,
ruleIndex,
Expand All @@ -55,7 +53,7 @@ func (l *bufLang) Resolve(
}
}

// resolveProtoTargetsForRule resolves targets of buf_breaking_test in Module mode and buf_push
// resolveProtoTargetsForRule resolves targets of buf_breaking_test in Module mode
func resolveProtoTargetsForRule(
gazelleConfig *config.Config,
ruleIndex *resolve.RuleIndex,
Expand All @@ -64,7 +62,7 @@ func resolveProtoTargetsForRule(
importsRaw interface{},
fromLabel label.Label,
) {
// importsRaw will be `[]string` for module mode and buf_push
// importsRaw will be `[]string` for module mode
imports, ok := importsRaw.([]string)
if !ok {
return
Expand Down
8 changes: 0 additions & 8 deletions gazelle/buf/testdata/push/BUILD.out

This file was deleted.

8 changes: 0 additions & 8 deletions gazelle/buf/testdata/push/buf.yaml

This file was deleted.

1 change: 1 addition & 0 deletions gazelle/buf/testdata/v2/breaking_module/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# gazelle:buf_breaking_against //:against_file
10 changes: 10 additions & 0 deletions gazelle/buf/testdata/v2/breaking_module/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
load("@rules_buf//buf:defs.bzl", "buf_breaking_test")

# gazelle:buf_breaking_against //:against_file

buf_breaking_test(
name = "buf_breaking",
against = "//:against_file",
config = "//:buf.yaml",
targets = ["//foo/v1:foo_proto"],
)
14 changes: 14 additions & 0 deletions gazelle/buf/testdata/v2/breaking_module/buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: v2
lint:
use:
- DEFAULT
except:
- FIELD_NOT_REQUIRED
- PACKAGE_NO_IMPORT_CYCLE
disallow_comment_ignores: true
breaking:
use:
- FILE
except:
- EXTENSION_NO_DELETE
- FIELD_SAME_DEFAULT
9 changes: 9 additions & 0 deletions gazelle/buf/testdata/v2/breaking_module/foo/v1/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_buf//buf:defs.bzl", "buf_lint_test")

proto_library(
name = "foo_proto",
srcs = ["foo.proto"],
visibility = ["//visibility:public"],
)

14 changes: 14 additions & 0 deletions gazelle/buf/testdata/v2/breaking_module/foo/v1/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_buf//buf:defs.bzl", "buf_lint_test")

proto_library(
name = "foo_proto",
srcs = ["foo.proto"],
visibility = ["//visibility:public"],
)

buf_lint_test(
name = "foo_proto_lint",
config = "//:buf.yaml",
targets = [":foo_proto"],
)
File renamed without changes.
2 changes: 2 additions & 0 deletions gazelle/buf/testdata/v2/breaking_package/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# gazelle:buf_breaking_against //:against_file
# gazelle:buf_breaking_mode package
2 changes: 2 additions & 0 deletions gazelle/buf/testdata/v2/breaking_package/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# gazelle:buf_breaking_against //:against_file
# gazelle:buf_breaking_mode package
14 changes: 14 additions & 0 deletions gazelle/buf/testdata/v2/breaking_package/buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: v2
lint:
use:
- DEFAULT
except:
- FIELD_NOT_REQUIRED
- PACKAGE_NO_IMPORT_CYCLE
disallow_comment_ignores: true
breaking:
use:
- FILE
except:
- EXTENSION_NO_DELETE
- FIELD_SAME_DEFAULT
9 changes: 9 additions & 0 deletions gazelle/buf/testdata/v2/breaking_package/foo/v1/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_buf//buf:defs.bzl", "buf_lint_test")

proto_library(
name = "foo_proto",
srcs = ["foo.proto"],
visibility = ["//visibility:public"],
)

Loading

0 comments on commit 98bebcd

Please sign in to comment.