Skip to content

[Proposal] x/mod: api to get the module path that will be loaded by go mod in code ? #32401

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

Closed
dfang opened this issue Jun 3, 2019 · 3 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@dfang
Copy link

dfang commented Jun 3, 2019

What version of Go are you using (go version)?

$ go version
go version go1.12.1 darwin/amd64

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/mj/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/mj/go"
GOPROXY="https://goproxy.io"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.12.1/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.12.1/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/mj/src/qor-demo/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/xb/dsd0_2b92x7bsl92xlzj6fbm0000gn/T/go-build351214963=/tmp/go-build -gno-record-gcc-switches -fno-common"

What do you want ?

in go.mod file

github.com/a/b

there are many dependencies, how do i get the path for this mod that will be loaded in a/b module ?

$GOPATH/pkg/mod/github.com/a/b@<hash>

simply read go.mod is not enough, because there may be lines for go mod edit --replace

or go mod vendor

want to get the results like this in code

go list -deps -f '{{with .Module}}{{.Path}}@{{.Version}}{{end}}' ./... | sort -u

internal package can do this, but it can't be called

@dfang dfang changed the title how to get the module path loaded by go mod in code ? api to get the module path that will be loaded by go mod in code ? Jun 3, 2019
@bcmills
Copy link
Contributor

bcmills commented Jun 3, 2019

You can always execute go list -json as a subprocess. (That's why the -json flag exists!)

Depending on exactly which details you need, you may be able to use golang.org/x/tools/go/packages instead.

What's the underlying problem you're trying to solve? (What is it that you want to do with the path once you've found it?)

CC @ianthehat

@bcmills bcmills added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jun 3, 2019
@bcmills bcmills changed the title api to get the module path that will be loaded by go mod in code ? x/mod: api to get the module path that will be loaded by go mod in code ? Jun 3, 2019
@gopherbot gopherbot added this to the Unreleased milestone Jun 3, 2019
@bcmills
Copy link
Contributor

bcmills commented Jun 3, 2019

What's the underlying problem you're trying to solve?

I'm guessing that's #32384, in which case there really isn't an API that does what you're after.

@dfang dfang changed the title x/mod: api to get the module path that will be loaded by go mod in code ? [Proposal] x/mod: api to get the module path that will be loaded by go mod in code ? Jun 3, 2019
@dfang
Copy link
Author

dfang commented Jun 3, 2019

@bcmills, internal packages for go list seems can do this, but it can't be called :(

yes, i'm trying to solve is #32384. i'm forking all github.com/qor sub projects, try to add mod support for viewpaths.

for example. in https://github.com/qor/admin/blob/master/admin.go#L102
if RegisterViewPath like this. template can be found when run in go mod way in Dockerfile.

it's not suitable for go list -json or golang.org/x/tools/go/packages here, i want to register viewpath in every sub project, not in module depends on them

currently my workaround is:

_, filename, _, _ := runtime.Caller(0)
dir := path.Join(path.Dir(filename), "views")
log.Debug().Str("viewpath", dir).Msg("registering viewpath")
admin.RegisterViewPath(dir)

@dfang dfang closed this as completed Jun 3, 2019
@golang golang locked and limited conversation to collaborators Jun 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

3 participants