golang/go · error
file URL missing path
Error message
file URL missing path
What it means
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.
Source
Thrown at src/cmd/go/internal/web/url.go:33
// net/url package.
var errNotAbsolute = errors.New("path is not absolute")
func urlToFilePath(u *url.URL) (string, error) {
if u.Scheme != "file" {
return "", errors.New("non-file URL")
}
checkAbs := func(path string) (string, error) {
if !filepath.IsAbs(path) {
return "", errNotAbsolute
}
return path, nil
}
if u.Path == "" {
if u.Host != "" || u.Opaque == "" {
return "", errors.New("file URL missing path")
}
return checkAbs(filepath.FromSlash(u.Opaque))
}
path, err := convertFileURLPath(u.Host, u.Path)
if err != nil {
return path, err
}
return checkAbs(path)
}
func urlFromFilePath(path string) (*url.URL, error) {
if !filepath.IsAbs(path) {
return nil, errNotAbsolute
}
// If path has a Windows volume name, convert the volume to a host and prefix
// per https://blogs.msdn.microsoft.com/ie/2006/12/06/file-uris-in-windows/.View on GitHub (pinned to b6b368adc5)
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.
Example fix
// before
u := &url.URL{Scheme: "file", Host: "localhost"} // Path empty
p, err := web.URLToFilePath(u) // -> "file URL missing path"
// after
u := &url.URL{Scheme: "file", Host: "localhost", Path: "/tmp/x"}
p, err := web.URLToFilePath(u) Defensive patterns
Strategy: validation
Validate before calling
if u.Path == "" && (u.Host != "" || u.Opaque == "") {
return errors.New("file URL has no path")
}
p, err := web.URLToFilePath(u) Type guard
func hasFilePath(u *url.URL) bool {
return u.Scheme == "file" && (u.Path != "" || (u.Opaque != "" && u.Host == ""))
} Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- non-file URL
- file URL encodes volume in host field: too few slashes?
- file URL missing drive letter
- file URL specifies non-local host
- path is not absolute
AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12).
Data as JSON: /api/errors/aed085d63b858561.
Report an issue: GitHub.