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: add -f (format) flag to go mod edit #34943

Open
josharian opened this issue Oct 17, 2019 · 6 comments

Comments

@josharian
Copy link
Contributor

@josharian josharian commented Oct 17, 2019

go mod edit -json dumps the entire mod file. However, having to parse json immediately takes this out of the realm of simple bash scripts.

As part of working on #34867, I've already hit two cases in which I need targeted information out of go mod edit -json: The required version of a module, and all replacements involving that module.

Writing a quick program to parse json is easy, but the obvious language to use is Go. But in my circumstances, I can't go run a Go program, because the mod file is in a broken state.

I propose that, like go list, go mod edit have a format flag to enable easy information requests from a shell script.

cc @bcmills @jayconrod

@bcmills

This comment has been minimized.

Copy link
Member

@bcmills bcmills commented Oct 17, 2019

I'm curious: why you need go mod edit -json for this task (rather than go list)?

@josharian

This comment has been minimized.

Copy link
Contributor Author

@josharian josharian commented Oct 17, 2019

Two reasons:

  • go.mod is in a bad state (replace directive points to non-existent dir), so go list fails.

  • The info I need is about replace directives (in particular what versions have replace directives), which go list does not expose.

@bcmills

This comment has been minimized.

Copy link
Member

@bcmills bcmills commented Oct 17, 2019

go list does expose replace directives: see the Replace field of type Module at https://golang.org/cmd/go/#hdr-List_packages_or_modules.

For the replace directive pointing to a non-existent directory: perhaps we should make go list -m -e more tolerant of bad edges in the module graph?

@josharian

This comment has been minimized.

Copy link
Contributor Author

@josharian josharian commented Oct 17, 2019

go list does expose replace directives: see the Replace field of type Module at https://golang.org/cmd/go/#hdr-List_packages_or_modules.

Maybe I'm holding it wrong, but I don't seem to be able to get a version out of that. Relevant parts of go.mod:

go 1.13

require (
        github.com/josharian/git2go v0.0.0-20191016055211-da2fefa41c8c
)

replace github.com/josharian/git2go v0.0.0-20191016055211-da2fefa41c8c => ./build/git2go/v0.0.0-20191016055211-da2fefa41c8c

Result of running go list:

$ go list -m -f '{{.Replace}}' github.com/josharian/git2go
./build/git2go/v0.0.0-20191016055211-da2fefa41c8c
$ go list -m -f '{{.Replace.Version}}' github.com/josharian/git2go
$

Also, I want to get all replace directives for that module, not just the active one. (This is lower priority, though.)

For the replace directive pointing to a non-existent directory: perhaps we should make go list -m -e more tolerant of bad edges in the module graph?

Seems like a good idea.

@bcmills

This comment has been minimized.

Copy link
Member

@bcmills bcmills commented Oct 17, 2019

Maybe I'm holding it wrong, but I don't seem to be able to get a version out of that.

Your replace directive specified a filesystem path, not a version. (The Version field is blank because there is no version on the right side of that directive.)

To see what I mean, try:

go list -m -f '{{.Path}} {{.Version}}{{with .Replace}} => {{.Path}} {{.Version}}{{end}}' all
@josharian

This comment has been minimized.

Copy link
Contributor Author

@josharian josharian commented Oct 17, 2019

Your replace directive specified a filesystem path, not a version. (The Version field is blank because there is no version on the right side of that directive.)

Ah. I was focused on trying to learn what was on the left hand side of the directive, because that's what go mod edit -dropreplace needs. But I can skip doing cleanup and just use the main version. Assuming -e is made more tolerant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.