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: list flag -json negates the -f flag #44738

Closed
mvdan opened this issue Mar 2, 2021 · 4 comments
Closed

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

mvdan opened this issue Mar 2, 2021 · 4 comments
Labels
FrozenDueToAge GoCommand cmd/go help wanted NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@mvdan
Copy link
Member

mvdan commented Mar 2, 2021

$ 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.

@mvdan mvdan added the GoCommand cmd/go label Mar 2, 2021
@mvdan
Copy link
Member Author

mvdan commented Mar 2, 2021

cc @bcmills @jayconrod @matloob per the owners doc

@bcmills
Copy link
Contributor

bcmills commented Mar 2, 2021

I think the first option [an error telling the user that the two flags don't make sense together] makes more sense. Silently ignoring the -f flag seems like the worst option to me.

Agreed.

@bcmills bcmills added help wanted NeedsFix The path to resolution is known, but the work has not been done. labels Mar 2, 2021
@bcmills bcmills added this to the Backlog milestone Mar 2, 2021
@mvdan
Copy link
Member Author

mvdan commented Mar 2, 2021

Cool, I'll look into a small fix sometime this cycle if noone beats me to it in the next couple of weeks.

@gopherbot
Copy link
Contributor

Change https://golang.org/cl/298029 mentions this issue: cmd/go: emit error when listing with -f and -json

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

No branches or pull requests

3 participants