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

Classify empty environment variables as unset #22497

Merged
merged 1 commit into from
Jan 27, 2025

Conversation

sno2
Copy link
Contributor

@sno2 sno2 commented Jan 15, 2025

Modifies EnvVar.isSet() to assume empty strings are unset. This is required for correctly parsing NO_COLOR values, and ifreund suggested that all variables should follow this behavior.

Closes #22380.

Copy link
Member

@ifreund ifreund left a comment

Choose a reason for hiding this comment

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

I think that the proper place to implement this check if the value of a set environment variable is empty would be EnvVar.isSet(), other environment variables such as ZIG_VERBOSE_LINK should have consistent behavior with NO_COLOR

Modifies EnvVar.isSet() to assume empty strings are unset. This is
required for correctly parsing NO_COLOR values, and @ifreund suggested
that all variables should follow this behavior.

Closes ziglang#22380.
@sno2 sno2 force-pushed the fix-color-parsing branch from 458038d to 185f9cf Compare January 22, 2025 17:24
@sno2 sno2 changed the title fix color preference parsing from env Classify empty environment variables as unset Jan 22, 2025
@sno2 sno2 requested a review from ifreund January 23, 2025 18:34
Comment on lines +716 to +719
pub fn isSet(ev: EnvVar, arena: std.mem.Allocator) !bool {
const value = try ev.get(arena) orelse return false;
return value.len != 0;
}
Copy link
Member

Choose a reason for hiding this comment

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

It feels a little silly to be allocating just to check if it has a non-zero length. It's not a huge deal given the places this is called in, but it does make me feel like the std.process API could use some enhancements in the future.

Copy link
Contributor Author

@sno2 sno2 Jan 27, 2025

Choose a reason for hiding this comment

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

I agree. It would be great to have a version of std.process.getEnvVarOwned that includes a boolean if an allocation was required.

Copy link
Member

@ifreund ifreund left a comment

Choose a reason for hiding this comment

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

It is Indeed silly that checking if an environment variable has a zero-length value using the cross-platform std.process API requires allocation.

Nonetheless, I think this patch makes the behavior of the Zig compiler more predictable and in line with established conventions. Improving the std.process API to save some allocations would be nice, but that can be done as a follow-up.

@ifreund ifreund merged commit 2043e8a into ziglang:master Jan 27, 2025
10 checks passed
@sno2 sno2 deleted the fix-color-parsing branch January 27, 2025 17:04
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.

NO_COLOR handling is wrong
4 participants