Why can renv not find library inside docker container after git checkout on GitHub Actions?

So I have a {targets} pipeline which runs on GitHub Actions. The required packages are recorded with {renv} and the whole thing runs in a docker container. Because I would prefer to have a long image build time and a short image run time, renv builds the library when the docker container is built so everything is available at runtime. The actual pipeline is not part of the image - it is checked out at runtime.

Here's the problem - if I run any R code in the GitHub workflow before using 'actions/checkout', renv works fine. But if I run anything after 'actions/checkout' renv can't find the library and instead needs to reinstall everything.

Dockerfile

FROM rocker/verse:4.3.0

ENV RENV_VERSION 0.17.3
RUN R -e "install.packages('remotes', repos = c(CRAN = 'https://cloud.r-project.org'))"
RUN R -e "remotes::install_github('rstudio/renv@${RENV_VERSION}')"

WORKDIR /project
COPY renv.lock renv.lock

ENV RENV_PATHS_LIBRARY renv/library

RUN R -e "renv::restore()"

GitHub Workflow

on:
  push:
    branches:
      - main
      - master
  pull_request:
    branches:
      - main
      - master
  schedule:
    - cron: '0 13 * * 0,2,4'

name: targets

jobs:
  targets:
    runs-on: ubuntu-latest
    container:
      image: (above image)
    env:
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
      LANG: en_US.UTF-8 
    steps:      
      - name: Test if renv works
        shell: Rscript {0}
        run: renv::restore() # Works - up to date with lock file

      - uses: actions/checkout@v3

      - name: Test if renv works after checkout
        shell: Rscript {0}
        run: renv::restore() # Fails - reinstalls library from lockfile

My first thought was that the working directory was being changed, but that's not the case (and, in retrospect, shouldn't matter if the environment variable is set). My second thought was that by checking out the repo I am replacing the renv.lock file. But, these are the same and even if they were a little bit different it doesn't explain why it installs every package again. Totally at a loss for what is happening here.

OK, I have a solution although I still don't really understand the cause. Maybe one of the renv devs can help me understand.

When the repository is checked out, it copies over the renv/ folder and the .Rprofile which sources renv/activate.R. Something in that process (possibly the settings.json) seems to override the environment variable that is set in the docker image that tells renv where to find the library.

My solution is to just not check these files out. That is:

      - uses: actions/checkout@v3
        with:
          sparse-checkout: |
            .
            /*
            !renv/
            !.Rprofile

The pipeline then runs as expected.

RENV_PATHS_LIBRARY should normally take precedence over any other configuration here, so I'm a little surprised too. Is it possible that the checkout step is overwriting the packages already installed in the renv/library library path? If so, then a sparse checkout with !renv/library would probably resolve the issue.

Can you confirm that the library paths used are the same in each stage of the process?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.