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

Can't read empty environment variables on Windows #8932

mroch opened this issue Sep 10, 2019 · 1 comment


Copy link

commented Sep 10, 2019

On Linux, Sys.getenv on an empty environment variable returns "". On Windows, it raises Not_found.

Note: I have no Windows experience, and I'm not sure it's even valid to have an empty environment variable on Windows, but you can do setx FOO "". It seems possible to make them consistent.


On Windows, Sys.getenv calls caml_win32_getenv and raises Not_found if it returns null:


Lines 360 to 367 in 5ad6430

#ifdef _WIN32
res = caml_win32_getenv(p);
res = caml_secure_getenv(p);
if (res == 0) caml_raise_not_found();
val = caml_copy_string_of_os(res);

caml_win32_getenv returns null when GetEnvironmentVariable returns 0. GetEnvironmentVariable returns the length of the environment variable, not including the null terminator (docs), so it'd return 0 for an empty variable. it also returns 0 on error.


Lines 726 to 731 in 5ad6430

res = GetEnvironmentVariable(lpName, lpBuffer, nSize);
if (res == 0) {
return NULL;

on Linux, getenv returns a char* or NULL, so it can differentiate between an empty string and an error.

I think caml_win32_getenv should only raise Not_found if GetLastError returns ERROR_ENVVAR_NOT_FOUND (per the GetEnvironmentVariable docs).


This comment has been minimized.

Copy link

commented Sep 10, 2019

I have a vague recollection that on Windows assigning an empty string to a variable is equivalent to deleting it, so am not sure how this is supposed to work. @dra27 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
2 participants
You can’t perform that action at this time.