{"record":{"id":"5dc65c7986942293","repo":"microsoft/aspire","slug":"could-not-find-sdk-name-aspire-apphost-sdk-element-in","errorCode":null,"errorMessage":"Could not find <Sdk Name='Aspire.AppHost.Sdk' /> element in {projectFile.FullName}","messagePattern":"Could not find <Sdk Name='Aspire\\.AppHost\\.Sdk' /> element in (.+?)","errorType":"exception","errorClass":"ProjectUpdaterException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Cli/Projects/ProjectUpdater.cs","lineNumber":720,"sourceCode":"            throw new ProjectUpdaterException(string.Format(CultureInfo.InvariantCulture, UpdateCommandStrings.CouldNotFindRootProjectElementFormat, projectFile.FullName));\n        }\n\n        // Check if the SDK is set via the Sdk attribute on the Project element (new format)\n        var sdkAttribute = projectNode.Attributes?[\"Sdk\"];\n        if (sdkAttribute is not null && ContainsAspireAppHostSdk(sdkAttribute.Value))\n        {\n            // Already using new format: <Project Sdk=\"Aspire.AppHost.Sdk/version\">\n            // Update the version, preserving any other SDKs in the attribute\n            sdkAttribute.Value = UpdateAspireAppHostSdkVersion(sdkAttribute.Value, package.Version);\n        }\n        else\n        {\n            // Migrate from old format to new format\n            // Old format: <Sdk Name=\"Aspire.AppHost.Sdk\" Version=\"...\" />\n            var sdkNode = projectNode.SelectSingleNode(\"Sdk[@Name='Aspire.AppHost.Sdk']\");\n            if (sdkNode is null)\n            {\n                throw new ProjectUpdaterException(string.Format(CultureInfo.InvariantCulture, UpdateCommandStrings.CouldNotFindSdkElementFormat, projectFile.FullName));\n            }\n\n            // Set the new format: <Project Sdk=\"Aspire.AppHost.Sdk/version\">\n            // The Aspire.AppHost.Sdk already includes the base .NET SDK, so we replace any existing SDK attribute\n            if (sdkAttribute is not null)\n            {\n                sdkAttribute.Value = $\"{AspireAppHostSdkName}/{package.Version}\";\n            }\n            else\n            {\n                var newSdkAttribute = projectDocument.CreateAttribute(\"Sdk\");\n                newSdkAttribute.Value = $\"{AspireAppHostSdkName}/{package.Version}\";\n                projectNode.Attributes!.SetNamedItem(newSdkAttribute);\n            }\n\n            // Remove the old <Sdk Name=\"Aspire.AppHost.Sdk\" /> element and any surrounding whitespace\n            RemoveNodeWithWhitespace(sdkNode);\n        }","sourceCodeStart":702,"sourceCodeEnd":738,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Cli/Projects/ProjectUpdater.cs#L702-L738","documentation":"For an old-format AppHost csproj (no Sdk attribute on <Project>), the updater looks for <Sdk Name='Aspire.AppHost.Sdk' /> to migrate to the new <Project Sdk=\"Aspire.AppHost.Sdk/version\"> format. If the Sdk element is absent, it cannot perform the migration and throws ProjectUpdaterException.","triggerScenarios":"UpdateSdkVersionInProjectAppHostAsync encountering a csproj with no Sdk attribute on <Project> AND no <Sdk Name=\"Aspire.AppHost.Sdk\" .../> child element — the file references the Aspire AppHost SDK in neither recognized format.","commonSituations":"An AppHost csproj where the SDK is declared through imported props/targets instead of a direct Sdk element; a file that lost its <Sdk> line in a merge; a hand-edited csproj with a misspelled SDK name (e.g. Name=\"Aspire.Apphost.Sdk\").","solutions":["Add <Sdk Name=\"Aspire.AppHost.Sdk\" Version=\"x.y.z\" /> as a child of <Project> (old format) or move to <Project Sdk=\"Aspire.AppHost.Sdk/x.y.z\"> (new format), then re-run update.","Check for typos/casing in the Sdk element's Name attribute — it must be exactly 'Aspire.AppHost.Sdk'.","Restore the original csproj from source control if the SDK declaration was accidentally deleted."],"exampleFix":"// before\n<Project>\n  <!-- no Sdk element -->\n</Project>\n// after\n<Project Sdk=\"Aspire.AppHost.Sdk/9.4.0\" />","handlingStrategy":"validation","validationCode":"var doc = new XmlDocument();\ndoc.Load(appHostCsproj);\nvar hasSdkAttr = doc.DocumentElement?.Attributes[\"Sdk\"] is not null;\nvar hasSdkEl = doc.SelectSingleNode(\"//Sdk[@Name='Aspire.AppHost.Sdk']\") is not null;\nif (!hasSdkAttr && !hasSdkEl)\n    throw new InvalidOperationException($\"{appHostCsproj} does not declare Aspire.AppHost.Sdk in either supported format.\");","typeGuard":null,"tryCatchPattern":"try { await updater.UpdateAsync(...); }\ncatch (ProjectUpdaterException ex) when (ex.Message.Contains(\"Could not find <Sdk Name='Aspire.AppHost.Sdk'\"))\n{\n    // add the Sdk declaration manually, then re-run update\n}","preventionTips":["Always declare the AppHost SDK via <Project Sdk=\"Aspire.AppHost.Sdk/version\"> (preferred).","Don't move SDK declaration into imported props/targets the updater cannot see.","After merges, verify the <Sdk> element or Sdk attribute is still present."],"tags":["aspire-cli","csproj","sdk","migration","update-command"],"backgroundTag":"record-not-found","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}