{"record":{"id":"d6f7218bf3f407e8","repo":"microsoft/aspire","slug":"could-not-find-sdk-aspire-apphost-sdk-version-directive-in","errorCode":null,"errorMessage":"Could not find '#:sdk Aspire.AppHost.Sdk@<version>' directive in single-file AppHost: {0}","messagePattern":"Could not find '#:sdk Aspire\\.AppHost\\.Sdk@<version>' directive in single-file AppHost: (.+?)","errorType":"exception","errorClass":"ProjectUpdaterException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Cli/Projects/ProjectUpdater.cs","lineNumber":881,"sourceCode":"                    \"removed from '{1}': {2}. Please manually remove the <PackageVersion Include=\\\"{0}\\\" ... /> \" +\n                    \"entry from this file to avoid NU1009 build errors.\",\n                    packageId,\n                    cpmInfo.DirectoryPackagesPropsFile.FullName,\n                    ex.Message),\n                ex);\n        }\n    }\n\n    private static async Task UpdateSdkVersionInSingleFileAppHostAsync(FileInfo projectFile, NuGetPackageCli package)\n    {\n        var fileContent = await File.ReadAllTextAsync(projectFile.FullName);\n\n        // Look for the #:sdk Aspire.AppHost.Sdk@<version> directive\n        var match = SdkDirectiveRegex().Match(fileContent);\n\n        if (!match.Success)\n        {\n            throw new ProjectUpdaterException(string.Format(CultureInfo.InvariantCulture,\n                \"Could not find '#:sdk Aspire.AppHost.Sdk@<version>' directive in single-file AppHost: {0}\", projectFile.FullName));\n        }\n\n        // Replace the matched SDK directive with the new version\n        var newDirective = $\"#:sdk Aspire.AppHost.Sdk@{package.Version}\";\n        var updatedContent = SdkDirectiveRegex().Replace(fileContent, newDirective, 1);\n\n        // The new SDK pulls Aspire.Hosting.AppHost in implicitly, so a leftover\n        // explicit `#:package Aspire.Hosting.AppHost@<version>` directive in the\n        // same file is redundant. Strip it here so the SDK-bump path matches\n        // the csproj migration in UpdateSdkVersionInProjectAppHostAsync. The\n        // SDK-already-current path goes through EnqueueLegacyAppHostCleanupStepIfNeeded.\n        updatedContent = LegacyAppHostPackageDirectiveRegex().Replace(updatedContent, string.Empty);\n\n        await File.WriteAllTextAsync(projectFile.FullName, updatedContent);\n    }\n\n    [GeneratedRegex(@\"#:sdk\\s+Aspire\\.AppHost\\.Sdk@(?:[\\d\\.\\-a-zA-Z]+|\\*)\")]","sourceCodeStart":863,"sourceCodeEnd":899,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Cli/Projects/ProjectUpdater.cs#L863-L899","documentation":"Single-file AppHosts (.cs) declare their SDK with a file directive `#:sdk Aspire.AppHost.Sdk@<version>`. UpdateSdkVersionInSingleFileAppHostAsync regex-matches that directive; if no line matches, it cannot update the SDK version and throws ProjectUpdaterException.","triggerScenarios":"Updating a single-file AppHost whose content lacks a `#:sdk Aspire.AppHost.Sdk@<version>` directive — e.g. directive deleted, different SDK name, wrong @ separator, or the .cs file isn't actually a single-file AppHost.","commonSituations":"Hand-edited single-file AppHost where the directive was removed or reformatted (e.g. `#:sdk aspire.apphost.sdk@9.0.0` casing mismatch); an older single-file layout predating the directive convention; a plain .cs file misidentified as an AppHost.","solutions":["Add or restore the directive at the top of the .cs AppHost: `#:sdk Aspire.AppHost.Sdk@<current-version>`, then re-run `aspire update`.","Check spelling/casing: the directive must be exactly `#:sdk Aspire.AppHost.Sdk@` followed by a version.","Edit the version in the directive manually if you prefer to skip the updater for this file."],"exampleFix":"// before (top of apphost.cs)\n#:project\n// after\n#:sdk Aspire.AppHost.Sdk@9.4.0","handlingStrategy":"validation","validationCode":"var content = File.ReadAllText(singleFileAppHost);\nif (!System.Text.RegularExpressions.Regex.IsMatch(content, \"#:sdk Aspire\\.AppHost\\.Sdk@[^\\s]+\"))\n    throw new InvalidOperationException($\"{singleFileAppHost} lacks the #:sdk Aspire.AppHost.Sdk@<version> directive.\");","typeGuard":null,"tryCatchPattern":"try { await updater.UpdateAsync(...); }\ncatch (ProjectUpdaterException ex) when (ex.Message.Contains(\"#:sdk Aspire.AppHost.Sdk\"))\n{\n    // add the directive at the top of the file, then re-run update\n}","preventionTips":["Keep the #:sdk directive as the first line of single-file AppHosts; never delete it during edits.","Use exact casing: `#:sdk Aspire.AppHost.Sdk@` followed by a version number.","Run `aspire update` only on files produced by the Aspire single-file AppHost template."],"tags":["aspire-cli","single-file-apphost","sdk-directive","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"}