Skip to content

in parseUTF8() on Windows with enc "unknown", src is ignored / / overwritten #916

Description

@ArcadeAntics
> sessioninfo::session_info()
─ Session info ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.3.1 (2023-06-16 ucrt)
 os       Windows 10 x64 (build 19045)
 system   x86_64, mingw32
 ui       RStudio
 language (EN)
 collate  English_Canada.utf8
 ctype    English_Canada.utf8
 tz       America/Toronto
 date     2023-09-07
 rstudio  2023.06.1+524 Mountain Hydrangea (desktop)
 pandoc   2.12 @ C:\\Users\\iris\\anaconda3\\Scripts\\pandoc.exePackages ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 package     * version  date (UTC) lib source
 cli           3.6.1    2023-03-23 [1] CRAN (R 4.3.0)
 crayon        1.5.2    2022-09-29 [1] CRAN (R 4.3.0)
 essentials  * 0.3.0.13 2023-08-01 [1] local
 jsonlite      1.8.5    2023-06-05 [1] CRAN (R 4.3.1)
 later         1.3.1    2023-05-02 [1] CRAN (R 4.3.0)
 lifecycle     1.0.3    2022-10-07 [1] CRAN (R 4.3.0)
 magrittr      2.0.3    2022-03-30 [1] CRAN (R 4.3.0)
 plumber       1.2.1    2022-09-06 [1] CRAN (R 4.3.1)
 promises      1.2.0.1  2021-02-11 [1] CRAN (R 4.3.0)
 R6            2.5.1    2021-08-19 [1] CRAN (R 4.3.0)
 Rcpp          1.0.11   2023-07-06 [1] CRAN (R 4.3.1)
 rlang         1.1.1    2023-04-28 [1] CRAN (R 4.3.1)
 rstudioapi    0.15.0   2023-07-07 [1] CRAN (R 4.3.1)
 sessioninfo   1.2.2    2021-12-06 [1] CRAN (R 4.3.1)
 stringi       1.7.12   2023-01-11 [1] CRAN (R 4.3.0)
 swagger       3.33.1   2020-10-02 [1] CRAN (R 4.3.1)
 this.path   * 2.0.0.7  2023-09-07 [1] local
 webutils      1.1      2020-04-28 [1] CRAN (R 4.3.1)

 [1] C:/Users/iris/AppData/Local/R/win-library/4.3
 [2] C:/Program Files/R/R-4.3.1/library

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
>

Example application or steps to reproduce the problem

if (.Platform$OS.type != "windows") {
    stop("windows only bug")
} else local({
    FILE.R <- tempfile(fileext = ".R")
    on.exit(unlink(FILE.R))
    writeLines("5 + 6", FILE.R)
    exprs <- plumber:::parseUTF8(FILE.R)
    cat("original filename: ", FILE.R, "\n", sep = "")
    cat("srcfile  filename: "); print(attr(exprs, "srcfile"))
})

Describe the problem in detail

In parseUTF8() the comments claim that on Windows with encoding "unknown" the file must be re-encoded to native. I would agree with this, though I would think this is no longer necessary since the C runtime on Windows is now ucrt and the native encoding is now UTF-8. Regardless, it writes the lines into a new file in the native encoding.

It then claims that despite parsing a different file, the source reference is pointed to the original file. This is incorrect since parse() overwrites its argument srcfile when file is a character string and keep.source = TRUE.

To avoid srcfile being overwritten when passed to parse(), you should use parse(keep.source = FALSE) (unintuitively).

Here is some code showing the fix working as intended:

my_parseUTF8 <- function (file)
{
    lines <- plumber:::readUTF8(file)
    src <- srcfilecopy(file, lines, isFile = TRUE)
    file <- tempfile()
    on.exit(unlink(file))
    writeLines(lines, file)
    parse(file, keep.source = FALSE, srcfile = src)
}


if (.Platform$OS.type != "windows") {
    stop("windows only bug")
} else local({
    FILE.R <- tempfile(fileext = ".R")
    on.exit(unlink(FILE.R))
    writeLines("5 + 6", FILE.R)
    exprs <- my_parseUTF8(FILE.R)
    cat("original filename: ", FILE.R, "\n", sep = "")
    cat("srcfile  filename: "); print(attr(exprs, "srcfile"))
})

Activity

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

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions