Skip to content

Commit 0aee080

Browse files
author
José Valim
committed
Merge pull request #1933 from Enders/elixirc_patterns
Elixirc: fail when one of the provided file pattern is missing
2 parents 9afc104 + cd43f61 commit 0aee080

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

lib/elixir/lib/kernel/cli.ex

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -339,21 +339,46 @@ defmodule Kernel.CLI do
339339
defp process_command({:compile, patterns}, config) do
340340
:filelib.ensure_dir(:filename.join(config.output, "."))
341341

342-
files = Enum.map patterns, &Path.wildcard(&1)
343-
files = Enum.uniq(Enum.concat(files))
344-
files = Enum.filter files, &:filelib.is_regular(&1)
345-
346-
if files != [] do
347-
wrapper fn ->
348-
Code.compiler_options(config.compiler_options)
349-
Kernel.ParallelCompiler.files_to_path(files, config.output,
350-
each_file: fn file -> if config.verbose_compile do IO.puts "Compiled #{file}" end end)
351-
end
352-
else
353-
{ :error, "--compile : No files matched patterns #{Enum.join(patterns, ",")}" }
342+
case match_regular_files(patterns) do
343+
{ :ok, [] } ->
344+
{ :error, "--compile : No files matched provided patterns." }
345+
{ :ok, files } ->
346+
wrapper fn ->
347+
Code.compiler_options(config.compiler_options)
348+
Kernel.ParallelCompiler.files_to_path(files, config.output,
349+
each_file: fn file -> if config.verbose_compile do IO.puts "Compiled #{file}" end end)
350+
end
351+
{ :missing, missing } ->
352+
{ :error, "--compile : No files matched pattern(s) #{Enum.join(missing, ",")}" }
354353
end
355354
end
356355

356+
defp match_regular_files(patterns) do
357+
358+
matched_files = Enum.map patterns, fn(pattern) ->
359+
case Path.wildcard(pattern) do
360+
[] -> {:missing, pattern }
361+
files -> {:ok, files }
362+
end
363+
end
364+
365+
files = Enum.filter_map matched_files,
366+
fn(match) -> elem(match, 0) == :ok end,
367+
&elem(&1, 1)
368+
369+
missing_patterns = Enum.filter_map matched_files,
370+
fn(match) -> elem(match, 0) == :missing end,
371+
&elem(&1, 1)
372+
373+
if missing_patterns == [] do
374+
files = Enum.uniq(Enum.concat(files))
375+
files = Enum.filter files, &:filelib.is_regular(&1)
376+
{ :ok, files }
377+
else
378+
{ :missing, Enum.uniq(missing_patterns) }
379+
end
380+
end
381+
357382
defp wrapper(fun) do
358383
fun.()
359384
:ok

lib/elixir/test/elixir/kernel/cli_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ defmodule Kernel.CLI.CompileTest do
9494
assert :string.str(output, bar) > 0, "expected bar.ex to miss module Foo"
9595
assert :string.str(output, 'elixir_compiler') == 0, "expected elixir_compiler to not be in output"
9696
end
97+
98+
test :compile_missing_patterns do
99+
fixture = fixture_path "compile_sample.ex"
100+
output = elixirc('#{fixture} non_existing.ex -o #{tmp_path}')
101+
assert :string.str(output, 'non_existing.ex') > 0, "expected non_existing.ex to be mentionned"
102+
assert :string.str(output, 'compile_sample.ex') == 0, "expected compile_sample.ex to not be mentionned"
103+
refute File.exists?(tmp_path("Elixir.CompileSample.beam")) , "expected the sample to not be compiled"
104+
end
97105
end
98106

99107
defmodule Kernel.CLI.ParallelCompilerTest do

0 commit comments

Comments
 (0)