Skip to content

Commit

Permalink
Remove double .cr.cr extension in require path lookup (#13749)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored Aug 19, 2023
1 parent bfc7e20 commit cc0c109
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
28 changes: 25 additions & 3 deletions spec/compiler/crystal_path/crystal_path_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,33 @@ describe Crystal::CrystalPath do
it "foo.cr" do
assert_iterates_yielding [
"x/foo.cr",
"x/foo.cr/foo.cr.cr",
"x/foo.cr/src/foo.cr.cr",
"x/foo.cr/foo.cr",
"x/foo.cr/src/foo.cr",
], path.each_file_expansion("foo.cr", "x")
end

it "foo.cr/bar" do
assert_iterates_yielding [
"x/foo.cr/bar.cr",
"x/foo.cr/src/bar.cr",
"x/foo.cr/src/foo.cr/bar.cr",
"x/foo.cr/bar/bar.cr",
"x/foo.cr/src/bar/bar.cr",
"x/foo.cr/src/foo.cr/bar/bar.cr",
], path.each_file_expansion("foo.cr/bar", "x")
end

it "foo.cr/bar.cr" do
assert_iterates_yielding [
"x/foo.cr/bar.cr",
"x/foo.cr/src/bar.cr",
"x/foo.cr/src/foo.cr/bar.cr",
"x/foo.cr/bar.cr/bar.cr",
"x/foo.cr/src/bar.cr/bar.cr",
"x/foo.cr/src/foo.cr/bar.cr/bar.cr",
], path.each_file_expansion("foo.cr/bar.cr", "x")
end

it "foo" do
assert_iterates_yielding [
"x/foo.cr",
Expand All @@ -134,7 +156,7 @@ describe Crystal::CrystalPath do
it "./foo.cr" do
assert_iterates_yielding [
"x/./foo.cr",
"x/./foo.cr/foo.cr.cr",
"x/./foo.cr/foo.cr",
], path.each_file_expansion("./foo.cr", "x")
end

Expand Down
29 changes: 14 additions & 15 deletions src/compiler/crystal/crystal_path.cr
Original file line number Diff line number Diff line change
Expand Up @@ -138,34 +138,33 @@ module Crystal

if !filename_is_relative && shard_path
shard_src = "#{relative_to}/#{shard_name}/src"
shard_path_stem = shard_path.rchop(".cr")

# If it's "foo/bar/baz", check if "foo/src/bar/baz.cr" exists (for a shard, non-namespaced structure)
yield "#{shard_src}/#{shard_path}.cr"
yield "#{shard_src}/#{shard_path_stem}.cr"

# Then check if "foo/src/foo/bar/baz.cr" exists (for a shard, namespaced structure)
yield "#{shard_src}/#{shard_name}/#{shard_path}.cr"
yield "#{shard_src}/#{shard_name}/#{shard_path_stem}.cr"

# If it's "foo/bar/baz", check if "foo/bar/baz/baz.cr" exists (std, nested)
basename = File.basename(relative_filename)
basename = File.basename(relative_filename, ".cr")
yield "#{relative_filename}/#{basename}.cr"

# If it's "foo/bar/baz", check if "foo/src/foo/bar/baz/baz.cr" exists (shard, non-namespaced, nested)
yield "#{shard_src}/#{shard_path}/#{shard_path}.cr"
yield "#{shard_src}/#{shard_path}/#{shard_path_stem}.cr"

# If it's "foo/bar/baz", check if "foo/src/foo/bar/baz/baz.cr" exists (shard, namespaced, nested)
yield "#{shard_src}/#{shard_name}/#{shard_path}/#{shard_path}.cr"

return nil
end

basename = File.basename(relative_filename)
yield "#{shard_src}/#{shard_name}/#{shard_path}/#{shard_path_stem}.cr"
else
basename = File.basename(relative_filename, ".cr")

# If it's "foo", check if "foo/foo.cr" exists (for the std, nested)
yield "#{relative_filename}/#{basename}.cr"
# If it's "foo", check if "foo/foo.cr" exists (for the std, nested)
yield "#{relative_filename}/#{basename}.cr"

unless filename_is_relative
# If it's "foo", check if "foo/src/foo.cr" exists (for a shard)
yield "#{relative_filename}/src/#{basename}.cr"
unless filename_is_relative
# If it's "foo", check if "foo/src/foo.cr" exists (for a shard)
yield "#{relative_filename}/src/#{basename}.cr"
end
end
end

Expand Down

0 comments on commit cc0c109

Please sign in to comment.