hashicorp/terraform · error

error parsing GitHub URL: %s

Error message

error parsing GitHub URL: %s

What it means

Returned by detectGitHub (detect_git.go:54) when url.Parse fails on the https URL assembled from the first three github.com path segments. After constructing https://github.com/<username>/<repo>, net/url.Parse rejects it. This is uncommon because the constructed URL is simple, but invalid characters in the username/repo segments can trigger it.

Source

Thrown at internal/getmodules/moduleaddrs/detect_git.go:54

// translates them into git HTTP source addresses.
func detectGitHub(src string) (string, bool, error) {
	if len(src) == 0 {
		return "", false, nil
	}

	if strings.HasPrefix(src, "github.com/") {
		src, rawQuery, _ := strings.Cut(src, "?")

		parts := strings.Split(src, "/")
		if len(parts) < 3 {
			return "", false, fmt.Errorf(
				"GitHub URLs should be github.com/username/repo")
		}

		urlStr := fmt.Sprintf("https://%s", strings.Join(parts[:3], "/"))
		url, err := url.Parse(urlStr)
		if err != nil {
			return "", true, fmt.Errorf("error parsing GitHub URL: %s", err)
		}
		url.RawQuery = rawQuery

		if !strings.HasSuffix(url.Path, ".git") {
			url.Path += ".git"
		}

		if len(parts) > 3 {
			url.Path += "//" + strings.Join(parts[3:], "/")
		}

		return "git::" + url.String(), true, nil
	}

	return "", false, nil
}

// detectBitBucket detects shorthand schemeless references to bitbucket.org and

View on GitHub (pinned to c9def3e214)

Solutions

  1. Trim whitespace and stray characters from the source string; ensure only valid GitHub username/repo characters (alphanumeric, dash, underscore, dot).
  2. URL-encode any legitimately special characters in the path segments.
  3. Use the full https URL form directly: source = "git::https://github.com/user/repo.git".
  4. Check the parse error detail in the message for the offending token.

Example fix

# before — stray trailing space / invalid char
source = "github.com/user repo"

# after — valid github shorthand
source = "github.com/user/repo"
Defensive patterns

Strategy: validation

Validate before calling

// Pre-parse the assembled GitHub URL to catch bad characters
func githubURLParses(src string) bool {
    if !strings.HasPrefix(src, "github.com/") {
        return true
    }
    parts := strings.Split(strings.SplitN(src, "?", 2)[0], "/")
    if len(parts) < 3 {
        return false
    }
    _, err := url.Parse("https://" + strings.Join(parts[:3], "/"))
    return err == nil
}

Type guard

func githubURLParses(src string) bool {
    parts := strings.Split(strings.SplitN(src, "?", 2)[0], "/")
    if len(parts) < 3 {
        return false
    }
    _, err := url.Parse("https://" + strings.Join(parts[:3], "/"))
    return err == nil
}

Prevention

When it happens

Trigger: A github.com shorthand source whose username or repo segment contains characters that make the assembled https URL unparseable by net/url.Parse (e.g. spaces, control chars, bad percent-encoding).

Common situations: A username or repo name with spaces, uppercase with stray characters, or a copy-paste that includes surrounding quotes/whitespace. A source string with embedded newlines or tabs. Misencoded special characters.

Related errors


AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07). Data as JSON: /api/errors/a38787470dded6a2. Report an issue: GitHub.