Skip to content

style_file() unexpectedly deletes non-ASCII characters on Windows #329

Closed
krlmlr/enc
#18

Description

@yutannihilation

(Sorry, this is a "manual" reprex, as it throws errors on reading/writing to the tempfile.)

tmp <- tempfile(fileext = ".R")

con <- file(tmp, encoding = "UTF-8")

# "1+1" needs spaces
writeLines("# 忍者\n1+1", con)

readLines(con)
#> [1] "# 忍者" "1+1"

styler::style_file(tmp)

#> ----------------------------------------
#> Status	Count	Legend 
#> √ 	0	File unchanged.
#> i 	1	File changed.
#> x 	0	Styling threw an eror.
#> ----------------------------------------
#> Please review the changes carefully!


# the comment "# 忍者" disappeared...
readLines(con)
# [1] ""      "1 + 1"

I think the root cause is this line in enc package; from = "UTF-8" should not set here as it seems that text is encoded as the default encoding.

https://github.com/krlmlr/enc/blob/883df89f77bb5f8646b9a8fa94cc72120cdf0c81/R/io.R#L59

Activity

  1. yutannihilation commented on Jan 23, 2018

    @yutannihilation
    ContributorAuthor

    Ah, sorry, I was wrong on this part:

    from = "UTF-8" should not set here

    ?write_lines_enc says "It always expects text in the UTF-8 encoding", but this assumption seems to fail on my Windows.

  2. yutannihilation commented on Jan 23, 2018

    @yutannihilation
    ContributorAuthor

    I got it. The string returned by transformer is not encoded in the UTF-8 encoding here: https://github.com/krlmlr/enc/blob/883df89f77bb5f8646b9a8fa94cc72120cdf0c81/R/io.R#L113

    reprex::reprex_info()
    #> Created by the reprex package v0.1.1.9000 on 2018-01-24
    
    utf8_string <- enc2utf8("# 忍者")
    
    tf <- styler:::make_transformer(styler::tidyverse_style())
    Encoding(tf(utf8_string))
    #> [1] "unknown"

    To me, while write_lines_enc() expects UTF-8 texts, there looks nothing to ensure or check that the result of fun() is in the UTF-8 encoding. Is this the problem on enc's side? Or, is it styler's fault that its transformers don't follow the convention of write_lines_enc()?

      text <- try_read_lines_enc(path, file_encoding = file_encoding, ok = ok, skipNul = skipNul)
      sep <- detect_sep(path)
      tryCatch(
        {
          new_text <- as.character(fun(text))
          if (!isTRUE(identical(unclass(text), unclass(new_text)))) {
            if (write_back) {
              write_lines_enc(new_text, path, file_encoding = file_encoding, sep = sep)

    (https://github.com/krlmlr/enc/blob/883df89f77bb5f8646b9a8fa94cc72120cdf0c81/R/io.R#L109-L116)

  3. lorenzwalthert commented on Jan 23, 2018

    @lorenzwalthert
    Collaborator

    Thanks for raising this @yutannihilation. I cannot reproduce the problematic behaviour on my mac.

    tmp <- tempfile(fileext = ".R")
    
    con <- file(tmp, encoding = "UTF-8")
    
    writeLines("# 忍者\n1+1", con)
    
    readLines(con)
    #> [1] "# 忍者" "1+1"
    
    
    styler::style_file(tmp)
    #> Styling  1  files:
    #>  fileae631147fdfd.R ℹ 
    #> ────────────────────────────────────────
    #> Status   Count   Legend 
    #> ✔    0   File unchanged.
    #> ℹ    1   File changed.
    #> ✖    0   Styling threw an error.
    #> ────────────────────────────────────────
    #> Please review the changes carefully!
    
    readLines(con)
    #> [1] "# 忍者" "1 + 1"

    Created on 2018-01-23 by the reprex package (v0.1.1.9000).

  4. lorenzwalthert commented on Jan 23, 2018

    @lorenzwalthert
    Collaborator

    Unfortunately, I don't have time to look into that now. Need to wait another week or so.

  5. yutannihilation commented on Jan 23, 2018

    @yutannihilation
    ContributorAuthor

    Thanks for the quick reply. Yeah, Linux and Mac are fine. I will try to reproduce this bug on AppVeyor.

  6. yutannihilation commented on Jan 24, 2018

    @yutannihilation
    ContributorAuthor

    There's no hurry and I'm ok to wait several weeks, but I believe krlmlr/enc#18 will fix the problem. If you get some time, please take a look :)

  7. lorenzwalthert commented on Jan 29, 2018

    @lorenzwalthert
    Collaborator

    Does that mean that once krlmlr/enc#18 is merged into master and released on CRAN, styler should specify a minimal version for enc in order to avoid this problem?

  8. lorenzwalthert commented on Jan 29, 2018

    @lorenzwalthert
    Collaborator

    If so, this issue should not be closed before that happens.

  9. yutannihilation commented on Jan 31, 2018

    @yutannihilation
    ContributorAuthor

    Yes, it does solve the problem.

    @krlmlr I think krlmlr/enc#18 is ready to merge now (except when there's some special reason to use as.character()). May I expect the release of the next version of enc package soon?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions