Skip to content

Slightly more std::filesystem for nix eval#11650

Merged
roberth merged 1 commit intoNixOS:masterfrom
obsidiansystems:nix-eval-slight-fs-cleanup
Oct 9, 2024
Merged

Slightly more std::filesystem for nix eval#11650
roberth merged 1 commit intoNixOS:masterfrom
obsidiansystems:nix-eval-slight-fs-cleanup

Conversation

@Ericson2314
Copy link
Copy Markdown
Member

Context

Progress on #9205

Priorities and Process

Add 👍 to pull requests you find important.

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

@Ericson2314 Ericson2314 requested a review from edolstra as a code owner October 7, 2024 14:49
@github-actions github-actions bot added the new-cli Relating to the "nix" command label Oct 7, 2024
Comment on lines +14 to +15
namespace nix::fs { using namespace std::filesystem; }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nitpick(non-blocking): Could maybe even move this inside of the class declaration.

Suggested change
namespace nix::fs { using namespace std::filesystem; }
using fs = std::filesystem;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Actually it can't be that, I'll say why below

@@ -11,11 +11,13 @@

using namespace nix;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nitpick(non-blocking): Would prefer to see this wrap the whole file.


using namespace nix;

namespace nix::fs { using namespace std::filesystem; }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I hadn't noticed this before, but there are a few of these nix::fs namespace declarations in src/nix now. I don't really understand what that's for? Just use std::filesystem so people don't have to wonder what nix::fs is.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That's because of

namespace fs {
/**
* ```
* symlink_exists(p) = std::filesystem::exists(std::filesystem::symlink_status(p))
* ```
* Missing convenience analogous to
* ```
* std::filesystem::exists(p) = std::filesystem::exists(std::filesystem::status(p))
* ```
*/
inline bool symlink_exists(const std::filesystem::path & path) {
return std::filesystem::exists(std::filesystem::symlink_status(path));
}
} // namespace fs

I hope that with more std::filesystem, most of file-system.hh goes away. However, there are a few things that aren't in there they we need, like this symlink_exists.

I put it in an fs namespace with the idea that if C++ files (not headers!) use fs:: as a shorthand for std::filesystem::, then it will nicely gell with the existing apis.

namespace nix::fs { using namespace std::filesystem; }

I do because if I do

using fs = std::filesystem;

then that fs will shadow, rather than expand, the namespace fs I put in file-system.hh.

I tried a few combinations, and the thing you commented on is the only one that will work (other than declaring symlink_exists in std::filesystem directly, which would be ill-advised.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

clangd reports for an fs::current_path identifier:

function current_path
provided by

→ path

// In namespace std::filesystem
path current_path()

This pattern also seems useful for polyfilling functions from new C++ versions.

@Ericson2314 Ericson2314 force-pushed the nix-eval-slight-fs-cleanup branch from 53fa147 to b5c8865 Compare October 7, 2024 16:10
Copy link
Copy Markdown
Member

@roberth roberth left a comment

Choose a reason for hiding this comment

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

Refactor LGTM. Details can be improved if need be.

@roberth roberth merged commit 4db9487 into NixOS:master Oct 9, 2024
@Ericson2314 Ericson2314 deleted the nix-eval-slight-fs-cleanup branch October 9, 2024 20:56
@xokdvium
Copy link
Copy Markdown
Contributor

Am I missing something or is this borked with release builds without asserts?

// TODO abstract mkdir perms for Windows
createDir(path.string(), 0777);
// Directory should not already exist
assert(fs::create_directory(path.string()));

Without asserts the call to fs::create_directory will be stripped out. More appropriate would be:

[[maybe_unused]] bool created = fs::create_directory(path.string());
assert(created);

@Ericson2314
Copy link
Copy Markdown
Member Author

Ericson2314 commented Oct 21, 2024

@xokdvium Sorry I missed your message above!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new-cli Relating to the "nix" command

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants