rstudio / renv

renv: Project environments for R.
https://rstudio.github.io/renv/
MIT License
965 stars 152 forks source link

How to authenticate with the {gitcreds} package upon returning to an older project? #1895

Open tomsing1 opened 2 weeks ago

tomsing1 commented 2 weeks ago

I have an existing {renv} project, whose lockfile includes packages installed from private repositories on github. When I (re)start R in the RStudio project folder, {renv} activates the project (as expected), but immediately tells me that {gitcreds} is not available.

Restarting R session...

ℹ Using R 4.4.0 (lockfile was generated with R 4.3.0)
- GitHub authentication credentials are not available.
- Please set GITHUB_PAT, or ensure the 'gitcreds' package is installed.
- See https://usethis.r-lib.org/articles/git-credentials.html for more details.
Error: failed to resolve remote 'myorg/mypackage' -- error downloading 'https://api.github.com/repos/myorg/mypackage' [error code 22]
Traceback (most recent calls last):
41: source("renv/activate.R")
40: withVisible(eval(ei, envir))
39: eval(ei, envir)
38: eval(ei, envir)
37: local(...) at activate.R#2
36: eval.parent(substitute(eval(quote(expr), envir)))
35: eval(expr, p)
34: eval(expr, p)
33: eval(quote(...), new.env())
32: eval(quote(...), new.env())
31: renv_bootstrap_exec(project, libpath, version) at activate.R#1216
30: if (!renv_bootstrap_load(project, libpath, version))
      renv_bootstrap_run(version, libpath) at activate.R#1049
29: renv::load(project) at activate.R#904
28: renv_load_bioconductor(project, lockfile$Bioconductor)
27: renv_bioconductor_init()
26: renv_bioconductor_init_biocmanager(library)
25: install("BiocManager", library = library, prompt = FALSE)
24: overlay(renv_project_remotes(project), remotes)
23: modifyList(as.list(lhs), as.list(rhs))
22: stopifnot(is.list(x), is.list(val))
21: as.list(lhs)
20: renv_project_remotes(project)
19: renv_description_remotes(descpath)
18: lapply(splat, function(spec) {
        idx <- c(regexpr("=", spec, fixed = TRUE))
        result <- if (idx == -1L) {
            remote <- renv_remotes_resolve(spec)
            list(Package = remote$Package, Remote = remote)
        }
        else {
            package <- substring(spec, 1L, idx - 1L)
            list(Package = package, Remote = function() renv_remotes_resolve(spec))
        }
    })
17: FUN(X[[i]], ...)
16: renv_remotes_resolve(spec)
15: withCallingHandlers(renv_remotes_resolve_impl(spec, latest), 
        error = error)
14: renv_remotes_resolve_impl(spec, latest)
13: renv_remotes_resolve_github(remote)
12: remote$ref %||% renv_remotes_resolve_github_ref(host, user, repo)
11: renv_remotes_resolve_github_ref(host, user, repo)
10: tryCatch(renv_remotes_resolve_github_ref_impl(host, user, repo), 
        error = function(e) {
            warning(e)
            getOption("renv.github.default_branch", default = "master")
        })
 9: tryCatchList(expr, classes, parentenv, handlers)
 8: tryCatchOne(expr, names, parentenv, handlers[[1L]])
 7: value[[3L]](cond)
 6: warning(e)
 5: withRestarts({
        .Internal(.signalCondition(cond, message, call))
        .Internal(.dfltWarn(message, call))
    }, muffleWarning = function() NULL)
 4: withOneRestart(expr, restarts[[1L]])
 3: doWithOneRestart(return(expr), restart)
 2: <condition-handler>(...)
 1: stop(simpleError(message = message, call = e$call))

The {gitcreds} package is installed in my user directory (outside the {renv} project) at /Users/sandmann/Library/R/arm64/4.4/library/gitcreds.

But how do I manage to install it within the {renv} project? Trying the use renv::install() fails because of the same problem {renv} reported above - even though {gitcreds} is available from CRAN.

renv::install("gitcreds")
Error: failed to resolve remote 'myorg/mypackage' -- error downloading 'https://api.github.com/repos/myorg/mypackage' [error code 22]
Traceback (most recent calls last):
25: renv::install("gitcreds")
24: overlay(renv_project_remotes(project), remotes)
23: modifyList(as.list(lhs), as.list(rhs))
22: stopifnot(is.list(x), is.list(val))
21: as.list(lhs)
20: renv_project_remotes(project)
19: renv_description_remotes(descpath)
18: lapply(splat, function(spec) {
        idx <- c(regexpr("=", spec, fixed = TRUE))
        result <- if (idx == -1L) {
            remote <- renv_remotes_resolve(spec)
            list(Package = remote$Package, Remote = remote)
        }
        else {
            package <- substring(spec, 1L, idx - 1L)
            list(Package = package, Remote = function() renv_remotes_resolve(spec))
        }
    })
17: FUN(X[[i]], ...)
16: renv_remotes_resolve(spec)
15: withCallingHandlers(renv_remotes_resolve_impl(spec, latest), 
        error = error)
14: renv_remotes_resolve_impl(spec, latest)
13: renv_remotes_resolve_github(remote)
12: remote$ref %||% renv_remotes_resolve_github_ref(host, user, repo)
11: renv_remotes_resolve_github_ref(host, user, repo)
10: tryCatch(renv_remotes_resolve_github_ref_impl(host, user, repo), 
        error = function(e) {
            warning(e)
            getOption("renv.github.default_branch", default = "master")
        })
 9: tryCatchList(expr, classes, parentenv, handlers)
 8: tryCatchOne(expr, names, parentenv, handlers[[1L]])
 7: value[[3L]](cond)
 6: warning(e)
 5: withRestarts({
        .Internal(.signalCondition(cond, message, call))
        .Internal(.dfltWarn(message, call))
    }, muffleWarning = function() NULL)
 4: withOneRestart(expr, restarts[[1L]])
 3: doWithOneRestart(return(expr), restart)
 2: <condition-handler>(...)
 1: stop(simpleError(message = message, call = e$call))

Many thanks for advice!

SessionInfo ```r sessionInfo() R version 4.4.0 (2024-04-24) Platform: aarch64-apple-darwin20 Running under: macOS Sonoma 14.4.1 Matrix products: default BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0 locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 time zone: America/Los_Angeles tzcode source: internal attached base packages: [1] stats graphics grDevices datasets utils methods base loaded via a namespace (and not attached): [1] compiler_4.4.0 tools_4.4.0 renv_1.0.7 ```
tomsing1 commented 2 weeks ago

Current workaround is to set my GITHUB_PAT environmental variable manually, after the {renv} project has activated. That allows me to install {gitcreds}:

renv::activate()
Sys.setenv("GITHUB_PAT"= "my_token")
renv::install("gitcreds") # succeeds

but I was looking for a way not to have to expose my GITHUB_PAT. I am probably missing something obvious? 🤔

conig commented 1 week ago

I have been hit by this—although I don't have a minimal repo. Recently when using renv::install, I've been given a dialogue to input a PAT. This despite having set up with ssh and being able to download from github private repos fine. Would be great to be able to turn off gitcreds.

kevinushey commented 1 week ago

To confirm, what version of renv are you using? The most recent CRAN release of renv should have some fixes for usages of gitcreds.

conig commented 1 week ago

To confirm, what version of renv are you using? The most recent CRAN release of renv should have some fixes for usages of gitcreds.

I'm seeing this on 1.0.7. If I install gitcreds the problem goes away.

https://github.com/rstudio/renv/assets/12742211/58959b4e-9bde-47bf-865b-a7e04339ce21

kevinushey commented 1 week ago

This despite having set up with ssh and being able to download from github private repos fine.

My guess is you need to set up the SSH agent, or something similar, so that your private keys can be loaded and used in RStudio? (Or perhaps R / renv could try do this on your behalf; I'm not sure.)

kevinushey commented 1 week ago

You might also want to file a separate issue; if I understand correctly, the original issue targeted for this thread was making sure that gitcreds would work when installed; yours is something separate (use SSH keys when available when authenticating with repositories)