Hi, I am trying to use ProcessInfo.processInfo.environment on Linux but I ran into SR-6968.
Basically, it caches the result the first time environment is accessed - subsequent calls always return the same and don’t reflect any changes made to environment variables in the meantime (which Darwin does).
I debugged it down into CoreFoundation and I see that the logic in __CFGetEnvironment() is wrapped in dispatch_once() which explains why the environment variables are only read once.
I want to ask why dispatch_once() was chosen and should it be replaced by dispatch_sync() instead? Or is there another approach that should be taken to fix it?
This found while using Swift 4.0.3 and Ubuntu 16.04.
Code to recreate bug:
import Foundation
let preset = ProcessInfo.processInfo.environment["test"]
setenv("test", "worked", 1)
let postset = ProcessInfo.processInfo.environment["test"]
print(preset)
print(postset)
I think __CFGetEnvrionment should stay the same, but we should probably implement the ProcessInfo.environment call not to use it. The CF call is really for CF's internal use only. As you noted, on Darwin, Foundation doesn't call into it for this method.