rstudio / rmarkdown

Dynamic Documents for R
https://rmarkdown.rstudio.com
GNU General Public License v3.0
2.87k stars 971 forks source link

`shiny_prerendered` parses `DESCRIPTION` file from wrong location #2540

Open nanxstats opened 8 months ago

nanxstats commented 8 months ago

Under specific conditions, rmarkdown/learnr incorrectly searches for and attempts to parse a DESCRIPTION file outside the intended project directory. This issue arises if the DESCRIPTION file contains errors such as formatting issues, causing the quiz component to remain in a "loading" state. This state persists without providing an error message to identify the issue for developers or users.

In Posit Workbench, this problem becomes a HTTP 500 error in the Chrome DevTools Network tab, with a possible DCF parsing error message in the response.

Minimal reproducible example

usethis::create_project("example")

In the new project window, run

usethis::use_description()
writeLines(append(readLines("DESCRIPTION"), "", after = 1), "DESCRIPTION")
fs::file_move("DESCRIPTION", "~/DESCRIPTION")
rmarkdown::draft("example.Rmd", template = "tutorial", package = "learnr", create_dir = FALSE, edit = FALSE)

Open example.Rmd, run the document, and navigate to "Topic 2". The quiz will display "Loading..." instead of the content.

Session info

> xfun::session_info('rmarkdown')
R version 4.3.2 (2023-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.2.1, RStudio 2023.12.1.402

Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8

Package version:
  base64enc_0.1.3   bslib_0.6.1       cachem_1.0.8     
  cli_3.6.2         digest_0.6.34     ellipsis_0.3.2   
  evaluate_0.23     fastmap_1.1.1     fontawesome_0.5.2
  fs_1.6.3          glue_1.7.0        graphics_4.3.2   
  grDevices_4.3.2   highr_0.10        htmltools_0.5.7  
  jquerylib_0.1.4   jsonlite_1.8.8    knitr_1.45       
  lifecycle_1.0.4   magrittr_2.0.3    memoise_2.0.1    
  methods_4.3.2     mime_0.12         R6_2.5.1         
  rappdirs_0.3.3    rlang_1.1.3       rmarkdown_2.25   
  sass_0.4.8        stats_4.3.2       stringi_1.8.3    
  stringr_1.5.1     tinytex_0.49      tools_4.3.2      
  utils_4.3.2       vctrs_0.6.5       xfun_0.41        
  yaml_2.3.8       

Pandoc version: 3.1.11.1

Checklist

When filing a bug report, please check the boxes below to confirm that you have provided us with the information we need. Have you:

gadenbuie commented 8 months ago

Thanks for the report @nanxstats! This seems specific to learnr, or have you seen this behavior with other rmarkdown formats? If not, I'll move this over to the learnr bug tracker.

nanxstats commented 8 months ago

@gadenbuie Sure thing. I think this issue is more prevalent for learnr projects because it's probably a major source of usage of the shiny_prerendered runtime, although the fix could come from different places.

gadenbuie commented 8 months ago

@nanxstats Can you try this with another shiny prerendered document that isn't a learnr tutorial? Does it also create problematic behavior under similar circumstances (where an unrelated DESCRIPTION file exists in a parent directory)?

nanxstats commented 8 months ago

@gadenbuie Good idea. I further tried the two examples in Prerendered Shiny Documents with html_document and flex_dashboard outputs - the Shiny components in both of them work ok even with the unrelated DESCRIPTION file in the parent directory. This might suggest the issue is more learnr-specific.

Also, here is a slightly updated minimal reproducible example - it gives a better hint on where the error happened (a read.dcf() call somewhere):

usethis::create_project("example")
# In the new project
usethis::use_description()
writeLines(append(readLines("DESCRIPTION"), "Imports", after = 1), "DESCRIPTION")
fs::file_move("DESCRIPTION", "~/DESCRIPTION")
rmarkdown::draft("example.Rmd", template = "tutorial", package = "learnr", create_dir = FALSE, edit = FALSE)

Run document:

Loading required package: shiny

Listening on http://127.0.0.1:3852
Warning: Error in read.dcf: Invalid DCF format.
Regular lines must have a tag.
Offending lines start with:
  Imports
  3: <Anonymous>
  1: rmarkdown::run
cderv commented 8 months ago

@gadenbuie do you want to move this issue then as it seems learnr related ?

Though we do read dcf in this code base when handling dependencies https://github.com/rstudio/rmarkdown/blob/07e2a9902915eef52ab0b085ca344f1a371dcdf0/R/shiny_prerendered.R#L330-L353

We do try to find project root but related to a dependency file. Is there also a read dcf call in learnr ? Or would this be this one ?