gleam-lang / gleam

⭐️ A friendly language for building type-safe, scalable systems!
https://gleam.run
Apache License 2.0
18.14k stars 762 forks source link

Gleam should inform the user that they don't have the correct Erlang version #3931

Closed ori-nonni closed 2 hours ago

ori-nonni commented 6 hours ago

Gleam version 1.6.2 WSL running Ubuntu 24.04.1

I installed Gleam using nix home manager and then installed erlang in the same way.

I got an error when I ran gleam build

> gleam build
Downloading packages
 Downloaded 2 packages in 0.00s
  Compiling gleam_stdlib
escript: exception error: undefined function code:del_paths/1
  in function  gleam@@compile_erl__escript__1732__535271__519110__2:compile_package/3 (/home/x/testgleam/build/dev/erlang/gleam_stdlib/_gleam_artefacts/gleam@@compile.erl, line 36)
  in call from gleam@@compile_erl__escript__1732__535271__519110__2:compile_package_loop/0 (/home/x/testgleam/build/dev/erlang/gleam_stdlib/_gleam_artefacts/gleam@@compile.erl, line 16)
  in call from escript:run/2 (escript.erl, line 750)
  in call from escript:start/1 (escript.erl, line 277)
  in call from init:start_em/1
  in call from init:do_boot/3
error: Shell command failure

There was a problem when running the shell command `escript`.

The issue was that home manager installs Erlang 25 by default, but it seems that Erlang 27 is required for Gleam.

Would it be possible to inform the user that they need to install the correct version of Erlang instead of erroring with a fairly cryptic error?

joshi-monster commented 5 hours ago

I used code:del_paths in the Erlang compiler backend because the standard library only supports the latest 2 versions. This function was introduced in Erlang/OTP 26, which is the oldest version the standard library currently supports. This has come up for a lot of folks now, and I kind of like that it fails loud and early instead of at some random point at runtime whenever a non-supported function is called. Of course the error you get is super bad at the moment.

Technically, you could run Gleam without the standard library or with a really old version, so now I'm not sure if having the same requirement in the compiler is a good idea.

Maybe adding a erlang.min_version field to gleam.toml for the standard library and other libraries that want to do ffi (like gleam_json@2) would be good? We could then start checking that one the same way we check the minimum Gleam version. This would give us a way to have nice errors during compile time. Of course I could also remove the call to del_paths again.

~ :purple_heart:

ori-nonni commented 5 hours ago

I'm a total noob, but adding the erlang version as a dependency for the standard library seems like a good solution. I assume that would generate a nice error message?

joshi-monster commented 4 hours ago

Yeah! it could roughly look like this:

$ gleam build
  Compiling gleam_stdlib
error: Incompatible Erlang version

The package `gleam_stdlib` requires an Erlang/OTP version satisfying >= 26 but you are using v25.3.2.15.
lpil commented 4 hours ago

We will not support version checking in the build tool at all. That's a runtime property so it cannot be checked by the build tool, but it can be checked by your program.

Let's make the Erlang compiler emit a helpful error in the instance this function is unavailable.

joshi-monster commented 4 hours ago

Wouldn't it b better to not use that function in the first place?

ori-nonni commented 3 hours ago

We will not support version checking in the build tool at all. That's a runtime property so it cannot be checked by the build tool, but it can be checked by your program.

Let's make the Erlang compiler emit a helpful error in the instance this function is unavailable.

In the general case (i.e. not just related to code:del_paths) how does the compiler know that an issue is due to the wrong Erlang version to point the user in the right direction?

lpil commented 3 hours ago

If we can replace that call with one that works on older versions that would be good too.

@ori-nonni The compiler does not know that. It doesn't know much of anything about runtime.

ori-nonni commented 3 hours ago

We will not support version checking in the build tool at all. That's a runtime property so it cannot be checked by the build tool, but it can be checked by your program.

@ori-nonni The compiler does not know that. It doesn't know much of anything about runtime.

I would assume that the build environment is known by the build tool when building, including what version erlang is available. (parsing erl -version in the worst case).

So wouldn't it make sense to be able to specify a minimum erlang version in the toml as a dev dependency since it is known at build time?

When I run gleam build without erlang installed I get a nice error message. Is the lack of escript less of a runtime property than the available erlang version when building?

error: Program not found

The program `escript` was not found. Is it installed?
Documentation for installing Erlang can be viewed here:
https://gleam.run/getting-started/installing/
lpil commented 2 hours ago

It's not known, no. We will not be attempt to support this. There is no way for the build tool to know what VM version is going to be used on your production computers.