Skip to content

Failing to load renv from project [...], reloading previously-loaded when loading one renv from the working directory of another via RStudio's system terminal #900

@lorenzwalthert

Description

@lorenzwalthert

The below behavior makes {renv} incompatible with pre-commit for language: r.

{precommit} has recently started to use {renv} as an environment manager for various reasons, and I am quite excited about that. However, there is a problem that this does not work when the repository in which hooks with language: r from pre-commit are used is at the same time a {renv} project (lorenzwalthert/precommit#342). After countless hours of digging, I think I was able to narrow the problem down. When

  • you have a global {renv} version installed
  • two {renv} projects e.g. ~/proj1, and ~/proj2.
  • The two {renv} repos don't use the same (and globally installed) renv version.
  • and you use the RStudio project in ~/proj1 -> open the system terminal in RStudio -> cd ~/proj2 -> R --vanilla.

You can't load the renv from ~/proj2 with source('renv/activate.R').

> search() # just to see there are no renv shims or renv is loaded
[1] ".GlobalEnv"        "package:stats"     "package:graphics" 
[4] "package:grDevices" "package:utils"     "package:datasets" 
[7] "package:methods"   "Autoloads"         "package:base"     
> source('renv/activate.R')
Warning message:
could not load renv from project "~/.cache/pre-commit/repoop2cc2mv/renv-default"; reloading previously-loaded renv 

The message apparently comes from here. It seems like the unload of one renv version and load of another can't be performed cleanly (although there was none loaded or attached to the search path).

What sounds like a very abstractly constructed case, is apparently pretty much what happens when you run pre-commit install --install-hooks or git commit if pre-commit install --install-hooks was not previously called. This means in most user's workflow, pre-commit can't be used in a project where {renv} is present within RStudio (unless you do a tedious workaround outside RStudio, which has to be done repeatedly on every precommit::autoupdate()) because the wrong renv is loaded.

The interesting part is that the correct renv is loaded unless you use the System terminal within RStudio. Outside RStudio, the correct renv is loaded. Also within the R prompt RStudio provides, the correct renv is loaded. I was inspecting environment variables that are set in the system terminal within RStudio and wonder if any environment variable that RStudio sets causes the problem:

> Sys.getenv()
__CF_USER_TEXT_ENCODING
                        0x1F5:0x0:0x2
_CE_CONDA               
_CE_M                   
BASH_SILENCE_DEPRECATION_WARNING
                        1
CLICOLOR_FORCE          1
COLUMNS                 107
CONDA_EXE               /Users/lorenz/opt/miniconda3/bin/conda
CONDA_PYTHON_EXE        /Users/lorenz/opt/miniconda3/bin/python
CONDA_SHLVL             0
DISPLAY                 /private/tmp/com.apple.launchd.l9E1W2GNNS/org.xquartz:0
DYLD_FALLBACK_LIBRARY_PATH
                        /Library/Frameworks/R.framework/Resources/lib:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/server:/Library/Frameworks/R.framework/Resources/lib:/Users/lorenz/lib:/usr/local/lib:/usr/lib:/lib:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/server:/var/folders/6g/bg5mcmsj7s51r33snbxhm1400000gn/T/rstudio-fallback-library-path-XbyCqQ
EDITOR                  vi
GGMAP_GOOGLE_API_KEY    *** 
GIT_ASKPASS             rpostback-askpass
GITHUB_PAT              ***
HISTCONTROL             ignoreboth
HOME                    /Users/lorenz
LANG                    en_US.UTF-8
LC_CTYPE                en_US.UTF-8
LESS                    -R
LINES                   34
LN_S                    ln -s
LOGNAME                 lorenz
LSCOLORS                Gxfxcxdxbxegedabagacad
MAKE                    make
MPLENGINE               tkAgg
PAGER                   /usr/bin/less
PATH                    /Users/lorenz/.poetry/bin:/Users/lorenz/Library/Preferences/netlify/helper/bin:/usr/local/opt/libpq/bin:/Users/lorenz/opt/miniconda3/condabin:/Users/lorenz/bin:/usr/local/bin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Applications/RStudio.app/Contents/MacOS/postback
PROMPT_COMMAND          echo -ne "\033]0;${PWD/#${HOME}/~}\007"
PWD                     /Users/lorenz/git/precommit
PYTHONIOENCODING        utf-8
R_ARCH                  
R_BROWSER               /usr/bin/open
R_BZIPCMD               /usr/bin/bzip2
R_DOC_DIR               /Library/Frameworks/R.framework/Resources/doc
R_GZIPCMD               /usr/bin/gzip
R_HOME                  /Library/Frameworks/R.framework/Resources
R_INCLUDE_DIR           /Library/Frameworks/R.framework/Resources/include
R_LIBS_SITE             
R_LIBS_USER             ~/Library/R/x86_64/4.1/library
R_PAPERSIZE             a4
R_PAPERSIZE_USER        a4
R_PDFVIEWER             /usr/bin/open
R_PLATFORM              x86_64-apple-darwin17.0
R_PRINTCMD              lpr
R_QPDF                  /Library/Frameworks/R.framework/Resources/bin/qpdf
R_RD4PDF                times,inconsolata,hyper
R_SESSION_TMPDIR        /var/folders/6g/bg5mcmsj7s51r33snbxhm1400000gn/T//RtmpQnYU9N
R_SHARE_DIR             /Library/Frameworks/R.framework/Resources/share
R_STRIP_SHARED_LIB      strip -x
R_STRIP_STATIC_LIB      strip -S
R_SYSTEM_ABI            macos,gcc,gxx,gfortran,gfortran
R_TEXI2DVICMD           /usr/local/bin/texi2dvi
R_UNZIPCMD              /usr/bin/unzip
R_ZIPCMD                /usr/bin/zip
RENV_PATHS_RENV         /Users/lorenz/git/precommit.and.proj.renv/renv
RETICULATE_MINICONDA_PYTHON_ENVPATH
                        /Users/lorenz/git/precommit.and.proj.renv/renv/python/r-reticulate
RMARKDOWN_MATHJAX_PATH
                        /Applications/RStudio.app/Contents/Resources/resources/mathjax-27
RS_PPM_FD_READ          17
RS_PPM_FD_WRITE         22
RS_RPOSTBACK_PATH       /Applications/RStudio.app/Contents/MacOS/rpostback
RS_SHARED_SECRET        168072824752491622650073
RSTUDIO_CONSOLE_COLOR   256
RSTUDIO_CONSOLE_WIDTH   100
RSTUDIO_FALLBACK_LIBRARY_PATH
                        /var/folders/6g/bg5mcmsj7s51r33snbxhm1400000gn/T/rstudio-fallback-library-path-XbyCqQ
RSTUDIO_PANDOC          /Applications/RStudio.app/Contents/MacOS/pandoc
RSTUDIO_PROGRAM_MODE    desktop
RSTUDIO_USER_IDENTITY   lorenz
RSTUDIO_WINUTILS        bin/winutils
SDKROOT                 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
SED                     /usr/bin/sed
SHELL                   /bin/zsh
SHLVL                   1
SSH_AGENT_PID           3781
SSH_ASKPASS             rpostback-askpass
SSH_AUTH_SOCK           /var/folders/6g/bg5mcmsj7s51r33snbxhm1400000gn/T//ssh-MlikIsCZS0YT/agent.3780
TAR                     /usr/bin/tar
TERM                    xterm-256color
TMPDIR                  /var/folders/6g/bg5mcmsj7s51r33snbxhm1400000gn/T/
USER                    lorenz
XPC_FLAGS               0x0
XPC_SERVICE_NAME        0
ZSH                     /Users/lorenz/.oh-my-zsh

Which is oviously different from when I use zsh:

> Sys.getenv()
COLORFGBG               0;15
COLORTERM               truecolor
COLUMNS                 80
CONDA_EXE               /Users/lorenz/opt/miniconda3/bin/conda
CONDA_PYTHON_EXE        /Users/lorenz/opt/miniconda3/bin/python
CONDA_SHLVL             0
DISPLAY                 /private/tmp/com.apple.launchd.l9E1W2GNNS/org.xquartz:0
DYLD_FALLBACK_LIBRARY_PATH
                        /Library/Frameworks/R.framework/Resources/lib:/Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home/jre/lib/server
EDITOR                  vi
HOME                    /Users/lorenz
ITERM_PROFILE           Default
ITERM_SESSION_ID        w0t0p0:D85E344D-F118-4AAC-8857-956C9CDE0FA8
LC_CTYPE                UTF-8
LC_TERMINAL             iTerm2
LC_TERMINAL_VERSION     3.4.10
LESS                    -R
LINES                   25
LN_S                    ln -s
LOGNAME                 lorenz
LSCOLORS                Gxfxcxdxbxegedabagacad
MAKE                    make
PAGER                   less
PATH                    /Users/lorenz/.poetry/bin:/Users/lorenz/Library/Preferences/netlify/helper/bin:/usr/local/opt/libpq/bin:/Users/lorenz/opt/miniconda3/condabin:/Users/lorenz/bin:/usr/local/bin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
PWD                     /Users/lorenz/.cache/pre-commit/repoop2cc2mv/renv-default
R_ARCH
R_BROWSER               /usr/bin/open
R_BZIPCMD               /usr/bin/bzip2
R_DOC_DIR               /Library/Frameworks/R.framework/Resources/doc
R_ENVIRON
R_ENVIRON_USER
R_GZIPCMD               /usr/bin/gzip
R_HOME                  /Library/Frameworks/R.framework/Resources
R_INCLUDE_DIR           /Library/Frameworks/R.framework/Resources/include
R_LIBS_SITE
R_LIBS_USER             ~/Library/R/x86_64/4.1/library
R_PAPERSIZE             a4
R_PDFVIEWER             /usr/bin/open
R_PLATFORM              x86_64-apple-darwin17.0
R_PRINTCMD              lpr
R_PROFILE
R_PROFILE_USER
R_QPDF                  /Library/Frameworks/R.framework/Resources/bin/qpdf
R_RD4PDF                times,inconsolata,hyper
R_SESSION_TMPDIR        /var/folders/6g/bg5mcmsj7s51r33snbxhm1400000gn/T//RtmpZWzE1U
R_SHARE_DIR             /Library/Frameworks/R.framework/Resources/share
R_STRIP_SHARED_LIB      strip -x
R_STRIP_STATIC_LIB      strip -S
R_SYSTEM_ABI            macos,gcc,gxx,gfortran,gfortran
R_TEXI2DVICMD           /usr/local/bin/texi2dvi
R_UNZIPCMD              /usr/bin/unzip
R_ZIPCMD                /usr/bin/zip
SED                     /usr/bin/sed
SHELL                   /bin/zsh
SHLVL                   1
SSH_AGENT_PID           487
SSH_AUTH_SOCK           /var/folders/6g/bg5mcmsj7s51r33snbxhm1400000gn/T//ssh-4qglsP4LuJWj/agent.486
TAR                     /usr/bin/tar
TERM                    xterm-256color
TERM_PROGRAM            iTerm.app
TERM_PROGRAM_VERSION    3.4.10
TERM_SESSION_ID         w0t0p0:D85E344D-F118-4AAC-8857-956C9CDE0FA8
TMPDIR                  /var/folders/6g/bg5mcmsj7s51r33snbxhm1400000gn/T/
USER                    lorenz
XPC_FLAGS               0x0
XPC_SERVICE_NAME        0
ZSH                     /Users/lorenz/.oh-my-zsh
_CE_CONDA
_CE_M
__CF_USER_TEXT_ENCODING
                        0x1F5:0x0:0x2

I tried to unset some of the RSTUDIO_* env variables, but no success.

I hope you can provide me some input in how to resolve the problem. Also, after running source('renv/activate.R') a second time, I don't get a warning, but is the correct renv now loaded or not? Calling source('renv/activate.R') multiple times sounds like a hack. The code pre-commit runs on setup can be found here. I don't think it's necessary for this issue to have that context of pre-commit. And the above linked code is not run from the directory in which the user's renv exists, but in a pre-commit cache directory (where also the renv.lock exists for the hooks).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions