Skip to content

Filesystem error improvements#15283

Merged
Ericson2314 merged 1 commit intoNixOS:masterfrom
obsidiansystems:filesytem-error-improvements
Feb 18, 2026
Merged

Filesystem error improvements#15283
Ericson2314 merged 1 commit intoNixOS:masterfrom
obsidiansystems:filesytem-error-improvements

Conversation

@Ericson2314
Copy link
Member

@Ericson2314 Ericson2314 commented Feb 18, 2026

Motivation

File system error improvements

  • Make descriptorToPath cross-platform (renamed from windows::handleToPath). Uses /proc/self/fd on Linux and F_GETPATH on macOS. Add HAVE_F_GETPATH meson check.

    This is based on 7226a11, which was removed in 479c356, but is now introduced more judiciously.

  • Unix error messages in readFull, writeFull, readLine now include file paths via descriptorToPath.

  • Convert std::filesystem::filesystem_error to SystemError

    Wrappers like readLink, createDirs, DirectoryIterator, etc. now catch std::filesystem::filesystem_error and rethrow as SystemError with the error code preserved. This ensures consistent exception types throughout the codebase.

    Call sites that previously caught filesystem_error and rethrew with throw; now throw SystemError(e.code(), ...) instead.

    Some call sites can stop catching filesystem_error at all, because they only call the wrapped functions.

  • Rework SystemError constructors to auto-append error message

    The public SystemError(std::error_code, ...) constructor now automatically appends errorCode.message() to the error message. A protected constructor takes an explicit error message string for subclasses.

    SysError delegates to the protected constructor with strerror(errNo). WinError delegates with renderError(lastError) (now static).

    This removes the need to manually append e.code().message() at call sites when converting filesystem_error to SystemError.

  • Use perfect forwarding (Args &&... with std::forward) consistently in BaseError, SystemError, SysError, and WinError constructors.

Context

Possibly fixes #11376


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@github-actions github-actions bot added new-cli Relating to the "nix" command store Issues and pull requests concerning the Nix store repl The Read Eval Print Loop, "nix repl" command and debugger fetching Networking with the outside (non-Nix) world, input locking labels Feb 18, 2026
@xokdvium
Copy link
Contributor

Probably somewhat fully addresses #11376? Now that we have more wrapper functions for std::filesystem things...

@Ericson2314 Ericson2314 force-pushed the filesytem-error-improvements branch from 3891764 to a53cfcc Compare February 18, 2026 17:25
- Make `descriptorToPath` cross-platform (renamed from
  `windows::handleToPath`). Uses `/proc/self/fd` on Linux and
  `F_GETPATH` on macOS. Add `HAVE_F_GETPATH` meson check.

  This is based on 7226a11, which was
  removed in 479c356, but is now
  introduced more judiciously.

- Unix error messages in `readFull`, `writeFull`, `readLine` now include
  file paths via `descriptorToPath`.

- Convert `std::filesystem::filesystem_error` to `SystemError`

  Wrappers like `readLink`, `createDirs`, `DirectoryIterator`, etc. now
  catch `std::filesystem::filesystem_error` and rethrow as `SystemError`
  with the error code preserved. This ensures consistent exception types
  throughout the codebase.

  Call sites that previously caught `filesystem_error` and rethrew with
  `throw;` now throw `SystemError(e.code(), ...)` instead.

  Some call sites can stop catching `filesystem_error` at all,
  because they only call the wrapped functions.

- Rework `SystemError` constructors to auto-append error message

  The public `SystemError(std::error_code, ...)` constructor now
  automatically appends `errorCode.message()` to the error message.
  A protected constructor takes an explicit error message string for
  subclasses.

  `SysError` delegates to the protected constructor with `strerror(errNo)`.
  `WinError` delegates with `renderError(lastError)` (now static).

  This removes the need to manually append `e.code().message()` at call
  sites when converting `filesystem_error` to `SystemError`.

- Use perfect forwarding (`Args &&...` with `std::forward`) consistently
  in `BaseError`, `SystemError`, `SysError`, and `WinError` constructors.

Co-authored-by: Sergei Zimmerman <sergei@zimmerman.foo>
@Ericson2314 Ericson2314 force-pushed the filesytem-error-improvements branch from a53cfcc to bbcf204 Compare February 18, 2026 17:29
struct ::stat st;
/* Possible since https://github.com/torvalds/linux/commit/55815f70147dcfa3ead5738fd56d3574e2e3c1c2 (3.6) */
if (::fstat(pathFd.get(), &st) == -1)
throw SysError("statting '%s' relative to parent directory via O_PATH file descriptor", path.rel());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also get updated?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be left for later I suppose

Copy link
Contributor

@xokdvium xokdvium left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good to me. The error message improvement is good

@Ericson2314 Ericson2314 added this pull request to the merge queue Feb 18, 2026
Merged via the queue into NixOS:master with commit 08ce8db Feb 18, 2026
14 checks passed
@Ericson2314 Ericson2314 deleted the filesytem-error-improvements branch February 18, 2026 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fetching Networking with the outside (non-Nix) world, input locking new-cli Relating to the "nix" command repl The Read Eval Print Loop, "nix repl" command and debugger store Issues and pull requests concerning the Nix store

Projects

None yet

Development

Successfully merging this pull request may close these issues.

All std::filesystem related errors need to be re-thrown as nix errors

2 participants