-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
testing: t.TempDir() doesn't remove tmp dir and files as expected on Windows OS #50510
Comments
I don't think that the testing package should remove a temporary file created by I don't think there is much we can do about this. People writing tests that run on Windows should be sure to close their temporary files in tests (which is good practice on any system). |
@ianlancetaylor Thank you, it makes sense.But I' still a little confused that the same code behaves differently on different OS, the tmp dir can be removed correctly on Linux even though we don't close file descriptor, can we tolerate this?And because of the Testing hook of tmp dir CleanUp, it usually makes developers forget to Close file, and On Linux it do work fine, which will mislead us that things work fine everywhere. |
That's because Unix and Windows handle open files differently. Unix permits removing an open file, Windows does not. You're right that the fact that it works on Linux may lead people to forget to close the file, but I don't see what we can do about that. Unix and Windows are different, and there is only so much Go can do to hide that fact. |
@ianlancetaylor Thanks a lot! Maybe we can close this. |
#50051 is somewhat related, but it (intentionally) probably won't paper over a missing |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I ran the test func below,which using t.TempDir() to create a tmp dir, from the func's comment, it will do Cleanup to remove the tmp dir after testing ends, but I got an err:
testing.go:967: TempDir RemoveAll cleanup: remove C:\Users\xxx\AppData\Local\Temp\TestTmpDirRemove3730116776\001\foo2619329076.txt: The process cannot access the file because it is being used by another process.
,and I found the tmp dir and file was not removed correctly, after some digging I found that this seems happen only on Windows, Linux works correctly. I tried uncomment
f.Close()
, it worked and the file was removed as expected. There is also some issues mentioned this problem like #44919,but it seems that this problem is not solved.And my question is: Should we call
f.Close()
when using t.TempDir() on Windows to make it work as expected, but this may causeTempDir()
becoming meaningless, or we should fix it on Windows?What did you expect to see?
The tmp was removed and no errors
What did you see instead?
The tmp dir and file was not removed
The text was updated successfully, but these errors were encountered: