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

Fix building requests from umbrella apps #1013

Merged
merged 3 commits into from
Apr 8, 2024
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
72 changes: 49 additions & 23 deletions lib/hex/mix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,22 @@ defmodule Hex.Mix do
in the original list of dependencies as they were likely filtered out
due to options like `:only`.
"""
def deps_to_requests(deps, overridden) do
apps = Enum.map(deps, & &1.app)

hex_deps_to_requests(deps, apps, overridden, _top_level? = true) ++
Enum.flat_map(deps, fn dep ->
if dep.scm != Hex.SCM and dep.deps != [] do
[
%{
repo: nil,
name: Atom.to_string(dep.app),
requirement: nil,
app: Atom.to_string(dep.app),
from: Path.relative_to_cwd(dep.from),
dependencies: hex_deps_to_requests(dep.deps, apps, overridden, _top_level? = false)
}
]
else
[]
end
end)
def deps_to_requests(all_deps, overridden) do
all_apps = Enum.map(all_deps, & &1.app)

hex_deps_to_requests(all_deps, all_apps, overridden) ++
non_hex_deps_to_requests(all_deps, all_deps, all_apps, overridden)
end

defp hex_deps_to_requests(deps, apps, overridden, top_level?) do
defp deps_to_requests(deps, all_deps, all_apps, overridden) do
hex_deps_to_requests(deps, all_apps, overridden) ++
non_hex_deps_to_requests(deps, all_deps, all_apps, overridden)
end

defp hex_deps_to_requests(deps, all_apps, overridden) do
Enum.flat_map(deps, fn dep ->
if dep.scm == Hex.SCM and dep.app in apps and (dep.top_level or dep.app not in overridden) and
dep.top_level == top_level? do
if dep.scm == Hex.SCM and dep.app in all_apps and
(dep.top_level or dep.app not in overridden) do
[
%{
repo: dep.opts[:repo],
Expand All @@ -68,6 +58,42 @@ defmodule Hex.Mix do
end)
end

defp non_hex_deps_to_requests(deps, all_deps, all_apps, overridden) do
Enum.flat_map(deps, fn dep ->
if has_non_hex_deps?(dep, all_apps) do
collect_non_hex_deps(dep, all_deps, all_apps, overridden)
else
[]
end
end)
end

defp has_non_hex_deps?(dep, all_apps) do
dep.scm != Hex.SCM and dep.deps != [] and dep.app in all_apps
end

defp collect_non_hex_deps(dep, all_deps, all_apps, overridden) do
sub_apps = Enum.map(dep.deps, & &1.app)
sub_deps = Enum.filter(dep.deps, &(&1.app in sub_apps))

dependencies = deps_to_requests(sub_deps, all_deps, all_apps, overridden)

if dependencies != [] do
[
%{
repo: nil,
name: Atom.to_string(dep.app),
requirement: nil,
app: Atom.to_string(dep.app),
from: Path.relative_to_cwd(dep.from),
dependencies: dependencies
}
]
else
[]
end
end

@doc """
Returns all top level dependencies.
"""
Expand Down
2 changes: 2 additions & 0 deletions lib/hex/remote_converger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ defmodule Hex.RemoteConverger do
* #{dependency.from} defined application :#{dependency.label}
""")
end

dependency
end)

verify_otp_app_names(dependencies, map)
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Hex.MixProject do
aliases: aliases(),
preferred_cli_env: ["deps.get": :test],
config_path: config_path(),
compilers: [:leex] ++ Mix.compilers(),
deps: deps(Mix.env()),
elixirc_options: elixirc_options(Mix.env()),
elixirc_paths: elixirc_paths(Mix.env())
Expand Down
19 changes: 19 additions & 0 deletions test/fixtures/umbrella/apps/my_app1/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Umbrella.MyApp1.Fixture.MixProject do
use Mix.Project

def project do
[
app: :my_app1,
version: "0.1.0",
build_path: "../../_build",
config_path: "../../config/config.exs",
deps_path: "../../deps",
lockfile: "../../mix.lock",
deps: deps()
]
end

defp deps do
[{:postgrex, ">= 0.0.0"}]
end
end
19 changes: 19 additions & 0 deletions test/fixtures/umbrella/apps/my_app2/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Umbrella.MyApp2.Fixture.MixProject do
use Mix.Project

def project do
[
app: :my_app2,
version: "0.1.0",
build_path: "../../_build",
config_path: "../../config/config.exs",
deps_path: "../../deps",
lockfile: "../../mix.lock",
deps: deps()
]
end

defp deps do
[{:my_app1, in_umbrella: true}]
end
end
19 changes: 19 additions & 0 deletions test/fixtures/umbrella/apps/my_app3/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Umbrella.MyApp3.Fixture.MixProject do
use Mix.Project

def project do
[
app: :my_app3,
version: "0.1.0",
build_path: "../../_build",
config_path: "../../config/config.exs",
deps_path: "../../deps",
lockfile: "../../mix.lock",
deps: deps()
]
end

defp deps do
[]
end
end
20 changes: 20 additions & 0 deletions test/fixtures/umbrella/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Umbrella.Fixture.MixProject do
use Mix.Project

def project do
[
apps_path: "apps",
version: "0.1.0",
deps: deps()
]
end

# Dependencies listed here are available only for this
# project and cannot be accessed from applications inside
# the apps folder.
#
# Run "mix help deps" for examples and options.
defp deps do
[]
end
end
22 changes: 22 additions & 0 deletions test/hex/mix_task_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1031,4 +1031,26 @@ defmodule Hex.MixTaskTest do
]}
end)
end

test "deps.get umbrella" do
in_fixture("umbrella", fn ->
Code.eval_file("mix.exs")
Mix.Task.run("deps.get")

assert_received {:mix_shell, :info, ["* Getting postgrex (Hex package)"]}
assert_received {:mix_shell, :info, ["* Getting ex_doc (Hex package)"]}

assert %{
ex_doc: {:hex, :ex_doc, "0.1.0", _, _, _, _, _},
postgrex: {:hex, :postgrex, "0.2.1", _, _, _, _, _}
} = Mix.Dep.Lock.read()
end)
after
purge([
Umbrella.Fixture.MixProject,
Umbrella.MyApp1.Fixture.MixProject,
Umbrella.MyApp2.Fixture.MixProject,
Umbrella.MyApp3.Fixture.MixProject
])
end
end
Loading