golang/go · error

invalid file:// proxy URL with non-path elements: %s

Error message

invalid file:// proxy URL with non-path elements: %s

What it means

newProxyRepo rejects a file:// proxy URL that carries anything beyond scheme+path+rawpath. A file URL must be purely local; a host segment, query, fragment, user info, or port makes it ambiguous/insecure and is refused.

Source

Thrown at src/cmd/go/internal/modfetch/proxy.go:208

	listLatestOnce sync.Once
	listLatest     *RevInfo
	listLatestErr  error
}

func newProxyRepo(baseURL, path string) (Repo, error) {
	// Parse the base proxy URL.
	base, err := url.Parse(baseURL)
	if err != nil {
		return nil, err
	}
	redactedBase := base.Redacted()
	switch base.Scheme {
	case "http", "https":
		// ok
	case "file":
		if *base != (url.URL{Scheme: base.Scheme, Path: base.Path, RawPath: base.RawPath}) {
			return nil, fmt.Errorf("invalid file:// proxy URL with non-path elements: %s", redactedBase)
		}
	case "":
		return nil, fmt.Errorf("invalid proxy URL missing scheme: %s", redactedBase)
	default:
		return nil, fmt.Errorf("invalid proxy URL scheme (must be https, http, file): %s", redactedBase)
	}

	// Append the module path to the URL.
	url := base
	enc, err := module.EscapePath(path)
	if err != nil {
		return nil, err
	}
	url.Path = strings.TrimSuffix(base.Path, "/") + "/" + enc
	url.RawPath = strings.TrimSuffix(base.RawPath, "/") + "/" + pathEscape(enc)

	return &proxyRepo{url, path, redactedBase, sync.Once{}, nil, nil}, nil
}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Use the three-slash form pointing at a local directory: file:///var/cache/goproxy.
  2. Remove any query, fragment, user, host, or port from the URL.
  3. Ensure the path is absolute and the directory is readable by the go process.

Example fix

// before
//   GOPROXY=file://localhost/cache
// after
//   GOPROXY=file:///cache
Defensive patterns

Strategy: validation

Validate before calling

func validateFileProxyURL(s string) error {
    u, err := url.Parse(s)
    if err != nil { return err }
    if u.Scheme != "file" { return nil }
    want := url.URL{Scheme: u.Scheme, Path: u.Path, RawPath: u.RawPath}
    if *u != want { return fmt.Errorf("file:// proxy must be path-only: %s", u.Redacted()) }
    return nil
}

Type guard

func isPlainFileURL(s string) bool {
    u, err := url.Parse(s)
    if err != nil { return false }
    if u.Scheme != "file" { return false }
    plain := url.URL{Scheme: u.Scheme, Path: u.Path, RawPath: u.RawPath}
    return *u == plain
}

Prevention

When it happens

Trigger: GOPROXY contains `file://host/path`, `file:///path?x=1`, or `file://user@host/path`. The comparison `*base != url.URL{Scheme,Path,RawPath}` fails.

Common situations: Confusing file:// (with host) and file:/// (path only); pasting an http URL and only changing the scheme; templating that injects a query string.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/772834b9c6295045. Report an issue: GitHub.