r-lib / cli

Tools for making beautiful & useful command line interfaces
https://cli.r-lib.org/
Other
644 stars 70 forks source link

`code_theme_default()` errors when in RStudio, but without {rstudioapi} #697

Closed dgkf-roche closed 3 months ago

dgkf-roche commented 3 months ago

code_theme_default() uses is_rstudio() (by way of rstudio_detect) to determine whether to theme the output for RStudio. This correctly discovers that one is in RStudio, but then assumes that one must have rstudioapi available to load.

This is often not the case. Especially when a project uses renv - only packages relevant to the project will be included in the local library. This results in an error when trying to display .Last.error

# Error:                                                                                       
# ! error in pak subprocess
# Caused by error in `verify_extracted_package(filename, pkg_cache)`:
# ! /tmp/RtmpLAlqvr/file158119387aa7/src/contrib/x86_64-pc-linux-gnu-ubuntu-20.04/4.2/fastglm_0.0.3.tar.gz is not a valid binary, it is missing fastglm/Meta/package.rds.
# Type .Last.error to see the more details.
> .Last.error
# Error in loadNamespace(x) : there is no package called ‘rstudioapi’

Perhaps this condition should also check whether requireNamespace("rstudioapi", quietly = TRUE)

https://github.com/r-lib/cli/blob/71fd2a58b79b63672d0d52fc69982fd3a6913952/R/prettycode.R#L239-L244