rescript-lang / rescript-vscode

Official VSCode plugin for ReScript
MIT License
329 stars 56 forks source link

tools linux binary dynamically links musl #1006

Closed woeps closed 4 months ago

woeps commented 4 months ago

Running npx --package @rescript/tools@0.6.2 -- rescript-tools -v exits with exit code 0 but gives no output. The same is true for any other tools command.

Runnin npx --package @rescript/tools@0.5.0 -- rescript-tools -v exits with exit coce 0 but outputs the expected string 0.5.0.

I'm running on linux x86_64 and node 20.14.0. (My colleague ran the commands on mac with success. It seams like a linux issue...)

Any ideas?

zth commented 4 months ago

We recently worked on arm64 support for Linux, maybe we messed something up with the binaries then?

woeps commented 4 months ago

I'm trying to find out what's happening:

AFAIK: npm i @rescript/tools@0.6.2 puts a link into <project>/node_modules/.bin/rescript-tools which links to <project>/node_modules/@rescript/tools/npm/cli.js. cli.js just calls getBinaryPath (which in my case returns <project>/node_modules/@rescript/tools/binaries/linux/rescript-tools.exe and spwans a process with this binary. Right?

Trying to run the binary directly in the terminal yields the following error message: zsh: no such file or directory: <project>/node_modules/@rescript/tools/binaries/linux/rescript-tools.exe Running file <project>/node_modules/@rescript/tools/binaries/linux/rescript-tools.exe yields

<project>/node_modules/@rescript/tools/binaries/linux/rescript-tools.exe: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-x86_64.so.1, BuildID[sha1]=b1b311c6abafbede111905ae90396f8d5caede7d, with debug_info, not stripped
woeps commented 4 months ago

Alright, the file call (see above comment) gave me the final clue to solve my issue: My linux system was missing the musl package.

After installing this package, tools cli binary runs as intended! 🥳

Maybe we should state the dependency on musl in the readme?

zth commented 4 months ago

Alright, the file call (see above comment) gave me the final clue to solve my issue: My linux system was missing the musl package.

After installing this package, tools cli binary runs as intended! 🥳

Maybe we should state the dependency on musl in the readme?

Yes definitely! That's a terrible experience/error 🙈 would you mind sending a PR? Also, I wonder if there's an alternative to that, and why the compiler doesn't require the same thing...

woeps commented 4 months ago

It seems like the alpine docker image uses musl instead of libc (as it is more lightweight) which is dynamically linked and therefore needs to be present on the system. I don't really know much about dune, but I found https://ocamlpro.com/blog/2021_09_02_generating_static_and_portable_executables_with_ocaml, which suggests to use (flags (:standard -cclib -static -cclib -no-pie)).

Current flags used in tools dune file: https://github.com/rescript-lang/rescript-vscode/blob/7c9f1bd6728fe039a6c3fb51188279a45fce7e50/tools/bin/dune#L3-L4

@cknitt do you have an idea on how to get rid of the dynamic link to musl?

cknitt commented 4 months ago

This is basically the same setup as in the compiler repo where it works fine, only that in the compiler repo CI, dune is invoked with --profile static.

In the rescript-vscode repo, the idea was that this https://github.com/rescript-lang/rescript-vscode/blob/7c9f1bd6728fe039a6c3fb51188279a45fce7e50/.github/workflows/ci.yml#L42

would do the same and I thought I also tested that.

Will have to investigate.

cknitt commented 4 months ago

Running file <project>/node_modules/@rescript/tools/binaries/linux/rescript-tools.exe yields

<project>/node_modules/@rescript/tools/binaries/linux/rescript-tools.exe: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-x86_64.so.1, BuildID[sha1]=b1b311c6abafbede111905ae90396f8d5caede7d, with debug_info, not stripped

Compare with the binaries created in the compiler repo:

rescript.exe: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, BuildID[sha1]=90f2bd1b69ed51f0318b4df20060b03b85481cd9, stripped

This is what it should look like.

zth commented 4 months ago

@cknitt so potentially we need some tweaking here?

cknitt commented 4 months ago

Yes, will have a look at it, probably the static profile is not passed to dune correctly or something like that.

cknitt commented 4 months ago

This is very weird. No matter what I do, the rescript-editor-analysis.exe is statically linked, but rescript-tools.exe is dynamically linked. 🤯

ef132b5090b5:/data# make
dune build --profile static
cp -f _build/install/default/bin/rescript-editor-analysis analysis/rescript-editor-analysis.exe
cp -f _build/install/default/bin/rescript-editor-analysis rescript-editor-analysis.exe
cp -f _build/install/default/bin/rescript-tools rescript-tools.exe
ef132b5090b5:/data# file rescript-editor-analysis.exe
rescript-editor-analysis.exe: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, BuildID[sha1]=348b8d5ae9ecd9b51539c9c2b2df5446eae7ae0b, with debug_info, not stripped
ef132b5090b5:/data# file rescript-tools.exe
rescript-tools.exe: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-aarch64.so.1, BuildID[sha1]=9642e8ce1c6d0230a982e19c887107466f61dd3c, with debug_info, not stripped

Will investigate more on the weekend.

cknitt commented 4 months ago

Fixed in #1013. 🎉

@woeps Can you please revert #1007?

woeps commented 4 months ago

@cknitt sure, I'll revert it.