Skip to content

file/dir: File::Stat surface, File.ftype, aliases, errno mapping + Dir Enumerable/enumerators (core/file +22, core/dir +29) - #591

Merged
tannevaled merged 4 commits into
mainfrom
wave19/filedir
Sep 8, 2026
Merged

tannevaled merged 4 commits into
mainfrom
wave19/filedir

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Advances MRI-Ruby-4.0.x conformance of File and Dir for the offline-fixable
remainder of core/file and core/dir, verified byte-for-byte against ruby
4.0.5. Bibliography: ruby/ruby v3_4_0 file.c / dir.c, and ruby/spec under
core/file and core/dir.

Measured deltas (ruby/spec, this corpus)

suite pass before → after fail error
core/file 577 → 599 (+22) 76 → 61 78 → 71
core/dir 235 → 264 (+29) 51 → 49 35 → 8

Net +51 passing examples. FLOOR is untouched.

File (internal/vm/file.go)

  • File.ftype — added (rb_file_s_ftype): lstat-based (a symlink is
    "link", not its target), #to_path argument, ArgumentError for a wrong
    arity, Errno::ENOENT for a missing path.
  • File::Separator — added (mixed-case alias of File::SEPARATOR).
  • File.chmod / File.umask — coerce the mode via #to_int and raise
    RangeError for a Bignum that overflows a C long (num2mode, mirroring
    NUM2MODET), instead of a TypeError; File.umask raises ArgumentError
    for more than one argument.
  • File.rename — checks its two-argument arity before coercing paths
    (ArgumentError, not a TypeError).
  • File.truncate — maps a negative length to Errno::EINVAL (truncate(2)),
    keeping Errno::ENOENT for a missing path.
  • File.exist? / File.file? / File.identical? — enforce their arity
    (ArgumentError).
  • File.unlink / File.empty? — genuine aliases of File.delete /
    File.zero? (shared Method records, so File.method(:unlink) == File.method(:delete)).
  • File.realpath / File.realdirpath — raise Errno::ELOOP for a symlink
    cycle instead of Errno::ENOENT. filepath.EvalSymlinks returns a bare
    "too many links" error that does not wrap syscall.ELOOP, so the real errno
    is recovered by re-stating the path (isSymlinkLoop).

Dir (internal/vm/dir.go)

  • Path coercion — Dir.entries / children / exist? / empty? /
    chdir / each_child / foreach / delete now use rb_get_path
    (#to_path / #to_str) instead of rejecting a non-String with a TypeError.
  • Aliases — Dir.getwd is a genuine alias of Dir.pwd; Dir.rmdir and
    Dir.unlink of Dir.delete.
  • Dir includes Enumerable — deferred to a post-prelude
    includeDirEnumerable (registerDir runs before the prelude defines the
    module), so Dir.include?(Enumerable) and Enumerable methods (to_a, …)
    work on an open Dir.
  • Dir.delete errno mapping — Errno::ENOTDIR for a non-directory,
    Errno::ENOTEMPTY for a non-empty directory, Errno::EACCES for a
    permission failure (raiseRmdirErr); it was always Errno::ENOENT.
  • Block-less iterators — Dir.each_child / Dir.foreach with no block now
    return an Enumerator whose #size is nil instead of dereferencing a nil
    block (was a crash); Dir#each / Dir#each_child likewise return
    nil-#size enumerators.
  • Instance methods — added Dir#children and Dir#chdir.

Tests & gates

  • New whitebox suite internal/vm/wave19_filedir_internal_test.go asserts every
    new branch against MRI-verified values. go tool cover -func reports 100%
    on file.go, dir.go and filestat.go. The errno-classification helpers are
    covered deterministically (synthetic os.PathErrors and the osStat seam),
    so the coverage gate does not depend on running unprivileged or on real
    symlink cycles.
  • go build ./..., GOWORK=off go test ./..., gofmt -l internal/vm/ (clean),
    and the wasip1 (GOOS=wasip1 GOARCH=wasm) and windows cross-builds all
    pass. OS-specific tests are guarded by runtime.GOOS.

Deliberately out of scope (noted for follow-up owners)

  • %w[] escaped-space lexing (lexer, not in this file set): %w[a\ b]
    splits into two words instead of one. This fabricates a stray top-level entry
    in the shared Dir spec fixtures (special/test\ +()[]{} → a phantom
    +()[]{} directory) and confounds several Dir ordering/glob examples that
    otherwise now have correct behaviour.
  • File# instance metadata (stat/lstat/mtime/atime/ctime/
    birthtime/flock, and File#chmod to_int/RangeError) lives in io.go,
    which this task does not touch.
  • File.mkfifo is intentionally NOT added: it would make open_spec.rb's
    threaded FIFO example reachable, which deadlocks under rbgo's current
    Thread/blocking-open model (a real MRI passes it) and would turn that file's
    ~50 passes into a per-file timeout — a net regression. The blocking-FIFO
    limitation is in the IO/threading layer.
  • Encoding-driven cases (File.basename encoding preservation,
    File.expand_path Encoding::CompatibilityError from default_external) and
    File.fnmatch FNM_EXTGLOB brace handling live outside this file set.

🤖 Generated with Claude Code

tannevaled and others added 4 commits September 7, 2026 23:36
…enumerators (core/file +20, core/dir +29)

Advance MRI-Ruby-4.0.x conformance of File and Dir (offline-fixable), verified
byte-for-byte against ruby 4.0.5.

File (internal/vm/file.go):
- Add File.ftype (lstat-based, #to_path arg, ArgumentError arity, Errno::ENOENT)
  — ruby/ruby v3_4_0 file.c rb_file_s_ftype.
- Add File::Separator (mixed-case alias of File::SEPARATOR).
- File.chmod / File.umask coerce the mode via #to_int and raise RangeError for a
  Bignum that overflows a C long (num2mode, mirroring NUM2MODET); File.umask
  raises ArgumentError for >1 argument.
- File.rename checks its two-argument arity before coercing (ArgumentError, not a
  TypeError).
- File.truncate maps a negative length to Errno::EINVAL (truncate(2)).
- File.exist?/file? (oneArg) and File.identical? enforce their arity.
- File.unlink is a genuine alias of File.delete, File.empty? of File.zero?
  (shared Method records, so File.method(:unlink) == File.method(:delete)).
- File.realpath / File.realdirpath raise Errno::ELOOP for a symlink loop instead
  of Errno::ENOENT (raiseRealpathErr).

Dir (internal/vm/dir.go):
- Path-taking singletons (entries/children/exist?/empty?/chdir/each_child/
  foreach/delete) coerce via rb_get_path (#to_path/#to_str) instead of rejecting
  a non-String with a TypeError.
- Dir.getwd is a genuine alias of Dir.pwd; Dir.rmdir and Dir.unlink of Dir.delete.
- Dir includes Enumerable (deferred to post-prelude includeDirEnumerable, since
  registerDir runs before the prelude defines the module).
- Dir.delete enforces rmdir(2) errnos: Errno::ENOTDIR for a non-directory,
  Errno::ENOTEMPTY for a non-empty directory, Errno::EACCES for a permission
  failure (raiseRmdirErr) — was always Errno::ENOENT.
- Dir.each_child / Dir.foreach with no block return a nil-#size Enumerator
  instead of dereferencing a nil block (was a crash); Dir#each / Dir#each_child
  likewise return nil-#size enumerators.
- Add Dir#children and Dir#chdir instance methods (Ruby 2.5+/3.5+).

Out of scope (noted for follow-up): the lexer mis-parses `%w[]` escaped-space
words (splits `a\ b` into two), which fabricates a stray top-level entry in the
Dir spec fixtures and confounds several ordering/glob examples; File# instance
metadata methods (stat/lstat/mtime/atime/ctime/birthtime/flock) live in io.go.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Add internal/vm/wave19_filedir_internal_test.go exercising every new File/Dir
branch against MRI-verified values (ftype, num2mode, umask/chmod coercion,
predicate arity, rename arity, truncate errno, realpath/realdirpath ELOOP, Dir
path coercion + Enumerable, Dir.delete errno mapping, class/instance
enumerators, Dir#children/#chdir, aliases). go tool cover -func reports 100% on
file.go, dir.go and filestat.go.

Fix ELOOP detection: filepath.EvalSymlinks returns a bare "too many links"
error that does not wrap syscall.ELOOP, so realpath/realdirpath re-stat the path
(os.Stat surfaces the kernel's ELOOP) via isSymlinkLoop — verified against ruby
4.0.5 for a self-referential symlink.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…nk deps)

Cover raiseRmdirErr (EACCES/ENOTEMPTY/ENOENT) with synthetic os.PathErrors and
raiseRealpathErr/isSymlinkLoop (ELOOP/ENOENT) through the osStat seam, reusing
the shared expectRaise helper. This keeps 100% coverage of the errno mapping
without relying on running as an unprivileged user or on real symlink cycles,
so the coverage gate holds regardless of the CI environment.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
TestFileTruncateErrno (truncate(-1) => EINVAL) and TestDirDeleteErrno
(delete(nonempty) => ENOTEMPTY) assert POSIX errnos; Windows reports ENOENT for
both, failing the windows lane. Guard those two assertions with
runtime.GOOS != "windows" (the 100% coverage gate runs on the POSIX lanes, so
coverage is unaffected); the other errno cases (ENOENT/ENOTDIR) hold on Windows.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 55b7f4a into main Sep 8, 2026
7 checks passed
@tannevaled
tannevaled deleted the wave19/filedir branch September 8, 2026 05:58
tannevaled added a commit that referenced this pull request Sep 8, 2026
…594)

Wave-19 merged three bibliography-first conformance PRs:
- #593 core/io: pread/pwrite, sysopen, dup, initialize/reopen, to_io/path/
  inspect, set_encoding, ungetbyte, lineno, sysread, std-stream halves — +56.
- #591 core/file+dir: File::Stat surface, ftype, chmod/umask/truncate/rename
  errno + coercion, realpath ELOOP, Dir Enumerable + entries/children/foreach/
  each_child (fixing a nil-pointer crash) — core/file +22, core/dir +29.
- #592 core/kernel: respond_to?/eql?/remove_instance_variable mirrored on
  Kernel, then/yield_self blockless Enumerator — +8.

Measured total on merged main: 19773 passing. Local measure at TIMEOUT=25;
CI runs TIMEOUT=60 so its total is >= this floor.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant