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/go: go mod tidy leaves trailing empty lines in require blocks #70632

Open
thaJeztah opened this issue Dec 2, 2024 · 9 comments
Open

cmd/go: go mod tidy leaves trailing empty lines in require blocks #70632

thaJeztah opened this issue Dec 2, 2024 · 9 comments
Labels
GoCommand cmd/go NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@thaJeztah
Copy link
Contributor

thaJeztah commented Dec 2, 2024

Go version

go version go1.23.3 linux/arm64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/root/.cache/go-build'
GOENV='/root/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_arm64'
GOVCS=''
GOVERSION='go1.23.3'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/root/.config/go/telemetry'
GCCGO='gccgo'
GOARM64='v8.0'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
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 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build2955114020=/tmp/go-build -gno-record-gcc-switches'

What did you do?

Ran go mod tidy with a go.mod that contained a stray empty line in a require block

Reproduction steps

Create a minimal module with at least one dependency

mkdir tidy && cd tidy

cat > main.go <<'EOF'
package main

import (
	"fmt"

	"golang.org/x/time/rate"
)
var Foo = rate.Limit

func main() {
	fmt.Println("hello")
}
EOF

Create a go.mod with trailing empty lines in the require section;

cat > go.mod <<'EOF'
module tidy

go 1.23.0

require (
	"golang.org/x/time" v0.8.0




)
EOF

Run go mod tidy;

go mod tidy

Check the content of go.mod, and observe that an empty line is left behind in the require section;

cat go.mod
module tidy

go 1.23.0

require (
	golang.org/x/time v0.8.0

)

What did you see happen?

The empty line was kept even after running go mod tidy

What did you expect to see?

The empty line to be removed.

@mknyszek mknyszek added this to the Backlog milestone Dec 2, 2024
@mknyszek
Copy link
Contributor

mknyszek commented Dec 2, 2024

CC @golang/tools-team

@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Dec 2, 2024
@seankhliao seankhliao added the GoCommand cmd/go label Dec 2, 2024
@Agent-Hellboy
Copy link

Agent-Hellboy commented Dec 3, 2024

Hi @seankhliao this looks like a good issue for a beginner like me, and I feel I could learn a lot from working on it. Can I give it a try if no one else is already on it? Thanks!

@matloob
Copy link
Contributor

matloob commented Dec 6, 2024

@Agent-Hellboy Go for it! This would have to wait until go1.25 development starts to get submitted but you could start working on it now.

This would need to be fixed in golang.org/x/mod/modfile in the mod repo. See this example: https://go.dev/play/p/Dl2Kix5Sq67

When we format the file, it should remove the blank line but it doesn't. Once we fix that and vendor the fix in go mod tidy will be fixed too.

@Agent-Hellboy
Copy link

Agent-Hellboy commented Dec 7, 2024

Hi @matloob , thanks! I have a doubt. I cloned modfile, made changes, and used my local path in imports of modfile the other day, but I was not able to build the binary. It was giving a weird error as modfile is not there in vendor. Do I need to clone it in a specific place?

I guess Go is assuming it’s a standalone library inside the Go project. What’s the flow to test the changes made inside the modfile repo?

@matloob
Copy link
Contributor

matloob commented Dec 9, 2024

@Agent-Hellboy So to test changes in the go command that depend on changes in the golang.org/x/mod repo you need to do the following:

  1. Add a replace directive to the src/cmd/go.mod in your go repo pointing to your modified x/mod repo.
  2. Run go mod vendor in the src/cmd directory in the go repo to vendor in your changes in to the src/cmd/vendor directory.

You will then have the updated changes to x/mod vendored in for use by cmd/go.

You will need to rerun go mod vendor whenever you make new changes in the x/mod repo so that you can try them out.

@Agent-Hellboy
Copy link

Agent-Hellboy commented Dec 10, 2024

Thanks @matloob , I am able to build and test.

what about updating https://go.dev/doc/contribute#where, or is it documented somewhere else ?

@Agent-Hellboy
Copy link

Agent-Hellboy commented Dec 10, 2024

Hi @matloob , I have added a PR golang/mod#22 , I am ignoring the newline during parsing of required block itself, looks like there are cleanup methods for all block but ignoring at parsing stage was easy.

Tests are passing , I hope it didn't introduce any regression

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/634875 mentions this issue: modfile: fix trailing empty lines in require blocks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GoCommand cmd/go 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

7 participants