diff options
author | 2022-08-29 13:01:40 -0700 | |
---|---|---|
committer | 2022-08-29 13:02:02 -0700 | |
commit | 9d8fb814130a5af6206233eb5c08d974b75148ac (patch) | |
tree | 70db9be9be3405111a07f21b610b72fbda1e1ec4 | |
parent | 34c0f773195eef9c7f62131febee40c267b1ec49 (diff) | |
download | bun-9d8fb814130a5af6206233eb5c08d974b75148ac.tar.gz bun-9d8fb814130a5af6206233eb5c08d974b75148ac.tar.zst bun-9d8fb814130a5af6206233eb5c08d974b75148ac.zip |
Improve error when FFI fails to dlopen()
-rw-r--r-- | src/bun.js/api/ffi.zig | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/src/bun.js/api/ffi.zig b/src/bun.js/api/ffi.zig index 8d4667bbe..0f8bfe22e 100644 --- a/src/bun.js/api/ffi.zig +++ b/src/bun.js/api/ffi.zig @@ -292,8 +292,21 @@ pub const FFI = struct { return JSC.toInvalidArguments("Expected at least one symbol", .{}, global.ref()); } - var dylib = std.DynLib.open(name) catch { - return JSC.toInvalidArguments("Failed to find or open library", .{}, global.ref()); + var dylib: std.DynLib = brk: { + // First try using the name directly + break :brk std.DynLib.open(name) catch { + const backup_name = Fs.FileSystem.instance.abs(&[1]string{name}); + // if that fails, try resolving the filepath relative to the current working directory + break :brk std.DynLib.open(backup_name) catch { + // Then, if that fails, report an error. + const system_error = JSC.SystemError{ + .code = ZigString.init(@tagName(JSC.Node.ErrorCode.ERR_DLOPEN_FAILED)), + .message = ZigString.init("Failed to open library. This is usually caused by a missing library or an invalid library path."), + .syscall = ZigString.init("dlopen"), + }; + return system_error.toErrorInstance(global); + }; + }; }; var obj = JSC.JSValue.c(JSC.C.JSObjectMake(global.ref(), null, null)); |