Error deleting file

My code unzips a data file, loads it, and deletes the temporary file. It has worked fine in the past but now it fails on the line that deletes the file. It happens in a module I’m working on, ClimateDataIO.jl in ghg_read.jl (line 89). The recent change was switching between DataFrames.readtable to CSV.read.

The error can be reproduced on Windows machines by running the module tests, Pkg.test("ClimateDataIO").

rm(temp)

ERROR: LoadError: unlink: operation not permitted (EPERM)
Stacktrace:
 [1] uv_error at .\libuv.jl:68 [inlined]
 [2] unlink(::String) at .\file.jl:500
 [3] #rm#7(::Bool, ::Bool, ::Function, ::String) at .\file.jl:145
 [4] #ghg_read#4(::Bool, ::Function, ::String) at C:\Users\JohnDoe\.julia\v0.6\ClimateDataIO\src\ghg_read.jl:89
 [5] (::ClimateDataIO.#kw##ghg_read)(::Array{Any,1}, ::ClimateDataIO.#ghg_read, ::String) at .\<missing>:0
 [6] include_from_node1(::String) at .\loading.jl:569
 [7] include(::String) at .\sysimg.jl:14
 [8] process_options(::Base.JLOptions) at .\client.jl:305
 [9] _start() at .\client.jl:371
while loading C:\Users\JohnDoe\.julia\v0.6\ClimateDataIO\test\runtests.jl, in expression starting on line 275

It’s not a problem in Linux/Mac but I’ve confirmed the problem on two different Windows machines. It runs successfully in Coveralls and Codecov.

Calling rm on the same file in REPL works fine. My guess was that the file wasn’t closing properly until the test failed. The code looks fine, as far as I can tell I’ve closed the file every time I’ve opened it. What is wrong here?

Hello, rm(temp) == unlink(temp) (after changing file rights)
It calls directly libuv C code which is responsible for generating your error message.

You can find lot of similar error for nodejs and python, it’s often for hidden or temporary directories.

One corresponding issue is here: https://github.com/libuv/libuv/pull/316
Another is here:https://github.com/libuv/libuv/pull/729

I guess your problem is using a temporary directory on windows.

I saw an issue on julia github to update libuv version, it may fix it.

I tried it on ENV["userprofile"] and joinpath(ENV["userprofile"],"Desktop") and well and neither worked.

Shouldn’t the problem occur at the REPL as well if this is the problem?

Sorry I don’t know and don’t have window to make tests.
Is a different approach consisting in creating a temporary directory to do your job and then deleting it, may be acceptable ?
like line 30:
replacing : temp_dir = tempdir() with temp_dir = mktempdir()
and replacing lines 89:93 with:
rm(temp_dir, true; recursive=true)

I gave it a shot but it didn’t work either. Well worth trying though, thanks for the idea.

What’s the error message when you remove the temporary directory ? it should be different.

It looks the same to me. Line 91 is where the rm command is.

ERROR: LoadError: unlink: operation not permitted (EPERM)
Stacktrace:
 [1] uv_error at .\libuv.jl:68 [inlined]
 [2] unlink(::String) at .\file.jl:500
 [3] #rm#7(::Bool, ::Bool, ::Function, ::String) at .\file.jl:145
 [4] (::Base.Filesystem.#kw##rm)(::Array{Any,1}, ::Base.Filesystem.#rm, ::String) at .\<missing>:0
 [5] #rm#7(::Bool, ::Bool, ::Function, ::String) at .\file.jl:155
 [6] (::Base.Filesystem.#kw##rm)(::Array{Any,1}, ::Base.Filesystem.#rm, ::String) at .\<missing>:0
 [7] #ghg_read#4(::Bool, ::Function, ::String) at C:\Users\JohnDoe\.julia\v0.6\ClimateDataIO\src\ghg_read.jl:91
 [8] (::ClimateDataIO.#kw##ghg_read)(::Array{Any,1}, ::ClimateDataIO.#ghg_read, ::String) at .\<missing>:0
 [9] include_from_node1(::String) at .\loading.jl:576
 [10] include(::String) at .\sysimg.jl:14
 [11] process_options(::Base.JLOptions) at .\client.jl:305
 [12] _start() at .\client.jl:371
while loading C:\Users\JohnDoe\.julia\v0.6\ClimateDataIO\test\runtests.jl, in expression starting on line 275

This command removes the folder without errors. It’s the same usage in the module.

rm("C:\\Users\\JohnDoe\\AppData\\Local\\Temp\\jl_73C.tmp",recursive=true)

Julia 0.6.2 reacts the same.