-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
go/ast: comment text changes newline type #35803
Comments
/cc @griesemer @josharian |
Generally, the We also want tools such as gofmt to produce the same result independent of whether one runs on a Windows machine or not, which requires that newlines are always represented in one way, using LF. I would expect this to work if you run the code on gofmt-ed code. Does it not? |
Timed out in state WaitingForInfo. Closing. (I am just a bot, though. Please speak up if this is a mistake or you have the requested information.) |
Definitely agree here:
I understand the reasoning here.
Probably, I haven't tested it but I guess it will. The cause is most likely git: it "helpfully" coverts newlines to CRLF. So while it is true that a simple I would understand if you closed this as "working as intended" even though it caused some headaches for me. |
gopherbot closed this - but yes, this is "working as intended" as far as I can tell. If you have any information indicating this is not the case, feel free to reopen with the relevant information. Thanks. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What did you do?
I am trying to print accurate source locations for AST comments.
In this specific case, I'm trying to construct a
token.Pos
at a given byte offset in the.Text
field of a*Comment
type in the AST. I'm doing that simply by adding the byte offset to the.Slash
field:This works well on Unix-like systems (tested on Linux and macOS). However, Windows is a problem because it uses CRLF line endings by default instead of LF line endings. That wouldn't be a problem if the
.Text
field also kept the comment in source form (with CRLF line endings) but unfortunately it appears that the parser converts the line endings to LF, thus losing track of exact source locations.A full program that shows this behavior:
https://play.golang.org/p/8AP05J5rQX8
Replace
if true {
withif false {
to get the expected output.What did you expect to see?
I would expect the
Text
field to keep the original line endings as they appear in the source code.Alternatively,
token.FileSet
should somehow keep track of the changed line endings but I think that would be much more complicated (and probably be incorrect in other cases).I don't know whether changing the behavior of the parser (to not convert line endings) would break existing tools, but it would fix the issue for me.
What did you see instead?
The parser appears to convert line endings, thus breaking accurate source location tracking inside comments.
The text was updated successfully, but these errors were encountered: