Repository navigation
Replace deprecated io/ioutil functions - #5595
Conversation
|
Hey, thanks for the patch! While this looks really good, it also seems like these changes may be consistently triggering one of the CI test flakes (per #4674) we're tracking in our Windows builds. So I think we'll have to pin that down and fix it before we can merge this change. |
There was a problem hiding this comment.
Looking for files that are not formatted correctly...
?? tools/dst3911297958
?? tools/src3438863009
Okay this is weird because ioutil.TempFile simply calls os.CreateTemp starting from Go 1.17+. I didn't expect the CI to fail here
For some reasons they are not being removed by the defer os.Remove calls below 🤔
There was a problem hiding this comment.
Some related links:
- https://stackoverflow.com/a/49163789/7902371
- https://stackoverflow.com/a/64569543/7902371
- os: Remove empty directory fails with "process cannot access the file because it is being used by another process" on <= Windows 10 golang/go#58860
There was a problem hiding this comment.
I agree, this is puzzling that this problem should show up here and now with this particular change; one wonders if perhaps this is just coincidence, but that also seems unlikely. Intruiguing!
I did a little searching around and found one other instance of this idiom in non-test code that's common to all platforms, including Windows, in the Spool() function in tools/iotools.go. I wonder if we should change that defer to also close the file before trying to remove it? And it probably doesn't hurt if we do the same in our Linux- and macOS-specific code, too. However, rather than add those into this PR, I think it's probably a bit cleaner if I just propose those changes in a followup PR.
The io/ioutil package has been deprecated as of Go 1.16 [1]. This commit replaces the existing io/ioutil functions with their new definitions in io and os packages. [1]: https://golang.org/doc/go1.16#ioutil Signed-off-by: Eng Zer Jun <[email protected]>
Reference: #5595 (comment) Signed-off-by: Eng Zer Jun <[email protected]>
chrisd8088
left a comment
There was a problem hiding this comment.
Thanks for all these updates and for digging into the unexpected test failures!
| _ = src.Close() | ||
| _ = os.Remove(src.Name()) |
There was a problem hiding this comment.
I bet we could just call these without assigning them to _ and our somewhat limited linting would be OK with it, but this certainly works too.
There was a problem hiding this comment.
I agree, this is puzzling that this problem should show up here and now with this particular change; one wonders if perhaps this is just coincidence, but that also seems unlikely. Intruiguing!
I did a little searching around and found one other instance of this idiom in non-test code that's common to all platforms, including Windows, in the Spool() function in tools/iotools.go. I wonder if we should change that defer to also close the file before trying to remove it? And it probably doesn't hurt if we do the same in our Linux- and macOS-specific code, too. However, rather than add those into this PR, I think it's probably a bit cleaner if I just propose those changes in a followup PR.
In commit e861b79 of git-lfs#5595 the Windows variant of our tools.CheckCloneFileSupported() function was updated to address the problem that it could fail to remove the temporary files it creates to test whether the "file clone" operation (as used by the "git lfs dedup" command) is supported in a given directory. This problem could occur on Windows because we did not close all of the temporary file's open descriptors, and so our deferred call to os.Remove() might not succeed. In a similar vein, the Windows variant of our tools.CloneFileByPath() function opens both the source and destination file paths provided for a "clone" operation but does not close the resultant file descriptors, so we now add deferred calls to ensure we always close both files. (This minor issue dates from the introduction in commit e55bc4c of the support for the "file clone" operation on ReFS in PR git-lfs#3790.)
In commit e861b79 of git-lfs#5595 the Windows variant of our tools.CheckCloneFileSupported() function was updated to address the problem that it could fail to remove the temporary files it creates to test whether the "file clone" operation (as used by the "git lfs dedup" command) is supported in a given directory. This problem could occur on Windows because we did not close all of the temporary file's open descriptors, and so our deferred call to os.Remove() might not succeed. Although this issue does not affect non-Windows systems, as they generally permit files to be deleted before all open descriptors for the files are closed, we can keep our implementations aligned and consistent if we also make sure to close any files we open in the Linux and macOS variants of our tools.CheckCloneFileSupported() and tools.CloneFileByPath() functions. Note that on macOS, the CheckCloneFileSupported() function creates two temporary files and then only passes them by name to the CloneFileByPath() function, so we can close them immediately after they are opened. This is because the tools.CloneFile() function is not supported on macOS. By constrast, on Linux, the CheckCloneFileSupported() function uses CloneFile(), so it must defer closing the temporary files it creates rather than close them directly after opening them.
In commit e861b79 of git-lfs#5595 the Windows variant of our tools.CheckCloneFileSupported() function was updated to address the problem that it could fail to remove the temporary files it creates to test whether the "file clone" operation (as used by the "git lfs dedup" command) is supported in a given directory. This problem could occur on Windows because we did not close all of the temporary file's open descriptors, and so our deferred call to os.Remove() might not succeed. In a similar vein, we also update our tools.Spool() function (which is used in the "git lfs smudge" command) to ensure that if it creates a temporary file into which it spools data, it always closes that file before trying to remove it. While this should have no advantage on non-Windows systems which allow for files to be deleted while there are still open file descriptors for them, on Windows this change should mean we never fail to clean up our temporary spool file,x if we create one.
In commit e861b79 of git-lfs#5595 the Windows variant of our tools.CheckCloneFileSupported() function was updated to address the problem that it could fail to remove the temporary files it creates to test whether the "file clone" operation (as used by the "git lfs dedup" command) is supported in a given directory. This problem could occur on Windows because we did not close all of the temporary file's open descriptors, and so our deferred call to os.Remove() might not succeed. Although this issue does not affect non-Windows systems, as they generally permit files to be deleted before all open descriptors for the files are closed, we can keep our implementations aligned and consistent if we also make sure to close any files we open in the Linux and macOS variants of our tools.CheckCloneFileSupported() and tools.CloneFileByPath() functions. Note that on macOS, the CheckCloneFileSupported() function creates two temporary files and then only passes them by name to the CloneFileByPath() function, so we can close them immediately after they are opened. This is because the tools.CloneFile() function is not supported on macOS. By constrast, on Linux, the CheckCloneFileSupported() function uses CloneFile(), so it must defer closing the temporary files it creates rather than close them directly after opening them.
In commit e861b79 of git-lfs#5595 the Windows variant of our tools.CheckCloneFileSupported() function was updated to address the problem that it could fail to remove the temporary files it creates to test whether the "file clone" operation (as used by the "git lfs dedup" command) is supported in a given directory. This problem could occur on Windows because we did not close all of the temporary file's open descriptors, and so our deferred call to os.Remove() might not succeed. In a similar vein, we also update our tools.Spool() function (which is used in the "git lfs smudge" command to handle non-pointer data) to ensure that if it creates a temporary file into which it spools data, it always closes that file before trying to remove it. While this should have no advantage on non-Windows systems which allow for files to be deleted while there are still open file descriptors for them, on Windows this change should mean we never fail to clean up our temporary spool file, if we create one.
In commit e861b79 of git-lfs#5595 the Windows variant of our tools.CheckCloneFileSupported() function was updated to address the problem that it could fail to remove the temporary files it creates to test whether the "file clone" operation (as used by the "git lfs dedup" command) is supported in a given directory. This problem could occur on Windows because we did not close all of the temporary file's open descriptors, and so our deferred call to os.Remove() might not succeed. Although this issue does not affect non-Windows systems, as they generally permit files to be deleted before all open descriptors for the files are closed, we can keep our implementations aligned and consistent if we also make sure to close any files we open in the Linux and macOS variants of our tools.CheckCloneFileSupported() and tools.CloneFileByPath() functions. Note that on macOS, the CheckCloneFileSupported() function creates two temporary files and then only passes them by name to the CloneFileByPath() function, so we can close them immediately after they are opened. This is because the tools.CloneFile() function is not supported on macOS. By contrast, on Linux, the CheckCloneFileSupported() function uses CloneFile(), so it must defer closing the temporary files it creates rather than close them directly after opening them.
In commit e861b79 of git-lfs#5595 the Windows variant of our tools.CheckCloneFileSupported() function was updated to address the problem that it could fail to remove the temporary files it creates to test whether the "file clone" operation (as used by the "git lfs dedup" command) is supported in a given directory. This problem could occur on Windows because we did not close all of the temporary file's open descriptors, and so our deferred call to os.Remove() might not succeed. In a similar vein, we also update our tools.Spool() function (which is used in the "git lfs smudge" command to handle non-pointer data) to ensure that if it creates a temporary file into which it spools data, it always closes that file before trying to remove it. While this should have no advantage on non-Windows systems which allow for files to be deleted while there are still open file descriptors for them, on Windows this change should mean we never fail to clean up our temporary spool file, if we create one.
In commit e861b79 of git-lfs#5595 the Windows variant of our tools.CheckCloneFileSupported() function was updated to address the problem that it could fail to remove the temporary files it creates to test whether the "file clone" operation (as used by the "git lfs dedup" command) is supported in a given directory. This problem could occur on Windows because we did not close all of the temporary file's open descriptors, and so our deferred call to os.Remove() might not succeed. In a similar vein, the Windows variant of our tools.CloneFileByPath() function opens both the source and destination file paths provided for a "clone" operation but does not close the resultant file descriptors, so we now add deferred calls to ensure we always close both files. (This minor issue dates from the introduction in commit e55bc4c of the support for the "file clone" operation on ReFS in PR git-lfs#3790.)
In commit e861b79 of git-lfs#5595 the Windows variant of our tools.CheckCloneFileSupported() function was updated to address the problem that it could fail to remove the temporary files it creates to test whether the "file clone" operation (as used by the "git lfs dedup" command) is supported in a given directory. This problem could occur on Windows because we did not close all of the temporary file's open descriptors, and so our deferred call to os.Remove() might not succeed. Although this issue does not affect non-Windows systems, as they generally permit files to be deleted before all open descriptors for the files are closed, we can keep our implementations aligned and consistent if we also make sure to close any files we open in the Linux and macOS variants of our tools.CheckCloneFileSupported() and tools.CloneFileByPath() functions. Note that on macOS, the CheckCloneFileSupported() function creates two temporary files and then only passes them by name to the CloneFileByPath() function, so we can close them immediately after they are opened. This is because the tools.CloneFile() function is not supported on macOS. By contrast, on Linux, the CheckCloneFileSupported() function uses CloneFile(), so it must defer closing the temporary files it creates rather than close them directly after opening them.
In commit e861b79 of git-lfs#5595 the Windows variant of our tools.CheckCloneFileSupported() function was updated to address the problem that it could fail to remove the temporary files it creates to test whether the "file clone" operation (as used by the "git lfs dedup" command) is supported in a given directory. This problem could occur on Windows because we did not close all of the temporary file's open descriptors, and so our deferred call to os.Remove() might not succeed. In a similar vein, we also update our tools.Spool() function (which is used in the "git lfs smudge" command to handle non-pointer data) to ensure that if it creates a temporary file into which it spools data, it always closes that file before trying to remove it. While this should have no advantage on non-Windows systems which allow for files to be deleted while there are still open file descriptors for them, on Windows this change should mean we never fail to clean up our temporary spool file, if we create one.
In commit e861b79 of git-lfs#5595 the Windows variant of our tools.CheckCloneFileSupported() function was updated to address the problem that it could fail to remove the temporary files it creates to test whether the "file clone" operation (as used by the "git lfs dedup" command) is supported in a given directory. This problem could occur on Windows because we did not close all of the temporary file's open descriptors, and so our deferred call to os.Remove() might not succeed. Although this issue does not affect non-Windows systems, as they generally permit files to be deleted before all open descriptors for the files are closed, we can keep our implementations aligned and consistent if we also make sure to close any files we open in the Linux and macOS variants of our tools.CheckCloneFileSupported() and tools.CloneFileByPath() functions. Note that on macOS, the CheckCloneFileSupported() function creates two temporary files and then only passes them by name to the CloneFileByPath() function, so we can close them immediately after they are opened. This is because the tools.CloneFile() function is not supported on macOS. By contrast, on Linux, the CheckCloneFileSupported() function uses CloneFile(), so it must defer closing the temporary files it creates rather than close them directly after opening them.
In commit e861b79 of git-lfs#5595 the Windows variant of our tools.CheckCloneFileSupported() function was updated to address the problem that it could fail to remove the temporary files it creates to test whether the "file clone" operation (as used by the "git lfs dedup" command) is supported in a given directory. This problem could occur on Windows because we did not close all of the temporary file's open descriptors, and so our deferred call to os.Remove() might not succeed. In a similar vein, we also update our tools.Spool() function (which is used in the "git lfs smudge" command to handle non-pointer data) to ensure that if it creates a temporary file into which it spools data, it always closes that file before trying to remove it. While this should have no advantage on non-Windows systems which allow for files to be deleted while there are still open file descriptors for them, on Windows this change should mean we never fail to clean up our temporary spool file, if we create one.
The
io/ioutilpackage has been deprecated as of Go 1.16 1. This commit replaces the existingio/ioutilfunctions with their new definitions inioandospackages.Footnotes
https://golang.org/doc/go1.16#ioutil ↩