Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initializing git in another project from a RStudio session #571

Closed
maelle opened this issue Jan 16, 2019 · 1 comment
Closed

Initializing git in another project from a RStudio session #571

maelle opened this issue Jan 16, 2019 · 1 comment

Comments

@maelle
Copy link
Contributor

@maelle maelle commented Jan 16, 2019

I'm opening this after a bug report in the starters previously called pRojects package cc @moldach. In the package we create and set up projects, possibly from RStudio, without opening them. We're encountering an error when trying to do that from RStudio, at the stage where we initialize a git repo, because usethis:::restart_rstudio() fails.

If I run the following lines in RStudio, not from any project

usethis::create_package("coolcool", open = FALSE)
usethis::proj_set(file.path(getwd(), "coolcool"))
usethis::use_git()

I get

> usethis::create_package("coolcool", open = FALSE)
✔ Creating 'coolcool/'Setting active project to '/home/maelle/coolcool'Creating 'R/'Creating 'man/'Writing 'DESCRIPTION'
Package: coolcool
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R (parsed):
    * First Last <first.last@example.com> [aut, cre]
Description: What the package does (one paragraph).
License: What license it uses
Encoding: UTF-8
LazyData: trueWriting 'NAMESPACE'Writing 'coolcool.Rproj'Adding '.Rproj.user' to '.gitignore'Adding '^coolcool\\.Rproj$', '^\\.Rproj\\.user$' to '.Rbuildignore'Setting active project to '<no active project>'
> usethis::proj_set(file.path(getwd(), "coolcool"))
✔ Setting active project to '/home/maelle/coolcool'
> usethis::use_git()
✔ Initialising Git repoAdding '.Rhistory', '.RData' to '.gitignore'
There are 5 files uncommited files:
* '.gitignore'
* '.Rbuildignore'
* 'coolcool.Rproj'
* 'DESCRIPTION'
* 'NAMESPACE'
Is it ok to commit them?

1: For sure
2: Not now
3: No way

Selection: 1Adding filesCommit with message 'Initial commit'
Error in enc2utf8(path) : argument is not a character vector

I've tried running the corresponding usethis internal functions one by one and the exact line causing the error is

usethis:::in_rstudio(usethis::proj_get())

More precisely what fails inside usethis:::in_rstudio is

fs::path_real(proj)
Error in enc2utf8(path) : argument is not a character vector

because proj is NULL.

I'm wondering whether this is something that could be fixed in usethis i.e. making it possible to initialize a git repo in another project, or whether we in starters should rather use git2r functions directly?

gadenbuie added a commit to gadenbuie/usethis that referenced this issue Feb 1, 2019
@gadenbuie
Copy link
Contributor

@gadenbuie gadenbuie commented Feb 1, 2019

I've also run into this problem and think I can fix it with a pretty straight-forward PR.

I can reproduce the error by calling usethis:::restart_rstudio() from RStudio but not inside a project.

rstudioapi::getActiveProject()
## NULL
usethis:::restart_rstudio()
## Error in enc2utf8(path) : argument is not a character vector
traceback()
## 3: path_real(proj)
## 2: in_rstudio(proj_get())
## 1: usethis:::restart_rstudio()

The error comes from in_rstudio() when usethis' active project is compared with RStudio's active project.

usethis/R/rstudio.R

Lines 97 to 110 in 02fc4b2

# Is base_path open in RStudio?
in_rstudio <- function(base_path = proj_get()) {
if (!rstudioapi::isAvailable()) {
return(FALSE)
}
if (!rstudioapi::hasFun("getActiveProject")) {
return(FALSE)
}
proj <- rstudioapi::getActiveProject()
path_real(proj) == path_real(base_path)
}

The goal of in_rstudio() is then to check that the usethis active project is open in RStudio, in which case it makes sense to restart RStudio by reopening the usethis project.

One very easy solution would be for in_rstudio() to return FALSE when proj is NULL. This would resolve the current issue, but would result in an uninformative message when usethis:::restart_rstudio() is called directly from the command line.

But because this isn't an exported function, this is probably reasonable. I'll submit a PR shortly with this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants