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

encoding/protobuf: dependency on glog adds some flags to a program's std flag list #1199

Closed
myitcv opened this issue Aug 10, 2021 Discussed in #1196 · 11 comments
Closed

encoding/protobuf: dependency on glog adds some flags to a program's std flag list #1199

myitcv opened this issue Aug 10, 2021 Discussed in #1196 · 11 comments
Assignees
Labels

Comments

@myitcv
Copy link
Member

myitcv commented Aug 10, 2021

Originally posted by knieriem August 8, 2021 in #1196

When upgrading a program importing a package from cue v0.3.0-beta* to v0.4.0 I noted that CUE's new dependency github.com/protocolbuffers/txtpbfmt adds another dependency, github.com/golang/glog (leveled execution logs for Go). go mod why lists, for instance:

cuelang.org/go/cue/load
  cuelang.org/go/internal/encoding
    cuelang.org/go/encoding/protobuf/textproto
       github.com/protocolbuffers/txtpbfmt/parser
         github.com/golang/glog

Glog, as a package, in its init function defines some command line flags using Go's standard flag package:

-alsologtostderr
-log_backtrace_at value
-log_dir string
-logtostderr
-stderrthreshold value
-v value
-vmodule value

As a result, any program importing, for example, cuelang.org/go/cue/load will automatically inherit these flags. In my program, which makes use of the standard flag package, I had been defining an own flag.Uint("v", 0, "verbosity level").
With cue v0.4.0 I'm getting a panic: ... flag redefined: v error now.

Since the cue program itself is using cobra+pflag, this effect is not visible when using cue itself, since flags defined using Go's standard flag package are ignored then.

An old thread at golang-nuts, glog and testing flag "pollution" mentions a workaround: defining one's own flag.FlagSet instead of using the default flag.CommandLine. But this also means, that all flags defined by a program's internal packages using e.g. flag.Bool must be adapted to the new FlagSet.
An alternative to this workaround would be to make textproto depend on a modified txtpbfmt that does not depend on glog. I think for my use-case I will apply the method described in that golang-nuts thread.

@myitcv myitcv added NeedsFix Discuss Requires maintainer discussion M labels Aug 10, 2021
@myitcv
Copy link
Member Author

myitcv commented Aug 10, 2021

This seems unfortunate at best, but also something that we can fix via a PR to the github.com/protocolbuffers/txtpbfmt module.

@ying-jeanne
Copy link

having the same problem when trying to update to version 0.4.0

@verdverm
Copy link

Of note, using flag.FlagSet (https://pkg.go.dev/flag#FlagSet) can solve this issue.

Without it, you are using a "global" scope for flags, where you can have name collisions or extra flags added from deps that use the "global" flag space.

@myitcv myitcv removed the M label Oct 26, 2021
@mpvl mpvl added the good first issue Good for newcomers label Nov 23, 2021
@myitcv myitcv added zzz Discuss and removed Discuss Requires maintainer discussion labels Jan 29, 2022
@gurjeet
Copy link

gurjeet commented Feb 9, 2022

@myitcv can you please describe what's the difference between Discuss and zzz Discuss labels. Perhaps that'll explain why you change the label on this issue.

@myitcv
Copy link
Member Author

myitcv commented Feb 9, 2022

@gurjeet ah that was just me trying to recategorise issues we needed to discuss prior to FOSDEM. The zzz Discuss category is now defunct so I will revert everything back to discuss.

@myitcv myitcv added Discuss Requires maintainer discussion and removed zzz Discuss labels Feb 9, 2022
gurjeet added a commit to gurjeet/glog that referenced this issue Feb 10, 2022
This problem has been reported quite a few times over the years; for
example, see it reported at golang-nuts mailing list [1], and cue lang
issue [2].

[1]: https://groups.google.com/g/golang-nuts/c/vj8ozVqemnQ
[2]: cue-lang/cue#1199

The problem is that, that glog package registers some flags in its
init() function. The list of registered flags also includes the `-v`
flag, which is usually used by developers either to control verbosity of
their code-execution, or to show the software version. It's notable that
all the complaints are regarding the `-v` flag, and none about the other
flags, since those other flags are unlikely be used by any other
developer.

The proposed fix allows the user of the glog package to change/prefix
glog's flags' names, so that they will not conflict with any flags that
they want to use.

This approach to the problem has a few advantages, compared to other
options like, disabling all the flags in glog.

.) The default behaviour of the glog library is unchanged. So the
current users of the library will not be affected.

.) Any new users who wish to use the -v, or other glog-occupied flag,
can do so at build time.

.) The new users can still use the glog features/flags, albeit with a
prefix.

.) We are not enforcing some specific prefix, which may also conflict.

.) The --help flag, correctly reports the changed/prefixd flag names.

```
$ ./main --help
Usage of ./main:

  ... other glog: prefixed flags ...

  -glog:v value
        log level for V logs
  -glog:vmodule value
        comma-separated list of pattern=N settings for file-filtered logging
  -v value
        Emit verbose execution progress

```
gurjeet added a commit to gurjeet/glog that referenced this issue Feb 10, 2022
This problem has been reported quite a few times over the years; for
example, see it reported at golang-nuts mailing list [1], and cue lang
issue [2].

[1]: https://groups.google.com/g/golang-nuts/c/vj8ozVqemnQ
[2]: cue-lang/cue#1199

The problem is that, that glog package registers some flags in its
init() function. The list of registered flags also includes the `-v`
flag, which is usually used by developers either to control verbosity of
their code-execution, or to show the software version. It's notable that
all the complaints are regarding the `-v` flag, and none about the other
flags, since those other flags are unlikely be used by any other
developer.

The proposed fix allows the user of the glog package to change/prefix
glog's flags' names, so that they will not conflict with any flags that
they want to use.

This approach to the problem has a few advantages, compared to other
options like, disabling all the flags in glog.

1. The default behaviour of the glog library is unchanged. So the
current users of the library will not be affected.

2. Any new users who wish to use the -v, or other glog-occupied flag,
can do so at build time.

3. The new users can still use the glog features/flags, albeit with a
prefix.

4. We are not enforcing some specific prefix, which may also conflict.

5. The --help flag, correctly reports the changed/prefixed flag names.

```
$ ./main --help
Usage of ./main:

  ... other glog: prefixed flags ...

  -glog:v value
        log level for V logs
  -glog:vmodule value
        comma-separated list of pattern=N settings for file-filtered logging
  -v value
        Emit verbose execution progress

```
gurjeet added a commit to gurjeet/glog that referenced this issue Feb 10, 2022
This problem has been reported quite a few times over the years; for
example, see it reported at golang-nuts mailing list [1], and cue lang
issue [2].

[1]: https://groups.google.com/g/golang-nuts/c/vj8ozVqemnQ
[2]: cue-lang/cue#1199

The problem is that, that glog package registers some flags in its
init() function. The list of registered flags also includes the `-v`
flag, which is usually used by developers either to control verbosity of
their code-execution, or to show the software version. It's notable that
all the complaints are regarding the `-v` flag, and none about the other
flags, since those other flags are unlikely be used by any other
developer.

The proposed fix allows the user of the glog package to change/prefix
glog's flags' names, so that they will not conflict with any flags that
they want to use.

This approach to the problem has a few advantages, compared to other
options like, disabling all the flags in glog.

1. The default behaviour of the glog library is unchanged. So the
current users of the library will not be affected.

2. Any new users who wish to use the -v, or other glog-occupied flag,
can do so at build time.

3. The new users can still use the glog features/flags, albeit with a
prefix.

4. We are not enforcing some specific prefix, which may also conflict.

5. The --help flag, correctly reports the changed/prefixed flag names.

```
$ ./main --help
Usage of ./main:

  ... other glog: prefixed flags ...

  -glog:v value
        log level for V logs
  -glog:vmodule value
        comma-separated list of pattern=N settings for file-filtered logging
  -v value
        Emit verbose execution progress

```

Please also see sample code [3] that demonstrates the problem, and how
the patch fixes the problem.

[3]: https://github.com/gurjeet/glog_fix_test
@gurjeet
Copy link

gurjeet commented Feb 10, 2022

The good first issue label made this problem look deceptively simple; false advertising :-)

But I have take a crack at solving this problem, so if the fix is merged, all users of the glog package will be able to override these flags at build time. See golang/glog#54

@mvdan
Copy link
Member

mvdan commented May 5, 2022

I think an easier way to fix this issue might be to change txtpbfmt to not import a CLI logging library in its relatively low level parser package:

https://github.com/protocolbuffers/txtpbfmt/blob/74888fd59c2b19248a51d8a5954db1588e18948f/parser/parser.go#L19

I personally think that's reasonable, because the user of the library should be in control of the logging - such as choosing what log library to use, or whether to use one at all. It seems like the parser only uses the library to make calls to Infof and Errorf, so perhaps those could be turned into a small interface to fit any similar logger into the parser API.

@mpvl
Copy link
Member

mpvl commented May 18, 2022

I agree with @mvdan. It is overbearing for txtpbfmt to be importing glog, so ideally it should be fixed there.

We may also consider using a different package or forking txtpbfmt into the cue-lang org and removing it there. The added benefit of that is that we may create some stability and control around the use of text protos, for which there is no real standard. But ideally the changes would be made upstream.

@mvdan
Copy link
Member

mvdan commented Aug 2, 2022

I've gone ahead and asked nicely: protocolbuffers/txtpbfmt#70

@mvdan
Copy link
Member

mvdan commented Jan 24, 2023

Took a long time to agree with upstream and get the changes reviewed, but it finally looks like both of my PRs are being merged. We should be able to update txtpbfmt and drop glog as a dependency soon.

@mvdan mvdan removed good first issue Good for newcomers Discuss Requires maintainer discussion labels Apr 7, 2023
@mvdan mvdan self-assigned this Apr 7, 2023
@mvdan
Copy link
Member

mvdan commented Apr 11, 2023

Now merged :) The glog dependency should be gone in master and the upcoming v0.6.0-alpha.1 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants