Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiple config files of different versions #68

Merged
merged 2 commits into from
May 22, 2024
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
5 changes: 5 additions & 0 deletions gazelle/buf/buf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
20 changes: 14 additions & 6 deletions gazelle/buf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io/fs"
"log"
"os"
"path"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions gazelle/buf/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
18 changes: 18 additions & 0 deletions gazelle/buf/testdata/mix/buf.yaml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions gazelle/buf/testdata/mix/fooapis/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# gazelle:buf_breaking_against //:against_file
11 changes: 11 additions & 0 deletions gazelle/buf/testdata/mix/fooapis/BUILD.out
Original file line number Diff line number Diff line change
@@ -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"],
)
Empty file.
16 changes: 16 additions & 0 deletions gazelle/buf/testdata/mix/fooapis/foo/v1/BUILD.out
Original file line number Diff line number Diff line change
@@ -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"],
)
5 changes: 5 additions & 0 deletions gazelle/buf/testdata/mix/fooapis/foo/v1/foo.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
syntax = "proto3";

package foo.v1;

message Foo {}
1 change: 1 addition & 0 deletions gazelle/buf/testdata/mix/petapis/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# gazelle:buf_breaking_against //:against_file
11 changes: 11 additions & 0 deletions gazelle/buf/testdata/mix/petapis/BUILD.out
Original file line number Diff line number Diff line change
@@ -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"],
)
Empty file.
17 changes: 17 additions & 0 deletions gazelle/buf/testdata/mix/petapis/pets/v1/BUILD.out
Original file line number Diff line number Diff line change
@@ -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"],
)
9 changes: 9 additions & 0 deletions gazelle/buf/testdata/mix/petapis/pets/v1/pets.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto3";

package pets.v1;

import "foo/v1/foo.proto";

message Pet {
foo.v1.Foo foo = 1;
}
Empty file.
Empty file.
7 changes: 7 additions & 0 deletions gazelle/buf/testdata/mix/v1/buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: v1
breaking:
use:
- FILE
lint:
use:
- DEFAULT
9 changes: 9 additions & 0 deletions gazelle/buf/testdata/mix/v1/zap/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 = "zap_proto",
srcs = ["zap.proto"],
visibility = ["//visibility:public"],
)

15 changes: 15 additions & 0 deletions gazelle/buf/testdata/mix/v1/zap/v1/BUILD.out
Original file line number Diff line number Diff line change
@@ -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"],
)
Empty file.
Empty file.
Empty file.
14 changes: 14 additions & 0 deletions gazelle/buf/testdata/mix/v2/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/mix/v2/tap/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 = "tap_proto",
srcs = ["tap.proto"],
visibility = ["//visibility:public"],
)

15 changes: 15 additions & 0 deletions gazelle/buf/testdata/mix/v2/tap/v1/BUILD.out
Original file line number Diff line number Diff line change
@@ -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"],
)
Empty file.
Loading