{"record":{"id":"c01196c0125de3a5","repo":"git-ecosystem/git-credential-manager","slug":"failed-to-parse-host-name-and-or-port","errorCode":null,"errorMessage":"Failed to parse host name and/or port","messagePattern":"Failed to parse host name and/or port","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.AzureRepos/AzureReposHostProvider.cs","lineNumber":476,"sourceCode":"                return UriHelpers.CreateOrganizationUri(remoteUri, out _).AbsoluteUri.TrimEnd('/');\n            }\n\n            // *.visualstudio.com\n            if (UriHelpers.IsVisualStudioComHost(remoteUri.Host))\n            {\n                // If we're given the full path for an older *.visualstudio.com-style URL then we should\n                // respect that in the service name.\n                return remoteUri.WithoutUserInfo().AbsoluteUri.TrimEnd('/');\n            }\n\n            throw new InvalidOperationException(\"Host is not Azure DevOps.\");\n        }\n\n        private static string GetAccountNameForCredentialQuery(GitRequest request)\n        {\n            if (!request.TryGetHostAndPort(out string hostName, out _))\n            {\n                throw new InvalidOperationException(\"Failed to parse host name and/or port\");\n            }\n\n            // dev.azure.com\n            if (UriHelpers.IsDevAzureComHost(hostName))\n            {\n                // We ignore the given username for dev.azure.com-style URLs because AzDevOps recommends\n                // adding the organization name as the user in the remote URL (resulting in URLs like\n                // https://org@dev.azure.com/org/foo/_git/bar) and we don't know if the given username\n                // is an actual username, or the org name.\n                // Use `null` as the account name so we match all possible credentials (regardless of\n                // the account).\n                return null;\n            }\n\n            // *.visualstudio.com\n            if (UriHelpers.IsVisualStudioComHost(hostName))\n            {\n                // If we're given a username for the vs.com-style URLs we can and should respect any","sourceCodeStart":458,"sourceCodeEnd":494,"githubUrl":"https://github.com/git-ecosystem/git-credential-manager/blob/e8ce762cd04b4100ae637b5fbf39ef9d0a96561e/src/Microsoft.AzureRepos/AzureReposHostProvider.cs#L458-L494","documentation":"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.","triggerScenarios":"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.","commonSituations":"See trigger scenarios.","solutions":["Inspect the remote URL: git remote -v and fix malformed entries with git remote set-url origin <full-https-url>","Ensure the URL includes scheme and host, e.g. https://dev.azure.com/org/project/_git/repo","If constructing GitRequest programmatically, pass an absolute well-formed absolute URI"],"exampleFix":"// before (malformed remote)\ngit remote set-url origin myorg/Proj/_git/repo\n// after\ngit remote set-url origin https://dev.azure.com/myorg/Proj/_git/repo","handlingStrategy":"validation","validationCode":"// C#\nstatic bool HasAbsoluteHost(Uri u) => !u.IsAbsoluteUri ||\n    (Uri.TryCreate(u.ToString(), UriKind.Absolute, out var abs) &&\n     !string.IsNullOrEmpty(abs.Host));\n// Or in shell: git remote get-url origin must start with http(s)://","typeGuard":"static bool IsAbsoluteHttpUri(Uri u) =>\n    u.IsAbsoluteUri && (u.Scheme == Uri.UriSchemeHttps || u.Scheme == Uri.UriSchemeHttp) &&\n    !string.IsNullOrEmpty(u.Host);","tryCatchPattern":"try {\n    var account = provider.Account;\n} catch (InvalidOperationException ex) when (ex.Message.Contains(\"Failed to parse host\")) {\n    // surface: fix remote URL in .git/config\n}","preventionTips":["Always configure remotes as absolute http(s) URLs with an explicit host","Sanitize URLs from scripts/tools before writing them into .git/config","Run git remote -v after changing remotes to catch malformed entries"],"tags":["azure-devops","url-parsing","remote-url","credential-query"],"backgroundTag":"invalid-url-format","analyzedSha":"e8ce762cd04b4100ae637b5fbf39ef9d0a96561e","analyzedAt":"2026-09-11T17:15:08.753Z","contentChangedAt":"2026-09-11T17:15:08.753Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}