Parsing go.mod

733 views
Skip to first unread message

James Pettyjohn

unread,
Sep 3, 2019, 1:44:28 AM9/3/19
to golang-nuts
Hi,

This might be a bad idea but I'm trying to parse the go.mod file for data as part of my build process - if at all possible I'd like to avoid duplicating the data that is already there in another file.

Is there an exposed package that can be used for this?

- J

Dan Kortschak

unread,
Sep 3, 2019, 1:52:45 AM9/3/19
to golang-nuts
Not really exposed, but there is code you could copy.

https://golang.org/pkg/cmd/go/internal/modfile/#Parse

Kurtis Rader

unread,
Sep 3, 2019, 1:58:53 AM9/3/19
to James Pettyjohn, golang-nuts
On Mon, Sep 2, 2019 at 10:44 PM James Pettyjohn <japet...@gmail.com> wrote:
This might be a bad idea but I'm trying to parse the go.mod file for data as part of my build process - if at all possible I'd like to avoid duplicating the data that is already there in another file.

In this type of situation you should explain why you need to parse a go.mod file. Especially since you seem to be asking something not already asked a thousand times. It is possible, perhaps likely, the reason you want to do this can be solved some other way.
 
--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

James Pettyjohn

unread,
Sep 3, 2019, 3:20:49 AM9/3/19
to golang-nuts
Thanks. I saw the same being done by vgo - might be viable.

James Pettyjohn

unread,
Sep 3, 2019, 3:23:14 AM9/3/19
to golang-nuts
Sorry I left out the details. So far it's two specific properties I need for instructions in my build process:

1) The full name of the package
2) The version of the protobuf dependency for locking the right version protoc-gen-go before running protoc (that has been asked quite a few times, total hack, but works).

Best,
James

Paul Jolly

unread,
Sep 3, 2019, 3:26:28 AM9/3/19
to James Pettyjohn, golang-nuts
Also note that you can run:

go mod edit -json

and get JSON output.

go help mod edit

will given you the types you can then use to unmarshal the JSON.

Failing that, the code that Dan referenced has been factored out into:

https://godoc.org/github.com/rogpeppe/go-internal/modfile
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/7a06f505-187e-473f-b748-a8bf7f114bf6%40googlegroups.com.

Paul Jolly

unread,
Sep 3, 2019, 3:30:03 AM9/3/19
to James Pettyjohn, golang-nuts
> Sorry I left out the details. So far it's two specific properties I need for instructions in my build process:
>
> 1) The full name of the package

go list -f {{.Name}} example.com/blah

> 2) The version of the protobuf dependency for locking the right version protoc-gen-go before running protoc (that has been asked quite a few times, total hack, but works).

go list -f {{.Version}} -m example.com/blah

Note that if you have a replace directive for example.com/blah then
the replace target might have a version.

For more detail on either of the above see:

go help list

>
> Best,
> James
>
> On Monday, September 2, 2019 at 10:58:53 PM UTC-7, Kurtis Rader wrote:
>>
>> On Mon, Sep 2, 2019 at 10:44 PM James Pettyjohn <japet...@gmail.com> wrote:
>>>
>>> This might be a bad idea but I'm trying to parse the go.mod file for data as part of my build process - if at all possible I'd like to avoid duplicating the data that is already there in another file.
>>
>>
>> In this type of situation you should explain why you need to parse a go.mod file. Especially since you seem to be asking something not already asked a thousand times. It is possible, perhaps likely, the reason you want to do this can be solved some other way.
>>
>> --
>> Kurtis Rader
>> Caretaker of the exceptional canines Junior and Hank
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/8b716cac-0eb6-4e95-bdde-12ee832e06a4%40googlegroups.com.

Ian Cottrell

unread,
Sep 3, 2019, 9:43:15 PM9/3/19
to Paul Jolly, James Pettyjohn, golang-nuts
The parsing code will eventually be public, see https://github.com/golang/go/issues/31761
When it is done, you would want golang.org/x/mod/modfile, which will be a (possibly modified) copy of the compiler internal package of the same name.


James Pettyjohn

unread,
Sep 3, 2019, 10:09:24 PM9/3/19
to golang-nuts
That's good news, gives me some confidence that the tooling will keep working after 1.14.


On Tuesday, September 3, 2019 at 6:43:15 PM UTC-7, Ian Cottrell wrote:
The parsing code will eventually be public, see https://github.com/golang/go/issues/31761
When it is done, you would want golang.org/x/mod/modfile, which will be a (possibly modified) copy of the compiler internal package of the same name.


On Tue, Sep 3, 2019 at 3:29 AM Paul Jolly <pa...@myitcv.io> wrote:
> Sorry I left out the details. So far it's two specific properties I need for instructions in my build process:
>
> 1) The full name of the package

    go list -f {{.Name}} example.com/blah

> 2) The version of the protobuf dependency for locking the right version protoc-gen-go before running protoc (that has been asked quite a few times, total hack, but works).

    go list -f {{.Version}} -m example.com/blah

Note that if you have a replace directive for example.com/blah then
the replace target might have a version.

For more detail on either of the above see:

    go help list

>
> Best,
> James
>
> On Monday, September 2, 2019 at 10:58:53 PM UTC-7, Kurtis Rader wrote:
>>
>> On Mon, Sep 2, 2019 at 10:44 PM James Pettyjohn <japet...@gmail.com> wrote:
>>>
>>> This might be a bad idea but I'm trying to parse the go.mod file for data as part of my build process - if at all possible I'd like to avoid duplicating the data that is already there in another file.
>>
>>
>> In this type of situation you should explain why you need to parse a go.mod file. Especially since you seem to be asking something not already asked a thousand times. It is possible, perhaps likely, the reason you want to do this can be solved some other way.
>>
>> --
>> Kurtis Rader
>> Caretaker of the exceptional canines Junior and Hank
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.

> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/8b716cac-0eb6-4e95-bdde-12ee832e06a4%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.

James Pettyjohn

unread,
Sep 3, 2019, 10:11:13 PM9/3/19
to golang-nuts
Thanks a lot for the tips. This repo is working out and should hold me over, as Ian mentioned, until golang.org/x/mod/modfile is out.


On Tuesday, September 3, 2019 at 12:26:28 AM UTC-7, Paul Jolly wrote:
Also note that you can run:

    go mod edit -json

and get JSON output.

    go help mod edit

will given you the types you can then use to unmarshal the JSON.

Failing that, the code that Dan referenced has been factored out into:

    https://godoc.org/github.com/rogpeppe/go-internal/modfile

On Tue, 3 Sep 2019 at 08:21, James Pettyjohn <japet...@gmail.com> wrote:
>
> Thanks. I saw the same being done by vgo - might be viable.
>
> On Monday, September 2, 2019 at 10:52:45 PM UTC-7, kortschak wrote:
>>
>> Not really exposed, but there is code you could copy.
>>
>> https://golang.org/pkg/cmd/go/internal/modfile/#Parse
>>
>> On Mon, 2019-09-02 at 22:44 -0700, James Pettyjohn wrote:
>> > Hi,
>> >
>> > This might be a bad idea but I'm trying to parse the go.mod file for
>> > data
>> > as part of my build process - if at all possible I'd like to avoid
>> > duplicating the data that is already there in another file.
>> >
>> > Is there an exposed package that can be used for this?
>> >
>> > - J
>> >
>>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages