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

resolve: Add a resolve_regexp directive #1542

Merged
merged 3 commits into from
Jun 22, 2023
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
25 changes: 25 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,31 @@ The following directives are recognized:
| # gazelle:resolve proto go foo/foo.proto //foo:foo_go_proto |
| |
+---------------------------------------------------+----------------------------------------+
| :direc:`# gazelle:resolve_regexp ...` | n/a |
+---------------------------------------------------+----------------------------------------+
| Specifies an explicit mapping from an import regex to a label for |
| `Dependency resolution`_. The format for a resolve directive is: |
| |
| ``# gazelle:resolve source-lang import-lang import-string-regex label`` |
| |
| * ``source-lang`` is the language of the source code being imported. |
| * ``import-lang`` is the language importing the library. This is usually |
| the same as ``source-lang`` but may differ with generated code. For |
| example, when resolving dependencies for a ``go_proto_library``, |
| ``source-lang`` would be ``"proto"`` and ``import-lang`` would be ``"go"``. |
| ``import-lang`` may be omitted if it is the same as ``source-lang``. |
| * ``import-string-regex`` is the regex applied to the import in the source code. |
| If it matches, that import will be resolved to the label specified below.
| * ``label`` is the Bazel label that Gazelle should write in ``deps``. |
| |
| For example: |
| |
| .. code:: bzl |
| |
| # gazelle:resolve_regexp go example.com/.* //foo:go_default_library |
| # gazelle:resolve_regexp proto go foo/.*\.proto //foo:foo_go_proto |
| |
+---------------------------------------------------+----------------------------------------+
| :direc:`# gazelle:go_visibility label` | n/a |
+---------------------------------------------------+----------------------------------------+
| By default, internal packages are only visible to its siblings. This directive adds a label|
Expand Down
40 changes: 40 additions & 0 deletions language/go/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,46 @@ go_proto_library(
importpath = "test",
deps = ["//:good"],
)
`,
}, {
desc: "proto_override_regexp",
index: []buildFile{{
rel: "",
content: `
# gazelle:resolve_regexp proto go google/rpc/.*\.proto :good

proto_library(
name = "bad_proto",
srcs = ["google/rpc/status.proto"],
)

go_proto_library(
name = "bad_go_proto",
proto = ":bad_proto",
importpath = "bad",
)
`,
}},
old: buildFile{
rel: "test",
content: `
go_proto_library(
name = "dep_go_proto",
importpath = "test",
_imports = [
"google/rpc/status.proto",
"google/rpc/status1.proto",
"google/rpc/status2.proto",
],
)
`,
},
want: `
go_proto_library(
name = "dep_go_proto",
importpath = "test",
deps = ["//:good"],
)
`,
}, {
desc: "proto_index",
Expand Down
65 changes: 62 additions & 3 deletions resolve/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package resolve
import (
"flag"
"log"
"regexp"
"strings"

"github.com/bazelbuild/bazel-gazelle/config"
Expand All @@ -37,6 +38,13 @@ func FindRuleWithOverride(c *config.Config, imp ImportSpec, lang string) (label.
return o.dep, true
}
}
for i := len(rc.regexpOverrides) - 1; i >= 0; i-- {
o := rc.regexpOverrides[i]
if o.matches(imp, lang) {
return o.dep, true
}
}

return label.NoLabel, false
}

Expand All @@ -52,8 +60,22 @@ func (o overrideSpec) matches(imp ImportSpec, lang string) bool {
(o.lang == "" || o.lang == lang)
}

type regexpOverrideSpec struct {
ImpLang string
ImpRegex *regexp.Regexp
lang string
dep label.Label
}

func (o regexpOverrideSpec) matches(imp ImportSpec, lang string) bool {
return imp.Lang == o.ImpLang &&
o.ImpRegex.MatchString(imp.Imp) &&
(o.lang == "" || o.lang == lang)
}

type resolveConfig struct {
overrides []overrideSpec
overrides []overrideSpec
regexpOverrides []regexpOverrideSpec
}

const resolveName = "_resolve"
Expand All @@ -71,13 +93,14 @@ func (*Configurer) RegisterFlags(fs *flag.FlagSet, cmd string, c *config.Config)
func (*Configurer) CheckFlags(fs *flag.FlagSet, c *config.Config) error { return nil }

func (*Configurer) KnownDirectives() []string {
return []string{"resolve"}
return []string{"resolve", "resolve_regexp"}
}

func (*Configurer) Configure(c *config.Config, rel string, f *rule.File) {
rc := getResolveConfig(c)
rcCopy := &resolveConfig{
overrides: rc.overrides[:len(rc.overrides):len(rc.overrides)],
overrides: rc.overrides[:len(rc.overrides):len(rc.overrides)],
regexpOverrides: rc.regexpOverrides[:len(rc.regexpOverrides):len(rc.regexpOverrides)],
}

if f != nil {
Expand Down Expand Up @@ -107,6 +130,42 @@ func (*Configurer) Configure(c *config.Config, rel string, f *rule.File) {
}
o.dep = o.dep.Abs("", rel)
rcCopy.overrides = append(rcCopy.overrides, o)
} else if d.Key == "resolve_regexp" {
parts := strings.Fields(d.Value)
o := regexpOverrideSpec{}
var lbl string
if len(parts) == 3 {
o.ImpLang = parts[0]
var err error
o.ImpRegex, err = regexp.Compile(parts[1])
if err != nil {
log.Printf("gazelle:resolve_exp %s: %v", d.Value, err)
continue
}
lbl = parts[2]
} else if len(parts) == 4 {
o.ImpLang = parts[0]
o.lang = parts[1]
var err error
o.ImpRegex, err = regexp.Compile(parts[2])
if err != nil {
log.Printf("gazelle:resolve_exp %s: %v", d.Value, err)
continue
}

lbl = parts[3]
} else {
log.Printf("could not parse directive: %s\n\texpected gazelle:resolve_regexp source-language [import-language] import-string-regex label", d.Value)
continue
}
var err error
o.dep, err = label.Parse(lbl)
if err != nil {
log.Printf("gazelle:resolve_regexp %s: %v", d.Value, err)
continue
}
o.dep = o.dep.Abs("", rel)
rcCopy.regexpOverrides = append(rcCopy.regexpOverrides, o)
}
}
}
Expand Down