Skip to content

Commit e270217

Browse files
authored
Merge pull request #195 from rabbitmq/erlang_app_path_ignore-directive
erlang_app_path_ignore directive
2 parents ac79f70 + 014fc69 commit e270217

File tree

6 files changed

+115
-0
lines changed

6 files changed

+115
-0
lines changed

gazelle/README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,84 @@ gazelle(
8787
```
8888

8989
Then run `bazel run //:gazelle`
90+
91+
## Directives
92+
93+
- `erlang_resolve`
94+
95+
Example: `# gazelle:erlang_resolve rabbit_common @rabbitmq-server//deps/rabbit_common:erlang_app`
96+
97+
Purpose: Provides explicit resolution from an erlang application name to it's label
98+
99+
- `erlang_module_source_lib`
100+
101+
Example: `# gazelle:erlang_module_source_lib Elixir.RabbitMQ.CLI.CommandBehaviour:rabbitmq_cli`
102+
103+
Purpose: Indicates which erlang application contains a given module, irrespective of `moduleindex.yaml`
104+
105+
- `erlang_exclude_when_rule_of_kind_exists`
106+
107+
Purpose: Tells the erlang extension for gazelle to skip rule generation for a given directory, if a BUILD file already exists and contains a rule of this type
108+
109+
- `erlang_generate_beam_files_macro`
110+
111+
Example: `# gazelle:erlang_generate_beam_files_macro`
112+
113+
Purpose: Tells the erlang extension to generate `app.bzl` and define all of the rules for compiling beam files within it. This allows for a much more compact BUILD file
114+
115+
- `erlang_generate_fewer_bytecode_rules`
116+
117+
Purpose: Tells the erlang extension to generate rules that compile byte code in up to 3 phases (parse transforms, behaviours, the rest) instead of generating a rule per beam file (the default). This can provide a speed boost for some projects, such as rabbitmq-server, which is an umbrella project with many sources and applications.
118+
119+
- `erlang_always_generate_test_beam_files`
120+
121+
Purpose: Tells the erlang extention to generate the rules that compile source for test, even if the application contains no test suites
122+
123+
- `erlang_skip_rules`
124+
125+
Example: `# gazelle:erlang_skip_rules assert_suites2,xref,plt,dialyze
126+
`
127+
128+
Purpose: Tells the erlang extension not to generate rules of these types
129+
130+
- `erlang_apps_dirs`
131+
132+
Example: `# gazelle:erlang_apps_dirs deps`
133+
134+
Purpose: Tells the erlang extension too look in additional directories within the project in addition to `apps` for local applications. Only useful for umbrella repos.
135+
136+
- `erlang_app_testonly`
137+
138+
Purpose: Sets `testonly = True` on the rules generated. Useful for marking a test dependency as something that should not be shipped.
139+
140+
- `erlang_app_dep`
141+
142+
Purpose: An erlang application name to add as a depencency, in addition to the detected deps
143+
144+
- `erlang_app_dep_ignore`
145+
146+
Purpose: Ignores a detected dependency entirely, both for compilation and runtime
147+
148+
- `erlang_app_dep_exclude`
149+
150+
Example: `# gazelle:erlang_app_dep_exclude rabbitmq_cli`
151+
152+
Purpose: Excludes a detected dependency from the generated `.app` file for an app, useful for optional dependencies
153+
154+
- `erlang_app_path_ignore`
155+
156+
Purpose: Exclude files matching the pattern from an `erlang_app` rule and its sources
157+
158+
- `erlang_app_extra_app`
159+
160+
Purpose: Inject an additional application into the `.app` file, under the `applications` key
161+
162+
- `erlang_no_tests`
163+
164+
Purpose: Don't generate rules associated with tests. Useful for dependencies
165+
166+
- `erlang_erlc_opt`
167+
168+
Example: `# gazelle:erlang_erlc_opt -DBUILD_WITHOUT_QUIC`
169+
170+
Purpose: Adds additional values to the `erlc_opts` rule. Useful as it applies when parsing sources to determine dependencies

gazelle/configure.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const (
2828
erlangAppDepDirective = "erlang_app_dep"
2929
erlangAppDepIgnoreDirective = "erlang_app_dep_ignore"
3030
erlangAppDepBuildOnlyDirective = "erlang_app_dep_exclude"
31+
erlangAppPathIgnoreDirective = "erlang_app_path_ignore"
3132
erlangAppExtraAppDirective = "erlang_app_extra_app"
3233
erlangNoTestsDirective = "erlang_no_tests"
3334
erlangErlcOptDirective = "erlang_erlc_opt"
@@ -102,6 +103,7 @@ type ErlangConfig struct {
102103
ExcludeWhenRuleOfKindExists mutable_set.MutableSet[string]
103104
IgnoredDeps mutable_set.MutableSet[string]
104105
ExcludedDeps mutable_set.MutableSet[string]
106+
IgnoredPaths mutable_set.MutableSet[string]
105107
GenerateBeamFilesMacro bool
106108
GenerateFewerBytecodeRules bool
107109
GenerateTestBeamUnconditionally bool
@@ -129,6 +131,7 @@ func (erlang *Configurer) defaultErlangConfig(globalConfig *ErlangGlobalConfig)
129131
ExcludeWhenRuleOfKindExists: mutable_set.New[string](),
130132
IgnoredDeps: defaultIgnoredDeps,
131133
ExcludedDeps: mutable_set.New[string](),
134+
IgnoredPaths: mutable_set.New[string](),
132135
GenerateBeamFilesMacro: false,
133136
GenerateFewerBytecodeRules: erlang.compact,
134137
GenerateTestBeamUnconditionally: false,
@@ -163,6 +166,7 @@ func erlangConfigForRel(c *config.Config, rel string) *ErlangConfig {
163166
ExcludeWhenRuleOfKindExists: mutable_set.Copy(parentConfig.ExcludeWhenRuleOfKindExists),
164167
IgnoredDeps: mutable_set.Copy(parentConfig.IgnoredDeps),
165168
ExcludedDeps: mutable_set.Copy(parentConfig.ExcludedDeps),
169+
IgnoredPaths: mutable_set.Copy(parentConfig.IgnoredPaths),
166170
GenerateBeamFilesMacro: parentConfig.GenerateBeamFilesMacro,
167171
GenerateFewerBytecodeRules: parentConfig.GenerateFewerBytecodeRules,
168172
GenerateTestBeamUnconditionally: parentConfig.GenerateTestBeamUnconditionally,
@@ -233,6 +237,7 @@ func (erlang *Configurer) KnownDirectives() []string {
233237
erlangAppDepDirective,
234238
erlangAppDepIgnoreDirective,
235239
erlangAppDepBuildOnlyDirective,
240+
erlangAppPathIgnoreDirective,
236241
erlangAppExtraAppDirective,
237242
erlangNoTestsDirective,
238243
erlangErlcOptDirective,
@@ -309,6 +314,13 @@ func (erlang *Configurer) Configure(c *config.Config, rel string, f *rule.File)
309314
erlangConfig.IgnoredDeps.Add(d.Value)
310315
case erlangAppDepBuildOnlyDirective:
311316
erlangConfig.ExcludedDeps.Add(d.Value)
317+
case erlangAppPathIgnoreDirective:
318+
p := path.Join(rel, d.Value)
319+
if err := checkPathMatchPattern(p); err != nil {
320+
Log(c, " the exclusion pattern is not valid", p, ":", err)
321+
continue
322+
}
323+
erlangConfig.IgnoredPaths.Add(p)
312324
case erlangAppExtraAppDirective:
313325
erlangConfig.ExtraApps.Add(d.Value)
314326
case erlangNoTestsDirective:

gazelle/generate.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/bazelbuild/bazel-gazelle/merger"
1313
"github.com/bazelbuild/bazel-gazelle/rule"
1414
"github.com/bazelbuild/buildtools/build"
15+
"github.com/bmatcuk/doublestar/v4"
1516
"github.com/rabbitmq/rules_erlang/gazelle/fetch"
1617
"github.com/rabbitmq/rules_erlang/gazelle/slices"
1718
)
@@ -192,6 +193,22 @@ func importBareErlang(args language.GenerateArgs, erlangApp *ErlangAppBuilder) e
192193
if err != nil {
193194
return err
194195
}
196+
197+
rootRel, err := filepath.Rel(args.Config.RepoRoot, path)
198+
if err != nil {
199+
return err
200+
}
201+
erlangConfig := erlangConfigForRel(args.Config, rootRel)
202+
for _, pattern := range erlangConfig.IgnoredPaths.Values(strings.Compare) {
203+
if m, _ := doublestar.PathMatch(pattern, rootRel); m {
204+
if info.IsDir() {
205+
return filepath.SkipDir
206+
} else {
207+
return nil
208+
}
209+
}
210+
}
211+
195212
if info.IsDir() {
196213
if slices.Contains(ignoredDirs, filepath.Base(path)) {
197214
return filepath.SkipDir
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
# gazelle:erlang_generate_fewer_bytecode_rules
2+
3+
# gazelle:erlang_app_path_ignore test/foo.*

test/gazelle/testdata/basic_rebar_compact_rules/BUILD.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ load("@rules_erlang//:ct.bzl", "assert_suites2")
77

88
# gazelle:erlang_generate_fewer_bytecode_rules
99

10+
# gazelle:erlang_app_path_ignore test/foo.*
11+
1012
erlc_opts(
1113
name = "erlc_opts",
1214
values = select({
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-module(foo).

0 commit comments

Comments
 (0)