Mike Franklin had a heck of a time dealing with an apparent bug in `std.process` where setting an environment variable in windows doesn't appear to take effect. By "take effect" I mean, a child process did not receive the new value set by the parent process using `environemnt["VAR"] = "VALUE";`.
phobos uses the `SetEnvironmentVariableW` function for all windows platforms/runtimes, however, Mike only started having success once he started using `putenv` for `version(Win32)`.
PR for reference: https://github.com/dlang/dmd/pull/7845
Comment #1 by r.sagitario — 2018-02-12T21:47:48Z
Here's a simple test:
import std.process;
import core.stdc.stdlib;
import core.stdc.stdio;
extern(C) int putenv(const char*);
void main()
{
putenv("DFLAGS=1");
printf("1.DFLAGS=%s\n", getenv("DFLAGS"));
environment["DLAGS"] = "2";
printf("2.DFLAGS=%s\n", getenv("DFLAGS"));
system("echo 3.DFLAGS=%DFLAGS%");
}
It prints "1" three times.
The issue is that the C runtime caches the environment changes, but std.process.environment bypasses the cache. Subsequent C runtime calls like getenv, system or spawn use the cached environment.
Comment #2 by r.sagitario — 2018-02-12T21:50:36Z
Happens with both dmc and VC libraries.
Comment #3 by schveiguy — 2018-02-15T17:12:12Z
(In reply to Rainer Schuetze from comment #1)
> environment["DLAGS"] = "2";
Was this a hand-copied typo, or was there a typo in the original?
> The issue is that the C runtime caches the environment changes, but
> std.process.environment bypasses the cache. Subsequent C runtime calls like
> getenv, system or spawn use the cached environment.
Bleh, too bad we can't update snn.lib. This is like papering over the problem.
I looked at the code, and it's very complex, so I don't know that it's worth trying to fix.
Comment #4 by r.sagitario — 2018-02-16T15:44:32Z
> Was this a hand-copied typo, or was there a typo in the original?
Ooops. Typo is in the test only. The result with DFLAGS is the same, though.
> I looked at the code, and it's very complex, so I don't know that it's worth trying to fix.
We could do the same as for posix: call the C runtime instead of SetEnvironmentVariable. Both dmc and VC have _wputenv
Comment #5 by robert.schadek — 2024-12-01T16:32:36Z