Skip to content

Commit

Permalink
fix: last --jobserver-auth wins
Browse files Browse the repository at this point in the history
From the GNU make manual[^1]:

> Be aware that the `MAKEFLAGS` variable may contain multiple
> instances of the `--jobserver-auth=` option.
> Only the last instance is relevant.

Hence this commit makes it so.

With this commit, `--jobserver-auth` also takes precedence over
`--jobserver-fds`, even when `--jobserver-fds` is the last instance
of these flags. This is made intentionally since `--jobserver-fds`
was an undocumented internal-only flag before `--jobserver-auth`
made public, according to this announcement[^2].

[^1]: https://www.gnu.org/software/make/manual/make.html#Job-Slots
[^2]: https://git.savannah.gnu.org/cgit/make.git/tree/NEWS?h=4.2#n31
  • Loading branch information
weihanglo committed Feb 1, 2024
1 parent 1575c78 commit 68a7f8c
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,10 +582,25 @@ impl HelperState {
}
}

/// Finds and returns the value of `--jobserver-auth=<VALUE>` in the given
/// environment variable.
///
/// Precedence rules:
///
/// * The last instance wins [^1].
/// * `--jobserver-fds=` as a fallback when no `--jobserver-auth=` is present [^2].
///
/// [^1]: See ["GNU `make` manual: Sharing Job Slots with GNU `make`"](https://www.gnu.org/software/make/manual/make.html#Job-Slots)
/// _"Be aware that the `MAKEFLAGS` variable may contain multiple instances of
/// the `--jobserver-auth=` option. Only the last instance is relevant."_
///
/// [^2]: Refer to [the release announcement](https://git.savannah.gnu.org/cgit/make.git/tree/NEWS?h=4.2#n31)
/// of GNU Make 4.2, which states that `--jobserver-fds` was initially an
/// internal-only flag and was later renamed to `--jobserver-auth`.
fn find_jobserver_auth(var: &str) -> Option<&str> {
["--jobserver-fds=", "--jobserver-auth="]
["--jobserver-auth=", "--jobserver-fds="]
.iter()
.find_map(|&arg| var.split_once(arg).map(|(_, s)| s))
.find_map(|&arg| var.rsplit_once(arg).map(|(_, s)| s))
.and_then(|s| s.split(' ').next())
}

Expand All @@ -599,17 +614,17 @@ fn no_helper_deadlock() {
#[test]
fn test_find_jobserver_auth() {
let cases = [
("--jobserver-auth=auth-a --jobserver-auth=auth-b", "auth-a"),
("--jobserver-auth=auth-b --jobserver-auth=auth-a", "auth-b"),
("--jobserver-fds=fds-a --jobserver-fds=fds-b", "fds-a"),
("--jobserver-fds=fds-b --jobserver-fds=fds-a", "fds-b"),
("--jobserver-auth=auth-a --jobserver-auth=auth-b", "auth-b"),
("--jobserver-auth=auth-b --jobserver-auth=auth-a", "auth-a"),
("--jobserver-fds=fds-a --jobserver-fds=fds-b", "fds-b"),
("--jobserver-fds=fds-b --jobserver-fds=fds-a", "fds-a"),
(
"--jobserver-auth=auth-a --jobserver-fds=fds-a --jobserver-auth=auth-b",
"fds-a",
"auth-b",
),
(
"--jobserver-fds=fds-a --jobserver-auth=auth-a --jobserver-fds=fds-b",
"fds-a",
"auth-a",
),
];
for (var, expected) in cases {
Expand Down

0 comments on commit 68a7f8c

Please sign in to comment.