Skip to content

cmd/go: list flag -json negates the -f flag #44738

Closed
@mvdan

Description

@mvdan
$ go version
go version devel +2b50ab2aee Tue Mar 2 06:38:07 2021 +0000 linux/amd64

When writing scripts, my workflow is usually:

  1. Do a go list -json query to find the fields/info I need
  2. Narrow that down with -f, so I can then get the field I need directly
  3. Write the script with the -f form

Sometimes, because I'm forgetful, I'll do step 2 as go list -json -f {{.Field}} without removing -json. That happened today again, and I was really confused for a solid five minutes:

$ go list -json unsafe
{
	"Dir": "/home/mvdan/tip/src/unsafe",
	"ImportPath": "unsafe",
	"Name": "unsafe",
	"Doc": "Package unsafe contains operations that step around the type safety of Go programs.",
	"Root": "/home/mvdan/tip",
	"Match": [
		"unsafe"
	],
	"Goroot": true,
	"Standard": true,
	"GoFiles": [
		"unsafe.go"
	]
}

$ go list -json -f {{.Dir}} unsafe
{
	"Dir": "/home/mvdan/tip/src/unsafe",
	"ImportPath": "unsafe",
	"Name": "unsafe",
	"Doc": "Package unsafe contains operations that step around the type safety of Go programs.",
	"Root": "/home/mvdan/tip",
	"Match": [
		"unsafe"
	],
	"Goroot": true,
	"Standard": true,
	"GoFiles": [
		"unsafe.go"
	]
}
$ go list -f {{.Dir}} unsafe
/home/mvdan/tip/src/unsafe

If both flags are present, I would expect one of the following:

  1. An error, telling the user that the two flags don't make sense together
  2. The -f flag would mean ignoring the -json flag, as it's more specific

I think the first option makes more sense. Silently ignoring the -f flag seems like the worst option to me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions