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

Suggest possible solutions to failing requires #5487

Merged
merged 3 commits into from
Apr 29, 2018
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
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