`IO.currentDir` ignores `errno` when `getcwd` fails and returns a `userError`

Aperta Adatta ai principianti
#16,805 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Ambito
backend

Direzione di ricerca

Inizia in src/runtime/io.cpp, in corrispondenza di lean_io_current_dir, e confronta la gestione dell’errore di getcwd con quella di lean_io_process_get_current_dir in src/runtime/process.cpp. Il rapporto include un riproduttore Lean minimo che usa IO.currentDir e IO.Process.getCurrentDir; eseguilo per confermare che i costruttori di errore sono diversi. Il lavoro è terminato quando entrambe le API segnalano noFileOrDirectory con il codice di errore del sistema operativo per lo stesso errore.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug
Prerequisites
  • Check that your issue is not already filed:
    https://github.com/leanprover/lean4/issues
  • Reduce the issue to a minimal, self-contained, reproducible test case.
    Avoid dependencies to Mathlib or Batteries.
  • Test your test case against the latest nightly release, for example on
    https://live.lean-lang.org/#project=lean-nightly
    (You can also use the settings there to switch to “Lean nightly”)
  • When reporting a perceived soundness bug: Does it affect lake check --paranoid?
    If in doubt, please reach out on [email protected] for responsible disclosure.
    (Not a soundness bug.)
Description

The POSIX failure path of IO.currentDir in src/runtime/io.cpp builds the IO.Error without decoding errno, unlike almost every other IO operation (which use decode_io_error(errno, ...)). The call fails as it should; only the error kind is wrong.

When getcwd fails, lean_io_current_dir (io.cpp:1489-1497) returns io_result_mk_error("failed to retrieve current working directory"), which is a userError, and discards errno. The sibling lean_io_process_get_current_dir (process.cpp:59-66) makes the same getcwd call but returns decode_io_error(errno), so it reports noFileOrDirectory 2 for the same failure. IO.Error's documentation says the OS constructors represent such errors and userError covers the other cases. So the error kind is wrong.

The program creates a fresh directory, changes into it, deletes it, and then reads the working directory through both public APIs, printing the IO.Error constructor and its os code for each failure. Both reads are the same getcwd call failing with POSIX ENOENT, so both must report IO.Error.noFileOrDirectory with ENOENT in its os code field. The program prints IO.currentDir: userError and IO.Process.getCurrentDir: noFileOrDirectory 2. The second line shows the same runtime reporting the same failure correctly, so the difference is specific to IO.currentDir.

Documented behavior it contradicts (reference manual, "IO: Logical Model"):

Error handling in the IO monad uses the same facilities as any other exception monad. In particular, throwing and catching exceptions uses the methods of the MonadExceptOf type class. The exceptions thrown in IO have the type IO.Error. The constructors of this type represent the low-level errors that occur on most operating systems, such as files not existing. The most-used constructor is userError, which covers all other cases and includes a string that describes the problem.

Context
  • Affected function: lean_io_current_dir (src/runtime/io.cpp)
  • Upstream: no existing issue found for IO.currentDir. The same class of fault in IO.FS.realPath (lean_io_realpath hardcodes ENOENT / noFileOrDirectory via mk_file_not_found_error) is already reported as #15683; it would make sense to fix both together. The cited code is unchanged in nightly-2026-11-26.
Steps to Reproduce
  1. Save the following as input.lean (imports only Init):
/-- The `IO.Error` constructor that names the POSIX error. -/
def classify : IO.Error → String
  | .noFileOrDirectory _ code _ => s!"noFileOrDirectory {code}"
  | .userError _ => "userError"
  | .otherError code _ => s!"otherError {code}"
  | _ => "other"

def main : IO Unit := do
  -- Move into a fresh directory and delete it, so that reading the working directory fails
  -- with POSIX ENOENT (getcwd reports "no such file or directory").
  IO.FS.createDirAll "gone"
  let dir ← IO.FS.realPath "gone"
  IO.Process.setCurrentDir dir
  IO.FS.removeDir dir
  match ← IO.currentDir.toBaseIO with
  | .ok p => IO.println s!"IO.currentDir: ok {p}"
  | .error e => IO.println s!"IO.currentDir: {classify e}"
  match ← IO.Process.getCurrentDir.toBaseIO with
  | .ok p => IO.println s!"IO.Process.getCurrentDir: ok {p}"
  | .error e => IO.println s!"IO.Process.getCurrentDir: {classify e}"
  1. Compile: lean -c input.c input.lean && leanc -o prog input.c
  2. Run: ./prog

Expected behavior:

IO.currentDir: noFileOrDirectory 2
IO.Process.getCurrentDir: noFileOrDirectory 2

Actual behavior:

IO.currentDir: userError
IO.Process.getCurrentDir: noFileOrDirectory 2
Versions

Lean (version 4.38.0-nightly-2026-11-26, arm64-apple-darwin24.6.0, commit e1228b8b45918ece1a00d68e5fc5e6a685ca59ee, Release)
macOS 26.6.2 (arm64)

Additional Information

Root cause analysis. getcwd returns NULL with errno ENOENT as its POSIX contract says (the sibling call in the same program proves the errno and its decoding), and io_result_mk_error/lean_mk_io_user_error build exactly the userError they are asked to build. The errno is discarded by this function's own else branch at io.cpp:1495.

Suggested fix: decode the failure instead, i.e. io_result_mk_error(decode_io_error(errno, nullptr)) in lean_io_current_dir, as lean_io_process_get_current_dir does. decode_io_error already maps ENOENT to noFileOrDirectory.

Impact

A program whose working directory was deleted gets a generic userError from IO.currentDir instead of noFileOrDirectory, so it cannot match on the error kind.

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

Lingua principale
Lean
Stelle
10.3k
Fork
1.1k
Merge medio
18h 27m
PR unite (30g)
390

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di leanprover/lean4

Tutte le issue di leanprover/lean4

Issue simili

Altre issue su Backend & API Design

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.