gren-lang / gren-lang/compiler

A node program that crashes exits with status 0

Open Beginner friendly
#385 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Haskell
Stars
503
Forks
29
PR merge metrics
No merged PRs in 30d

Description

Repository: gren-lang/compiler
Found against: gren 0.6.6, node 22

Summary

Generate.Node.sandwich wraps the whole generated program in a try that
catches everything and calls console.error(e). It never sets an exit status,
so a program that died reports success:

$ node app
Error: Cannot perform mod 0. Division by zero error.
    at _Debug_crash (…)
    …
$ echo $?
0

Anything that decides whether a run worked by looking at the exit status — a
shell script, make, CI, a supervisor, a test harness, && — is told the
program succeeded. A crash in production is invisible to the thing whose job is
to notice it.

Reproduction

module Main exposing (main)

import Init
import Math
import Node
import Stream
import Task exposing (Task)


main : Node.SimpleProgram a
main =
    Node.defineSimpleProgram
        (\env -> Node.endSimpleProgram (emit env))


emit : Node.Environment -> Task Never {}
emit env =
    Stream.writeLineAsBytes (String.fromInt (Math.modBy 0 7)) env.stdout
        |> Task.map (\_ -> {})
        |> Task.onError (\_ -> Task.succeed {})
$ gren make Main --output=app
$ node app > /dev/null 2>&1 ; echo $?
0

Any other crash reaches the same place: Debug.todo, a failed Debug.log on a
cyclic value, a RangeError from deep non-tail recursion.

Cause

compiler/src/Generate/Node.hs:

sandwich :: Name.Name -> B.Builder -> B.Builder
sandwich moduleName javascript =
  let name = Name.toBuilder moduleName
   in [r|#!/usr/bin/env node

try {
|]
        <> javascript
        <> …
        <> [r|.init({});
}
catch (e)
{
console.error(e);
}
|]

The catch is what turns an uncaught exception — which node would otherwise
report and exit 1 for — into a printed message and a clean exit.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read compiler/src/Generate/Node.hs, especially Generate.Node.sandwich, and run the supplied Gren reproduction with node app to confirm the current status. The error should still be printed, but the crashed program must exit with a nonzero status; rerun the shown shell command to verify it is no longer 0.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell, node.js
Domain
compilers
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
84/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.