gleam-lang / gleam

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

`gleam build` generates `gleam.mjs` and `gleam.d.mts` even if they already exist #3178

Closed ofekd closed 4 months ago

ofekd commented 5 months ago

Here are some of the logs from vite:

CHANGED build/dev/javascript/gleam_stdlib/gleam.mjs
CHANGED build/dev/javascript/gleam_stdlib/gleam.d.mts
CHANGED build/dev/javascript/ranger/gleam.mjs
CHANGED build/dev/javascript/ranger/gleam.d.mts
CHANGED build/dev/javascript/birl/gleam.mjs
CHANGED build/dev/javascript/birl/gleam.d.mts
CHANGED build/dev/javascript/gleam_javascript/gleam.mjs
CHANGED build/dev/javascript/gleam_javascript/gleam.d.mts
CHANGED build/dev/javascript/gleam_json/gleam.mjs
CHANGED build/dev/javascript/gleam_json/gleam.d.mts
CHANGED build/dev/javascript/gleeunit/gleam.mjs
CHANGED build/dev/javascript/gleeunit/gleam.d.mts
CHANGED build/dev/javascript/gluid/gleam.mjs
CHANGED build/dev/javascript/gluid/gleam.d.mts
CHANGED build/dev/javascript/juno/gleam.mjs
CHANGED build/dev/javascript/juno/gleam.d.mts

Since I use vite-gleam, which builds on changes detected by vite, this results in an infinite loop. Top-level application files depend on those gleam.mjss, thus marked as changed themselves, and then they trigger another build.

This is troublesome in another way -- since HMR loads duplicates of a changed file (see vitejs/vite#645), all the code that relies on instanceof fails. This is because an object that was created before the underlying library file was replaced is not an instanceof the class defined in the new file. Example of failing code:

let definitely_something = Some(5)

fn make_result() {
  definitely_something
  |> option.to_result
}

// this returns an Error if called after HMR was fired for option.mjs, which is always the case with this bug
make_result()

I've compiled 9ba060ddf12fd01302606b269d5fde0c2ffa6c08 to check if the issue exists and it does.

NixOS 23.11

lpil commented 5 months ago

Thank you.

ofekd commented 5 months ago

I have some time to take a stab at this, would appreciate pointers

Edit: I have found the relevant area of the code, please see #3238