{"id":"853172e04422974c","repo":"gorilla/mux","slug":"mux-path-must-start-with-a-slash-got-q","errorCode":null,"errorMessage":"mux: path must start with a slash, got %q","messagePattern":"mux: path must start with a slash, got %q","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"route.go","lineNumber":253,"sourceCode":"\tMatch(*http.Request, *RouteMatch) bool\n}\n\n// addMatcher adds a matcher to the route.\nfunc (r *Route) addMatcher(m matcher) *Route {\n\tif r.err == nil {\n\t\tr.matchers = append(r.matchers, m)\n\t}\n\treturn r\n}\n\n// addRegexpMatcher adds a host or path matcher and builder to a route.\nfunc (r *Route) addRegexpMatcher(tpl string, typ regexpType) error {\n\tif r.err != nil {\n\t\treturn r.err\n\t}\n\tif typ == regexpTypePath || typ == regexpTypePrefix {\n\t\tif len(tpl) > 0 && tpl[0] != '/' {\n\t\t\treturn fmt.Errorf(\"mux: path must start with a slash, got %q\", tpl)\n\t\t}\n\t\tif r.regexp.path != nil {\n\t\t\ttpl = strings.TrimRight(r.regexp.path.template, \"/\") + tpl\n\t\t}\n\t}\n\trr, err := newRouteRegexp(tpl, typ, routeRegexpOptions{\n\t\tstrictSlash:    r.strictSlash,\n\t\tuseEncodedPath: r.useEncodedPath,\n\t})\n\tif err != nil {\n\t\treturn err\n\t}\n\tfor _, q := range r.regexp.queries {\n\t\tif err = uniqueVars(rr.varsN, q.varsN); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\tif typ == regexpTypeHost {","sourceCodeStart":235,"sourceCodeEnd":271,"githubUrl":"https://github.com/gorilla/mux/blob/db9d1d0073d27a0a2d9a8c1bc52aa0af4374d265/route.go#L235-L271","documentation":"Returned by addRegexpMatcher (route.go:251-253) when a Path or PathPrefix template is non-empty and does not begin with '/'. Path matchers require absolute paths; relative or scheme-prefixed strings are rejected. The error is stored on r.err, disabling the route.","triggerScenarios":"r.Path(\"users/{id}\") (no leading slash); r.PathPrefix(\"api/\"); passing a full URL like \"http://host/users\" to Path.","commonSituations":"Treating mux paths like Express/Flask (no leading slash); concatenating a base path without ensuring a leading slash; passing the full URL instead of just the path.","solutions":["Prefix the template with '/'.","For PathPrefix, include the leading slash and usually a trailing slash (e.g. \"/api/\").","Normalize user-supplied path config before registering routes."],"exampleFix":"// before\nr.Path(\"users/{id}\")\n// -> mux: path must start with a slash, got \"users/{id}\"\n\n// after\nr.Path(\"/users/{id}\")","handlingStrategy":"validation","validationCode":"func ensureSlash(tpl string) string {\n    if tpl != \"\" && !strings.HasPrefix(tpl, \"/\") { return \"/\" + tpl }\n    return tpl\n}","typeGuard":"func validPathTpl(tpl string) bool { return tpl == \"\" || strings.HasPrefix(tpl, \"/\") }","tryCatchPattern":"rt := r.Path(tpl)\nif err := rt.GetError(); err != nil {\n    return fmt.Errorf(\"bad path %q: %w\", tpl, err)\n}","preventionTips":["Normalize config-driven paths with ensureSlash before Path().","Always use PathPrefix(\"/...\") with a leading slash.","Unit-test route construction."],"tags":["routing","config","path","build-time"],"analyzedSha":"db9d1d0073d27a0a2d9a8c1bc52aa0af4374d265","analyzedAt":"2026-08-04T21:35:47.097Z","schemaVersion":2}