Skip to content

Commit a9a6e26

Browse files
committed
Improve rebar.config parsing
to avoid generating incorrect erlc_opts
1 parent b7e52e3 commit a9a6e26

File tree

9 files changed

+533
-10
lines changed

9 files changed

+533
-10
lines changed

gazelle/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
44
exports_files([
55
"dot_app_to_json.sh",
66
"erl_attrs_to_json.sh",
7+
"rebar_config_to_json.sh",
78
])
89

910
go_library(

gazelle/generate.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,10 @@ func importRebar(args language.GenerateArgs, erlangApp *ErlangApp) error {
164164
}
165165

166166
if rebarConfig.ErlOpts != nil {
167-
for _, o := range *rebarConfig.ErlOpts {
168-
erlangApp.ErlcOpts.Add("+" + o)
167+
for _, opt := range *rebarConfig.ErlOpts {
168+
if opt.Kind == "erlc" {
169+
erlangApp.ErlcOpts.Add("+" + opt.Value)
170+
}
169171
}
170172
}
171173

gazelle/rebar_config_parser.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,14 @@ func (p *rebarConfigParser) parseRebarConfig(configFilename string) (*rebarConfi
110110
return &metadata, nil
111111
}
112112

113+
type rebarConfigErlOpt struct {
114+
Kind string `json:"kind"`
115+
Value string `json:"value"`
116+
}
117+
113118
type rebarConfig struct {
114-
Deps []map[string]string `json:"deps"`
115-
ErlOpts *[]string `json:"erl_opts"`
119+
Deps []map[string]string `json:"deps"`
120+
ErlOpts *[]rebarConfigErlOpt `json:"erl_opts"`
116121
}
117122

118123
func (p *rebarConfigParser) parseRebarLock(lockFilename string) (*rebarLock, error) {

gazelle/rebar_config_to_json.sh

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
-export([main/1]).
77

8+
-ifdef(TEST).
9+
-export([parse/1]).
10+
-endif.
11+
812
main(Args) ->
913
case io:get_line("") of
1014
eof ->
@@ -27,21 +31,61 @@ parse_json_string("\"" ++ Tail) ->
2731
mapify_dep(Name) when is_atom(Name) ->
2832
#{name => Name,
2933
kind => hex};
30-
mapify_dep({Name, _, {git = Kind, Remote, Ref}}) ->
34+
mapify_dep({Name, Version}) when is_list(Version) ->
35+
#{name => Name,
36+
kind => hex,
37+
version => Version};
38+
mapify_dep({Name, Version, {git = Kind, Remote, Ref}}) ->
3139
#{name => Name,
3240
kind => Kind,
41+
version => Version,
3342
remote => Remote,
3443
ref => Ref};
35-
mapify_dep({Name, Version}) ->
44+
mapify_dep({Name, {git = Kind, Remote, Ref}}) ->
3645
#{name => Name,
37-
kind => hex,
38-
version => Version}.
46+
kind => Kind,
47+
remote => Remote,
48+
ref => Ref};
49+
mapify_dep({Name, {hg = Kind, Remote, Ref}}) ->
50+
#{name => Name,
51+
kind => Kind,
52+
remote => Remote,
53+
ref => Ref};
54+
%% legacy formats
55+
mapify_dep({Name, {git = Kind, Remote}}) ->
56+
#{name => Name,
57+
kind => Kind,
58+
remote => Remote};
59+
mapify_dep({Name, Version, {git = Kind, Remote}}) ->
60+
#{name => Name,
61+
kind => Kind,
62+
version => Version,
63+
remote => Remote};
64+
mapify_dep({Name, Version, {git = Kind, Remote, Ref}, [raw]}) ->
65+
#{name => Name,
66+
kind => Kind,
67+
version => Version,
68+
remote => Remote,
69+
ref => Ref}.
70+
71+
conformErlOpt(Opt) when is_atom(Opt)->
72+
#{kind => erlc,
73+
value => Opt};
74+
conformErlOpt({i, Include}) ->
75+
#{kind => include,
76+
value => Include};
77+
conformErlOpt({platform_define, _Platform, _Key}) ->
78+
#{kind => platform_define,
79+
value => ignored};
80+
conformErlOpt({platform_define, _Platform, _Key, _Value}) ->
81+
#{kind => platform_define,
82+
value => ignored}.
3983

4084
conformConfig(List) ->
4185
maps:map(
4286
fun
4387
(erl_opts, Opts) ->
44-
Opts;
88+
[conformErlOpt(O) || O <- Opts];
4589
(deps, Deps) ->
4690
[mapify_dep(D) || D <- Deps];
4791
(_, _) ->
File renamed without changes.

test/MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ erlang_package = use_extension(
6666

6767
erlang_package.hex_package(
6868
name = "thoas",
69-
build_file = "@//dot_app_to_json:BUILD.thoas",
69+
build_file = "@//:BUILD.thoas",
7070
sha256 = "442296847aca11db8d25180693d7ca3073d6d7179f66952f07b16415306513b6",
7171
version = "0.4.0",
7272
)
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
load(
2+
"@rules_erlang//:app_file.bzl",
3+
"app_file",
4+
)
5+
load(
6+
"@rules_erlang//:erlang_bytecode.bzl",
7+
"erlang_bytecode",
8+
)
9+
load(
10+
"@rules_erlang//:erlang_app_info.bzl",
11+
"erlang_app_info",
12+
)
13+
load(
14+
"@rules_erlang//:erlang_app.bzl",
15+
"DEFAULT_ERLC_OPTS",
16+
"DEFAULT_TEST_ERLC_OPTS",
17+
)
18+
load(
19+
"@rules_erlang//:xref2.bzl",
20+
"xref",
21+
)
22+
load(
23+
"@rules_erlang//:dialyze.bzl",
24+
"DEFAULT_PLT_APPS",
25+
"dialyze",
26+
"plt",
27+
)
28+
load(
29+
"@rules_erlang//:ct.bzl",
30+
"assert_suites2",
31+
"ct_suite",
32+
)
33+
34+
genrule(
35+
name = "src",
36+
srcs = ["@rules_erlang//gazelle:rebar_config_to_json.sh"],
37+
outs = ["src/rebar_config_to_json.erl"],
38+
cmd = """\
39+
echo "-module(rebar_config_to_json)." > $@
40+
tail -n +4 $< >> $@
41+
""",
42+
)
43+
44+
APP_NAME = "rebar_config_to_json"
45+
46+
APP_VERSION = "1.0.0"
47+
48+
erlang_bytecode(
49+
name = "beam_files",
50+
srcs = ["src/rebar_config_to_json.erl"],
51+
dest = "ebin",
52+
erlc_opts = DEFAULT_ERLC_OPTS,
53+
)
54+
55+
erlang_bytecode(
56+
name = "test_beam_files",
57+
testonly = True,
58+
srcs = ["src/rebar_config_to_json.erl"],
59+
dest = "test",
60+
erlc_opts = DEFAULT_TEST_ERLC_OPTS + [
61+
"+nowarn_export_all",
62+
],
63+
)
64+
65+
app_file(
66+
name = "app_file",
67+
app_name = APP_NAME,
68+
app_version = APP_VERSION,
69+
)
70+
71+
erlang_app_info(
72+
name = "erlang_app",
73+
srcs = ["src/rebar_config_to_json.erl"],
74+
app = ":app_file",
75+
app_name = APP_NAME,
76+
beam = [":beam_files"],
77+
)
78+
79+
erlang_app_info(
80+
name = "test_erlang_app",
81+
testonly = True,
82+
srcs = ["src/rebar_config_to_json.erl"],
83+
app = ":app_file",
84+
app_name = APP_NAME,
85+
beam = [":test_beam_files"],
86+
)
87+
88+
xref()
89+
90+
plt(
91+
name = "base_plt",
92+
apps = DEFAULT_PLT_APPS,
93+
)
94+
95+
dialyze(
96+
plt = ":base_plt",
97+
)
98+
99+
ct_suite(
100+
name = "rebar_config_to_json_SUITE",
101+
size = "small",
102+
data = [
103+
# https://github.com/erlang/rebar3/blob/main/rebar.config.sample
104+
"test/rebar.config",
105+
],
106+
runtime_deps = [
107+
"@thoas//:erlang_app",
108+
],
109+
)
110+
111+
assert_suites2()

0 commit comments

Comments
 (0)