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

cmd/gofmt: inconsistent indentation of struct fields of containing tab #51910

Closed
fraenky8 opened this issue Mar 24, 2022 · 4 comments
Closed
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@fraenky8
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.18 darwin/amd64

Does this issue reproduce with the latest release?

Yes, also playground.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/user/Library/Caches/go-build"
GOENV="/Users/user/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/user/Coding/Go/pkg/mod"
GOOS="darwin"
GOPATH="/Users/user/Coding/Go"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.18/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.18/libexec/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.18"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/8v/jgh2dzcn62n0lfyws13mc_rw0000gp/T/go-build3442110454=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Run gofmt -d on the following program

https://go.dev/play/p/bVJxvovrvWB

package main

import "fmt"

type S struct {
	S       string
	Integer int //  extra long field name to point out the indentation
}

func main() {
	s := []S{
		{
			S:       "Hello World",
			Integer: 42,
		},
		{
			S:       "\t",
			Integer: 42,
		},
		{
			S:       "	", // an actual <tab>
			Integer: 42,
		},
		{
			S:       `	`, // an actual <tab>
			Integer: 42,
		},
		{
			S:       "\u0009",
			Integer: 42,
		},
	}

	fmt.Printf("%#v", s)
}

What did you expect to see?

	s := []S{
		{
			S:       "Hello World",
			Integer: 42,
		},
		{
			S:       "\t",
			Integer: 42,
		},
		{
			S:       "	", // an actual <tab>
			Integer: 42,
		},
		{
			S:       `	`, // an actual <tab>
			Integer: 42,
		},
		{
			S:       "\u0009",
			Integer: 42,
		},
	}

What did you see instead?

The fields with the actual tab-value are not indented as expected. Is this expected behavior?

	s := []S{
		{
			S:       "Hello World",
			Integer: 42,
		},
		{
			S:       "\t",
			Integer: 42,
		},
		{
			S: "	", // an actual <tab>
			Integer: 42,
		},
		{
			S: `	`, // an actual <tab>
			Integer: 42,
		},
		{
			S:       "\u0009",
			Integer: 42,
		},
	}
@seankhliao seankhliao changed the title gofmt: inconsistent indentation of struct fields of type string cmd/gofmt: inconsistent indentation of struct fields of containing tab Mar 24, 2022
@mknyszek
Copy link
Contributor

Is this new in Go 1.18?

CC @griesemer via https://dev.golang.org/owners

@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 24, 2022
@mknyszek mknyszek added this to the Backlog milestone Mar 24, 2022
@griesemer
Copy link
Contributor

griesemer commented Mar 24, 2022

This doesn't look like a new issue; I can reproduce this with Go 1.16. Looks like some unexpected interaction of the "actual " character with the formatting routine (tabwriter) which uses tabs for alignment, even though in the case of literal strings (as we have here), the string contents should be protected from such interaction.

cc: @mvdan any interest in looking into this?

@gopherbot
Copy link

Change https://go.dev/cl/404955 mentions this issue: go/printer: align expression list elements containing tabs

@mvdan
Copy link
Member

mvdan commented May 8, 2022

Fix sent :) Definitely one of those that took longer to debug than to actually fix.

The literals are indeed escaped from the tabwriter. The problem was in the code that predicts the size of each node as printed on a single line; it got confused by tab characters.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants