Skip to content

fix getMode() fallback for old rstudio versions#327

Merged
kevinushey merged 2 commits into
mainfrom
bugfix/getmode-old-rstudio
May 28, 2026
Merged

fix getMode() fallback for old rstudio versions#327
kevinushey merged 2 commits into
mainfrom
bugfix/getmode-old-rstudio

Conversation

@kevinushey

Copy link
Copy Markdown
Contributor

Fixes #326.

Problem

On very old versions of RStudio, getMode() fails with attempt to apply non-function. Neither path works:

  • hasFun("getMode") is FALSE (the API function is recent), so the fallback runs.
  • The fallback calls rstudio$.rs.isDesktop(), but .rs.isDesktop does not exist in that old version, so it resolves to NULL and calling it errors.

Why .rs.isDesktop() is in the fallback

getMode() was added in #280 as a fast shortcut for versionInfo()$mode. versionInfo() is slow because it reads the citation file (readCitationFile()), and the benchmark in that issue showed the internal call at ~410ns vs versionInfo()$mode at ~386us. .rs.isDesktop() was chosen specifically to avoid that cost, so simply replacing the fallback with versionInfo()$mode would reintroduce the slowness #280 fixed.

Fix

Make the fallback three-tier so it honors the performance intent and works on old versions:

  1. API getMode -- fast path on modern RStudio (unchanged).
  2. .rs.isDesktop() -- retained, but guarded with is.function() so a missing helper no longer throws.
  3. versionInfo()$mode -- new final fallback for ancient RStudio that lacks both. $mode has been available since RStudio 0.97.124.

The slow versionInfo() path only triggers on RStudio old enough to lack both the API function and .rs.isDesktop(), so callers like cli stay on the fast path on current RStudio.

Also switched as.environment("tools:rstudio") to the existing toolsEnv() helper, for consistency with the rest of the file and to make the path mockable.

Tests

Added tests/testthat/test-code.R covering both the .rs.isDesktop() path (and asserting versionInfo() is not called there) and the versionInfo() fallback when the helper is absent. devtools::test(): 20 pass, 0 fail (the 1 skip is the pre-existing RStudio-only document-API test).

On very old versions of RStudio that lack the internal .rs.isDesktop()
helper, the getMode() fallback path threw 'attempt to apply
non-function'. Guard the .rs.isDesktop() call and fall back to
versionInfo()$mode (available since RStudio 0.97.124) when it is absent.

The .rs.isDesktop() check is retained as the preferred fallback for
performance reasons (#280): versionInfo() is slow because it reads the
citation file.

Fixes #326.
@kevinushey kevinushey merged commit df4e683 into main May 28, 2026
6 of 7 checks passed
@kevinushey kevinushey deleted the bugfix/getmode-old-rstudio branch May 28, 2026 21:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rstudio$.rs.isDesktop() fails on very old versions of RStudio

1 participant