{"record":{"id":"4c8977b77f113391","repo":"microsoft/aspire","slug":"could-not-find-packageversion-element-for-0-in-1","errorCode":null,"errorMessage":"Could not find PackageVersion element for '{0}' in {1}","messagePattern":"Could not find PackageVersion element for '(.+?)' in (.+?)","errorType":"exception","errorClass":"ProjectUpdaterException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Cli/Projects/ProjectUpdater.cs","lineNumber":1219,"sourceCode":"            logger.LogWarning(ex, \"Exception while resolving MSBuild property '{PropertyName}' for project '{ProjectFile}'\", propertyName, projectFile.FullName);\n            return null;\n        }\n    }\n\n    private static bool IsValidSemanticVersion(string version)\n    {\n        return SemVersion.TryParse(version, SemVersionStyles.Strict, out _);\n    }\n\n    private static async Task UpdatePackageVersionInDirectoryPackagesProps(string packageId, string newVersion, FileInfo directoryPackagesPropsFile)\n    {\n        var doc = new XmlDocument { PreserveWhitespace = true };\n        doc.Load(directoryPackagesPropsFile.FullName);\n\n        var packageVersionNode = doc.SelectSingleNode(CaseInsensitiveIncludeXPath(\"/Project/ItemGroup/PackageVersion\", packageId));\n        if (packageVersionNode?.Attributes?[\"Version\"] is null)\n        {\n            throw new ProjectUpdaterException(string.Format(CultureInfo.InvariantCulture, UpdateCommandStrings.CouldNotFindPackageVersionInDirectoryPackagesProps, packageId, directoryPackagesPropsFile.FullName));\n        }\n\n        packageVersionNode.Attributes[\"Version\"]!.Value = newVersion;\n        doc.Save(directoryPackagesPropsFile.FullName);\n\n        await Task.CompletedTask;\n    }\n\n    private async Task UpdatePackageReferenceInProject(FileInfo projectFile, NuGetPackageCli package, CancellationToken cancellationToken)\n    {\n        // Pass --no-restore here so each per-package edit only mutates the project / file-based AppHost.\n        // A single restore is performed once *all* update steps have completed (see UpdateProjectAsync).\n        // Restoring per-package would run NuGet against a half-updated reference graph, which is fatal\n        // when the channel's nuget.config (already merged earlier in UpdateProjectAsync for Explicit\n        // channels) contains a packageSourceMapping pinning Aspire* to a feed that does not carry the\n        // not-yet-bumped versions. See https://github.com/dotnet/aspire/issues/15891.\n        var exitCode = await runner.AddPackageAsync(\n            projectFilePath: projectFile,","sourceCodeStart":1201,"sourceCodeEnd":1237,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Cli/Projects/ProjectUpdater.cs#L1201-L1237","documentation":"When updating Aspire package versions under Central Package Management, the updater looks up <PackageVersion Include=\"{packageId}\"> in Directory.Packages.props via case-insensitive XPath and requires a Version attribute. If the element is missing or has no Version attribute, it throws ProjectUpdaterException naming the package and file.","triggerScenarios":"Updating a package whose id has no <PackageVersion Include=\"...\"> entry in Directory.Packages.props, or whose entry lacks a Version attribute — i.e. the package is referenced by the project but centrally managed metadata is absent/incomplete.","commonSituations":"Package referenced directly in the csproj with an inline Version while Directory.Packages.props was never given an entry; a typo'd package id in the props file; a hand-edited props file missing the Version attribute; CPM enabled (ManagePackageVersionsCentrally) but props file out of sync.","solutions":["Add <PackageVersion Include=\"{packageId}\" Version=\"x.y.z\" /> to Directory.Packages.props for the named package, then re-run `aspire update`.","Fix the package id spelling in Directory.Packages.props so it matches the ProjectReference/PackageReference id exactly.","Give the existing <PackageVersion> element a Version attribute if it is missing."],"exampleFix":"// before (Directory.Packages.props)\n<PackageVersion Include=\"Aspire.Hosting.AppHost\" />\n// after\n<PackageVersion Include=\"Aspire.Hosting.AppHost\" Version=\"9.4.0\" />","handlingStrategy":"validation","validationCode":"var doc = new XmlDocument(); doc.Load(\"Directory.Packages.props\");\nvar node = doc.SelectSingleNode($\"//PackageVersion[translate(@Include,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='{packageId.ToLowerInvariant()']\");\nif (node?.Attributes[\"Version\"] is null)\n    throw new InvalidOperationException($\"Directory.Packages.props is missing a Version for '{packageId}'.\");","typeGuard":null,"tryCatchPattern":"try { await updater.UpdateAsync(...); }\ncatch (ProjectUpdaterException ex) when (ex.Message.StartsWith(\"Could not find PackageVersion element\"))\n{\n    // add <PackageVersion Include=\"{id}\" Version=\"...\"/> to Directory.Packages.props, then retry\n}","preventionTips":["Whenever a project references an Aspire.* package, add the matching PackageVersion entry to Directory.Packages.props.","Never hand-delete the Version attribute from PackageVersion elements.","Keep package ids in Directory.Packages.props spelled exactly as in the referencing projects; validate CPM consistency in CI."],"tags":["aspire-cli","central-package-management","packageversion","directory-packages-props"],"backgroundTag":"config-file-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"}