Skip to content

Commit

Permalink
Warn on unknown dependency options (#1027)
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmach authored May 20, 2024
1 parent a0742b5 commit 385e63f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/hex/scm.ex
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ defmodule Hex.SCM do
end
end

# https://hexdocs.pm/mix/Mix.Tasks.Deps.html#module-dependency-definition-options
mix_keys = ~w[app env compile optional only targets override manager runtime system_env]a

# https://hex.pm/docs/usage#options
hex_keys = ~w[hex repo organization]a

internal_keys = ~w[dest lock build]a

@allowed_keys mix_keys ++ hex_keys ++ internal_keys

def update(opts) do
Registry.open()

Expand All @@ -125,6 +135,15 @@ defmodule Hex.SCM do
repo = opts[:repo] || "hexpm"
path = cache_path(repo, name, lock.version)

unknown_options = Keyword.keys(opts) -- @allowed_keys

if unknown_options != [] do
Hex.Shell.warn(
"#{name} is using unknown options: " <>
Enum.map_join(unknown_options, ", ", &inspect/1)
)
end

case Hex.Parallel.await(:hex_fetcher, {:tarball, repo, name, lock.version}, @fetch_timeout) do
{:ok, :cached} ->
Hex.Shell.debug(" Using locally cached package (#{path})")
Expand Down
29 changes: 29 additions & 0 deletions test/hex/mix_task_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,19 @@ defmodule Hex.MixTaskTest do
end
end

defmodule WithUnknownOptions do
def project do
[
app: :with_unknown_options,
version: "0.1.0",
consolidate_protocols: false,
deps: [
{:ex_doc, dir: "/bad", typo: true}
]
]
end
end

defmodule WithNonMatchingRequirement do
def project do
[
Expand Down Expand Up @@ -882,6 +895,22 @@ defmodule Hex.MixTaskTest do
end)
end

test "deps.get with unknown options" do
Mix.Project.push(WithUnknownOptions)

in_tmp(fn ->
Hex.State.put(:cache_home, File.cwd!())

Mix.Task.run("deps.get")

assert_received {:mix_shell, :info,
["\e[33mex_doc is missing its version requirement, use \">= 0.0.0\"" <> _]}

assert_received {:mix_shell, :info,
["\e[33mex_doc is using unknown options: :dir, :typo\e[0m"]}
end)
end

defp old_lock_tuple(lock_tuple) do
{elem(lock_tuple, 0), elem(lock_tuple, 1), elem(lock_tuple, 2), elem(lock_tuple, 3)}
end
Expand Down

0 comments on commit 385e63f

Please sign in to comment.