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

Improve Crystal::Loader errors #11860

Merged
Merged
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
21 changes: 20 additions & 1 deletion src/compiler/crystal/loader/unix.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@
class Crystal::Loader
alias Handle = Void*

class LoadError
def self.new_dl_error(message)
if char_pointer = LibC.dlerror
new(String.build do |io|
io << message
io << " ("
io.write_string(Slice.new(char_pointer, LibC.strlen(char_pointer)))
io << ")"
end)
Comment on lines +26 to +31
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using String.build here to avoid two intermediary string allocations. Maybe that's overkill for error handling...

else
new message
end
end
end

SHARED_LIBRARY_EXTENSION = {% if flag?(:darwin) %}
".dylib"
{% else %}
Expand Down Expand Up @@ -64,7 +79,11 @@ class Crystal::Loader
end

def load_file(path : String | ::Path) : Handle
load_file?(path) || raise LoadError.new String.new(LibC.dlerror)
load_file?(path) || raise LoadError.new_dl_error "cannot load #{path}"
end

def load_library(libname : String) : Handle
load_library?(libname) || raise LoadError.new_dl_error "cannot find -l#{libname}"
end

private def open_library(path : String)
Expand Down