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: undocumented behaviour for go build -mod=mod with no go.mod #57768

Open
craig65535 opened this issue Jan 12, 2023 · 4 comments
Open

cmd/go: undocumented behaviour for go build -mod=mod with no go.mod #57768

craig65535 opened this issue Jan 12, 2023 · 4 comments
Labels
Documentation GoCommand cmd/go modules NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@craig65535
Copy link

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

$ go version
1.18.10

Does this issue reproduce with the latest release?

Yes

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

darwin/arm64

What did you do?

I noticed I can build an executable from a bare directory (no go.mod) with go build -mod=mod:

go build -mod=mod -o xyz.exe github.com/some/go/tool

This does not work without -mod=mod: no required module provides package github.com/some/go/tool: go.mod file not found in current directory or any parent directory; see 'go help modules'.

At https://go.dev/ref/mod#build-commands I can see the following for -mod=mod:

-mod=mod tells the go command to ignore the vendor directory and to automatically update go.mod, for example, when an imported package is not provided by any known module.

And,

The -mod=mod flag instructs the go command to attempt to find new modules providing missing packages and to update go.mod and go.sum.

It is finding the module providing a missing package, but it's not updating go.mod (there isn't any), and it's not spelled out in the documentation which go.mod it's using in the first place.

I asked about this on stackoverflow, and someone suggested that he would never have expected this to work and it might be a bug. I find this useful and I'd like to retain this behaviour, but would like to see more clarity in the documentation.

What did you expect to see?

Documentation about this use case

What did you see instead?

No documentation about this use case

@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 13, 2023
@cagedmantis cagedmantis added this to the Backlog milestone Jan 13, 2023
@cagedmantis cagedmantis changed the title cmd/go: Undocumented behaviour for go build -mod=mod with no go.mod cmd/go: undocumented behaviour for go build -mod=mod with no go.mod Jan 13, 2023
@cagedmantis
Copy link
Contributor

/cc @bcmills

@bcmills
Copy link
Member

bcmills commented Jan 13, 2023

I, too, would not have expected that command to work. 😅

@craig65535, I'm curious: why go build -mod=mod -o xyz.exe github.com/some/go/tool instead of go install -o xyz.exe githhub.com/some/go/tool@latest?

@seankhliao
Copy link
Member

go install -o isn't implemented... ref #44469 (comment)

@seankhliao
Copy link
Member

apparently this (go build -mod=mod pkg) was introduced in 1.17

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