From hb at biostat.ucsf.edu  Fri Oct 11 03:15:52 2013
From: hb at biostat.ucsf.edu (Henrik Bengtsson)
Date: Thu, 10 Oct 2013 18:15:52 -0700
Subject: [Rd] install.packages() removes package on Windows
In-Reply-To: <702440245.1273820.1381447687920.JavaMail.root@fhcrc.org>
References: <433997010.1272981.1381443245564.JavaMail.root@fhcrc.org>
	<702440245.1273820.1381447687920.JavaMail.root@fhcrc.org>
Message-ID: <CAFDcVCTvhokihvGs+--c0y+EYbOYyq465KOHYXEqyFJR9iOFEA@mail.gmail.com>

My guess is that the DLL of the already installed XML package is
loaded by another R session and that prevents the corresponding DLL
file:

> path <- system.file("libs", package="XML")
> list.files(path, recursive=TRUE, pattern="dll$")
[1] "i386/XML.dll" "x64/XML.dll"

from being deleted by install.packages() -> unpackPkgZip() ->

            ret <- unlink(instPath, recursive=TRUE, force=TRUE)

which gives the warnings.  You can verify this by checking that all
files but the DLLs are deleted;

> path <- system.file("libs", package="XML")
> list.files(path, recursive=TRUE)


REPRODUCIBLE EXAMPLE:

# Download package with native code (=has DLLs)
url <- "http://cran.r-project.org/bin/windows/contrib/r-release/png_0.1-6.zip"
pkgfile <- basename(url)
if (!file_test("-f", pkgfile)) download.file(url, dest=pkgfile, mode="wb")

# Setup temporary library
if (!file_test("-d", "local-libs")) dir.create("local-libs")

# Install to temporary library
install.packages(pkgfile, repos=NULL, lib="local-libs")

# Record package path
path <- system.file(package="png", lib.loc="local-libs")

# Launch *another* session that loads the package
Rscript <- file.path(R.home("bin"), "Rscript")
code <- "library('png', lib.loc='local-libs'); img <-
readPNG(system.file('img','Rlogo.png',package='png')); Sys.sleep(60)"
code <- "library('png', lib.loc='local-libs'); Sys.sleep(60)"
system2(Rscript, args=c("-e", dQuote(code)), wait=FALSE)

# Try to install; will fail
install.packages(pkgfile, repos=NULL, lib="local-libs")
## Gives:
## package 'png' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'png'
files <- list.files(path, recursive=TRUE)
print(files)
## [1] "libs/x64/png.dll"


SUGGESTION:
The problem is that unlink(..., recursive=TRUE) is not atomic, leaving
a corrupt installation behind.  A better solution would be to use
file.rename() to move the existing installation to a temporary
directory/location, move the new installation in place, and then
remove the temporary/old one.  The last step will fail if there is a
session holding onto the DLL, but at least it does not leave a corrupt
installation behind.


/Henrik

On Thu, Oct 10, 2013 at 4:28 PM, Dan Tenenbaum <dtenenba at fhcrc.org> wrote:
> Hi,
>
> Starting with the XML package installed:
>
>> "XML" %in% rownames(installed.packages())
> [1] TRUE
>>
>
> I ran the following script:
>
> pkgs <- c("XML")
>
> for (i in 1:100)
> {
>     install.packages(pkgs, repos="http://cran.fhcrc.org")
>     if (!all(pkgs %in% rownames(installed.packages())))
>     {
>         print("failed to install pkgs!")
>         print(paste("Iteration", i))
>         break
>     }
> }
>
> And it failed on the third iteration:
>
> trying URL 'http://cran.fhcrc.org/bin/windows/contrib/3.0/XML_3.98-1.1.zip'
> Content type 'application/zip' length 4287270 bytes (4.1 Mb)
> opened URL
> downloaded 4.1 Mb
>
> package 'XML' successfully unpacked and MD5 sums checked
>
> The downloaded binary packages are in
>         C:\Users\biocbuild\AppData\Local\Temp\3\Rtmps7OWh0\downloaded_packages
> trying URL 'http://cran.fhcrc.org/bin/windows/contrib/3.0/XML_3.98-1.1.zip'
> Content type 'application/zip' length 4287270 bytes (4.1 Mb)
> opened URL
> downloaded 4.1 Mb
>
> package 'XML' successfully unpacked and MD5 sums checked
>
> The downloaded binary packages are in
>         C:\Users\biocbuild\AppData\Local\Temp\3\Rtmps7OWh0\downloaded_packages
> trying URL 'http://cran.fhcrc.org/bin/windows/contrib/3.0/XML_3.98-1.1.zip'
> Content type 'application/zip' length 4287270 bytes (4.1 Mb)
> opened URL
> downloaded 4.1 Mb
>
> package 'XML' successfully unpacked and MD5 sums checked
> Warning: cannot remove prior installation of package 'XML'
>
> The downloaded binary packages are in
>         C:\Users\biocbuild\AppData\Local\Temp\3\Rtmps7OWh0\downloaded_packages
> [1] "failed to install pkgs!"
> [1] "Iteration 3"
>
> At this point the XML package is not installed:
>
>> "XML" %in% rownames(installed.packages())
> [1] FALSE
>
> Any idea what could cause this? There is no virus scanner running.
>
> I notice the warning about failing to remove prior installation, but it looks like it removed enough of it so that XML is no longer installed.
>
> I realize my script is a little contrived but I'm trying to track down an elusive problem in our build system that is causing a lot of grief....this may or may not be the same problem but it's certainly a problem, so I thought I'd report it.
>
> Is there a workaround?
>
> I've only ever seen this issue on Windows.
>
> I did try running the same script on a vanilla windows machine and it did not fail. In fact, it does not always fail on the machine where it fails above. But once is enough to mess us up.
>
>
>> sessionInfo()
> R version 3.0.2 (2013-09-25)
> Platform: i386-w64-mingw32/i386 (32-bit)
>
> locale:
> [1] LC_COLLATE=English_United States.1252
> [2] LC_CTYPE=English_United States.1252
> [3] LC_MONETARY=English_United States.1252
> [4] LC_NUMERIC=C
> [5] LC_TIME=English_United States.1252
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> Thanks,
> Dan
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] install.packages() removes package on Windows
Henrik Bengtsson