Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"No newline at end of file" after formatting file in VS Code #462

Closed
bersbersbers opened this issue Aug 5, 2021 · 15 comments · Fixed by #509
Closed

"No newline at end of file" after formatting file in VS Code #462

bersbersbers opened this issue Aug 5, 2021 · 15 comments · Fixed by #509

Comments

@bersbersbers
Copy link

bersbersbers commented Aug 5, 2021

  1. Create a file bug.R containting this line: styler::style_file("bug.R") (no newline at end of file).
  2. Run it using Rscript bug.R
  3. Note that a newline at the end of file has been introduced. So this seems to be tidyverse style.
  4. Note also that the GitHub web-interface file editor introduces a newline at the end of the file - there is no way to even avoid it.
  5. .Rprofile ignores the last line if it does not end in a newline, see https://yihui.org/en/2018/04/rprofile-trailing-newline/

I think the above establishes that a file should end in a newline character.

Now, the R language server seems to remove that newline character - inconsistently!

hTLPI9Kei2

Note especially how with "editor.formatOnSave": true, it takes me 5 tries to reproduce the problem.

@bersbersbers bersbersbers changed the title "No newline at end of file" after saving file in R "No newline at end of file" after formatting file in VS Code Aug 5, 2021
@renkun-ken
Copy link
Member

You might want to turn on files.insertFinalNewline in your vscode settings as a quick walk-around.

I'll take a closer look at the document formatting provider then.

@bersbersbers
Copy link
Author

Thanks for looking into this! Just know that files.insertFinalNewline inserts an additional newline, which results in a duplicate newline in those cases where the trailing newline was not removed originally. So you either have 0 or 1 newline, OR 1 or 2 newlines - never 1 newline consistently.

@lorenzwalthert
Copy link
Contributor

lorenzwalthert commented Aug 9, 2021

FWIW: The reason why styler ensures always exactly one trailing empty line (when the input is a file, e.g. not in style_text()) is described here: https://unix.stackexchange.com/questions/18743/whats-the-point-in-adding-a-new-line-to-the-end-of-a-file

@lorenzwalthert
Copy link
Contributor

lorenzwalthert commented Aug 9, 2021

Other people have experienced this while formatting on save:

@lorenzwalthert
Copy link
Contributor

This seems to be a major headache for some people. Can we fix it?

@renkun-ken
Copy link
Member

Let me take a look into this.

@renkun-ken
Copy link
Member

renkun-ken commented Dec 28, 2021

Looks like it is caused by how styler uses cache and how it handles empty line are not consistent.

First, clear the styler cache.

rm -rf ~/.cache/R/R.cache/styler

Then try the following:

> str(styler::style_text(c("print(1)", "")))                                                                        
 'vertical' chr "print(1)"

> str(styler::style_text(c("print(1)", "")))                                                                        
 'vertical' chr [1:2] "print(1)" ""

> str(styler::style_text(c("print(1)", "")))                                                                        
 'vertical' chr [1:2] "print(1)" ""

> str(styler::style_text(c("print(2)", "")))                                                                        
 'vertical' chr "print(2)"

> str(styler::style_text(c("print(2)", "")))                                                                        
 'vertical' chr [1:2] "print(2)" ""

> str(styler::style_text(c("print(2)", "")))                                                                        
 'vertical' chr [1:2] "print(2)" ""

> str(styler::style_text(c("print(3)", "")))                                                                        
 'vertical' chr "print(3)"

> str(styler::style_text(c("print(3)", "")))                                                                        
 'vertical' chr [1:2] "print(3)" ""

> str(styler::style_text(c("print(3)", "")))                                                                        
 'vertical' chr [1:2] "print(3)" ""

Looks like it should be fixed in styler where inconsistent results are returned? @lorenzwalthert

@lorenzwalthert
Copy link
Contributor

lorenzwalthert commented Dec 28, 2021

Ugh, you are right @renkun-ken. Thanks for looking into it. I will have a look and Open an issue in styler.

@lorenzwalthert
Copy link
Contributor

Seems like it's fixed in dev version of {styler}. Should we just ask people to install the dev version of {styler} or do we need a fix here too? You could check for packageVersion('styler') < '1.6.2.9000 and if that's true, styler:::ensure_last_n_empty(styled_code, n = 1) (or n = 0, not sure how to make sure there is exactly one trailing newline after the formatting is completed). That function exists since {styler} 1.3.2 (released over two years ago).

@lorenzwalthert
Copy link
Contributor

In any case, I think once I release a {styler} update, there should be a minimal version requirement for {styler}.

@renkun-ken
Copy link
Member

@lorenzwalthert Do you think #509 should work with both CRAN release and latest dev of styler?

@bersbersbers Does #509 work for you?

@lorenzwalthert
Copy link
Contributor

@nealrichardson @assignUser, with dev styler and dev languageserver, formatting should now be more consistent with regard to trailing blank line. Let us know if it works now for you.

@nealrichardson
Copy link

@lorenzwalthert seems to be working for me now--thanks a lot!

@assignUser
Copy link

Yep perfect, thanks!

@888Greys
Copy link

888Greys commented May 7, 2024

Thanks a lot. It worked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants