From kevinushey at gmail.com  Tue Jul 19 18:46:46 2016
From: kevinushey at gmail.com (Kevin Ushey)
Date: Tue, 19 Jul 2016 09:46:46 -0700
Subject: [Rd] package installation fails when symlink of same name exists
Message-ID: <CAJXgQP328epDSPz7r+LcrXD3o_SgDpzFxo6gTYL++TBdaFo7yQ@mail.gmail.com>

R fails to install a package from source over a pre-existing package
when the path to that package is a symlink, rather than a directory. A
reproducible example to illustrate (using MASS as an example):

    # create a temporary R library in tempdir
    library <- tempfile()
    if (!dir.exists(library)) dir.create(library)

    # symlink MASS from system library to temporary library
    MASS <- find.package("MASS", lib.loc = .Library)
    file.symlink(MASS, file.path(library, "MASS"))

    # use our custom library, and try to install MASS
    .libPaths(library)
    utils::install.packages("MASS", lib = library, type = "source")

On running this, I see a surprising error message:

    ERROR: 'MASS' is not a legal package name

The code that I'm bumping into lives here:

https://github.com/wch/r-source/blob/62f5acbdbdf36e1fc618510312125d1677d79941/src/library/tools/R/install.R#L283-L287

I guess my wish here would be that R would check if any file already
existed at the 'instdir' path, and if it existed and was a symlink, R
would remove that symlink before install. It could happen before
creating the directory, e.g. here:

https://github.com/wch/r-source/blob/62f5acbdbdf36e1fc618510312125d1677d79941/src/library/tools/R/install.R#L277-L281

One thing that was a bit surprising to me -- R does not remove a
pre-existing package installation if it exists (when installing from
source), it merely installs over it, so files / artifacts from a
previous package installation could be left over after installing a
new package. It seems this is not a problem in practice since I don't
think anyone's reported this being an issue before, but for hygiene it
seems like a pre-existing directory could / should be removed when
installing a new package. (It appears that R does clear out a
pre-existing directory when downloading and installing a package
binary directly from CRAN.)

For motivation: I bumped into this when attempting to implement a
package caching feature with packrat. A packrat project using a global
cache will have a (private) R library containing symlinks to R package
installations in a separate, global library. This allows projects to
effectively be isolated from one another, while avoiding duplication
of packages used across multiple projects. Unfortunately, some packrat
users bump into this when attempting to update a package that has
entered the cache (and so is a symlink in their R library).

Thanks for your time,
Kevin

---

$ R --vanilla --slave -e 'sessionInfo()' | head -n 3
R version 3.3.0 Patched (2016-05-23 r70665)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.5 (El Capitan)



[Rd] package installation fails when symlink of same name exists
Kevin Ushey