Skip to content

yaml_front_matter throws error if YAML ends with comment that ends with a :. #2483

Closed

Description

@keithnewman

Expected behaviour

A line starting with # would be treated as a comment and not affect the parsing of the rest of a YAML block when using yaml_front_matter().

Actual behaviour

If the YAML block ends with a comment that ends the line with a colon, that colon causes yaml_front_matter() to return a parsing error because the YAML ends in :, despite the colon being part of a comment. The comment can exist earlier on in the YAML, as long as it has another term afterwards.

Reprex (CRAN version)

Make two Rmd files, with the same lines of YAML, but swapping the author and (commented out) date lines around.

file.create("okay.Rmd")
#> [1] TRUE
okay <- file("okay.Rmd")
writeLines(
  c(
    '---',
    'title: "My document name"',
    ' # date:',
    'author: "Me"',
    '---',
    'Document body'
  ),
  con = okay
)
close(okay)

file.create("not_okay.Rmd")
#> [1] TRUE
not_okay <- file("not_okay.Rmd")
writeLines(
  c(
    '---',
    'title: "My document name"',
    'author: "Me"',
    ' # date:',
    '---',
    'Document body'
  ),
  con = not_okay
)
close(not_okay)

rmarkdown::yaml_front_matter("okay.Rmd")
#> $title
#> [1] "My document name"
#> 
#> $author
#> [1] "Me"
rmarkdown::yaml_front_matter("not_okay.Rmd")
#> Error: Invalid YAML front matter (ends with ':')

xfun::session_info('rmarkdown')
#> R version 4.3.0 (2023-04-21)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 22.04.2 LTS
#> 
#> 
#> Locale:
#>   LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
#>   LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
#>   LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
#>   LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
#>   LC_ADDRESS=C               LC_TELEPHONE=C            
#>   LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       
#> 
#> time zone: Europe/London
#> tzcode source: system (glibc)
#> 
#> Package version:
#>   base64enc_0.1.3   bslib_0.4.2       cachem_1.0.8      cli_3.6.1        
#>   digest_0.6.31     ellipsis_0.3.2    evaluate_0.20     fastmap_1.1.1    
#>   fontawesome_0.5.1 fs_1.6.2          glue_1.6.2        graphics_4.3.0   
#>   grDevices_4.3.0   highr_0.10        htmltools_0.5.5   jquerylib_0.1.4  
#>   jsonlite_1.8.4    knitr_1.42        lifecycle_1.0.3   magrittr_2.0.3   
#>   memoise_2.0.1     methods_4.3.0     mime_0.12         R6_2.5.1         
#>   rappdirs_0.3.3    rlang_1.1.1       rmarkdown_2.21    sass_0.4.5       
#>   stats_4.3.0       stringi_1.7.12    stringr_1.5.0     tinytex_0.45     
#>   tools_4.3.0       utils_4.3.0       vctrs_0.6.2       xfun_0.39        
#>   yaml_2.3.7       
#> 
#> Pandoc version: 2.19.2

Created on 2023-05-03 with reprex v2.0.2

I had the same result in R 4.2.1.

Reprex (development version)

file.create("okay.Rmd")
#> [1] TRUE
okay <- file("okay.Rmd")
writeLines(
  c(
    '---',
    'title: "My document name"',
    ' # date:',
    'author: "Me"',
    '---',
    'Document body'
  ),
  con = okay
)
close(okay)

file.create("not_okay.Rmd")
#> [1] TRUE
not_okay <- file("not_okay.Rmd")
writeLines(
  c(
    '---',
    'title: "My document name"',
    'author: "Me"',
    ' # date:',
    '---',
    'Document body'
  ),
  con = not_okay
)
close(not_okay)

rmarkdown::yaml_front_matter("okay.Rmd")
#> $title
#> [1] "My document name"
#> 
#> $author
#> [1] "Me"
rmarkdown::yaml_front_matter("not_okay.Rmd")
#> Error: Invalid YAML front matter (ends with ':')

xfun::session_info('rmarkdown')
#> R version 4.3.0 (2023-04-21)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 22.04.2 LTS
#> 
#> 
#> Locale:
#>   LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
#>   LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
#>   LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
#>   LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
#>   LC_ADDRESS=C               LC_TELEPHONE=C            
#>   LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       
#> 
#> time zone: Europe/London
#> tzcode source: system (glibc)
#> 
#> Package version:
#>   base64enc_0.1.3   bslib_0.4.2       cachem_1.0.8      cli_3.6.1        
#>   digest_0.6.31     ellipsis_0.3.2    evaluate_0.20     fastmap_1.1.1    
#>   fontawesome_0.5.1 fs_1.6.2          glue_1.6.2        graphics_4.3.0   
#>   grDevices_4.3.0   highr_0.10        htmltools_0.5.5   jquerylib_0.1.4  
#>   jsonlite_1.8.4    knitr_1.42        lifecycle_1.0.3   magrittr_2.0.3   
#>   memoise_2.0.1     methods_4.3.0     mime_0.12         R6_2.5.1         
#>   rappdirs_0.3.3    rlang_1.1.1       rmarkdown_2.21.3  sass_0.4.5       
#>   stats_4.3.0       stringi_1.7.12    stringr_1.5.0     tinytex_0.45     
#>   tools_4.3.0       utils_4.3.0       vctrs_0.6.2       xfun_0.39        
#>   yaml_2.3.7       
#> 
#> Pandoc version: 2.19.2

Created on 2023-05-03 with reprex v2.0.2

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:

  • formatted your issue so it is easier for us to read?

  • included a minimal, self-contained, and reproducible example?

  • pasted the output from xfun::session_info('rmarkdown') in your issue?

  • upgraded all your packages to their latest versions (including your versions of R, the RStudio IDE, and relevant R packages)?

  • installed and tested your bug with the development version of the rmarkdown package using remotes::install_github("rstudio/rmarkdown")?

Activity

  1. cderv commented on May 3, 2023

    @cderv
    Collaborator

    The error comes from

    rmarkdown/R/output_format.R

    Lines 727 to 731 in 9568cde

    validate_front_matter <- function(front_matter) {
    front_matter <- trim_trailing_ws(front_matter)
    if (grepl(":$", front_matter))
    stop2("Invalid YAML front matter (ends with ':')")
    }

    We purposely error since the start to warn user for an incomplete front matter that would cause (at the time) YAML parsing issue.
    Changes are from 9 years ago : a13c13f and 59c22ff

    So this is by design, and we do not account for comment in the validation. (we could try though)

    However, I am wondering if we should still validate.

    This is called in

    rmarkdown/R/output_format.R

    Lines 713 to 714 in 9568cde

    validate_front_matter(front_matter)
    parsed_yaml <- yaml_load(front_matter)

    to prevent the hang in yaml_load() . Maybe this is not needed anymore. If the YAML is malformed, we'll get an error

    > yaml_load('"title: \"My document name\"\nauthor: \"Me\"\ndate:"')
    Error in yaml::yaml.load(..., eval.expr = TRUE) : 
      Parser error: did not find expected <document start> at line 1, column 10
    

    @yihui what do you think ?

  2. yihui commented on May 3, 2023

    @yihui
    Collaborator

    I think we can get rid of the validation.

  3. cderv commented on May 4, 2023

    @cderv
    Collaborator

    Thanks I went with removal. I don't think this internal function is used anywhere else (I checked Quarto and RStudio IDE and Connect)

  4. moved this from Backlog to Done in R Markdown Team Projectson May 4, 2023
  5. github-actions commented on Nov 1, 2023

    @github-actions

    This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

  6. locked as resolved and limited conversation to collaborators on Nov 1, 2023
  7. added a commit that references this issue on Oct 17, 2024
    4de1c96
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions