diff --git a/spec/compiler/crystal_path/crystal_path_spec.cr b/spec/compiler/crystal_path/crystal_path_spec.cr index c7a69aa7fd90..75bbf6f65411 100644 --- a/spec/compiler/crystal_path/crystal_path_spec.cr +++ b/spec/compiler/crystal_path/crystal_path_spec.cr @@ -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 diff --git a/src/compiler/crystal/crystal_path.cr b/src/compiler/crystal/crystal_path.cr index 5be401414150..52e3ff9670e8 100644 --- a/src/compiler/crystal/crystal_path.cr +++ b/src/compiler/crystal/crystal_path.cr @@ -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