Skip to content

Add gleam support to Mix - #14262

Open
Papipo wants to merge 43 commits into
elixir-lang:mainfrom
Papipo:add-gleam-compiler
Open

Papipo wants to merge 43 commits into
elixir-lang:mainfrom
Papipo:add-gleam-compiler

Conversation

@Papipo

@Papipo Papipo commented Feb 13, 2025 •

Copy link
Copy Markdown

This PR adds support for the gleam language.

  • Support for gleam.toml in path deps
  • Support gleam in deps loader.
  • Support for gleam in deps.compile
  • Require specific gleam binary version (hardcoded to 1.9.0 for now, see below)
  • Handle absent gleam binary
  • Support git deps (support for them in gleam is in main already but not yet released)
  • Support for :application option in Mix.ProjectStack.push (thus an application function is not needed)
  • Setup gleam on CI (Otherwise gleam tests will be skipped).

Notes:

  • Packages having a gleam binary version requirements are handled automatically when executing gleam compile-package.
  • The gleam binary version requirement for mix integration is hardcoded because I could just try to run gleam export package-info but I can't know the reason for a bad exit status (maybe the dep path was wrong and the command was run in a dir without a gleam.toml).
  • The required command to export information about gleam packages and their deps is not yet released but has already been merged. Should land on v1.10.0. Was released on v1.10.0

Comment thread lib/mix/lib/mix/gleam.ex Outdated
Comment thread lib/mix/lib/mix/tasks/deps.compile.ex Outdated
@Papipo

Papipo commented Feb 17, 2025

Copy link
Copy Markdown
Author

@josevalim when working on this I was always thinking of erlang target packages, but it would be nice to be able to use javascript ones as well (ie. to use them in Phoenix frontend). Not sure how we can handle that, any hints?

@josevalim

Copy link
Copy Markdown
Member

It should be possible because we use heroicons, a regular JS package, as a dependency on Phoenix applications. So we could probably support a :gleam_targets or similar option that is used by by deps.compile? You will probably need to set app: false true, similar to how heroicons are added.

@lpil

lpil commented Feb 17, 2025

Copy link
Copy Markdown
Contributor

Hello! You shouldn't need to do anything special target-wise as Gleam performs dead code elimination for code that is for other targets.

@josevalim when working on this I was always thinking of erlang target packages, but it would be nice to be able to use javascript ones as well (ie. to use them in Phoenix frontend). Not sure how we can handle that, any hints?

I would not expect Mix to become a front-end build tool by adding Gleam support. I think the workflow should be the same as it is today etc, with the desired build tool (esbuild, webpack, gleam etc) being run as a Phoenix watcher.

Adding frontend dependencies to the BEAM application dependency tree would either be wasted work compiling and managing them, or the programmer would need to go through every dependency in the lock file and add configuration to their mix.exs for each one they don't want to use on the backend.

We are going to #14262 (comment), so I also need the target config, if any. I think that just exporting everything is the best bet.

There is no such config. A Gleam package doesn't have an explicit target, each function in the public API is available on Erlang, JavaScript, or both, and they get either compiled or eliminated as needed. Whether a package is for Erlang or JavaScript is typically a matter of how the dependant is using them, not a matter of the configuration of the dependency.

There is a target flag in gleam.toml, but that is "if I run code from this page (almost always the tests) what target should be used by default, unless otherwise specified".

@Papipo
Papipo force-pushed the add-gleam-compiler branch 3 times, most recently from b91ae71 to c181ecd Compare February 20, 2025 11:34
Comment thread lib/mix/lib/mix/tasks/deps.compile.ex Outdated
Comment thread lib/mix/test/test_helper.exs Outdated
@Papipo
Papipo force-pushed the add-gleam-compiler branch from a18bf89 to 26c8aff Compare March 17, 2025 12:31
@Papipo
Papipo force-pushed the add-gleam-compiler branch 2 times, most recently from 8a7d9fa to 360db23 Compare March 25, 2025 09:14
@Papipo Papipo changed the title [DRAFT] Add gleam support to Mix Add gleam support to Mix Mar 26, 2025
@Papipo
Papipo marked this pull request as ready for review March 26, 2025 08:46
@Papipo

Papipo commented Mar 26, 2025 •

Copy link
Copy Markdown
Author

I am kind of copypasting this from the Gleam discord in case someone wants to try this out before merging into main:

What is this?

These changes add first-class support for Gleam in Elixir's Mix, thus allowing gleam path dependencies in mix.exs.
Those dependencies are expected to be 100% valid gleam projects and as such need to have a gleam.toml file.

What's missing?

Probably nothing. Work on the gleam compiler is already merged and work on the Mix side is completed, but there might be rough edges, that's what we need to test.

Instructions

Mix integration will expect you to have gleam >= 1.10.0 installed and that hasn't been released yet. So:

  • Install Gleam using the nightly release.
    • Notice that some package managers might allow you to install it this way.
  • Grab this elixir branch.
  • Edit lib/mix/lib/mix/gleam.ex and replace @required_gleam_version with 1.9.0.
  • Run make in the root folder of the Elixir project and add its bin folder to your $PATH

If you were using mix_gleam, undo its setup.

Usage

After running mix deps.get the Mix workflow doesn't change. deps.compile should compile Gleam path deps.
If you use gleam code from your elixir app, it should just work (as long as you have a gleam.toml file - you might want to move gleam code to a sibling folder or into gleam/src) I think that otherwise the mix.exs file will take precedence over the gleam one.

What needs to be tested?

  • Anything related to Erlang/OTP shenanigans (.app files, extra_applications and ensuring those start, etc.)
  • If you have an existing project using mix_gleam, that's a great opportunity to replace it with this.
  • Gleam Git dependencies.
  • Just try to break it please.

Thanks

Just report back with your findings and thanks 🙇🏽

Note: I hope these instructions are enough and correct. Let me know if that's not the case.

Comment thread lib/mix/lib/mix/tasks/deps.compile.ex Outdated
@josevalim

Copy link
Copy Markdown
Member

@Papipo thanks for all the work so far!

One question: I thought that gleam shipped as a Hex package, so wouldn't it be possible for the user to depend on the gleam package or similar instead of us depending on a system wide gleam installation?

@Papipo

Papipo commented Mar 26, 2025

Copy link
Copy Markdown
Author

@Papipo thanks for all the work so far!

One question: I thought that gleam shipped as a Hex package, so wouldn't it be possible for the user to depend on the gleam package or similar instead of us depending on a system wide gleam installation?

No, gleam is a single binary written in Rust. I don't know if we can provide a hex archive maybe? I don't know how these work.

How is rebar installation handled? I know that mix is able to install it locally, but I am not sure what that means exactly. It's still a binary as well, right?

@josevalim

josevalim commented Mar 26, 2025 •

Copy link
Copy Markdown
Member

For Rebar, we have some binaries which we upload to hex.pm, and we version control them every year or so. Although I don't think this will work for Gleam, because people want to upgrade it more frequently, and Rebar is only a build tool, not really a compiler. So I guess we need to be clear we are using the system one and it is up to them to enforce the team uses the same version across the board (which is how we deal with make and the elixir executable itself).

@Papipo

Papipo commented Mar 26, 2025

Copy link
Copy Markdown
Author

For Rebar, we have some binaries which we upload to hex.pm, and we version control them every year or so. Although I don't think this will work for Gleam, because people want to upgrade it more frequently, and Rebar is only a build tool, not really a compiler. So I guess we need to be clear we are using the system one and it is up to them to enforce the team uses the same version across the board (which is how we deal with make and the elixir executable itself).

In fact the binary is also the LS, so you definitely want to have full control of the version you are using, etc.

@josevalim

josevalim commented Apr 2, 2025 •

Copy link
Copy Markdown
Member

Quoting @lpil on the forums:

The Gleam compiler only generates Erlang from Gleam modules, but it doesn’t know what any Elixir modules in the package expand to, so it cannot create .app files unless it owns the whole project and so will be performing Elixir and Erlang compilation. This means that any build tool that uses the Gleam compiler will need to generate the required BEAM .beam and .app files.

Apologies but I am a bit confused. We are using gleam compile-package which emits the .beam files already, right? Here is what we invoke: https://github.com/elixir-lang/elixir/pull/14262/files#diff-da9fa810e81e1fe6ac8d4defde11dc457f0066fc9e442e85a96ea2e182414eacR336 - in this case, doesn't Gleam already own the whole package? We don't invoke anything else so if additional steps are necessary, they are not being executed here.

The .app file has things like app name, version, description, registered processes, app environment, and module names. We would need to lift all of this information from the Gleam .toml and make assumptions on how Gleam wants those be used. Worst case scenario, couldn't Gleam support an --app file on compile-package that generates it, based on the outputs of its --out directory?

@Papipo

Papipo commented Apr 2, 2025

Copy link
Copy Markdown
Author

Yes, compile-package does emit beam files.

From @lpil comment over elixirforum I assume the problem are colocated Elixir files within a Gleam project. The Gleam compiler knows nothing about them so any modules they define can't be written in the .app file, I guess.

What I have done here is use the Mix task to generate the .app file (and inject the two options gleam.toml supports for the erlang target: mod and extra_applications). As you can see we now have a full-fledged .app file.

Let me know if this looks good or if I should revert it. Thanks.

@josevalim

Copy link
Copy Markdown
Member

Yes, for collocated projects that’s the responsibility of Mix, but for packages I would say that’s Gleam responsibility. Otherwise there is even more we need to understand from Gleam TOML’s and we may need to keep track of it as it evolves. So is there a chance for it to emit an .app file either by default or via a flag? On the plus side, if Rebar wants to integrate Gleam as well, they can reuse it to emit apps too. :)

@Papipo
Papipo force-pushed the add-gleam-compiler branch 3 times, most recently from b70f8fd to b64f23a Compare April 3, 2025 10:14
@Papipo

Papipo commented Apr 7, 2025

Copy link
Copy Markdown
Author

Apparently this seems to be working fine except that people sometimes need to compile twice . Could this be code path related maybe? It's probably something stupid I missed.

@Papipo

Papipo commented Aug 31, 2026

Copy link
Copy Markdown
Author

@josevalim I have made the requested changes. For now the minimal required version of gleam is 1.19.0, but I don't know if my changes will make it. I will update the version if that's not the case.

I ran a test with a locally compiled gleam with my fork and all tests still pass. With gleam 1.18 and the latest changes from the last commit in this PR they don't.

Comment thread lib/mix/lib/mix/tasks/compile.app.ex Outdated
Comment thread lib/mix/lib/mix/tasks/deps.compile.ex Outdated
Comment thread lib/mix/lib/mix/gleam.ex Outdated
Comment thread lib/mix/lib/mix/gleam.ex Outdated
Comment thread lib/mix/test/test_helper.exs
Comment thread lib/mix/test/fixtures/subfolder/deeper_gleam_dep/src/deeper_gleam_dep.gleam Outdated
@josevalim

Copy link
Copy Markdown
Member

@Papipo thank you, I did one initial pass!

@josevalim

Copy link
Copy Markdown
Member

Btw, this looks much better, as we depend way less on Gleam internals!

@Papipo

Papipo commented Sep 1, 2026

Copy link
Copy Markdown
Author

@josevalim I am also removing the check for the per-package required gleam version. When mix tries to compile the package, the gleam command will already make this check so I think it's redundant.

@Papipo

Papipo commented Sep 1, 2026 •

Copy link
Copy Markdown
Author

@josevalim everything has been dealt with except the windows thingy. I'll come back once that is settled.

Edit: it has been dealt with here

@Papipo
Papipo force-pushed the add-gleam-compiler branch from 0a1b51f to 33380fd Compare September 1, 2026 11:56
@Papipo

Papipo commented Sep 23, 2026

Copy link
Copy Markdown
Author

@josevalim next Gleam release will have all the needed changes (we have an RC already), in case you want to review this now and run CI with the real thing once it lands. Thanks!

@spicychickensauce

spicychickensauce commented Sep 23, 2026 •

Copy link
Copy Markdown

Very nice @Papipo!
I tried it out on my project and it works like a charm! I do use path deps and git deps, which didn't work with mix_gleam, but works perfectly with this PR.

For others who like to try, here is how I ran this easily with mise, without messing with my $PATH:

> mise use elixir@ref:28dfeb05d0b375831c301810baf9a3baec871583
> mise use gleam@v1.19.0-rc1
# If v1.19.0 was already released, this would just work now, but because it's still rc, we need to trick mix via a wrapper script:
> DIR=$(mktemp -d) && echo -e '#!/bin/sh\nif [ "$1" = "--version" ]; then echo "gleam 1.19.0"; else exec '$(which gleam)' "$@"; fi' > "$DIR/gleam" && chmod +x "$DIR/gleam" && PATH="$DIR:$PATH" mix deps.get

@Papipo

Papipo commented Sep 23, 2026

Copy link
Copy Markdown
Author

Thanks for testing it @spicychickensauce!

Comment thread lib/mix/lib/mix/tasks/deps.compile.ex Outdated
Co-authored-by: Michał Łępicki <michallepicki@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

10 participants