r-lib / lintr

Static Code Analysis for R
https://lintr.r-lib.org
Other
1.19k stars 185 forks source link

first lintr call works, subsequent calls fail with error: `Error in iconv(lines, from = encoding, to = "UTF-8") : ` #2061

Open ergz opened 1 year ago

ergz commented 1 year ago

When using a .lintr file in Rstudio, using the the addin lintr:::addin_lint() works the first time, but trying to again fails with the following error message:

Error in iconv(lines, from = encoding, to = "UTF-8") : 
  invalid 'from' argument

My .lintr file is the one generated by: lintr::use_lintr()

linters: linters_with_defaults() # see vignette("lintr")
encoding: "UTF-8"

I've updated to the latest on CRAN, and then updated to the latest on github but am experiencing the same error. I also tried changing the line ending from CRLF to LF but same error happens.

System Info: OS: Windows 11 IDE: Rstudio RStudio 2023.06.1+524 "Mountain Hydrangea" Release (547dcf861cac0253a8abb52c135e44e02ba407a1, 2023-07-07)

MichaelChirico commented 1 year ago

Can you share an error traceback()?

ergz commented 1 year ago

I am using the addin shortcut I mapped to Alt+Shift+L (as recommended by lintr)

> lintr:::addin_lint()
Error in iconv(lines, from = encoding, to = "UTF-8") : 
  invalid 'from' argument
> traceback()
5: iconv(lines, from = encoding, to = "UTF-8")
4: read_lines(filename)
3: get_lines(filename, text)
2: lintr::lint(filename$path, linters = linters)
1: lintr:::addin_lint()
MichaelChirico commented 1 year ago

Can you try tracing to see what encoding is being picked up?

trace(lintr:::read_lines, at = 1, quote(message("encoding=", encoding)))
ergz commented 1 year ago

Ran this on my open R session and got this

> lintr:::addin_lint()
Tracing read_lines(filename) step 1 
encoding=NA
Error in iconv(lines, from = encoding, to = "UTF-8") : 
  invalid 'from' argument

So I restarted and it started working fine

> lintr:::addin_lint()
Tracing read_lines(filename) step 1 
encoding=UTF-8

I decided to copy over the lintr example here: https://lintr.r-lib.org/articles/lintr.html#lintr-file-example but forgot to add an empty line so it correctly warned me about malformed file, I fixed it and ran the linter and error message reappeared.

> lintr:::addin_lint()
Tracing read_lines(filename) step 1 
encoding=NA
Error in iconv(lines, from = encoding, to = "UTF-8") : 
  invalid 'from' argument

It seem that the encoding gets lost if the previous lintr run was stopped due to malformed lintr file, after restart it works fine, but I can make it error out over and over again if it first errors out due to a malformed lintr file.

MichaelChirico commented 1 year ago

Thanks, that's very useful info.

MichaelChirico commented 1 year ago

2180 might help here

MichaelChirico commented 11 months ago

@ergz would you mind trying to reproduce the error with either the latest CRAN release or the current main?