Thanks to visit codestin.com
Credit goes to github.com

Skip to content

libutil: Make MemorySourceAccessor throw more precise errors#14862

Merged
xokdvium merged 1 commit intomasterfrom
more-precise-exceptions-memory-source-accessor
Dec 24, 2025
Merged

libutil: Make MemorySourceAccessor throw more precise errors#14862
xokdvium merged 1 commit intomasterfrom
more-precise-exceptions-memory-source-accessor

Conversation

@xokdvium
Copy link
Contributor

Motivation

Makes the error messages render paths correctly, also introduces a new hierarchy of error classes for SourceAccessor related errors that we might want to handle differently (e.g. like when doing a readFile on a directory and such). This should make it easier to implement better UnionSourceAccessor and AllowListSourceAccessor by catching these errors consistently.

Context


Add 👍 to pull requests you find important.

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

Makes the error messages render paths correctly, also introduces
a new hierarchy of error classes for SourceAccessor related errors
that we might want to handle differently (e.g. like when doing a readFile
on a directory and such). This should make it easier to implement better
UnionSourceAccessor and AllowListSourceAccessor by catching these errors
consistently.
@xokdvium xokdvium requested a review from Ericson2314 December 23, 2025 22:51
@xokdvium xokdvium requested a review from edolstra as a code owner December 23, 2025 22:51
Copy link
Contributor

@Radvendii Radvendii left a comment

Choose a reason for hiding this comment

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

This seems like an improvement to me, amd I don't see any problems. Approved.

A couple of tests fail locally, but they also fail on master, and they don't fail in CI, so I think that's an unrelated issue.

My comments are additional improvements that looking at this PR made me think of. We could roll them into this PR or tackle them separately or not at all.

Additional behaviours we might want to add tests for:

  • readDirectory(CanonPath("/")) with an empty source accessor
  • readFile() / readDirectory() called on a symlink
  • createRegularFile() / createDirectory() when the file already exists and is a symlink
  • create*() when a parent directory is a symlink (either to a directory (valid) or to a regular file (invalid))
  • createFile() on something that's already a file
  • createDirectory() on something that's already a directory

r->contents = std::move(contents);
else
throw Error("file '%s' is not a regular file", path);
throw NotARegularFile("file '%s' is not a regular file", showPath(path));
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 be "cannot be created because it already exists and is not a regular file" or something like that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, ideally. I didn't want to make the scope too large for now, since MemorySourceAccessor isn't very user-facing.

throw Error("directory '%s' cannot be created because some parent file is not a directory", dst.showPath(path));

if (!std::holds_alternative<File::Directory>(f->raw))
throw Error("file '%s' is not a directory", path);
Copy link
Contributor

Choose a reason for hiding this comment

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

similar transformation here "cannot be created because it already exists and is not a directory"

@xokdvium xokdvium added this pull request to the merge queue Dec 24, 2025
Merged via the queue into master with commit a3bcd25 Dec 24, 2025
20 checks passed
@xokdvium xokdvium deleted the more-precise-exceptions-memory-source-accessor branch December 24, 2025 21:02
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.

2 participants

Comments