Skip to content

Commit

Permalink
Suggest possible solutions to failing requires (#5487)
Browse files Browse the repository at this point in the history
* Suggest possible solutions to failing requires

* fixup! Suggest possible solutions to failing requires

* fixup! Suggest possible solutions to failing requires
  • Loading branch information
RX14 authored Apr 29, 2018
1 parent 382529f commit db0dc67
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
16 changes: 16 additions & 0 deletions spec/compiler/crystal_path/crystal_path_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,20 @@ describe Crystal::CrystalPath do
assert_doesnt_find "./crystal_path_spec", relative_to: "test_files/file_one.cr"
assert_doesnt_find "./crystal_path_spec.cr", relative_to: "test_files/file_one.cr"
assert_doesnt_find "../crystal_path/test_files/file_one"

it "prints an explanatory message for non-relative requires" do
crystal_path = Crystal::CrystalPath.new(__DIR__)
ex = expect_raises Exception, /If you're trying to require a shard/ do
crystal_path.find "non_existent", relative_to: __DIR__
end
end

it "doesn't print an explanatory message for relative requires" do
crystal_path = Crystal::CrystalPath.new(__DIR__)
ex = expect_raises Exception, /can't find file/ do
crystal_path.find "./non_existant", relative_to: __DIR__
end

ex.message.not_nil!.should_not contain "If you're trying to require a shard"
end
end
16 changes: 13 additions & 3 deletions src/compiler/crystal/crystal_path.cr
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,21 @@ module Crystal
end

private def cant_find_file(filename, relative_to)
if relative_to
raise Error.new("can't find file '#{filename}' relative to '#{relative_to}'")
error = "can't find file '#{filename}'"

if filename.starts_with? '.'
error += " relative to '#{relative_to}'" if relative_to
else
raise Error.new("can't find file '#{filename}'")
error = <<-NOTE
#{error}
If you're trying to require a shard:
- Did you remember to run `shards install`?
- Did you make sure you're running the compiler in the same directory as your shard.yml?
NOTE
end

raise Error.new(error)
end
end
end

0 comments on commit db0dc67

Please sign in to comment.