Unresolved references are not supported: Gleam module imports in low-traffic deployments
0 reputation · 10 Apr 2023, 22:51 UTC
0 reputation · 10 Apr 2023, 22:51 UTC
Reducing the compiled footprint of a Gleam service is a common goal for low-traffic workloads where BEAM VM baseline memory consumption dominates operational cost. Because Gleam lacks lazy evaluation or deferred loading, every referenced module must be present in the generated Erlang artifact, making import structure a natural target for reduction.
The compiler emits Unresolved references are not supported when module imports are incomplete. Error message formats may vary between compiler versions, and it remains unclear whether manual import pruning can safely eliminate unused dependencies without risking latent reference gaps in generated code.
Given these constraints, what is the safest verification strategy for confirming that a minimized import set remains complete across Gleam compiler versions? Does the unresolved-reference check fully cover transitive dependencies within the generated Erlang modules, or can incomplete resolution pass compilation yet fail at runtime on the BEAM? Are there documented compiler flags or tooling to audit module inclusion before deployment?
35275 reputation · 11 Apr 2023, 07:21 UTC
The question asks for a safe way to confirm that a trimmed import graph in a Gleam project remains correct across compiler versions, and whether the compiler’s unresolved‑reference error guarantees that all transitive dependencies are present in the generated BEAM code. The answer is split into three parts:
Unresolved references are not supported error.:code.load_file/1 or :code.ensure_loaded/1.gleam build after any change to the import graph. Treat any unresolved‑reference error as a hard failure; do not proceed to packaging or deployment.gleam dependency in gleam.toml). Re‑run the above build after each compiler upgrade to catch regressions in resolution behaviour._build/*/lib/your_app/ebin directory. All modules that the runtime will touch should have a corresponding .beam file. If a module is referenced in the generated Erlang but missing from ebin, the build would have failed, so this step mainly serves as a sanity audit.undef or module_not_found errors that the compiler could not detect.:code.load_file/1 or loading third‑party libraries that are not statically imported), add an explicit startup check that attempts to load each such module and aborts with a clear error if any are missing.Only when dynamic loading is present does the unresolved‑reference check fail to guarantee runtime safety. In that case the additional startup verification (step 5) becomes mandatory.
Does your application use any dynamic Erlang module loading or external libraries that are not referenced directly in the Gleam source? If the answer is yes, the runtime check described above is required; if not, the build‑time unresolved‑reference error and the full test suite are sufficient.
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 11 Apr 2023, 03:09 UTC
Gleam’s Unresolved references are not supported gate is a compile‑time check that only covers the Gleam source. It guarantees every import you write resolves to a module in the project tree, but it does not audit the Erlang release artefact. After the build, any module that appears in the generated .app file must also be present in the ebin directory; otherwise the BEAM will raise an undef at runtime.
To verify a minimal set:
gleam check – any unresolved reference will abort._build/default/lib/your_app/ebin/your_app.app and confirm the modules list matches the modules you intentionally kept.ebin on the code path and calling :code.ensure_loaded/1 for each module, or using :beam_lib to extract remote call targets from the .beam files and diff them against the .app list.gleam.toml dependencies alone; any package listed there is compiled into the release even if no import references it.This layered approach catches both compile‑time gaps and post‑build packaging omissions, giving confidence that a low‑traffic deployment will not fail with an undef.