Skip to content

read_file conflates directories, errors, and empty files (all read as "") #953

Closed

Description

@eas4ai

read_file conflates directories, errors, and empty files: all read as "".

Spec reference

Audit target 2 (call/return correctness) and target 18 (artifact
provenance). Found during the build-graph audit; retained as BGO-002 in
.audit/modules/src__BuildGraphOps.w.md with confined -O1 fixtures, and
re-observed on the current revision. The forwarding branch is
src/BuildGraphRuntime.w:88-89.

Minimal reproduction

use BuildGraphRuntime

let sandbox = ".audit/probes/build_graph_runtime_sandbox"
let _ = build_graph_rt_mkdir_p(sandbox)
let _ = build_graph_rt_write_file(sandbox ++ "/empty.txt", "")
print(f"dir={build_graph_rt_read_file(sandbox).len()}")
print(f"missing={build_graph_rt_read_file(sandbox ++ "/missing").len()}")
print(f"empty={build_graph_rt_read_file(sandbox ++ "/empty.txt").len()}")

Output: dir=0 missing=0 empty=0 — the three cases are indistinguishable.
The retained fixtures go further: :compare-directories -O1 exits 0 with a
green survey verdict for two directories containing different files, and
:copy-directory -O1 exits 0 while replacing the destination with a
zero-byte regular file.

Expected vs actual

Expected: a read failure (directory, missing, permission) is
distinguishable from a successful empty read. Actual: all three yield "",
so build_graph_compare_files and build_graph_copy_file_to_path treat
unreadable inputs as empty files. Because fixpoint comparison shares this
function, a malformed or unreadable stage-input pair can be reported
byte-identical — a false-green integrity failure, not a poor diagnostic.

Root cause

with_fs_read_file (rt/rt_core.w:3277-3300) returns the same empty
string for a directory, for open failure, and when size-seek yields <= 0;
operations then treat the convenience string as proof bytes were read
(src/BuildGraphOps.w:31-38, :511-525).

Workaround

Check existence and file type before reading (still racy against
disappearing files and says nothing about short reads).

Suggested repair boundary

Introduce a status-bearing exact-read result distinguishing empty success
from every failure, validate regular-file inputs where operations require
files, make all consumers handle the error before comparing or writing, and
make fixpoint fail closed on any non-regular or unreadable stage input.
Artifact writes must likewise surface short writes (see the write_file
short-write issue).

Activity

  1. ehartford commented on Sep 14, 2026

    @ehartford
    Contributor

    Independently reproduced the legacy read ambiguity. The tested public API and build-graph consumers are already repaired. This is a valid report with a subsequent fix, rather than an invalid report.

    Reproduction and current controls

    Environment: Darwin arm64; /Users/eric/.local/bin/with reports v0.15.2.1; runtime probes compiled at -O1. Current source reviewed and imported: 5809ac10aee5033d4811a9f023dae7282e4ef500. I also imported BuildGraphRuntime from an isolated worktree at the reported revision 450733e58a1a7cce14f9cb2084943fc178815111. That historical-source probe still used the installed compiler/runtime, not a compiler rebuilt from the historical revision.

    Minimal probe, after creating a directory containing an empty file and an eight-byte file:

    use BuildGraphRuntime
    
    fn main:
        let root = "/tmp/triage953"
        assert(build_graph_rt_mkdir_p(root) == 0)
        assert(build_graph_rt_write_file(root ++ "/empty", "") == 0)
        assert(build_graph_rt_write_file(root ++ "/full", "payload\n") == 0)
        print(f"dir={build_graph_rt_read_file(root).len()}")
        print(f"missing={build_graph_rt_read_file(root ++ "/missing").len()}")
        print(f"empty={build_graph_rt_read_file(root ++ "/empty").len()}")
        print(f"full={build_graph_rt_read_file(root ++ "/full").len()}")
    

    with run -O1 repro.w prints dir=0, missing=0, empty=0, full=8, exit 0. I observed this with both current and historical wrapper source. My executed fixtures used an isolated directory outside the repository; the path above is shortened for readability.

    The repaired std.fs.read_file gives distinct results:

    Input Observed result
    Directory Err(IoError.Os(21, path))
    Missing file Err(IoError.Os(2, path))
    Empty file Ok("")
    Eight-byte file Ok, length 8

    Direct probes of current BuildGraphOps also passed the relevant controls:

    • Different directories: comparison returns 1 with OS error 21.
    • Two empty files: comparison returns 0.
    • Empty versus nonempty file: comparison returns 1 with a byte-difference diagnostic.
    • Copying a directory over an existing file: returns 1; destination still contains its original KEEP bytes.

    with run -O1 test/behavior/behav_fs_read_file_io_error.w additionally printed ok, exit 0, covering the error payload/message, directory rejection, empty success, and ? propagation.

    Source, specification, and history

    The historical runtime implementation explicitly returns the same empty string for directory, open-failure, and nonpositive-size paths. That is source evidence for the reported mechanism, not a claim of historical instruction-level debugger proof.

    Repair commit 08f87b6c adds status-bearing reads, changes std.fs.read_file to Result[str, IoError], makes ToolFs.read_text report errors, and routes compare/copy/promote/install through checked reads. It references this issue and closes the overlapping #909. The current checked graph read and public API preserve the distinction. The legacy runtime helper intentionally retains the empty-on-failure contract for internal probes; observing it alone no longer demonstrates that comparison/copy is broken.

    Specification §10.2 shows read_file(path)? propagating IoError. The decision log supplies no exception authorizing failed artifact reads to count as successful empty reads. D20's spec-leads rule and the mission's guardrails support the implemented repair.

    I checked relevant merged history and open PRs. The repair landed as the commit above; GitHub's commit-to-PR lookup returned no associated PR. Subsequent changes to these graph files are path/platform work, and the open audit PR #1078 records the older findings rather than supplying a competing fix.

    Assessment and limits

    I would champion this failure-handling requirement: an unreadable artifact must never become evidence of byte equality or silently overwrite a valid destination. The existing fix addresses the independently tested cases, so I would prioritize retaining regression coverage and reviewing any remaining consumers over reimplementing that fix.

    I could not independently execute the historical comparison/copy paths: their old dependencies fail checking on retired Vec.set_i32 calls with both available local binaries. I did not alter them to manufacture a passing reproduction. I also did not inject permission races, short reads, devices, or platform-specific failures, nor run a full stage fixpoint. The current operation probes therefore establish the cases above, not complete closure of every proposed acceptance criterion. The historical false-green comparison remains supported by the reporter's evidence and source review, not a new historical execution claim.

  2. ehartford commented on Oct 25, 2026

    @ehartford
    Contributor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): does not reproduce; the fix landed.

    $ cat main.w
    use std.fs
    print(read_file(".").is_ok())          # a directory
    print(read_file("empty.txt").is_ok())  # an empty file
    print(read_file("missing.txt").is_ok())
    $ with run main.w
    false
    true
    false
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions