Skip to content
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

Trying to read an invalid .xml file makes it so that the file cannot be deleted #376

Open
billdenney opened this issue Oct 29, 2022 · 2 comments
Labels
bug an unexpected problem or unintended behavior

Comments

@billdenney
Copy link

When trying to read an invalid .xml file, I cannot delete the. I think that this is because the connection remains open. If important, this happens on Windows (I've not tested other operating systems).

filename <- tempfile(fileext = ".xml")
bad_xml_file <- writeLines("<a>", con = filename)
file.remove(filename)
#> [1] TRUE

filename <- tempfile(fileext = ".xml")
bad_xml_file <- writeLines("<a>", con = filename)
xml2::read_xml(filename)
#> Error in read_xml.character(filename): EndTag: '</' not found [74]
file.remove(filename)
#> Warning in file.remove(filename): cannot remove file 'C:
#> \Users\wdenn\AppData\Local\Temp\RtmpQR65s3\file5e74463b35d8.xml', reason
#> 'Permission denied'
#> [1] FALSE

Created on 2022-10-29 with reprex v2.0.2

@billdenney
Copy link
Author

My guess is that after

con <- path_to_connection(x)

something like

on.exit({close(con)})

is needed. But, I'm not sure.

@TFKentUSDA
Copy link

TFKentUSDA commented Dec 15, 2022

I have also just run into this problem. at work. I checked it on both my work computer and home computer running several versions of R. In all cases, I'm using xml2_1.3.3. I also verified that this situation appears to be isolated to this package and does not occur if I replace xml2::read_xml(filename) with something else like readxl::read_excel(filename).

Work computer:
RStudio 2022.02.1 Build 461
R version 3.5.3 (2019-03-11)
R version 3.6.3 (2020-02-29)
R version 4.0.3 (2020-10-10)
R version 4.1.3 (2022-03-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Home computer:
RStudio 2022.07.2 Build 576
R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

billdenney added a commit to billdenney/xml2 that referenced this issue Dec 15, 2022
@hadley hadley added the bug an unexpected problem or unintended behavior label Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

3 participants