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

Account for "Git: ... no available repositories" in rstudioapi::executeCommand("vcsRefresh") #2697

Closed
jennybc opened this issue Apr 8, 2024 · 4 comments

Comments

@jennybc
Copy link
Member

jennybc commented Apr 8, 2024

Reported in private beta: https://github.com/posit-dev/positron-beta/discussions/63

Apparently, rstudioapi::executeCommand("vcsRefresh") is a silent no-op in RStudio if the current project is not a Git repo.

However, in Positron, this creates a pop-up (which can just be dismissed, but it's still distracting/confusing):

image

usethis calls its internal rstudio_git_tickle() often to force RStudio to refresh its git pane. This was done in reaction to confusion if "Git facts" have changed on the ground, but the RStudio UI didn't reflect that.

It seems like vcsRefresh might require a bit more wrapping in our rstudioapi shim, i.e. do some sort of pre-check.

https://github.com/r-lib/usethis/blob/9e64daf13ac1636187d59e6446d9526a414d8ba6/R/rstudio.R#L184-L189

@jennybc
Copy link
Member Author

jennybc commented Jun 28, 2024

I've looked into this a bit more and I suspect @hadley's hypothesis of a race condition is correct:

r-lib/usethis#2011 (comment)

Changing the definition of usethis:::rstudio_git_tickle() to add uses_git() to the guard does not eliminate the pop-up for plain old usethis::use_git():

rstudio_git_tickle <- function() {
  if (uses_git() && rstudioapi::hasFun("executeCommand")) {
    rstudioapi::executeCommand("vcsRefresh")
  }
  invisible()
}

So now I think I may be wrong about this having (only?) to do with rstudio_git_tickle() and vcsRefresh. With use_git(), it's starting to look like the pop-up comes from some other git operation, such as gert::git_status(). In any case, I can apply a bandaid in usethis by adding a wee bit of sleep after initializing the repo and adding the guard above (to address the case when we truly aren't in a git repo).

@juliasilge
Copy link
Contributor

This comes up, as described in #4002, when you run devtools::test() on an R package that is not a git repo.

@juliasilge juliasilge self-assigned this Jul 26, 2024
juliasilge added a commit that referenced this issue Jul 30, 2024
Goes along with posit-dev/ark#449

I was thinking about how to address
#2697 and have a proposed
approach in this PR plus the accompanying PR for ark. I looked at how
various extensions (like the git extension, etc) check for whether we
are in a git repo in the workspace and all that checking is typically
based on context keys. In an extension specifically, you can check those
keys with a ["when"
clause](https://code.visualstudio.com/api/references/when-clause-contexts)
and I realized that approach (i.e. a string) could work well as an
OpenRPC method and would be flexible for other context keys we will need
to check when we are not in the main thread.

Alternatively, we could add methods to return more specific info (are we
in a git repo? etc etc etc) but I like the idea of using "when" clauses
for this, like an extension could directly for commands, etc.

### QA Notes

Evaluating code like this in R should show the correct results for your
situation:

```r
.ps.ui.evaluateWhenClause("isLinux || isWindows")
.ps.ui.evaluateWhenClause("isMac")
.ps.ui.evaluateWhenClause("gitOpenRepositoryCount >= 1")
```

---------

Signed-off-by: Julia Silge <[email protected]>
@juliasilge
Copy link
Contributor

To verify this, there are a couple of options but probably the most direct/easy is to run rstudioapi::executeCommand("vcsRefresh") in a workspace that is (git pane will refresh) and is not (nothing happens, including no popup) a git repo.

@testlabauto
Copy link
Contributor

Verified Fixed

Positron Version(s) : 2024.07.0-125
OS Version          : OSX

Test scenario(s)

rstudioapi::executeCommand("vcsRefresh") in a git repo and a plain folder.

Link(s) to TestRail test cases run or created:
N/A

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants