From 02862a48405ae84ca48e9b511a58d8b74f0708e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Meadows-J=C3=B6nsson?= Date: Wed, 22 May 2024 17:27:16 +0200 Subject: [PATCH] Only filter non Hex overrides from requests (#1030) Closes #1029. --- lib/hex/mix.ex | 19 ++++--- lib/hex/remote_converger.ex | 2 +- test/fixtures/ecto_override/mix.exs | 11 ++++ .../umbrella_override/apps/my_app1/mix.exs | 19 +++++++ test/fixtures/umbrella_override/mix.exs | 20 ++++++++ test/hex/mix_task_test.exs | 50 +++++++++++++++++++ 6 files changed, 114 insertions(+), 7 deletions(-) create mode 100644 test/fixtures/ecto_override/mix.exs create mode 100644 test/fixtures/umbrella_override/apps/my_app1/mix.exs create mode 100644 test/fixtures/umbrella_override/mix.exs diff --git a/lib/hex/mix.ex b/lib/hex/mix.ex index 9f88b66d..45e9f5fc 100644 --- a/lib/hex/mix.ex +++ b/lib/hex/mix.ex @@ -14,6 +14,16 @@ defmodule Hex.Mix do |> Enum.uniq() end + defp non_hex_overridden_deps(deps) do + for( + dep <- deps, + dep.opts[:override], + dep.scm != Hex.SCM, + do: dep.app + ) + |> Enum.uniq() + end + @doc """ Converts a list of dependencies to a requests to the resolver. Skips dependencies overriding with another SCM (but include dependencies @@ -26,7 +36,8 @@ 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(all_deps, overridden) do + def deps_to_requests(all_deps) do + overridden = non_hex_overridden_deps(all_deps) all_apps = Enum.map(all_deps, & &1.app) hex_deps_to_requests(all_deps, all_apps, overridden) ++ @@ -60,7 +71,7 @@ defmodule Hex.Mix do 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 + if dep.scm != Hex.SCM and dep.deps != [] and dep.app in all_apps do collect_non_hex_deps(dep, all_deps, all_apps, overridden) else [] @@ -68,10 +79,6 @@ defmodule Hex.Mix do 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)) diff --git a/lib/hex/remote_converger.ex b/lib/hex/remote_converger.ex index 3a300990..e8b2c2e5 100644 --- a/lib/hex/remote_converger.ex +++ b/lib/hex/remote_converger.ex @@ -33,7 +33,7 @@ defmodule Hex.RemoteConverger do old_lock = Mix.Dep.Lock.read() overridden = Hex.Mix.overridden_deps(deps) - requests = Hex.Mix.deps_to_requests(deps, overridden) + requests = Hex.Mix.deps_to_requests(deps) [ Hex.Mix.packages_from_lock(lock), diff --git a/test/fixtures/ecto_override/mix.exs b/test/fixtures/ecto_override/mix.exs new file mode 100644 index 00000000..fc920593 --- /dev/null +++ b/test/fixtures/ecto_override/mix.exs @@ -0,0 +1,11 @@ +defmodule EctoOverride.Fixture.MixProject do + use Mix.Project + + def project do + [app: :ecto, version: "0.2.1", deps: deps()] + end + + defp deps do + [{:postgrex, "0.2.0", override: true}] + end +end diff --git a/test/fixtures/umbrella_override/apps/my_app1/mix.exs b/test/fixtures/umbrella_override/apps/my_app1/mix.exs new file mode 100644 index 00000000..e9d189bc --- /dev/null +++ b/test/fixtures/umbrella_override/apps/my_app1/mix.exs @@ -0,0 +1,19 @@ +defmodule UmbrellaOverride.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 + [{:ecto_override, path: HexTest.Case.fixture_path("ecto_override")}] + end +end diff --git a/test/fixtures/umbrella_override/mix.exs b/test/fixtures/umbrella_override/mix.exs new file mode 100644 index 00000000..ddf7540a --- /dev/null +++ b/test/fixtures/umbrella_override/mix.exs @@ -0,0 +1,20 @@ +defmodule UmbrellaOverride.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 diff --git a/test/hex/mix_task_test.exs b/test/hex/mix_task_test.exs index 96a20328..c6be7c49 100644 --- a/test/hex/mix_task_test.exs +++ b/test/hex/mix_task_test.exs @@ -295,6 +295,19 @@ defmodule Hex.MixTaskTest do end end + defmodule EctoOverrideParent do + def project do + [ + app: :ecto_override_parent, + version: "0.1.0", + consolidate_protocols: false, + deps: [ + {:ecto_override, path: HexTest.Case.fixture_path("ecto_override")} + ] + ] + end + end + defp reset_code_paths(fun) do path = :code.get_path() @@ -1082,4 +1095,41 @@ defmodule Hex.MixTaskTest do Umbrella.MyApp3.Fixture.MixProject ]) end + + test "deps.get with path override" do + Mix.Project.push(EctoOverrideParent) + + in_tmp(fn -> + Mix.Task.run("deps.get") + + assert_received {:mix_shell, :info, ["* Getting postgrex (Hex package)"]} + + assert %{ + postgrex: {:hex, :postgrex, "0.2.0", _, _, _, _, _} + } = Mix.Dep.Lock.read() + end) + after + purge([ + EctoOverride.Fixture.MixProject + ]) + end + + test "deps.get umbrella with path override" do + in_fixture("umbrella_override", fn -> + Code.eval_file("mix.exs") + Mix.Task.run("deps.get") + + assert_received {:mix_shell, :info, ["* Getting postgrex (Hex package)"]} + + assert %{ + postgrex: {:hex, :postgrex, "0.2.0", _, _, _, _, _} + } = Mix.Dep.Lock.read() + end) + after + purge([ + UmbrellaOverride.Fixture.MixProject, + UmbrellaOverride.MyApp1.Fixture.MixProject, + EctoOverride.Fixture.MixProject + ]) + end end