vfmt does not produce EOL

Created on 20 Apr 2020  Ā·  10Comments  Ā·  Source: vlang/v

4515 was closed too soon - vfmt still removes trailing EOL.

I created a simple file (foo.v):

fn main() {
        println("hi")
}

It _does_ have EOL after the closing '}'.

Then I typed

v fmt foo.v >bar.v

and when I bring bar.v up in editor - no trailing EOL.

Bug vfmt

Most helpful comment

cat works fine, just tried.

But it looks like you are right

according to POSIX, every text file (including Ruby and JavaScript source files) should end with a \n, or ā€œnewlineā€ (not ā€œa new lineā€) character. This acts as the eol, or the ā€œend of lineā€ character. It is a line ā€œterminatorā€.

Wonder why Go does this. Only found this: https://github.com/golang/go/issues/1178

All 10 comments

Looks like commit ee2f1652b58e075e04190efb2d7aacb63c764467 added

+ "\n"

to line 57 of fmt.v, then commit e006da72593449df192f3b5d40ddb7b16cc84e27 commented that out.

Hm, just be careful with hardcoded values as that should be rather dependent on which EOLs were used in the original file (i.e. not hard coded, not dependent on the environment/operating_system vfmt currently runs on, not dependent on compile-time environment/operating_system).

vfmt uses LFs everywhere even if CRLFs were used in a file.

As it should be. Any CRLFs should be replaced with LF only. It's called "normalization".

And in case someone didn't know, even Windows is perfectly happy with LF only, most of the time. Most Windows editors can be set to do LF line endings, as well.

Indeed, that's the correct behavior. Otherwise all Git projects would be messed up.

@JalonSolov I realized that gofmt doesn't add an EOL, and this fix broke lots of tests, so I reverted it for now.

Will need to investigate.

gofmt removes all trailing newlines, vfmt's behavior is the same.

I would argue instead that gofmt is broken, and vfmt should correct that issue.

According to the POSIX standard, it isn't properly a full line unless it has a newline:

3.206 Line
    A sequence of zero or more non- <newline> characters plus a terminating <newline> character.

There are more than a few programs out there that cannot properly process a file that doesn't have a newline at the end.

Even a simple cat of a file without a trailing newline "messes up" the display, since the next prompt is ON THE SAME LINE.

cat works fine, just tried.

But it looks like you are right

according to POSIX, every text file (including Ruby and JavaScript source files) should end with a \n, or ā€œnewlineā€ (not ā€œa new lineā€) character. This acts as the eol, or the ā€œend of lineā€ character. It is a line ā€œterminatorā€.

Wonder why Go does this. Only found this: https://github.com/golang/go/issues/1178

No idea. go fmt definitely doesn't add the trailing EOL (just tried), but it should.

Fixed in newer versions of V.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PavelVozenilek picture PavelVozenilek  Ā·  3Comments

markgraydev picture markgraydev  Ā·  3Comments

medvednikov picture medvednikov  Ā·  3Comments

cjmxp picture cjmxp  Ā·  3Comments

XVilka picture XVilka  Ā·  3Comments