Skip to content

Commit

Permalink
Merge branch 'master' into jeromep/file-mode-patch
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromep-stripe authored May 20, 2024
2 parents 86505fb + f43d5d0 commit 0fbf13d
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 114 deletions.
18 changes: 10 additions & 8 deletions cmd/gazelle/fix-update.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"strings"
"syscall"

"github.com/bazelbuild/buildtools/build"

"github.com/bazelbuild/bazel-gazelle/config"
gzflag "github.com/bazelbuild/bazel-gazelle/flag"
"github.com/bazelbuild/bazel-gazelle/internal/wspace"
Expand All @@ -38,7 +40,6 @@ import (
"github.com/bazelbuild/bazel-gazelle/resolve"
"github.com/bazelbuild/bazel-gazelle/rule"
"github.com/bazelbuild/bazel-gazelle/walk"
"github.com/bazelbuild/buildtools/build"
)

// updateConfig holds configuration information needed to run the fix and
Expand Down Expand Up @@ -125,7 +126,7 @@ func (ucr *updateConfigurer) CheckFlags(fs *flag.FlagSet, c *config.Config) erro
if !filepath.IsAbs(dir) {
dir = filepath.Join(c.WorkDir, dir)
}
dir, err := filepath.EvalSymlinks(dir)
dir, err = filepath.EvalSymlinks(dir)
if err != nil {
return fmt.Errorf("%s: failed to resolve symlinks: %v", arg, err)
}
Expand Down Expand Up @@ -220,7 +221,7 @@ func (ucr *updateConfigurer) KnownDirectives() []string { return nil }

func (ucr *updateConfigurer) Configure(c *config.Config, rel string, f *rule.File) {}

// visitRecord stores information about about a directory visited with
// visitRecord stores information about a directory visited with
// packages.Walk.
type visitRecord struct {
// pkgRel is the slash-separated path to the visited directory, relative to
Expand Down Expand Up @@ -289,7 +290,7 @@ func runFixUpdate(wd string, cmd command, args []string) (err error) {
}
ruleIndex := resolve.NewRuleIndex(mrslv.Resolver, exts...)

if err := fixRepoFiles(c, loads); err != nil {
if err = fixRepoFiles(c, loads); err != nil {
return err
}

Expand Down Expand Up @@ -372,7 +373,8 @@ func runFixUpdate(wd string, cmd command, args []string) (err error) {
}

maybeRecordReplacement := func(ruleKind string) (*string, error) {
repl, err := lookupMapKindReplacement(c.KindMap, ruleKind)
var repl *config.MappedKind
repl, err = lookupMapKindReplacement(c.KindMap, ruleKind)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -463,8 +465,8 @@ func runFixUpdate(wd string, cmd command, args []string) (err error) {

if len(errorsFromWalk) > 1 {
var additionalErrors []string
for _, error := range errorsFromWalk[1:] {
additionalErrors = append(additionalErrors, error.Error())
for _, err = range errorsFromWalk[1:] {
additionalErrors = append(additionalErrors, err.Error())
}

return fmt.Errorf("encountered multiple errors: %w, %v", errorsFromWalk[0], strings.Join(additionalErrors, ", "))
Expand All @@ -480,7 +482,7 @@ func runFixUpdate(wd string, cmd command, args []string) (err error) {
err = cerr
}
}()
if err := maybePopulateRemoteCacheFromGoMod(c, rc); err != nil {
if err = maybePopulateRemoteCacheFromGoMod(c, rc); err != nil {
log.Print(err)
}
for _, v := range visits {
Expand Down
8 changes: 6 additions & 2 deletions internal/bzlmod/go_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ load(
"DEFAULT_DIRECTIVES_BY_PATH",
)
load(":go_mod.bzl", "deps_from_go_mod", "go_work_from_label", "sums_from_go_mod", "sums_from_go_work")
load(":semver.bzl", "semver", "COMPARES_HIGHEST_SENTINEL")
load(":semver.bzl", "COMPARES_HIGHEST_SENTINEL", "semver")
load(
":utils.bzl",
"drop_nones",
Expand Down Expand Up @@ -352,6 +352,7 @@ def _go_deps_impl(module_ctx):
outdated_direct_dep_printer = print
go_env = {}
dep_files = []
debug_mode = False
for module in module_ctx.modules:
if len(module.tags.config) > 1:
fail(
Expand All @@ -371,6 +372,7 @@ def _go_deps_impl(module_ctx):
elif check_direct_deps == "error":
outdated_direct_dep_printer = fail
go_env = mod_config.go_env
debug_mode = mod_config.debug_mode

_process_overrides(module_ctx, module, "gazelle_override", gazelle_overrides, _process_gazelle_override)
_process_overrides(module_ctx, module, "module_override", module_overrides, _process_module_override, archive_overrides)
Expand Down Expand Up @@ -605,6 +607,7 @@ def _go_deps_impl(module_ctx):
"build_extra_args": _get_build_extra_args(path, gazelle_overrides, gazelle_default_attributes),
"patches": _get_patches(path, module_overrides),
"patch_args": _get_patch_args(path, module_overrides),
"debug_mode": debug_mode,
}

archive_override = archive_overrides.get(path)
Expand Down Expand Up @@ -679,7 +682,7 @@ def _get_sum_from_module(path, module, sums):
if module.raw_version == COMPARES_HIGHEST_SENTINEL:
# replacement have no sums, so we can skip this
return None
elif module.local_path== None:
elif module.local_path == None:
fail("No sum for {}@{} from {} found. You may need to run: bazel run @rules_go//go -- mod tidy".format(path, module.raw_version, "parent-label-todo")) #module.parent_label))

return sums[entry]
Expand All @@ -702,6 +705,7 @@ _config_tag = tag_class(
"go_env": attr.string_dict(
doc = "The environment variables to use when fetching Go dependencies or running the `@rules_go//go` tool.",
),
"debug_mode": attr.bool(doc = "Whether or not to print stdout and stderr messages from gazelle", default = False),
},
)

Expand Down
40 changes: 8 additions & 32 deletions language/go/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,9 +683,9 @@ func (g *generator) maybePublishToolLib(lib *rule.Rule, pkg *goPackage) {
}

// maybeGenerateExtraLib generates extra equivalent library targets for
// certain protobuf libraries. These "_gen" targets depend on Well Known Types
// certain protobuf libraries. Historically, these "_gen" targets depend on Well Known Types
// built with go_proto_library and are used together with go_proto_library.
// The original targets are used when proto rule generation is disabled.
// However, these are no longer needed and are kept as aliases to be backward-compatible
func (g *generator) maybeGenerateExtraLib(lib *rule.Rule, pkg *goPackage) *rule.Rule {
gc := getGoConfig(g.c)
if gc.prefix != "github.com/golang/protobuf" || gc.prefixRel != "" {
Expand All @@ -695,48 +695,24 @@ func (g *generator) maybeGenerateExtraLib(lib *rule.Rule, pkg *goPackage) *rule.
var r *rule.Rule
switch pkg.importPath {
case "github.com/golang/protobuf/descriptor":
r = rule.NewRule("go_library", "go_default_library_gen")
r.SetAttr("srcs", pkg.library.sources.buildFlat())
r.SetAttr("importpath", pkg.importPath)
r = rule.NewRule("alias", "go_default_library_gen")
r.SetAttr("actual", ":go_default_library")
r.SetAttr("visibility", []string{"//visibility:public"})
r.SetAttr("deps", []string{
"//proto:go_default_library",
"@io_bazel_rules_go//proto/wkt:descriptor_go_proto",
"@org_golang_google_protobuf//reflect/protodesc:go_default_library",
"@org_golang_google_protobuf//reflect/protoreflect:go_default_library",
"@org_golang_google_protobuf//runtime/protoimpl:go_default_library",
})

case "github.com/golang/protobuf/jsonpb":
r = rule.NewRule("alias", "go_default_library_gen")
r.SetAttr("actual", ":go_default_library")
r.SetAttr("visibility", []string{"//visibility:public"})

case "github.com/golang/protobuf/protoc-gen-go/generator":
r = rule.NewRule("go_library", "go_default_library_gen")
r.SetAttr("srcs", pkg.library.sources.buildFlat())
r.SetAttr("importpath", pkg.importPath)
r = rule.NewRule("alias", "go_default_library_gen")
r.SetAttr("actual", ":go_default_library")
r.SetAttr("visibility", []string{"//visibility:public"})
r.SetAttr("deps", []string{
"//proto:go_default_library",
"//protoc-gen-go/generator/internal/remap:go_default_library",
"@io_bazel_rules_go//proto/wkt:compiler_plugin_go_proto",
"@io_bazel_rules_go//proto/wkt:descriptor_go_proto",
})

case "github.com/golang/protobuf/ptypes":
r = rule.NewRule("go_library", "go_default_library_gen")
r.SetAttr("srcs", pkg.library.sources.buildFlat())
r.SetAttr("importpath", pkg.importPath)
r = rule.NewRule("alias", "go_default_library_gen")
r.SetAttr("actual", ":go_default_library")
r.SetAttr("visibility", []string{"//visibility:public"})
r.SetAttr("deps", []string{
"//proto:go_default_library",
"@io_bazel_rules_go//proto/wkt:any_go_proto",
"@io_bazel_rules_go//proto/wkt:duration_go_proto",
"@io_bazel_rules_go//proto/wkt:timestamp_go_proto",
"@org_golang_google_protobuf//reflect/protoreflect:go_default_library",
"@org_golang_google_protobuf//reflect/protoregistry:go_default_library",
})
}

return r
Expand Down
52 changes: 12 additions & 40 deletions language/go/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,46 +830,18 @@ go_proto_library(name = "wkts_go_proto")
go_library(
name = "wkts_go_lib",
deps = [
"@com_github_golang_protobuf//protoc-gen-go/descriptor",
"@com_github_golang_protobuf//protoc-gen-go/plugin",
"@com_github_golang_protobuf//ptypes/any",
"@com_github_golang_protobuf//ptypes/duration",
"@com_github_golang_protobuf//ptypes/empty",
"@com_github_golang_protobuf//ptypes/struct",
"@com_github_golang_protobuf//ptypes/timestamp",
"@com_github_golang_protobuf//ptypes/wrappers",
"@org_golang_google_genproto//protobuf/api",
"@org_golang_google_genproto//protobuf/field_mask",
"@org_golang_google_genproto//protobuf/ptype",
"@org_golang_google_genproto//protobuf/source_context",
],
)
`,
}, {
desc: "proto_special_cross_resolve",
old: buildFile{content: `
go_library(
name = "go_default_library",
_imports = [
"github.com/golang/protobuf/proto",
"github.com/golang/protobuf/jsonpb",
"github.com/golang/protobuf/descriptor",
"github.com/golang/protobuf/protoc-gen-go/generator",
"github.com/golang/protobuf/ptypes",
"google.golang.org/grpc",
],
)
`},
want: `
go_library(
name = "go_default_library",
deps = [
"@com_github_golang_protobuf//descriptor:go_default_library_gen",
"@com_github_golang_protobuf//jsonpb:go_default_library_gen",
"@com_github_golang_protobuf//proto:go_default_library",
"@com_github_golang_protobuf//protoc-gen-go/generator:go_default_library_gen",
"@com_github_golang_protobuf//ptypes:go_default_library_gen",
"@org_golang_google_grpc//:go_default_library",
"//vendor/github.com/golang/protobuf/protoc-gen-go/descriptor",
"//vendor/github.com/golang/protobuf/protoc-gen-go/plugin",
"//vendor/github.com/golang/protobuf/ptypes/any",
"//vendor/github.com/golang/protobuf/ptypes/duration",
"//vendor/github.com/golang/protobuf/ptypes/empty",
"//vendor/github.com/golang/protobuf/ptypes/struct",
"//vendor/github.com/golang/protobuf/ptypes/timestamp",
"//vendor/github.com/golang/protobuf/ptypes/wrappers",
"//vendor/google.golang.org/genproto/protobuf/api",
"//vendor/google.golang.org/genproto/protobuf/field_mask",
"//vendor/google.golang.org/genproto/protobuf/ptype",
"//vendor/google.golang.org/genproto/protobuf/source_context",
],
)
`,
Expand Down
25 changes: 0 additions & 25 deletions language/proto/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,30 +176,5 @@ func (*protoLang) CrossResolve(c *config.Config, ix *resolve.RuleIndex, imp reso
return []resolve.FindResult{{Label: l}}
}
}
if imp.Lang == "go" && pc.Mode.ShouldUseKnownImports() {
// These are commonly used libraries that depend on Well Known Types.
// They depend on the generated versions of these protos to avoid conflicts.
// However, since protoc-gen-go depends on these libraries, we generate
// its rules in disable_global mode (to avoid cyclic dependency), so the
// "go_default_library" versions of these libraries depend on the
// pre-generated versions of the proto libraries.
switch imp.Imp {
case "github.com/golang/protobuf/proto":
return []resolve.FindResult{{Label: label.New("com_github_golang_protobuf", "proto", "go_default_library")}}
case "github.com/golang/protobuf/jsonpb":
return []resolve.FindResult{{Label: label.New("com_github_golang_protobuf", "jsonpb", "go_default_library_gen")}}
case "github.com/golang/protobuf/descriptor":
return []resolve.FindResult{{Label: label.New("com_github_golang_protobuf", "descriptor", "go_default_library_gen")}}
case "github.com/golang/protobuf/ptypes":
return []resolve.FindResult{{Label: label.New("com_github_golang_protobuf", "ptypes", "go_default_library_gen")}}
case "github.com/golang/protobuf/protoc-gen-go/generator":
return []resolve.FindResult{{Label: label.New("com_github_golang_protobuf", "protoc-gen-go/generator", "go_default_library_gen")}}
case "google.golang.org/grpc":
return []resolve.FindResult{{Label: label.New("org_golang_google_grpc", "", "go_default_library")}}
}
if l, ok := knownGoProtoImports[imp.Imp]; ok {
return []resolve.FindResult{{Label: l}}
}
}
return nil
}
7 changes: 0 additions & 7 deletions language/proto/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,13 +465,6 @@ func TestCrossResolve(t *testing.T) {
lang: "go",
want: nil,
},
{
desc: "go known import",
protoMode: DefaultMode,
imp: resolve.ImportSpec{Lang: "go", Imp: "github.com/golang/protobuf/proto"},
lang: "go",
want: []resolve.FindResult{{Label: label.New("com_github_golang_protobuf", "proto", "go_default_library")}},
},
{
desc: "go unknown import",
protoMode: DefaultMode,
Expand Down
1 change: 1 addition & 0 deletions tests/bcr/go_mod/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ bazel_dep(name = "circl", version = "1.3.7")

go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps.config(
debug_mode = True,
go_env = {
"GOPRIVATE": "example.com/*",
},
Expand Down

0 comments on commit 0fbf13d

Please sign in to comment.