Zulip Chat Archive

Stream: lean4

Topic: how to set environment variable


Asei Inoue (Nov 02 2025 at 03:19):

How to set environment variable in Lean?

Aaron Liu (Nov 02 2025 at 03:20):

What do you mean by environment variable?

Asei Inoue (Nov 02 2025 at 03:20):

environment variable in OS, such as LEAN_ABORT_ON_PANIC

Aaron Liu (Nov 02 2025 at 03:22):

after a quick search I found docs#Std.Internal.IO.Async.System.setEnvVar

Aaron Liu (Nov 02 2025 at 03:22):

and also docs#Std.Internal.UV.System.osSetenv

Aaron Liu (Nov 02 2025 at 03:23):

they're both Internal so I'm not sure if it's meant to be used directly

Kenny Lau (Nov 02 2025 at 03:26):

there's stuff to run a command right

Aaron Liu (Nov 02 2025 at 03:29):

yeah

Aaron Liu (Nov 02 2025 at 03:29):

docs#IO.Process.run

Markus Himmel (Nov 02 2025 at 07:25):

For a variable like LEAN_ABORT_ON_PANIC, you usually set them when invoking the Lean program, not while the Lean program is running. For example, on the command line you can write LEAN_ABORT_ON_PANIC=1 lake exe myprogram.

Markus Himmel (Nov 02 2025 at 07:28):

But yes, running Std.Internal.IO.Async.System.setEnvVar "LEAN_ABORT_ON_PANIC" "1" does also work (we're going to stabilize this API soon and move it out of the Internal namespace, so no harm in using it except that the name will change without deprecation sometime soon). I would not recommend doing this though, modifying the behavior of your program like that while it is running is asking for trouble.

Asei Inoue (Nov 02 2025 at 08:27):

Thank you.

but why this code outputs "hello"?
(fail to abort?)

import Std

open Std Internal IO Async

#eval System.setEnvVar "LEAN_ABORT_ON_PANIC" "1"

-- "hello" appears
#eval show IO Unit from do
  let a : Nat := panic! "panic message"
  IO.println a

  IO.println "hello"

Markus Himmel (Nov 02 2025 at 08:30):

It doesn't for me. In VS Code it crashes the server and when runnning lake build it prints the backtrace and then lake reports "Lean exited with code 134".

Asei Inoue (Nov 02 2025 at 08:31):

I'm on Windows. It depends on OS?

Markus Himmel (Nov 02 2025 at 08:37):

Ah, yes, from a quick search it looks like it is known that due to the way that we set and access the environment variables, this update is not picked up on on Windows. We could fix this on the Lean side, but it is probably low priority because like I said you should set this environment variable before running your progam.

Asei Inoue (Nov 02 2025 at 08:41):

"set environment variable before running you program" works well in Windows, too.

11325@gtune MINGW64 ~/lean-by-example (main)
$ export LEAN_ABORT_ON_PANIC=1

11325@gtune MINGW64 ~/lean-by-example (main)
$ lean --run LeanByExample/Syntax/Panic/Abort.lean
panic message

Asei Inoue (Nov 02 2025 at 08:42):

my problem is resolved. Thanks!

Asei Inoue (Nov 02 2025 at 10:53):

oh... in windows, lean --run does not consider this environment variable?

11325@gtune MINGW64 ~/lean-by-example (main)
$ export LEAN_ABORT_ON_PANIC=0

11325@gtune MINGW64 ~/lean-by-example (main)
$ lean --run LeanByExample/Syntax/Panic/Abort.lean
panic message

Asei Inoue (Nov 02 2025 at 10:54):

the content of LeanByExample/Syntax/Panic/Abort.lean is here:

import Std

open Std Internal IO Async

def main : IO Unit := do
  let a : Nat := panic "panic message"
  IO.println a

  IO.println "hello"

Markus Himmel (Nov 02 2025 at 11:15):

For LEAN_ABORT_ON_PANIC we only check if the variable has any value at all and interpret this as "should abort" (yes, this is inconsistent with LEAN_BACKTRACE). Try unset LEAN_ABORT_ON_PANIC.


Last updated: Dec 20 2025 at 21:32 UTC