From fd6feec0b63b1d24c8f0430976979deb3fd99ce2 Mon Sep 17 00:00:00 2001 From: RX14 Date: Sat, 30 Dec 2017 21:13:57 +0000 Subject: [PATCH 1/3] Suggest possible solutions to failing requires --- spec/compiler/crystal_path/crystal_path_spec.cr | 16 ++++++++++++++++ src/compiler/crystal/crystal_path.cr | 16 ++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/spec/compiler/crystal_path/crystal_path_spec.cr b/spec/compiler/crystal_path/crystal_path_spec.cr index c7a69aa7fd90..66fec29654c6 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__) + expect_raises Exception, /Looks like 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 "Looks like 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..c1fe7fbff988 100644 --- a/src/compiler/crystal/crystal_path.cr +++ b/src/compiler/crystal/crystal_path.cr @@ -169,10 +169,22 @@ module Crystal 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}' relative to '#{relative_to}'" else - raise Error.new("can't find file '#{filename}'") + error = "can't find file '#{filename}'" end + + unless filename.starts_with? '.' + error = <<-NOTE + #{error} + + Looks like 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 From 565f34acd1950b1d6fe29914ca01590c8b51ca0a Mon Sep 17 00:00:00 2001 From: RX14 Date: Sun, 31 Dec 2017 12:37:35 +0000 Subject: [PATCH 2/3] fixup! Suggest possible solutions to failing requires --- src/compiler/crystal/crystal_path.cr | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/compiler/crystal/crystal_path.cr b/src/compiler/crystal/crystal_path.cr index c1fe7fbff988..52e3ff9670e8 100644 --- a/src/compiler/crystal/crystal_path.cr +++ b/src/compiler/crystal/crystal_path.cr @@ -168,17 +168,15 @@ module Crystal end private def cant_find_file(filename, relative_to) - if relative_to - error = "can't find file '#{filename}' relative to '#{relative_to}'" - else - error = "can't find file '#{filename}'" - end + error = "can't find file '#{filename}'" - unless filename.starts_with? '.' + if filename.starts_with? '.' + error += " relative to '#{relative_to}'" if relative_to + else error = <<-NOTE #{error} - Looks like you're trying to require a shard. + 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 From 3257fefca96335f5affd9eda78cc506068e9fb48 Mon Sep 17 00:00:00 2001 From: RX14 Date: Sun, 31 Dec 2017 20:50:24 +0000 Subject: [PATCH 3/3] fixup! Suggest possible solutions to failing requires --- spec/compiler/crystal_path/crystal_path_spec.cr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/compiler/crystal_path/crystal_path_spec.cr b/spec/compiler/crystal_path/crystal_path_spec.cr index 66fec29654c6..75bbf6f65411 100644 --- a/spec/compiler/crystal_path/crystal_path_spec.cr +++ b/spec/compiler/crystal_path/crystal_path_spec.cr @@ -98,7 +98,7 @@ describe Crystal::CrystalPath do it "prints an explanatory message for non-relative requires" do crystal_path = Crystal::CrystalPath.new(__DIR__) - expect_raises Exception, /Looks like you're trying to require a shard/ do + ex = expect_raises Exception, /If you're trying to require a shard/ do crystal_path.find "non_existent", relative_to: __DIR__ end end @@ -109,6 +109,6 @@ describe Crystal::CrystalPath do crystal_path.find "./non_existant", relative_to: __DIR__ end - ex.message.not_nil!.should_not contain "Looks like you're trying to require a shard" + ex.message.not_nil!.should_not contain "If you're trying to require a shard" end end