Skip to content

Commit

Permalink
Always include lib directory in the CRYSTAL_PATH (#9315)
Browse files Browse the repository at this point in the history
  • Loading branch information
waj authored May 19, 2020
1 parent 28821fc commit ea80f4c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 23 deletions.
15 changes: 15 additions & 0 deletions spec/compiler/crystal_path/crystal_path_spec.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "../../spec_helper"
require "../../support/env"

private def assert_finds(search, results, relative_to = nil, path = __DIR__, file = __FILE__, line = __LINE__)
it "finds #{search.inspect}", file, line do
Expand Down Expand Up @@ -114,4 +115,18 @@ describe Crystal::CrystalPath do

ex.message.not_nil!.should_not contain "If you're trying to require a shard"
end

it "includes 'lib' by default" do
with_env("CRYSTAL_PATH": nil) do
crystal_path = Crystal::CrystalPath.new
crystal_path.entries[0].should eq("lib")
end
end

it "overrides path with environment variable" do
with_env("CRYSTAL_PATH": "foo:bar") do
crystal_path = Crystal::CrystalPath.new
crystal_path.entries.should eq(%w(foo bar))
end
end
end
18 changes: 1 addition & 17 deletions spec/std/log/env_config_spec.cr
Original file line number Diff line number Diff line change
@@ -1,28 +1,12 @@
require "spec"
require "log"
require "log/spec"
require "../../support/env"

private def s(value : Log::Severity)
value
end

private def with_env(**values)
old_values = {} of String => String?
begin
values.each do |key, value|
key = key.to_s
old_values[key] = ENV[key]?
ENV[key] = value
end

yield
ensure
old_values.each do |key, old_value|
ENV[key] = old_value
end
end
end

describe "Log.setup_from_env" do
after_all do
# Setup logging in specs (again) since these specs perform Log.setup
Expand Down
16 changes: 16 additions & 0 deletions spec/support/env.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def with_env(**values)
old_values = {} of String => String?
begin
values.each do |key, value|
key = key.to_s
old_values[key] = ENV[key]?
ENV[key] = value
end

yield
ensure
old_values.each do |key, old_value|
ENV[key] = old_value
end
end
end
20 changes: 14 additions & 6 deletions src/compiler/crystal/crystal_path.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,32 @@ module Crystal
class Error < LocationlessException
end

private DEFAULT_LIB_PATH = "lib"

def self.default_path
ENV["CRYSTAL_PATH"]? || Crystal::Config.path
ENV["CRYSTAL_PATH"]? || begin
if Crystal::Config.path.split(Process::PATH_DELIMITER).includes?(DEFAULT_LIB_PATH)
Crystal::Config.path
else
{DEFAULT_LIB_PATH, Crystal::Config.path}.join(Process::PATH_DELIMITER)
end
end
end

@crystal_path : Array(String)
property entries : Array(String)

def initialize(path = CrystalPath.default_path, codegen_target = Config.host_target)
@crystal_path = path.split(Process::PATH_DELIMITER).reject &.empty?
@entries = path.split(Process::PATH_DELIMITER).reject &.empty?
add_target_path(codegen_target)
end

private def add_target_path(codegen_target)
target = "#{codegen_target.architecture}-#{codegen_target.os_name}"

@crystal_path.each do |path|
@entries.each do |path|
path = File.join(path, "lib_c", target)
if Dir.exists?(path)
@crystal_path << path unless @crystal_path.includes?(path)
@entries << path unless @entries.includes?(path)
return
end
end
Expand Down Expand Up @@ -144,7 +152,7 @@ module Crystal
end

private def find_in_crystal_path(filename)
@crystal_path.each do |path|
@entries.each do |path|
required = find_in_path_relative_to_dir(filename, path)
return required if required
end
Expand Down

0 comments on commit ea80f4c

Please sign in to comment.