-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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
proposal: cmd/go: support peer dependencies #63457
Comments
I think you meant |
@AlexanderYastrebov Thanks, fixed. |
Peer Dependencies in Node exist to prevent multiple copies of the same package. If you don't explicitly specify a dependency on a transitive peer dependency, the version (of the peer dependency) specified by the direct dependency is used. This is very different from the behaviour you're proposing. |
@icholy OK, then maybe my terminology is wrong here? Should it be called "optional dependency"? I am not married to the concrete proposal here. But the use case is pretty real. In addition: my package does not really care about the version of the |
Can you use generics to solve this? type StackTrace interface {
~[]uintptr
}
type StackTracer[S StackTrace] interface {
StackTrace() S
} |
@carlmjohnson Hm, can you share play link how would this work? My understanding is that I would still have to make pass |
I was thinking if you had a top level function that accepted a StackTracer, it could work with a tozd StackTrace or a pkg StackTrace. Looking at the code for // Does it have a StackTrace method?
m, ok := reflect.TypeOf(err).MethodByName("StackTrace")
// Does StackTrace have no input and one output?
if ok && m.Type.NumIn() == 0 && m.Type.NumOut() == 1 {
outType := m.Type.Out(0)
// Is the output a []uintptr?
if outType.Kind == reflect.Slice && outType.Elem().Kind() == reflect.Uintptr {
// Do something with m
}
} |
@carlmjohnson: |
Seems like you need a v2 of your module. |
Of which module? |
@mitar I mean a v2 of your module which doesn't integrate with pkg/errors |
Sure. But the idea is that users should be able to decide if they want or not compatibility with pkg/errors. This is a feature I would like to have. |
Background
I am the author of the
gitlab.com/tozd/go/errors
, a modern drop-in replacement forgithub.com/pkg/errors
which enables errors to be annotated with stack traces and structured details. It is a new package with fresh implementation and is not a fork ofgithub.com/pkg/errors
, but it does attempt to keep compatibility with it by reusing its API and by supporting reusing stack traces made bygithub.com/pkg/errors
. The latter is important because in large codebases it can happen that bothgithub.com/pkg/errors
andgitlab.com/tozd/go/errors
are used.github.com/pkg/errors
exposes its stack trace using the following interface:errors.StackTrace
is a type (underlying type is[]uintptr
) exported bygithub.com/pkg/errors
. So to be able to access the stack trace, one has to importgithub.com/pkg/errors
to be able to define the interface. To extract the stack trace (just the underlying[]uintptr
data), I do something (approximately) like:github.com/pkg/errors
has been archived in 2021 and is not maintained anymore. Because of this people are searching for maintained alternatives. It is not thatgithub.com/pkg/errors
has any known security issues, but it does lack newer features (likeerrors.Join
) and in general people seem to not enjoy having an unmaintained dependency. From what I have seen, even to a degree that they blocklist the dependency using linter tools.The issue
github.com/pkg/errors
dependency.gitlab.com/tozd/go/errors
might be a good alternative.gitlab.com/tozd/go/errors
also has a dependency ongithub.com/pkg/errors
so that it works better in codebases wheregithub.com/pkg/errors
is used as well and can be interoperable withgithub.com/pkg/errors
.The proposal
NPM has peer dependencies so solve exactly this problem. I think it could be useful for Go as well. The idea would be that a separate list of dependencies is made in
go.mod
. So you would have direct, indirect, and peer dependencies. And if that peer dependency is satisfied, an automatic build flag would enable, including the file which uses that peer dependency. (I am open to how that build flag format would look like.)Alternatives
Build tags seems to be one alternative. I could define a build tag one could use to enable/disable checking for
github.com/pkg/errors
stack trace on errors (and importing theStackTrace
symbol). But the issue is that dependency ingo.mod
still stays which can trick tools into thinking that the package still usesgithub.com/pkg/errors
.The text was updated successfully, but these errors were encountered: