Skip to content

Commit 4277af3

Browse files
authored
Merge pull request #2982 from maennchen/jm/optional_reject
Ignore optional Hex package dependencies
2 parents c31499a + c101db6 commit 4277af3

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

THANKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,4 @@ Manas Chaudhari
148148
Luís Rascão
149149
Marko Minđek
150150
LEdoian
151+
Jonatan Männchen

apps/rebar/src/rebar.hrl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
-define(LOCK_FILE, "rebar.lock").
3030
-define(DEFAULT_COMPILER_SOURCE_FORMAT, relative).
3131
-define(DEFAULT_COMPILER_ERROR_FORMAT, rich). % 'minimal' for default values as of 3.23.0 and earlier
32-
-define(PACKAGE_INDEX_VERSION, 6).
32+
-define(PACKAGE_INDEX_VERSION, 7).
3333
-define(PACKAGE_TABLE, package_index).
3434
-define(INDEX_FILE, "packages.idx").
3535
-define(HEX_AUTH_FILE, "hex.config").

apps/rebar/src/rebar_packages.erl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
,resolve_version/6]).
1212

1313
-ifdef(TEST).
14-
-export([new_package_table/0, find_highest_matching_/5]).
14+
-export([new_package_table/0, find_highest_matching_/5, parse_deps/1]).
1515
-endif.
1616

1717
-export_type([package/0]).
@@ -205,10 +205,16 @@ find_highest_matching_(Dep, DepVsn, #{name := Repo}, Table, State) when is_binar
205205
verify_table(State) ->
206206
ets:info(?PACKAGE_TABLE, named_table) =:= true orelse load_and_verify_version(State).
207207

208+
%% Filter out optional dependencies. Rebar3 does not support optional
209+
%% dependencies, so we simply ignore them. For full parity with Mix, we would
210+
%% need to check if the dependency is included elsewhere (even transitively)
211+
%% without the optional flag, and only exclude it if all references are
212+
%% optional.
208213
parse_deps(Deps) ->
209214
[{maps:get(app, D, Name), {pkg, Name, Constraint, undefined, undefined}}
210215
|| D=#{package := Name,
211-
requirement := Constraint} <- Deps].
216+
requirement := Constraint} <- Deps,
217+
not maps:get(optional, D, false)].
212218

213219
parse_checksum(<<Checksum:256/big-unsigned>>) ->
214220
list_to_binary(

apps/rebar/test/rebar_pkg_SUITE.erl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
all() -> [good_uncached, good_cached, badpkg, badhash_nocache,
1818
badindexchk, badhash_cache, bad_to_good, good_disconnect,
19-
bad_disconnect, pkgs_provider, find_highest_matching].
19+
bad_disconnect, pkgs_provider, find_highest_matching,
20+
parse_deps_ignores_optional].
2021

2122
init_per_suite(Config) ->
2223
application:start(meck),
@@ -250,6 +251,23 @@ find_highest_matching(_Config) ->
250251
#{name => <<"hexpm">>}, ?PACKAGE_TABLE, State),
251252
?assertEqual({{3,0,0},{[<<"rc">>,0],[]}}, Vsn3).
252253

254+
parse_deps_ignores_optional(_Config) ->
255+
%% Test that optional dependencies are filtered out
256+
Deps = [
257+
#{package => <<"req">>, requirement => <<"~> 1.0">>},
258+
#{package => <<"opt">>, requirement => <<"~> 2.0">>, optional => true},
259+
#{package => <<"exp">>, requirement => <<"~> 3.0">>, optional => false},
260+
#{package => <<"ali">>, requirement => <<"4.0">>, app => <<"alias">>},
261+
#{package => <<"ali_opt">>, requirement => <<"~> 5.0">>,
262+
app => <<"alias2">>, optional => true}
263+
],
264+
Result = rebar_packages:parse_deps(Deps),
265+
?assertEqual(3, length(Result)),
266+
?assertMatch([{<<"req">>, {pkg, <<"req">>, <<"~> 1.0">>, _, _}},
267+
{<<"exp">>, {pkg, <<"exp">>, <<"~> 3.0">>, _, _}},
268+
{<<"alias">>, {pkg, <<"ali">>, <<"4.0">>, _, _}}],
269+
Result).
270+
253271
%%%%%%%%%%%%%%%
254272
%%% Helpers %%%
255273
%%%%%%%%%%%%%%%

0 commit comments

Comments
 (0)