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

Warn on unknown dependency options #1027

Merged
merged 3 commits into from
May 20, 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
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
Copy link
Member

Choose a reason for hiding this comment

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

This comment 👍

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
Loading