Skip to content

libutil: return 0 from setEnv on success on Windows - #16354

Merged
Ericson2314 merged 1 commit into
NixOS:masterfrom
awsmadi:pr/setenv-return-convention
Aug 24, 2026
Merged

libutil: return 0 from setEnv on success on Windows#16354
Ericson2314 merged 1 commit into
NixOS:masterfrom
awsmadi:pr/setenv-return-convention

Conversation

@awsmadi

@awsmadi awsmadi commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

environment-variables.hh describes these as POSIX-like: "Like POSIX setenv, but
always overrides." POSIX returns 0 on success and -1 on failure.
SetEnvironmentVariable reports the opposite, nonzero for success, and negating it
produced -1 for success and 0 for failure.

So on Windows all four of setEnv, setEnvOs, unsetenv and unsetEnvOs returned
-1 when they worked.

Nothing is broken by this today: no caller in the tree checks the return value. The
first thing to look at it was a test I was writing for a different change, which
failed on ASSERT_EQ(setEnvOs(name, OS_STR("")), 0) with "Which is: -1".

Two tests in libutil-tests covering the convention. They pass on both platforms
and fail on Windows without this change.

nix-util-tests passes at 784 under Wine and builds clean natively.

The header describes these as POSIX-like, where success is 0 and failure -1.
SetEnvironmentVariable reports the opposite, nonzero for success, and negating
it gave -1 for success and 0 for failure.

No caller checks the return value today, so nothing is currently broken by it;
the first thing to look was a test.

Assisted-by: Claude Code (claude-opus-5)
@awsmadi
awsmadi requested a review from edolstra as a code owner August 24, 2026 18:02
@Ericson2314
Ericson2314 enabled auto-merge August 24, 2026 18:48
@Ericson2314

Ericson2314 commented Aug 24, 2026

Copy link
Copy Markdown
Member

@awsmadi I am going to approve and queue this as an incremental improvement, but I suspect that what we really want to happen is that these wrapper functions return Void, and instead we throw SysError or throw WinError accordingly.

Let's look into that in a follow-up PR.

@Ericson2314
Ericson2314 added this pull request to the merge queue Aug 24, 2026
Merged via the queue into NixOS:master with commit 60d6793 Aug 24, 2026
16 checks passed
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