`tempdir()` differences in Windows and Linux

Using Julia v1.1.0 on Windows 10, tempdir() returns a path with a separator at the end:

julia> tempdir()
“C:\Users\mxbiee\AppData\Local\Temp\”

and on Linux, it doesn’t:

julia> tempdir()
“/tmp”

This caused some mildly hard-to-track issues when “porting” some Linux-developed code to Windows. I realise that some system differences are hard to avoid, but this one seems a bit arbritrary.

See https://github.com/JuliaLang/julia/pull/31434. How did you use tempdir() to cause this problem? If you use e.g. joinpath a trailing / should not matter I think.

2 Likes

Thanks for the link! I missed that issue (for some reason it doesn’t come up on a search for tempdir).

I had some very old test code where I wanted a temporary file with a specific name, something like
outfile = "$(tempdir())/specifictest"
which works on Linux but not on Windows, which gets confused by the /.

Now I’m using tempname().

The \ problem can be avoided by using joinpath/splitpath whenever paths are employed. It still requires specific handling whenever reading valid paths generated outside julia.

1 Like

That is correct – but it’d still be nice to have tempdir() behave consistently regarding the trailing slash.

I am not sure why it would matter. You cannot consistently rely on the presence of trailing /s in most contexts, and as others have pointed out, should always use the relevant API for working with paths, instead of treating them as strings.

Incidentally, perhaps it would be useful to experiment with a specific type for paths (in a package), possibly wrapping a string but in an opaque way. Beside trivial puns (eg * could be joinpath, abs would make absolute paths), it would enforce the API more idiomatically.

See GitHub - rofinn/FilePaths.jl: A type based approach to working with filesystem paths in julia and https://github.com/JuliaLang/Juleps/pull/26

1 Like