Skip to content

cmd/go: go build -mod=mod with no go.mod erroneously resolves dependencies #57768

@craig65535

Description

@craig65535

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    GoCommandcmd/goNeedsFixThe path to resolution is known, but the work has not been done.help wantedmodules

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions