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 build -mod=mod with no go.mod erroneously resolves dependencies #57768

Open
craig65535 opened this issue Jan 12, 2023 · 6 comments
Open
Labels
GoCommand cmd/go help wanted modules NeedsFix The path to resolution is known, but the work has not been done.
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
Contributor

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

@bcmills
Copy link
Contributor

bcmills commented Oct 13, 2023

This is indeed a bug.

We're supposed to avoid resolving missing imports if we don't have a module root:
https://cs.opensource.google/go/go/+/master:src/cmd/go/internal/modload/load.go;l=1138-1141;drc=89a457844f2b39794f9a8c16f008d3825f1ab455

However, in Go 1.17 (for module graph pruning) we started preloading root modules to maintain the graph pruning invariants:
https://cs.opensource.google/go/go/+/master:src/cmd/go/internal/modload/load.go;l=1088-1104;drc=89a457844f2b39794f9a8c16f008d3825f1ab455
And preloading will resolve missing modules:
https://cs.opensource.google/go/go/+/master:src/cmd/go/internal/modload/load.go;l=1729-1733;drc=cc13161a0d62fc1deab019996c17a7da1ff6a7f1

Probably that queryImport call in (*loader).preloadRootModules should include an extra condition:

if errors.As(err, &missing) && ld.ResolveMissingImports && (HasModRoot() || allowMissingModuleImports) {

@bcmills bcmills added help wanted NeedsFix The path to resolution is known, but the work has not been done. and removed Documentation labels Oct 13, 2023
@gopherbot gopherbot removed the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 13, 2023
@bcmills bcmills changed the title cmd/go: undocumented behaviour for go build -mod=mod with no go.mod cmd/go: go build -mod=mod with no go.mod erroneously resolves dependencies Oct 13, 2023
@gopherbot
Copy link

Change https://go.dev/cl/536416 mentions this issue: cmd/go: do not resolve missing imports if go.mod does not exist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GoCommand cmd/go help wanted modules NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants