{"record":{"id":"aed085d63b858561","repo":"golang/go","slug":"file-url-missing-path","errorCode":null,"errorMessage":"file URL missing path","messagePattern":"file URL missing path","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/web/url.go","lineNumber":33,"sourceCode":"// net/url package.\n\nvar errNotAbsolute = errors.New(\"path is not absolute\")\n\nfunc urlToFilePath(u *url.URL) (string, error) {\n\tif u.Scheme != \"file\" {\n\t\treturn \"\", errors.New(\"non-file URL\")\n\t}\n\n\tcheckAbs := func(path string) (string, error) {\n\t\tif !filepath.IsAbs(path) {\n\t\t\treturn \"\", errNotAbsolute\n\t\t}\n\t\treturn path, nil\n\t}\n\n\tif u.Path == \"\" {\n\t\tif u.Host != \"\" || u.Opaque == \"\" {\n\t\t\treturn \"\", errors.New(\"file URL missing path\")\n\t\t}\n\t\treturn checkAbs(filepath.FromSlash(u.Opaque))\n\t}\n\n\tpath, err := convertFileURLPath(u.Host, u.Path)\n\tif err != nil {\n\t\treturn path, err\n\t}\n\treturn checkAbs(path)\n}\n\nfunc urlFromFilePath(path string) (*url.URL, error) {\n\tif !filepath.IsAbs(path) {\n\t\treturn nil, errNotAbsolute\n\t}\n\n\t// If path has a Windows volume name, convert the volume to a host and prefix\n\t// per https://blogs.msdn.microsoft.com/ie/2006/12/06/file-uris-in-windows/.","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/web/url.go#L15-L51","documentation":"Thrown by urlToFilePath in cmd/go/internal/web when the URL has scheme \"file\" but u.Path is empty AND either a host is set or the opaque part is also empty. A file: URL must resolve to a concrete absolute path; an empty path component means the URL points at nothing addressable on disk. The Opaque branch is only taken for the legacy file:localhost-style opaque URLs, so a non-empty host with empty Path is rejected.","triggerScenarios":"Parsing \"file://\" (nothing after the authority), \"file://host\" (host but no path), or \"file:\" alone. Constructing &url.URL{Scheme: \"file\"} with neither Path nor Opaque set. A file URL whose path was accidentally trimmed or stripped (e.g. filepath.Clean over the whole URL string).","commonSituations":"Code that builds file URLs from join operations that yield an empty string when the input dir was empty. Misuse of url.URL fields where the developer sets Host but forgets Path (e.g. file://localhost with no trailing slash). Configuration files that store a base file URL and concatenate nothing onto it.","solutions":["Always include an absolute path: build the URL from a real path using web.URLFromFilePath(path) instead of assembling fields by hand.","If constructing manually, ensure u.Path = \"/\" + filepath.ToSlash(absPath) and leave Host empty (or \"localhost\").","Validate len(u.Path) > 0 || (u.Opaque != \"\" && u.Host == \"\") before calling URLToFilePath.","Log the raw URL string at the call site to catch empty-path inputs during development."],"exampleFix":"// before\nu := &url.URL{Scheme: \"file\", Host: \"localhost\"} // Path empty\np, err := web.URLToFilePath(u) // -> \"file URL missing path\"\n\n// after\nu := &url.URL{Scheme: \"file\", Host: \"localhost\", Path: \"/tmp/x\"}\np, err := web.URLToFilePath(u)","handlingStrategy":"validation","validationCode":"if u.Path == \"\" && (u.Host != \"\" || u.Opaque == \"\") {\n    return errors.New(\"file URL has no path\")\n}\np, err := web.URLToFilePath(u)","typeGuard":"func hasFilePath(u *url.URL) bool {\n    return u.Scheme == \"file\" && (u.Path != \"\" || (u.Opaque != \"\" && u.Host == \"\"))\n}","tryCatchPattern":null,"preventionTips":["Always build file URLs from an absolute filesystem path via web.URLFromFilePath.","Unit-test URL builders with empty/whitespace path inputs.","Log the URL string at the call site during development."],"tags":["go-toolchain","url","file-url","validation"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}