Skip to content

Commit 17b53b5

Browse files
committed
Address follow-up review of 7ae97db
- Filter unparseable versions in Linker.resolve_nested_version before version_gt? to prevent Version.parse! crash on lenient npm versions - Restore rescue _ in Exec.find_in_packages with explanatory comment (ErlangError was nearly as broad and less honest about intent) - Update Cache.ensure/5 docstring to document :missing_optional return - Fix dead || branch in FileSize.by_extension (Path.extname returns "" not nil — the || "(none)" could never trigger) - Remove dead {:error, :cycle} branch in DepSort.install_order (sort/1 always returns {:ok, order} for acyclic graphs)
1 parent 4e615b9 commit 17b53b5

5 files changed

Lines changed: 15 additions & 8 deletions

File tree

lib/npm/cache.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ defmodule NPM.Cache do
3131
Ensure a package version is in the cache.
3232
3333
Downloads and extracts the tarball if not already cached.
34-
Returns `{:ok, cache_path}` or `{:error, reason}`.
34+
Returns `{:ok, cache_path}`, `{:ok, :missing_optional}` when an
35+
optional package fails to fetch, or `{:error, reason}`.
3536
"""
3637
@spec ensure(String.t(), String.t(), String.t(), String.t(), keyword()) ::
3738
{:ok, String.t()} | {:ok, :missing_optional} | {:error, term()}

lib/npm/dep_sort.ex

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ defmodule NPM.DepSort do
2121
"""
2222
@spec install_order(map()) :: [String.t()]
2323
def install_order(adj) do
24-
case sort(adj) do
25-
{:ok, order} -> Enum.reverse(order)
26-
{:error, :cycle} -> Map.keys(adj) |> Enum.sort()
27-
end
24+
{:ok, order} = sort(adj)
25+
Enum.reverse(order)
2826
end
2927

3028
@doc """

lib/npm/exec.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ defmodule NPM.Exec do
9898
{:error, :not_found}
9999
end
100100
rescue
101-
ErlangError -> {:error, :not_found}
101+
# JSON decode, missing keys, or unexpected package.json shape
102+
_ -> {:error, :not_found}
102103
end
103104

104105
defp resolve_bin(%{"bin" => bin}, _command, nm_dir) when is_binary(bin) do

lib/npm/file_size.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ defmodule NPM.FileSize do
3737
@spec by_extension(String.t()) :: %{String.t() => non_neg_integer()}
3838
def by_extension(package_dir) do
3939
analyze(package_dir)
40-
|> Enum.group_by(fn %{path: path} -> Path.extname(path) || "(none)" end)
40+
|> Enum.group_by(fn %{path: path} ->
41+
case Path.extname(path) do
42+
"" -> "(none)"
43+
ext -> ext
44+
end
45+
end)
4146
|> Map.new(fn {ext, files} ->
4247
{ext, files |> Enum.map(& &1.size) |> Enum.sum()}
4348
end)

lib/npm/linker.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,9 @@ defmodule NPM.Linker do
251251
{:ok, packument} ->
252252
packument.versions
253253
|> Map.keys()
254-
|> Enum.filter(&version_matches?(&1, range))
254+
|> Enum.filter(fn v ->
255+
version_matches?(v, range) and match?({:ok, _}, Version.parse(v))
256+
end)
255257
|> Enum.sort(&version_gt?/2)
256258
|> List.first()
257259

0 commit comments

Comments
 (0)