4

I have a local GitLab account with an R package that can be installed by:

devtools::install_git( 
  url = "http://my-gitlab/my-projects/package",
  credentials = git2r::cred_user_pass("user", "pass")
)

When I run renv::init() the package source is unknown. The getting started article does give details about how to setup functions to deal with private repos but I can't figure it out for GitLab when using devtools and git2r::cred_user_pass. I could store GIT_USER and GIT_PASSWORD in an .Renviron file but I'm not sure how to force the init function to user devtools::install_git with those credentials. I did try but then I get an error:

fatal: could not read Username for 'http://my-gitlab': No such device or address

All the example options they provide use a GitLab token for authentication:

# use a named list directly
options(renv.auth = list(
  MyPackage = list(GITHUB_PAT = "<pat>")
))

Perhaps there is a better way to install from a local GitLab repo that will work better with renv?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Pete900
  • 2,016
  • 1
  • 21
  • 44

2 Answers2

7

For anyone else having the same problem, this worked for me (see here):

  • Create a PAT in GITLAB making sure to set the SCOPE to api. Note: I'm not sure this has to be 'api' I didn't check all options but it worked for me
  • Set the PAT as a env variable using Sys.setenv(GITLAB_PAT = <...>)
  • Set the gitlab host server as an option using options(renv.config.gitlab.host = "http://my-gitlab-server"). Note: I had to include "http://" to get it working.
  • Run renv::install("gitlab::project/package")
  • This did not work for me: renv::install("gitlab@my-gitlab-server::project/package")
Pete900
  • 2,016
  • 1
  • 21
  • 44
4

When I run renv::init() the package source is unknown.

renv infers a package's source based on its DESCRIPTION file, which (for custom remotes) is usually annotated post-hoc by the install function itself. I suspect install_git() is not annotating the package in one of the ways expected by renv, and so renv is unable to infer the package source.

You could try and confirm this by checking the DESCRIPTION file; e.g. a quick way to dump its contents would be:

writeLines(readLines(system.file("DESCRIPTION", package = <package>)))

and check for fields like RemoteType: git to see if the package source has indeed been annotated.


For what it's worth, it looks like devtools::install_git() delegates to remotes::install_git(), and default Git credentials can be set via the remotes.git_credentials option. For example:

options(remotes.git_credentials = git2r::cred_user_pass("user", "pass"))

Ultimately though, renv relies on the use of a private access token (accessed via the GITLAB_PAT environment variable for GitLab) in order to authenticate with private repositories.

Are you able to successfully install the package using renv::install() with GITLAB_PAT set? For example:

Sys.setenv(GITLAB_PAT = <...>)
renv::install("gitlab::user/project")

This is the workflow I would recommend, and should also be supported by the remotes / devtools packages out-of-the-box.

Kevin Ushey
  • 20,530
  • 5
  • 56
  • 88
  • I see `RemoteType: git2r`. I get an error when trying to install with `renv::install` after setting the `GITLAB_PAT`: `download failed [error code 22]`. I'm not sure what path to use though. My package url is http://my-gitlab-server/r-projects/package.git. In that case would the user be `r-projects`? How would I pass the server address? – Pete900 Jul 09 '20 at 07:34
  • I also tried: `renv::install("gitlab::server/my_user_name/package")` but I got the same error. – Pete900 Jul 09 '20 at 07:42
  • 1
    At this point, I think I'd recommend filing an issue at https://github.com/rstudio/renv/issues as this might require more follow-up than is appropriate for Stack Overflow (and hopefully we can update the answer after we find a resolution) – Kevin Ushey Jul 09 '20 at 18:22