Go 1.13: one shall not call flag.Parse() in init #56

Closed
opened 2019-08-29 16:21:52 +00:00 by eclipseo · 1 comment

See bug https://github.com/golang/go/issues/33475 and bug https://github.com/golang/go/issues/31859 for the rationale

To be clear, we are talking only about flag.Parse here, not flag.*. The flag package is designed so that multiple packages can define flags, without those packages being aware of each other. It's essential that flag.Parse be called only after all flags are defined, meaning only after all packages are initialized. The normal practice is that the main package calls flag.Parse, because the main package is the only package that is sure to run after all packages are initialized.

Your program may well be designed so that it is OK to call flag.Parse in an init function of some non-main package. But most programs do not and cannot work that way. So I am questioning how much we should twist the testing package to support that use case. Of course if the change to the testing package is simple, then that is fine. But I would not like to see a lot of complexity added to support a very unusual use case.

This causes a bug when running tests in 1.13:

Testing    in: /builddir/build/BUILD/core/_build/src
         PATH: /builddir/build/BUILD/core/_build/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin
       GOPATH: /builddir/build/BUILD/core/_build:/usr/share/gocode
  GO111MODULE: off
      command: go test -buildmode pie -compiler gc -ldflags "-X xorm.io/core/version=0.7.0 -extldflags '-Wl,-z,relro -Wl,--as-needed  -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld '"
      testing: xorm.io/core
xorm.io/core
flag provided but not defined: -test.timeout
Usage of /tmp/go-build559150288/b001/core.test:
  -dbConn string
    	database connect string (default "root:@/core_test?charset=utf8")
  -dbtype string
    	database type (default "sqlite3")
exit status 2
See bug https://github.com/golang/go/issues/33475 and bug https://github.com/golang/go/issues/31859 for the rationale > To be clear, we are talking only about flag.Parse here, not flag.*. The flag package is designed so that multiple packages can define flags, without those packages being aware of each other. It's essential that flag.Parse be called only after all flags are defined, meaning only after all packages are initialized. The normal practice is that the main package calls flag.Parse, because the main package is the only package that is sure to run after all packages are initialized. > > Your program may well be designed so that it is OK to call flag.Parse in an init function of some non-main package. But most programs do not and cannot work that way. So I am questioning how much we should twist the testing package to support that use case. Of course if the change to the testing package is simple, then that is fine. But I would not like to see a lot of complexity added to support a very unusual use case. This causes a bug when running tests in 1.13: ``` Testing in: /builddir/build/BUILD/core/_build/src PATH: /builddir/build/BUILD/core/_build/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin GOPATH: /builddir/build/BUILD/core/_build:/usr/share/gocode GO111MODULE: off command: go test -buildmode pie -compiler gc -ldflags "-X xorm.io/core/version=0.7.0 -extldflags '-Wl,-z,relro -Wl,--as-needed -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld '" testing: xorm.io/core xorm.io/core flag provided but not defined: -test.timeout Usage of /tmp/go-build559150288/b001/core.test: -dbConn string database connect string (default "root:@/core_test?charset=utf8") -dbtype string database type (default "sqlite3") exit status 2 ```
lunny added the
bug
label 2019-08-30 02:04:30 +00:00
Owner

Closed by #57

Closed by #57
lunny closed this issue 2019-08-30 05:23:10 +00:00
This repo is archived. You cannot comment on issues.
No Milestone
No Assignees
2 Participants
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/core#56
No description provided.