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: wrong indentation when chaining methods in var block #63823

Closed
masseelch opened this issue Oct 30, 2023 · 5 comments
Closed

cmd/gofmt: wrong indentation when chaining methods in var block #63823

masseelch opened this issue Oct 30, 2023 · 5 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@masseelch
Copy link

masseelch commented Oct 30, 2023

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

$ go version
go version go1.21.0 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE='on'
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/***/.cache/go-build'
GOENV='/home/***/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/***/go/pkg/mod'
GONOPROXY=***
GONOSUMDB=***
GOOS='linux'
GOPATH='/home/***/go'
GOPRIVATE=***
GOPROXY='https://goproxy.cn,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.21.0'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD=***
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3477416207=/tmp/go-build -gno-record-gcc-switches'

What did you do?

When chaining methods, indentation is wrong inside a var block if the variable names have different lengths:

func main() {
	var (
		a = foo.Bar.
			Do().
			This().
			Again()
		longerVar = foo.Bar.
				Do().
				This().
				Again()
	)
}

Playground

What I would expect the code to be formatted like:

func main() {
	var (
		a = foo.Bar.
			Do().
			This().
			Again()
		longerVar = foo.Bar.
			Do().
			This().
			Again()
	)
}

This is especially confusing, since go fmt will format to what I'd expect, if the declarations weren't enclosed in a var block:

func main() {
	a := foo.Bar.
		Do().
		This().
		Again()
	longerVar := foo.Bar.
		Do().
		This().
		Again()
}

Playground

I found one (somehow) related issue: #32480

@masseelch masseelch changed the title cmd/gofmt: cmd/gofmt: wrong indentation when chaining methods in var block Oct 30, 2023
@cherrymui
Copy link
Member

@masseelch What indentation do you expect? Thanks.

@cherrymui cherrymui added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Oct 30, 2023
@masseelch
Copy link
Author

@masseelch What indentation do you expect? Thanks.

I updated the issue description.

@cherrymui
Copy link
Member

Thanks. So you're expecting one tab indentation regardless of the length of the variable name?

cc @griesemer @mvdan

@cherrymui cherrymui added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. and removed WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Oct 30, 2023
@cherrymui cherrymui added this to the Backlog milestone Oct 30, 2023
@masseelch
Copy link
Author

Thanks. So you're expecting one tab indentation regardless of the length of the variable name?

cc @griesemer @mvdan

Yes. This only happens in a var block, if you compare it to the same variables, without the var block, it works as I expect: Playground.

@seankhliao
Copy link
Member

Duplicate of #45491

@seankhliao seankhliao marked this as a duplicate of #45491 Oct 30, 2023
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

3 participants