git-ecosystem/git-credential-manager · error · InvalidOperationException

Failed to parse host name and/or port

Error message

Failed to parse host name and/or port

What it means

GetAccountNameForCredentialQuery throws this InvalidOperationException when request.TryGetHostAndPort fails to parse a host (and port) out of the request URI, so the account name for the Azure DevOps credential query cannot be determined. Building the credential lookup requires at minimum a parseable host name.

Solutions

  1. Inspect the remote URL: git remote -v and fix malformed entries with git remote set-url origin <full-https-url>
  2. Ensure the URL includes scheme and host, e.g. https://dev.azure.com/org/project/_git/repo
  3. If constructing GitRequest programmatically, pass an absolute well-formed absolute URI

Example fix

// before (malformed remote)
git remote set-url origin myorg/Proj/_git/repo
// after
git remote set-url origin https://dev.azure.com/myorg/Proj/_git/repo
Defensive patterns

Strategy: validation

Validate before calling

// C#
static bool HasAbsoluteHost(Uri u) => !u.IsAbsoluteUri ||
    (Uri.TryCreate(u.ToString(), UriKind.Absolute, out var abs) &&
     !string.IsNullOrEmpty(abs.Host));
// Or in shell: git remote get-url origin must start with http(s)://

Type guard

static bool IsAbsoluteHttpUri(Uri u) =>
    u.IsAbsoluteUri && (u.Scheme == Uri.UriSchemeHttps || u.Scheme == Uri.UriSchemeHttp) &&
    !string.IsNullOrEmpty(u.Host);

Try / catch

try {
    var account = provider.Account;
} catch (InvalidOperationException ex) when (ex.Message.Contains("Failed to parse host")) {
    // surface: fix remote URL in .git/config
}

Prevention

When it happens

Trigger: Calling the Account property (GetAccountNameForCredentialQuery) on a GitRequest whose URI cannot be decomposed into host and port — typically a relative, malformed, or non-hierarchical remote URL.

Common situations: See trigger scenarios.

Understand the failure class

Background: "Invalid URL" errors: why new URL(), URI.parse, and reqwest::Url reject your string — missing scheme, whitespace, and bad path format — this error's family across 39 libraries.

Related errors


AI-assisted analysis of git-ecosystem/git-credential-manager@e8ce762cd0 (2026-09-11). Data as JSON: /api/errors/c01196c0125de3a5. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.AzureRepos/AzureReposHostProvider.cs:476

                return UriHelpers.CreateOrganizationUri(remoteUri, out _).AbsoluteUri.TrimEnd('/');
            }

            // *.visualstudio.com
            if (UriHelpers.IsVisualStudioComHost(remoteUri.Host))
            {
                // If we're given the full path for an older *.visualstudio.com-style URL then we should
                // respect that in the service name.
                return remoteUri.WithoutUserInfo().AbsoluteUri.TrimEnd('/');
            }

            throw new InvalidOperationException("Host is not Azure DevOps.");
        }

        private static string GetAccountNameForCredentialQuery(GitRequest request)
        {
            if (!request.TryGetHostAndPort(out string hostName, out _))
            {
                throw new InvalidOperationException("Failed to parse host name and/or port");
            }

            // dev.azure.com
            if (UriHelpers.IsDevAzureComHost(hostName))
            {
                // We ignore the given username for dev.azure.com-style URLs because AzDevOps recommends
                // adding the organization name as the user in the remote URL (resulting in URLs like
                // https://org@dev.azure.com/org/foo/_git/bar) and we don't know if the given username
                // is an actual username, or the org name.
                // Use `null` as the account name so we match all possible credentials (regardless of
                // the account).
                return null;
            }

            // *.visualstudio.com
            if (UriHelpers.IsVisualStudioComHost(hostName))
            {
                // If we're given a username for the vs.com-style URLs we can and should respect any

View on GitHub (pinned to e8ce762cd0)