Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factor out lookupExecutable and other PATH improvements #11218

Merged
merged 2 commits into from
Aug 8, 2024

Conversation

Ericson2314
Copy link
Member

Motivation

This ended up motivating a good deal of other infra improvements in order to get Windows right.

Contains some other mics Windows/Meson fixes because there were regressions.

Context

Good for Windows support in general, also buttresses #11178 slightly.

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 July 29, 2024 21:12
@github-actions github-actions bot added new-cli Relating to the "nix" command store Issues and pull requests concerning the Nix store labels Jul 29, 2024
@Ericson2314 Ericson2314 force-pushed the better-executable-path branch from 57c9d8a to 3c761de Compare July 29, 2024 21:32
@Ericson2314 Ericson2314 changed the title Factor out lookupExecutable and other PATH improvments Factor out lookupExecutable and other PATH improvments Jul 29, 2024
@Ericson2314 Ericson2314 force-pushed the better-executable-path branch from 3c761de to 5e6e965 Compare July 29, 2024 21:49
Comment on lines 7 to 11
#ifdef WIN32
# define PATH_VAR_SEP ";"
#else
# define PATH_VAR_SEP ":"
#endif
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to do this again, when executable-path.cc already has the (weirdly capitalized) pATH_separator?

Copy link
Member Author

Choose a reason for hiding this comment

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

I did something similar for unit tests before, I don't mind having the unit tests have their own redundant one-off definition just for a bit of spec vs implementation separation purposes.


std::vector<fs::path> parsePATH(const OsString & path)
{
auto strings = basicTokenizeString<std::list<OsString>, OsString::value_type>(path, pATH_separator);
Copy link
Member

Choose a reason for hiding this comment

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

This is untested and broken.
Feel free to borrow everything from a4b489d (part of #11021). That was a rabbit hole (#11093) I haven't finished climbing back out of, because of other priorities, but I see that you need it here too.

Copy link
Member Author

@Ericson2314 Ericson2314 Aug 2, 2024

Choose a reason for hiding this comment

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

I punted on the path lookup case (with an assert, documented of course) (as opposed to name look up case) since I am not sure sort of error message we want to do, and as it turns out in most cases we are just looking up statically known names.

This brings us back into compliance. I think just in the next PR, for the build hook, we look up a dynamic choice. So we can see what we want to do then.

@Ericson2314 Ericson2314 force-pushed the better-executable-path branch from 5e6e965 to 7c6c854 Compare August 1, 2024 06:38
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/2024-07-31-nix-team-meeting-minutes-166/49972/1

@Ericson2314 Ericson2314 force-pushed the better-executable-path branch 2 times, most recently from 5f8393b to a6d8ba7 Compare August 2, 2024 06:54
@roberth roberth changed the title Factor out lookupExecutable and other PATH improvments Factor out lookupExecutable and other PATH improvements Aug 2, 2024
@Ericson2314
Copy link
Member Author

#11178 (comment) Naming question about "OS" vs "Native" that needs to be resolved.

@Ericson2314 Ericson2314 force-pushed the better-executable-path branch 2 times, most recently from 4e693f3 to 1d55514 Compare August 5, 2024 16:13
@Ericson2314 Ericson2314 force-pushed the better-executable-path branch from 1d55514 to 1216778 Compare August 5, 2024 16:55
src/libutil/executable-path.cc Show resolved Hide resolved
src/libutil/executable-path.hh Outdated Show resolved Hide resolved
src/libutil/file-system.cc Outdated Show resolved Hide resolved
src/libutil/file-system.cc Show resolved Hide resolved
tests/unit/libutil/strings.cc Outdated Show resolved Hide resolved
tests/unit/libutil/strings.cc Outdated Show resolved Hide resolved
This ended up motivating a good deal of other infra improvements in
order to get Windows right:

- `OsString` to complement `std::filesystem::path`

- env var code for working with the underlying `OsString`s

- Rename `PATHNG_LITERAL` to `OS_STR`

- `NativePathTrait` renamed to `OsPathTrait`, given a character template
  parameter until NixOS#9205 is complete.

Split `tests.cc` matching split of `util.{cc,hh}` last year.

Co-authored-by: Robert Hensing <[email protected]>
@Ericson2314 Ericson2314 force-pushed the better-executable-path branch from 20acf6b to 6c861b9 Compare August 7, 2024 22:13
Copy link
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.

Clarification and optional suggestion, but other than that, LGTM

src/libutil/file-system.cc Show resolved Hide resolved
auto s2 = v.render();
EXPECT_EQ(s2, OS_STR("." PATH_VAR_SEP "." PATH_VAR_SEP "." PATH_VAR_SEP "."));
}

Copy link
Member

Choose a reason for hiding this comment

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

It'd be nice to use the isExecutableFile parameter to add a (pure) unit test here.
The test could use a lambda that returns true for a fixed set of paths.

src/libutil/file-system.hh Outdated Show resolved Hide resolved
// "If the pathname being sought contains a <slash>, the search
// through the path prefixes shall not be performed."
// https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_03
assert(OsPathTrait<fs::path::value_type>::rfindPathSep(exe) == exe.npos);
Copy link
Member

Choose a reason for hiding this comment

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

I think they mean to just return exe so that it's later resolved relative to the working directory.

Copy link
Member Author

Choose a reason for hiding this comment

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

I am pretty sure shells do not let you run things in the current directory without ./ as a safety feature? But yeah that is probably extra effort on their part. Regardless, the other find function in the next commit will always return something.

Copy link
Member

Choose a reason for hiding this comment

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

That is correct, but it doesn't mean that a command with a slash is wrong.

$ (export PATH=/foo; cd result/bin; hello ; )
hello: command not found

$ (export PATH=/foo; result/bin/hello ; )
Hello, world!

other find function in the next commit

Doesn't seem to be part of this PR.

I don't think this assertion is useful. The lack of a directory separator is easily observed in the string literals that we tend to pass to this function.

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 store Issues and pull requests concerning the Nix store
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants