diff --git a/gazelle/buf/buf_test.go b/gazelle/buf/buf_test.go index ba43aac..b9c0bcc 100644 --- a/gazelle/buf/buf_test.go +++ b/gazelle/buf/buf_test.go @@ -71,6 +71,11 @@ func TestMerge(t *testing.T) { testRunGazelle(t, "v2/merge") } +func TestMix(t *testing.T) { + t.Parallel() + testRunGazelle(t, "mix") +} + 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") diff --git a/gazelle/buf/config.go b/gazelle/buf/config.go index 43a2dba..905e473 100644 --- a/gazelle/buf/config.go +++ b/gazelle/buf/config.go @@ -22,6 +22,7 @@ import ( "io/fs" "log" "os" + "path" "path/filepath" "strconv" "strings" @@ -85,19 +86,26 @@ func loadConfig(gazelleConfig *config.Config, packageRelativePath string, file * if err != nil { log.Print("error trying to load default config", err) } + // log.Println("loadConfig", packageRelativePath) + // Two cases where this could be the module root: + // 1. If there is a buf.yaml file and no modules are defined. + if bufModule != nil { + config.Module = bufModule + config.BufConfigFile = label.New("", packageRelativePath, bufConfigFile) + config.ModuleRoot = len(bufModule.Modules) == 0 + } + // 2. If the ancestor has a v2 buf.yaml file with matching path. if config.Module != nil && config.Module.Version == "v2" { for _, module := range config.Module.Modules { - if module.Path == packageRelativePath { + if module.Path == "." { + module.Path = "" + } + if path.Join(config.BufConfigFile.Pkg, 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) } // When using workspaces, for gazelle to generate accurate proto_library rules // we need add `# gazelle:proto_strip_import_prefix /path` to BUILD file at each module root diff --git a/gazelle/buf/generate.go b/gazelle/buf/generate.go index f22f5cd..373a0a8 100644 --- a/gazelle/buf/generate.go +++ b/gazelle/buf/generate.go @@ -86,7 +86,7 @@ func generateLintRule(config *Config, target string) *rule.Rule { if config.Module != nil { r.SetAttr("config", config.BufConfigFile.String()) } - if config.ModuleConfig != nil { + if config.ModuleConfig != nil && config.ModuleConfig.Path != "" { r.SetAttr("module", config.ModuleConfig.Path) } return r @@ -106,7 +106,7 @@ func generateBreakingRule(config *Config, target string) *rule.Rule { if config.Module != nil { r.SetAttr("config", config.BufConfigFile.String()) } - if config.ModuleConfig != nil { + if config.ModuleConfig != nil && config.ModuleConfig.Path != "" { r.SetAttr("module", config.ModuleConfig.Path) } return r diff --git a/gazelle/buf/testdata/mix/buf.yaml b/gazelle/buf/testdata/mix/buf.yaml new file mode 100644 index 0000000..b8e8d51 --- /dev/null +++ b/gazelle/buf/testdata/mix/buf.yaml @@ -0,0 +1,18 @@ +version: v2 +modules: + - path: barapis + - path: fooapis + - path: petapis +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 diff --git a/gazelle/buf/testdata/mix/fooapis/BUILD.in b/gazelle/buf/testdata/mix/fooapis/BUILD.in new file mode 100644 index 0000000..c1d725b --- /dev/null +++ b/gazelle/buf/testdata/mix/fooapis/BUILD.in @@ -0,0 +1 @@ +# gazelle:buf_breaking_against //:against_file diff --git a/gazelle/buf/testdata/mix/fooapis/BUILD.out b/gazelle/buf/testdata/mix/fooapis/BUILD.out new file mode 100644 index 0000000..5b16619 --- /dev/null +++ b/gazelle/buf/testdata/mix/fooapis/BUILD.out @@ -0,0 +1,11 @@ +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", + module = "fooapis", + targets = ["//fooapis/foo/v1:foo_v1_proto"], +) diff --git a/gazelle/buf/testdata/mix/fooapis/foo/v1/BUILD.in b/gazelle/buf/testdata/mix/fooapis/foo/v1/BUILD.in new file mode 100644 index 0000000..e69de29 diff --git a/gazelle/buf/testdata/mix/fooapis/foo/v1/BUILD.out b/gazelle/buf/testdata/mix/fooapis/foo/v1/BUILD.out new file mode 100644 index 0000000..91fb131 --- /dev/null +++ b/gazelle/buf/testdata/mix/fooapis/foo/v1/BUILD.out @@ -0,0 +1,16 @@ +load("@rules_proto//proto:defs.bzl", "proto_library") +load("@rules_buf//buf:defs.bzl", "buf_lint_test") + +proto_library( + name = "foo_v1_proto", + srcs = ["foo.proto"], + strip_import_prefix = "/fooapis", + visibility = ["//visibility:public"], +) + +buf_lint_test( + name = "foo_v1_proto_lint", + config = "//:buf.yaml", + module = "fooapis", + targets = [":foo_v1_proto"], +) diff --git a/gazelle/buf/testdata/mix/fooapis/foo/v1/foo.proto b/gazelle/buf/testdata/mix/fooapis/foo/v1/foo.proto new file mode 100644 index 0000000..403aebb --- /dev/null +++ b/gazelle/buf/testdata/mix/fooapis/foo/v1/foo.proto @@ -0,0 +1,5 @@ +syntax = "proto3"; + +package foo.v1; + +message Foo {} diff --git a/gazelle/buf/testdata/mix/petapis/BUILD.in b/gazelle/buf/testdata/mix/petapis/BUILD.in new file mode 100644 index 0000000..c1d725b --- /dev/null +++ b/gazelle/buf/testdata/mix/petapis/BUILD.in @@ -0,0 +1 @@ +# gazelle:buf_breaking_against //:against_file diff --git a/gazelle/buf/testdata/mix/petapis/BUILD.out b/gazelle/buf/testdata/mix/petapis/BUILD.out new file mode 100644 index 0000000..a38cc63 --- /dev/null +++ b/gazelle/buf/testdata/mix/petapis/BUILD.out @@ -0,0 +1,11 @@ +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", + module = "petapis", + targets = ["//petapis/pets/v1:pets_v1_proto"], +) diff --git a/gazelle/buf/testdata/mix/petapis/pets/v1/BUILD.in b/gazelle/buf/testdata/mix/petapis/pets/v1/BUILD.in new file mode 100644 index 0000000..e69de29 diff --git a/gazelle/buf/testdata/mix/petapis/pets/v1/BUILD.out b/gazelle/buf/testdata/mix/petapis/pets/v1/BUILD.out new file mode 100644 index 0000000..4c6f8c3 --- /dev/null +++ b/gazelle/buf/testdata/mix/petapis/pets/v1/BUILD.out @@ -0,0 +1,17 @@ +load("@rules_proto//proto:defs.bzl", "proto_library") +load("@rules_buf//buf:defs.bzl", "buf_lint_test") + +proto_library( + name = "pets_v1_proto", + srcs = ["pets.proto"], + strip_import_prefix = "/petapis", + visibility = ["//visibility:public"], + deps = ["//fooapis/foo/v1:foo_v1_proto"], +) + +buf_lint_test( + name = "pets_v1_proto_lint", + config = "//:buf.yaml", + module = "petapis", + targets = [":pets_v1_proto"], +) diff --git a/gazelle/buf/testdata/mix/petapis/pets/v1/pets.proto b/gazelle/buf/testdata/mix/petapis/pets/v1/pets.proto new file mode 100644 index 0000000..ba7c841 --- /dev/null +++ b/gazelle/buf/testdata/mix/petapis/pets/v1/pets.proto @@ -0,0 +1,9 @@ +syntax = "proto3"; + +package pets.v1; + +import "foo/v1/foo.proto"; + +message Pet { + foo.v1.Foo foo = 1; +} diff --git a/gazelle/buf/testdata/mix/v1/BUILD.in b/gazelle/buf/testdata/mix/v1/BUILD.in new file mode 100644 index 0000000..e69de29 diff --git a/gazelle/buf/testdata/mix/v1/BUILD.out b/gazelle/buf/testdata/mix/v1/BUILD.out new file mode 100644 index 0000000..e69de29 diff --git a/gazelle/buf/testdata/mix/v1/buf.yaml b/gazelle/buf/testdata/mix/v1/buf.yaml new file mode 100644 index 0000000..1a51945 --- /dev/null +++ b/gazelle/buf/testdata/mix/v1/buf.yaml @@ -0,0 +1,7 @@ +version: v1 +breaking: + use: + - FILE +lint: + use: + - DEFAULT diff --git a/gazelle/buf/testdata/mix/v1/zap/v1/BUILD.in b/gazelle/buf/testdata/mix/v1/zap/v1/BUILD.in new file mode 100644 index 0000000..f4bd97d --- /dev/null +++ b/gazelle/buf/testdata/mix/v1/zap/v1/BUILD.in @@ -0,0 +1,9 @@ +load("@rules_proto//proto:defs.bzl", "proto_library") +load("@rules_buf//buf:defs.bzl", "buf_lint_test") + +proto_library( + name = "zap_proto", + srcs = ["zap.proto"], + visibility = ["//visibility:public"], +) + diff --git a/gazelle/buf/testdata/mix/v1/zap/v1/BUILD.out b/gazelle/buf/testdata/mix/v1/zap/v1/BUILD.out new file mode 100644 index 0000000..41bc718 --- /dev/null +++ b/gazelle/buf/testdata/mix/v1/zap/v1/BUILD.out @@ -0,0 +1,15 @@ +load("@rules_proto//proto:defs.bzl", "proto_library") +load("@rules_buf//buf:defs.bzl", "buf_lint_test") + +proto_library( + name = "zap_proto", + srcs = ["zap.proto"], + strip_import_prefix = "/v1", + visibility = ["//visibility:public"], +) + +buf_lint_test( + name = "zap_proto_lint", + config = "//v1:buf.yaml", + targets = [":zap_proto"], +) diff --git a/gazelle/buf/testdata/mix/v1/zap/v1/zap.proto b/gazelle/buf/testdata/mix/v1/zap/v1/zap.proto new file mode 100644 index 0000000..e69de29 diff --git a/gazelle/buf/testdata/mix/v2/BUILD.in b/gazelle/buf/testdata/mix/v2/BUILD.in new file mode 100644 index 0000000..e69de29 diff --git a/gazelle/buf/testdata/mix/v2/BUILD.out b/gazelle/buf/testdata/mix/v2/BUILD.out new file mode 100644 index 0000000..e69de29 diff --git a/gazelle/buf/testdata/mix/v2/buf.yaml b/gazelle/buf/testdata/mix/v2/buf.yaml new file mode 100644 index 0000000..6fe9ec4 --- /dev/null +++ b/gazelle/buf/testdata/mix/v2/buf.yaml @@ -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 diff --git a/gazelle/buf/testdata/mix/v2/tap/v1/BUILD.in b/gazelle/buf/testdata/mix/v2/tap/v1/BUILD.in new file mode 100644 index 0000000..737aaf1 --- /dev/null +++ b/gazelle/buf/testdata/mix/v2/tap/v1/BUILD.in @@ -0,0 +1,9 @@ +load("@rules_proto//proto:defs.bzl", "proto_library") +load("@rules_buf//buf:defs.bzl", "buf_lint_test") + +proto_library( + name = "tap_proto", + srcs = ["tap.proto"], + visibility = ["//visibility:public"], +) + diff --git a/gazelle/buf/testdata/mix/v2/tap/v1/BUILD.out b/gazelle/buf/testdata/mix/v2/tap/v1/BUILD.out new file mode 100644 index 0000000..eaa3497 --- /dev/null +++ b/gazelle/buf/testdata/mix/v2/tap/v1/BUILD.out @@ -0,0 +1,15 @@ +load("@rules_proto//proto:defs.bzl", "proto_library") +load("@rules_buf//buf:defs.bzl", "buf_lint_test") + +proto_library( + name = "tap_proto", + srcs = ["tap.proto"], + strip_import_prefix = "/v2", + visibility = ["//visibility:public"], +) + +buf_lint_test( + name = "tap_proto_lint", + config = "//v2:buf.yaml", + targets = [":tap_proto"], +) diff --git a/gazelle/buf/testdata/mix/v2/tap/v1/tap.proto b/gazelle/buf/testdata/mix/v2/tap/v1/tap.proto new file mode 100644 index 0000000..e69de29