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: mod tidy has no effect on Windows #44458

Closed
ValleZ opened this issue Feb 20, 2021 · 12 comments
Closed

cmd/go: mod tidy has no effect on Windows #44458

ValleZ opened this issue Feb 20, 2021 · 12 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@ValleZ
Copy link

ValleZ commented Feb 20, 2021

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

$ go version
go version go1.16 windows/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
set GO111MODULE=                                                                                                                                                                                                                                                                          set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Valle\AppData\Local\go-build
set GOENV=C:\Users\Valle\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\Valle\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Valle\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=                                                                                                                                                                                                                                                                         set CGO_CXXFLAGS=-g -O2                                                                                                                                                                                                                                                                   set CGO_FFLAGS=-g -O2                                                                                                                                                                                                                                                                     set CGO_LDFLAGS=-g -O2                                                                                                                                                                                                                                                                    set PKG_CONFIG=pkg-config                                                                                                                                                                                                                                                                 set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\Valle\AppData\Local\Temp\go-build935010229=/tmp/go-build -gno-record-gcc-switches 

What did you do?

I followed steps from https://golang.google.cn/doc/tutorial/call-module-code
I created greetings folder with greetings.go file and run
go mod init example.com/greetings
what created go.mod with

module example.com/greetings

go 1.16

then I created hello folder on the same level, hello.go
with

package main

import (
    "fmt"

    "example.com/greetings"
)

func main() {
    // Get a greeting message and print it.
    message := greetings.Hello("Gladys")
    fmt.Println(message)
}

and go.mod with

module hello

go 1.16

replace example.com/greetings => ../greetings

then I run go build while in the hello folder and get error

hello.go:6:5: cannot find package

(I'm not sure how to run the code from hello folder in play.golang.org, but this mirrors the tutorial files)
https://play.golang.org/p/rtHtLMQHXUO

What did you expect to see?

The tutorial says I should see
go: found example.com/greetings in example.com/greetings v0.0.0-00010101000000-000000000000

What did you see instead?

hello.go:6:5: cannot find package

The previous issue was closed as duplicate of #44241 (comment)
but the problem is that modules do not work for me at all.
Running "go mod tidy" doesn't produce any effect or output, even replicating simple example from https://play.golang.org/p/rq4J90MxTKJ gives the same error on my local machine.

@seankhliao
Copy link
Member

please don't open multiple issues, we would've reopened the previous one

keeping this one open, because it is necessary to run go mod tidy after the replace which #44241 doesn't mention

17:25:20 ~/tmp3 0:00:00
» mkdir greetings && cd greetings

17:25:31 ~/tmp3/greetings 0:00:00
» go mod init example.com/greetings
go: creating new go.mod: module example.com/greetings

17:25:37 ~/tmp3/greetings 0:00:00
» cat << EOF > greetings.go
package greetings

import "fmt"

// Hello returns a greeting for the named person.
func Hello(name string) string {
    // Return a greeting that embeds the name in a message.
    message := fmt.Sprintf("Hi, %v. Welcome!", name)
    return message
}
EOF

17:25:46 ~/tmp3/greetings 0:00:00
» cd ..

17:25:52 ~/tmp3 0:00:00
» mkdir hello && cd hello

17:25:58 ~/tmp3/hello 0:00:00
» cat << EOF > hello.go
package main

import (
    "fmt"

    "example.com/greetings"
)

func main() {
    // Get a greeting message and print it.
    message := greetings.Hello("Gladys")
    fmt.Println(message)
}
EOF

17:26:04 ~/tmp3/hello 0:00:00
» go mod init hello
go: creating new go.mod: module hello
go: to add module requirements and sums:
	go mod tidy

17:26:13 ~/tmp3/hello 0:00:00
» echo "replace example.com/greetings => ../greetings" >> go.mod

17:26:19 ~/tmp3/hello 0:00:00
» go mod tidy
go: found example.com/greetings in example.com/greetings v0.0.0-00010101000000-000000000000

17:26:24 ~/tmp3/hello 0:00:00
» go run .
Hi, Gladys. Welcome!

@seankhliao seankhliao changed the title Module tutorial doesn't work for Go 1.16 even with running "go mod tidy" x/website: tutorial on replace needs go mod tidy Feb 20, 2021
@gopherbot gopherbot added this to the Unreleased milestone Feb 20, 2021
@seankhliao seankhliao added NeedsFix The path to resolution is known, but the work has not been done. Documentation labels Feb 20, 2021
@ValleZ
Copy link
Author

ValleZ commented Feb 20, 2021

This is "works on my machine" response type.

C:\Users\Valle>mkdir greetings && cd greetings

C:\Users\Valle\greetings>go mod init example.com/greetings
go: creating new go.mod: module example.com/greetings

C:\Users\Valle\greetings>cat << EOF > greetings.go
<< was unexpected at this time.

C:\Users\Valle\greetings>cat greetings.go
'cat' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\Valle\greetings>echo greetings.go
greetings.go

C:\Users\Valle\greetings>type greetings.go
package greetings

import "fmt"

// Hello returns a greeting for the named person.
func Hello(name string) string {
    // Return a greeting that embeds the name in a message.
    message := fmt.Sprintf("Hi, %v. Welcome!", name)
    return message
}

C:\Users\Valle\greetings>cd ..

C:\Users\Valle>mkdir hello && cd hello

C:\Users\Valle\hello>type hello.go
package main

import (
    "fmt"

    "example.com/greetings"
)

func main() {
    // Get a greeting message and print it.
    message := greetings.Hello("Gladys")
    fmt.Println(message)
}

C:\Users\Valle\hello>go mod init hello
go: creating new go.mod: module hello
go: to add module requirements and sums:
        go mod tidy

C:\Users\Valle\hello>echo "replace example.com/greetings => ../greetings" >> go.mod

C:\Users\Valle\hello>type go.mod
module hello

go 1.16
"replace example.com/greetings => ../greetings"

C:\Users\Valle\hello>type go.mod
module hello

go 1.16
replace example.com/greetings => ../greetings

C:\Users\Valle\hello>go mod tidy

C:\Users\Valle\hello>go run .
hello.go:6:5: cannot find package

@ValleZ
Copy link
Author

ValleZ commented Feb 20, 2021

I'll repeat: "go mod tidy" doesn't have any effect for me.

@ValleZ
Copy link
Author

ValleZ commented Feb 20, 2021

Actually I notice an effect - if I enter "require example.com/greetings v0.0.0-00010101000000-000000000000" manually to the go.mod and then run "go mod tidy" it deletes the line

@ValleZ ValleZ changed the title x/website: tutorial on replace needs go mod tidy Module tutorial doesn't work for go 1.16, "go mod tidy" has no effect on Windows Feb 20, 2021
@seankhliao
Copy link
Member

can you run go env from the hello directory?

@ValleZ
Copy link
Author

ValleZ commented Feb 20, 2021

set GO111MODULE=auto
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Valle\AppData\Local\go-build
set GOENV=C:\Users\Valle\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\Valle\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Valle\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\Valle\hello\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\Valle\AppData\Local\Temp\go-build4087477873=/tmp/go-build -gno-record-gcc-switches

@hyangah
Copy link
Contributor

hyangah commented Feb 20, 2021

@stevetraut @jayconrod @bcmills It looks to me, since go1.16, go build no longer edits go.mod file. In order to add the require section, go get example.com/greetings or other go.mod modifying commands are needed.

And, I noticed the tutorial asks users to manually edit go.mod file. Can we replace that part with go mod edit -replace command?

FYI - go mod tidy worked for me on windows, so there must be something else going on in @ValleZ's case

PS C:\Users\golang\code\tutorial\hello> type hello.go
package main

import (
        "fmt"

        "example.com/greetings"
)

func main() {
        message := greetings.Hello("Gladys")
        fmt.Println(message)
}
PS C:\Users\golang\code\tutorial\hello> type go.mod
module hello

go 1.16

replace example.com/greetings => ../greetings
PS C:\Users\golang\code\tutorial\hello> go mod tidy   
go: found example.com/greetings in example.com/greetings v0.0.0-00010101000000-000000000000
PS C:\Users\golang\code\tutorial\hello> type go.mod
module hello

go 1.16

replace example.com/greetings => ../greetings

require example.com/greetings v0.0.0-00010101000000-000000000000
PS C:\Users\golang\code\tutorial\hello> go run .
Hi, Gladys. Welcome!

@seankhliao
Copy link
Member

@ValleZ can we do this on slack, #modules channel on https://gopher.slack.com invite link

@seankhliao seankhliao added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. and removed Documentation NeedsFix The path to resolution is known, but the work has not been done. labels Feb 20, 2021
@seankhliao seankhliao changed the title Module tutorial doesn't work for go 1.16, "go mod tidy" has no effect on Windows cmd/go: mod tidy has no effect on Windows Feb 20, 2021
@hyangah
Copy link
Contributor

hyangah commented Feb 20, 2021

Ah - @ValleZ I think echo "replace example.com/greetings => ../greetings" >> go.mod made go.mod file ill-formed.

Note "" around the replace statement in your log:

C:\Users\Valle\hello>echo "replace example.com/greetings => ../greetings" >> go.mod
C:\Users\Valle\hello>type go.mod
module hello

go 1.16
"replace example.com/greetings => ../greetings"

There shouldn't be "". Can you try to fix that and run go mod tidy again?

@ValleZ
Copy link
Author

ValleZ commented Feb 20, 2021

I fixed it in external editor, see log.

@hyangah
Copy link
Contributor

hyangah commented Feb 20, 2021

@ValleZ Is the log included in #44458 (comment) up-to-date? I still see the quoted replace statement.

go mod tidy or go get example.com/greetings is still necessary for 1.16 to populate the require statement. I hope that will be addressed in #44241

@ValleZ
Copy link
Author

ValleZ commented Feb 20, 2021

Turns out it was caused by BOM: #35726

@ValleZ ValleZ closed this as completed Feb 20, 2021
@golang golang locked and limited conversation to collaborators Feb 20, 2022
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

4 participants