Skip to content

Replace deprecated io/ioutil functions - #5595

Merged
chrisd8088 merged 3 commits into
git-lfs:mainfrom
Juneezee:refactor/deprecate-ioutil
Jan 11, 2024
Merged

chrisd8088 merged 3 commits into
git-lfs:mainfrom
Juneezee:refactor/deprecate-ioutil

Conversation

@Juneezee

Copy link
Copy Markdown
Contributor

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.

Footnotes

  1. https://golang.org/doc/go1.16#ioutil ↩

@Juneezee
Juneezee requested a review from a team as a code owner December 21, 2023 13:31
@chrisd8088

Copy link
Copy Markdown
Member

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.

Comment thread tools/util_windows.go

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking for files that are not formatted correctly...
?? tools/dst3911297958
?? tools/src3438863009

https://github.com/git-lfs/git-lfs/actions/runs/7477718701/job/20351044777?pr=5595#step:14:361

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 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Juneezee and others added 3 commits January 11, 2024 03:48
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]>

@chrisd8088 chrisd8088 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for all these updates and for digging into the unexpected test failures!

Comment thread tools/util_windows.go
Comment on lines +46 to +47
_ = src.Close()
_ = os.Remove(src.Name())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread tools/util_windows.go

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@chrisd8088
chrisd8088 merged commit ad64c6c into git-lfs:main Jan 11, 2024
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Jan 15, 2024
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.)
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Jan 15, 2024
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.
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Jan 15, 2024
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.
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Jan 15, 2024
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.
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Jan 15, 2024
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.
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Jan 15, 2024
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.
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Jan 15, 2024
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.
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Jan 15, 2024
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.)
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Jan 15, 2024
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.
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Jan 15, 2024
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.
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Jan 16, 2024
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.
chrisd8088 added a commit to chrisd8088/git-lfs that referenced this pull request Jan 16, 2024
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants