openpeeps / denim

Node 💖 Nim = Denim! Build powerful NodeJS / BunJS addons with Nim language via Node API (NAPI)
https://openpeeps.github.io/denim/
MIT License
40 stars 1 forks source link

Compiled binary segfaults when run #5

Closed jfilby closed 1 year ago

jfilby commented 1 year ago

Here's my test Nim program:

import myprogramapppkg/submodule

when defined napibuild:
  # Running `denim build myprogram.nim` compiles with `-d:napibuild`
  import denim # NAPI bindings
  import jsony

  init proc(module: Module) =
    module.registerFn(0, "getWelcomeMessage"):
      return %* getWelcomeMessage()

elif isMainModule:
  echo(getWelcomeMessage())

When I run the binary created in the bin directory I get a segfault 11. I suspect node-gyp has a problem somewhere. When I searched on this problem I saw that people logged similar issues for previous versions of node-gyp, but those are fixed by now.

Version information:

$ node --version
v18.16.0
$ node-gyp --version
v9.4.0
$ nim --version
Nim Compiler Version 1.6.12 [Linux: amd64]
Compiled at 2023-03-10
Copyright (c) 2006-2023 by Andreas Rumpf

git hash: 1aa9273640c0c51486cf3a7b67282fe58f360e91
active boot switches: -d:release
georgelemon commented 1 year ago

All good here. This is weird. Try with bun?

Btw, jsony is not necessary here.

$ node -v
v18.16.1

$ node-gyp --version
v9.4.0

Nim Compiler Version 1.6.12 [Linux: amd64]
Compiled at 2023-03-10
Copyright (c) 2006-2023 by Andreas Rumpf

git hash: 1aa9273640c0c51486cf3a7b67282fe58f360e91
active boot switches: -d:release
georgelemon commented 1 year ago

Just added a simple can build/run test.

Here is the job

Ubuntu 22.04.2 LTS
Nim 1.6.12
...
gyp info using node-gyp@9.4.0
gyp info using node@18.16.0 | linux | x64
jfilby commented 1 year ago

I upgraded to Node 18.16.1, but it didn't help. I'm running Fedora, but that shouldn't be a problem. I also tried with the tests you committed, but both had the same problem.

How would I test with Bun? Does it have its own extension compiler?

georgelemon commented 1 year ago

No, but you can try run the compiled .node addon with Bun.

jfilby commented 1 year ago

Here's a similar issue someone has with a Go add-on segfaulting: https://github.com/nodejs/node-gyp/issues/2695. Although their case segfaults at the end, while mine segfaults at the very beginning. That's why I think this is a node-gyp issue.

jfilby commented 1 year ago

Your comment on trying Bun made me think that I should try Node and not give up so soon. It worked! So it loads and runs fine via Node, just not when I run the binary directly. Sorry about the hassle!

BTW, just tested and it works fine with Bun too.