HenrikBengtsson / future

:rocket: R package: future: Unified Parallel and Distributed Processing in R for Everyone
https://future.futureverse.org
946 stars 82 forks source link

TESTS: Undo assertions of environment variables may fail on MS Windows #661

Closed HenrikBengtsson closed 1 year ago

HenrikBengtsson commented 1 year ago

Tomas Kalibera wrote on 2023-01-09:

... the future package has been failing on my Windows check server. The test failing is:

Running the tests in 'tests/future,optsenvvars.R' failed.
Last 13 lines of output:
   +     ## Drop empty environment variables, because they are not
supported by
   +     ## MS Windows, but may exist because they're inherited from a
host OS
   +     old_envvars <- old_envvars[nzchar(old_envvars)]
   +     envvars <- Sys.getenv()
   +     envvars <- envvars[nzchar(envvars)]
   +     stopifnot(identical(envvars[names(old_envvars)], old_envvars))
   +   } else {
   +     stopifnot(identical(Sys.getenv()[names(old_envvars)], old_envvars))
   +   }
   +
   +   message(sprintf("- plan('%s') ... DONE", strategy))
   + } ## for (strategy ...)
   - plan('sequential') ...
   Error: identical(envvars[names(old_envvars)], old_envvars) is not TRUE
   Execution halted

I've save the old_envvars and envvars just after they are set up from Sys.getenv().

old_envvars had only one empty variable, named DEBUG.

old_envvars had variables TEMP and TMP, but these were no longer present in envvars, and this is probably why the check fails.

See:

 > old_envvars[["TEMP"]]
[1] "C:\\msys64\\tmp"
 > old_envvars[["TMP"]]
[1] "C:\\msys64\\tmp"
 > old_envvars[["temp"]]
[1] "C:\\Users\\tomas\\AppData\\Local\\Temp"
 > old_envvars[["tmp"]]
[1] "C:\\Users\\tomas\\AppData\\Local\\Temp"
 > envvars[["TEMP"]]
Error in envvars[["TEMP"]] : subscript out of bounds
 > envvars[["TMP"]]
Error in envvars[["TMP"]] : subscript out of bounds
 > envvars[["temp"]]
[1] "C:\\Users\\tomas\\AppData\\Local\\Temp"
 > envvars[["tmp"]]
[1] "C:\\Users\\tomas\\AppData\\Local\\Temp"
 > envvars[["DEBUG"]]
Error in envvars[["DEBUG"]] : subscript out of bounds
 > old_envvars[["DEBUG"]]
[1] ""
HenrikBengtsson commented 1 year ago

My guess is that this has to with environment variables are not case-sensitive on MS Windows, e.g.

> grep("TEMP", names(Sys.getenv()), value = TRUE, ignore.case = TRUE)
[1] "TEMP"
> Sys.getenv("TEMP")
[1] "C:\\Users\\hb\\AppData\\Local\\Temp"

Now, because it's not case-sensitive, we can get TEMP also as temp, or Temp, or tEmP, e.g.

> Sys.getenv(c("temp", "Temp", "tEmP"))
                                 temp                                  Temp
"C:\\Users\\hb\\AppData\\Local\\Temp" "C:\\Users\\hb\\AppData\\Local\\Temp"
                                 tEmP
"C:\\Users\\hb\\AppData\\Local\\Temp"

But, I'm not sure how we can have two different environment variables, TEMP and temp with different values, as in this report, e.g.

> old_envvars[["TEMP"]]
[1] "C:\\msys64\\tmp"
 > old_envvars[["temp"]]
[1] "C:\\Users\\tomas\\AppData\\Local\\Temp"

I first thought it was because one of them is inherited from the host system and the other one is set by MS Windows. But don't know how that can happen, because:

$ temp=foo 2> /dev/null wine "C:\Program Files\R\R-4.1.2\bin\Rscript.exe" --vanilla -e "Sys.getenv()" | grep -E "^(TEMP|temp)"
TEMP                    C:\users\hb\Temp
HenrikBengtsson commented 1 year ago

With can have two environment variables on MS Windows named temp and TEMP when we run R via msys2, e.g.

$ env | grep -iE "^TEMP="
TEMP=/tmp
temp=C:\Users\hb\AppData\Local\Temp

which can be reproduced in a vanilla R session if we do:

$ Rscript --vanilla -e "Sys.getenv()[c('TEMP', 'temp')]"
TEMP                    C:\rtools42\tmp
temp                    C:\Users\hb\AppData\Local\Temp

Interestingly, we can only get the value of temp via Sys.getenv()[["temp"]]. If we attempt Sys.getenv("temp"), we get the value of TEMP, e.g.

 if we do:

$ Rscript --vanilla -e "Sys.getenv(c('TEMP', 'temp'))"
               TEMP                temp
"C:\\rtools42\\tmp" "C:\\rtools42\\tmp"
HenrikBengtsson commented 1 year ago

Fixed and tested on msys2