Skip to content

[build] build_graph_dirname("/") returns "" instead of "/" (non-POSIX root) #1021

Closed

Description

@eas4ai

AUDIT finding (read-only review at 450733e, EXECUTED against the real function).

build_graph_dirname("/") returns "", not "/" (POSIX):

  • src/BuildGraphSupport.w:54-61: last slash of "/" is index 0, not < 0,
    so it returns path.slice(0, 0) = "".
  • Consequently build_graph_path_basename("/") returns "" (:63-67)
    instead of POSIX "/".

Executed probe (seed compiler out/bootstrap/bin/with-stage1 run,
calling the real functions):

build_graph_dirname("/")          -> ""    (POSIX: "/")
build_graph_dirname("/a/b")       -> "/a"   (correct)
build_graph_path_basename("/")    -> ""    (POSIX: "/")

Severity Low: all in-repo callers (BuildGraphDispatch.w:81,
BuildGraphOps.w:98,136,158, BuildGraphTests.w:24-33,101,174) pass build
output/test paths, for which "/" is unreachable; no spec mandates POSIX
conformance for this internal helper. Filing so the deviation is tracked,
not because it bites today.

Activity

  1. ehartford commented on Sep 16, 2026

    @ehartford
    Contributor

    Review: reproduced

    Verdict. The central claim is established by running the real functions on current main: build_graph_dirname("/") returns "" and build_graph_path_basename("/") returns "", where POSIX dirname/basename return "/". The defect is wider than the root path: any direct child of root gets an empty dirname (build_graph_dirname("/a") is "", POSIX is "/"). That makes the report's "unreachable in practice" assessment slightly too generous, since a user can spell an output path like -o /prog; see the consequence below. The code is unchanged since the reported revision apart from the byte_at to s[i] sweep.

    Tested.

    Source main @ 5809ac1 (2026-09-14); logic identical to 450733e (only c25e844's mechanical byte_at rewrite touched these lines)
    Compiler with v0.15.2.1 built from 5809ac1 in a staging worktree; probe run from that worktree root so use BuildGraphSupport resolves to the tree's own src/
    Platform macOS Darwin 25.6.0 arm64

    Probe (with run p.w, rc 0), calling the real functions:

    use BuildGraphSupport
    fn show(p: str):
        print("dirname(\"" ++ p ++ "\") = \"" ++ build_graph_dirname(p) ++ "\"   basename = \"" ++ build_graph_path_basename(p) ++ "\"")
    show("/"); show("/a"); show("/a/b"); show("a/b"); show("a"); show("/a/"); show("")
    
    Input build_graph_dirname POSIX dirname build_graph_path_basename POSIX basename
    / "" / "" /
    /a "" / a a
    /a/b /a /a b b
    a/b a a b b
    a . . a a
    /a/ /a / "" a
    "" . . "" ""

    The trailing-slash row is a further POSIX deviation (dirname("/a/") is / and basename("/a/") is a under POSIX, which strips trailing slashes first). It is outside the report's claim and listed only to bound the helper's contract.

    Root cause. src/BuildGraphSupport.w:54-67: the loop records the last / index and returns path.slice(0, last_slash); when the only slash is at index 0 the slice is empty. No root special case exists. Established by reading plus the table above; no debugger evidence is needed for a pure string function.

    Practical consequence (source-read, not executed). Every action in BuildGraphOps.w computes output_dir = build_graph_dirname(output_path) and then build_graph_rt_mkdir_p(output_dir) (:110-113, :148-151, and six more sites). with_fs_mkdir_p("") ends in mkdir(""), which fails with ENOENT (rt/rt_core.w:3453-3472), so a target whose output lives directly under / would abort with "could not create ... output directory" naming an empty path. I did not run a root-level build to confirm this because it would write outside the scratch area; it follows directly from the two functions above.

    Spec. None; build_graph_dirname is an internal helper and the specification does not mandate POSIX semantics for it. The report says the same. The mismatch is still a defect by the repo's own bar: the name promises dirname, callers feed the result to mkdir -p, and the empty result turns a valid path into an error.

    Related PRs. #1082 (4815309) added a single absolute-path predicate for POSIX, drive, and UNC roots but did not touch build_graph_dirname; the drive-root case (C:\prog) presumably has the same shape on Windows and was not tested here.

    Recommendation. Fix it, small and self-contained: return "/" when the last slash is at index 0 (and treat a drive/UNC root the same way through the #1082 predicate), and strip trailing separators before splitting so basename matches POSIX. Acceptance criteria:

    • The table above matches the POSIX column for every row.
    • BuildGraphTests.w gains cases for /, /a, /a/, and a Windows drive root.
    • A build with -o /<name> (or a temp-mounted equivalent in CI) creates the output instead of failing on an empty directory path.
  2. ehartford commented on Oct 25, 2026

    @ehartford
    Contributor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): reproduced in source. build_graph_dirname (src/BuildGraphSupport.w:64-71) is unchanged: for "/" the last slash is index 0, so it returns path.slice(0, 0), the empty string. The functions are private to the compiler, so no user program can call them directly; the review's run of the real functions stands.

    post-release: no current build path passes the root, and the fix is one branch.

  3. added this to the post-v0.16.0 milestone on Oct 28, 2026
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions