napi-rs icon indicating copy to clipboard operation
napi-rs copied to clipboard

Failed to open shared object file when using re2

Open naskya opened this issue 1 year ago • 1 comments

Description

I ran into an issue where re2 doesn't work because a shared object is not found. This is probably related to #1490.

Steps to reproduce

  1. Clone the package-template and confirm it works
$ git clone https://github.com/napi-rs/package-template.git
$ cd package-template
$ yarn && yarn build
$ yarn bench  # this works
  1. Use re2
$ vim Cargo.toml  # add re2 = "0.0.8" to [dependencies]
$ cat >> src/lib.rs <<EOF
#[napi]
pub fn foo() {
  let _ = "[a-z]".parse::<re2::RE2>().expect("");
}
EOF
$ yarn && yarn build
$ yarn bench
/home/naskya/workspace/package-template/index.js:232
    throw loadError
    ^

Error: libre2.so.11: cannot open shared object file: No such file or directory
    at Object.Module._extensions..node (node:internal/modules/cjs/loader:1475:18)
    at Module.load (node:internal/modules/cjs/loader:1212:32)
    at Function.Module._load (node:internal/modules/cjs/loader:1028:12)
    at Module.require (node:internal/modules/cjs/loader:1237:19)
    at require (node:internal/modules/helpers:176:18)
    at Object.<anonymous> (/home/naskya/workspace/package-template/index.js:176:31)
    at Module._compile (node:internal/modules/cjs/loader:1378:14)
    at Module._compile (/home/naskya/workspace/package-template/node_modules/pirates/lib/index.js:117:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1437:10)
    at Object.newLoader [as .js] (/home/naskya/workspace/package-template/node_modules/pirates/lib/index.js:121:7) {
  code: 'ERR_DLOPEN_FAILED'
}

Node.js v21.6.1

re2 has a static feature flag, but it produces another error (which doesn't occur without napi)

[dependencies]
napi = "2.15.0"
napi-derive = "2.15.0"
re2 = { version = "0.0.8", default-features = false, features = ["static"] }
$ yarn
$ yarn build
... (omitted)
   Compiling re2 v0.0.8
   Compiling re2-sys v0.0.6
   Compiling napi-package-template v0.1.0 (/home/naskya/workspace/package-template)
   Compiling napi-derive-backend v1.0.59
   Compiling ctor v0.2.6
   Compiling proc-macro-crate v1.3.1
   Compiling napi-sys v2.3.0
   Compiling napi v2.15.0
   Compiling napi-derive v2.15.0
   Compiling num_enum_derive v0.6.1
   Compiling num_enum v0.6.1
error: linking with `cc` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="/home/naskya/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/tmp/xfs-58d3056c:
/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/usr/lib/rustup/bin:/home/naskya/.cargo/bin" VSLANG
="1033" "cc" "-Wl,--version-script=/tmp/rustc5zj4wr/list" "-Wl,--no-undefined-version" "-m64" "/tmp/rustc5zj4wr/symbols.o" "/home/naskya/workspace/pac
kage-template/target/release/deps/napi_package_template.napi_package_template.d73add6295b1dbc9-cgu.0.rcgu.o" "-Wl,--as-needed" "-L" "/home/naskya/work
space/package-template/target/release/deps" "-L" "/home/naskya/.spack/summonings/spack-cargo-patches-v2/spack-cargo-patches-v2/opt/spack/linux-arch-ze
n2/gcc-13.2.1/abseil-cpp-20230802.1-qimakci3w42tyfzripoufujdehwzamti/lib" "-L" "/home/naskya/.spack/summonings/spack-cargo-patches-v2/spack-cargo-patc
hes-v2/opt/spack/linux-arch-zen2/gcc-13.2.1/re2-2023-11-01-sts5pglkpuiw7kfwfwdktymjs6xjxl2q/lib" "-L" "/home/naskya/workspace/package-template/target/
release/build/re2-sys-7717b3afa9059b00/out" "-L" "/home/naskya/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu
/lib" "-Wl,-Bstatic" "/tmp/rustc5zj4wr/libre2_sys-54894e97e46b6010.rlib" "/home/naskya/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/
x86_64-unknown-linux-gnu/lib/libcompiler_builtins-9908bc779e4ab019.rlib" "-Wl,-Bdynamic" "-lstdc++" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl"
 "-lc" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/home/naskya/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linu
x-gnu/lib" "-o" "/home/naskya/workspace/package-template/target/release/deps/libnapi_package_template.so" "-Wl,--gc-sections" "-shared" "-Wl,-z,relro,
-z,now" "-Wl,-O1" "-nodefaultlibs"
  = note: /usr/bin/ld: /tmp/rustc5zj4wr/libre2_sys-54894e97e46b6010.rlib(re2.o): relocation R_X86_64_TPOFF32 against symbol `_ZN3re25hooks7contextE' c
an not be used when making a shared object; recompile with -fPIC
          /usr/bin/ld: failed to set dynamic section sizes: bad value
          collect2: error: ld returned 1 exit status


error: could not compile `napi-package-template` (lib) due to previous error
(node:455454) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
Internal Error: Command failed: cargo build --release
    at genericNodeError (node:internal/errors:984:15)
    at wrappedFn (node:internal/errors:538:14)
    at checkExecSyncError (node:child_process:890:11)
    at Object.execSync (node:child_process:962:15)
    at BuildCommand.<anonymous> (/home/naskya/workspace/package-template/node_modules/@napi-rs/cli/scripts/index.js:11515:30)
    at Generator.next (<anonymous>)
    at /home/naskya/workspace/package-template/node_modules/@napi-rs/cli/scripts/index.js:3552:69
    at new Promise (<anonymous>)
    at __awaiter$1 (/home/naskya/workspace/package-template/node_modules/@napi-rs/cli/scripts/index.js:3548:10)
    at BuildCommand.execute (/home/naskya/workspace/package-template/node_modules/@napi-rs/cli/scripts/index.js:11292:16)

naskya avatar Jan 27 '24 18:01 naskya

Well, I was going to use re2 because of its (bi)linear time complexity, but I just realised that the standard regex crate also meets the criteria:

This implementation uses finite automata and guarantees linear time matching on all inputs.

https://github.com/rust-lang/regex

so my problem has gone (I feel stupid...)

Similar problems may occur with other crates, so I'll keep this ticket open.

naskya avatar Jan 27 '24 20:01 naskya