Skip to content

Accordingly mark top level optional dependencies from child deps #8035

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

Merged
merged 3 commits into from
Aug 4, 2018
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
30 changes: 27 additions & 3 deletions lib/mix/lib/mix/dep.ex
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,42 @@ defmodule Mix.Dep do
end
end

# optional and runtime only matter at the top level.
# Any non-top level dependency that is optional and
# is still available means it has been fulfilled.
@child_keep_opts [:optional, :runtime]

defp load_and_cache(_config, top, top, env) do
converge(env: env)
end

defp load_and_cache(config, _top, bottom, _env) do
{_, deps} = Mix.ProjectStack.read_cache({:cached_deps, bottom})
app = Keyword.fetch!(config, :app)
top_level = for dep <- deps, dep.app == app, child <- dep.deps, do: child.app

seen = populate_seen(MapSet.new(), [app])
children = get_deps(deps, tl(Enum.uniq(get_children(deps, seen, [app]))))
Enum.map(children, &%{&1 | top_level: &1.app in top_level})

top_level =
for dep <- deps,
dep.app == app,
child <- dep.deps,
do: {child.app, Keyword.take(child.opts, @child_keep_opts)},
into: %{}

Enum.map(children, fn %{app: app} = dep ->
case top_level do
%{^app => child_opts} ->
opts =
dep.opts
|> Keyword.drop(@child_keep_opts)
|> Keyword.merge(child_opts)

%{dep | top_level: true, opts: opts}

%{} ->
%{dep | top_level: false}
end
end)
end

defp read_cached_deps(project, env) do
Expand Down
71 changes: 71 additions & 0 deletions lib/mix/test/mix/dep_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,77 @@ defmodule Mix.DepTest do
end)
end

test "nested deps with optional matching" do
Process.put(:custom_deps_git_repo_opts, optional: true)

# deps_repo brings git_repo but it is optional
deps = [
{:deps_repo, "0.1.0", path: "custom/deps_repo"},
{:git_repo, "0.1.0", git: MixTest.Case.fixture_path("git_repo")}
]

with_deps(deps, fn ->
in_fixture("deps_status", fn ->
File.mkdir_p!("custom/deps_repo/lib")

File.write!("custom/deps_repo/lib/a.ex", """
# Check that the child dependency is top_level and optional
[%Mix.Dep{app: :git_repo, top_level: true, opts: opts}] = Mix.Dep.cached()
true = Keyword.fetch!(opts, :optional)
""")

Mix.Tasks.Deps.Get.run([])
Mix.Tasks.Deps.Compile.run([])
end)
end)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great test 👏


test "nested deps with runtime override on parent" do
Process.put(:custom_deps_git_repo_opts, runtime: false)

deps = [
{:deps_repo, "0.1.0", path: "custom/deps_repo"},
{:git_repo, "0.1.0", git: MixTest.Case.fixture_path("git_repo")}
]

with_deps(deps, fn ->
in_fixture("deps_status", fn ->
File.mkdir_p!("custom/deps_repo/lib")

File.write!("custom/deps_repo/lib/a.ex", """
# Check that the child dependency is top_level and optional
[%Mix.Dep{app: :git_repo, top_level: true, opts: opts}] = Mix.Dep.cached()
false = Keyword.fetch!(opts, :runtime)
""")

Mix.Tasks.Deps.Get.run([])
Mix.Tasks.Deps.Compile.run([])
end)
end)
end

test "nested deps with runtime override on child" do
deps = [
{:deps_repo, "0.1.0", path: "custom/deps_repo"},
{:git_repo, "0.1.0", git: MixTest.Case.fixture_path("git_repo"), runtime: false}
]

with_deps(deps, fn ->
in_fixture("deps_status", fn ->
File.mkdir_p!("custom/deps_repo/lib")

File.write!("custom/deps_repo/lib/a.ex", """
# Check that the child dependency is top_level and optional
[%Mix.Dep{app: :git_repo, top_level: true, opts: opts}] = Mix.Dep.cached()
false = Keyword.has_key?(opts, :runtime)
""")

Mix.Tasks.Deps.Get.run([])
Mix.Tasks.Deps.Compile.run([])
end)
end)
end

test "nested deps with overrides" do
# deps_repo brings git_repo but it is overriden
deps = [
Expand Down