Open zamai opened 1 month ago
Related Issues and Documentation
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
I suppose x/net/trace can register the paths with GET
if the go version is >= 1.21 (though it would be inaccurate in the face of someone setting GODEBUG=httpmuxgo121=0
explicitly), and it may be considered breaking for it to no longer respond to other methods.
/cc @jba
It's unfortunate that it registers routes with the default ServeMux in init, so there's no way to avoid it.
So @zamai, one workaround is to create your own ServeMux for your own routes using a prefix, and let x/net/trace have the global routes. That's not very satisfactory, though.
Another choice is to have a way to turn off x/net/trace's default registration. But since that happens in init, it would have to be an environment variable.
The third choice is being clever about when to add GET to the routes, looking at the go version and GODEBUG flag.
Any preferences?
Change https://go.dev/cl/621176 mentions this issue: golang.org/x/net: have conflict with path "GET /"
thanks @jba,
The workaround that I implemented in this case is to drop the "GET" directive from the root handler:
http.HandleFunc("/", helloHandler)
This created inconsistency in my codebase: I specify HTTP methods in all handler registration except the root one.
I don't understand the design of the trace package enough to give thoughtful opinion, but as a user I think it's strange/unexpected that /debug/requests
route is added to my handler when I import some package that uses the trace.
It would be okay if I did the explicit import, but in my case I stumbled upon it when I added "cloud.google.com/go/storage" package to my app.
I agree, registering paths in init is a design flaw.
There is more trouble with registering those routes in the package init function.
Render()
uses html/template
to generate responses, and html/template
uses GetMethodByName()
on random strings extracted from the template. When the go compiler finds a call to GetMethodByName()
with a non-constant method name, it disables the DCE altogether. Since Render()
is referenced by the init function, simply importing x/net/trace
disables the DCE. This pessimises the code generation in a lot of applications since google.golang.org/grpc
imports x/net/trace
.
I believe that x/net/traces
must not register routes in the init.
Ran into this same issue. Here is my workaround:
mux := http.NewServeMux()
mux.Handle("GET /", http.FileServer(http.Dir("")))
log.Fatal(http.ListenAndServe(PORT, mux))
Ran into this same issue. Here is my workaround:
mux := http.NewServeMux() mux.Handle("GET /", http.FileServer(http.Dir(""))) log.Fatal(http.ListenAndServe(PORT, mux))
good to know, valid workaround for anyone who will stumble upon this.
Go version
go version go1.23.2 darwin/arm64
Output of
go env
in your module/workspace:What did you do?
https://go.dev/play/p/oQEzMhqVvwn
When I added dependency "cloud.google.com/go/storage" (or any other google library that has sub dependency of "golang.org/x/net/trace") + handler func with "GET /", go run panics with error:
panic: pattern "GET /" (registered at /Users/alex/Code/test-trace-bug/main.go:12) conflicts with pattern "/debug/requests" (registered at /Users/alex/go/pkg/mod/golang.org/x/net@v0.29.0/trace/trace.go:130):
GET / matches fewer methods than /debug/requests, but has a more general path pattern
What did you see happen?
output of the go run command with the example program(https://go.dev/play/p/oQEzMhqVvwn)
What did you expect to see?
Expected build to work.
Let me know if this is an error related to x/net/trace or google cloud storage package.