{"record":{"id":"15cdf2322fd3cb18","repo":"chocolatey/choco","slug":"an-unexpected-error-was-encountered-parsing-a-malf","errorCode":null,"errorMessage":"An unexpected error was encountered parsing a malformed nuspec file. This may occur if corrupt files are present in the package's install directory.","messagePattern":"An unexpected error was encountered parsing a malformed nuspec file\\. This may occur if corrupt files are present in the package's install directory\\.","errorType":"exception","errorClass":"ApplicationException","httpStatus":null,"severity":"error","filePath":"src/chocolatey/infrastructure.app/services/NugetService.cs","lineNumber":988,"sourceCode":"\r\n                        using (var downloadResult = downloadResource.GetDownloadResourceResultAsync(\r\n                                   packageDependencyInfo,\r\n                                   new PackageDownloadContext(sourceCacheContext),\r\n                                   NuGetEnvironment.GetFolderPath(NuGetFolderPath.Temp),\r\n                                   _nugetLogger, CancellationToken.None).GetAwaiter().GetResult())\r\n                        {\r\n                            ValidatePackageHash(config, packageDependencyInfo, downloadResult);\r\n                            try\r\n                            {\r\n                                nugetProject.InstallPackageAsync(\r\n                                    packageDependencyInfo,\r\n                                    downloadResult,\r\n                                    projectContext,\r\n                                    CancellationToken.None).GetAwaiter().GetResult();\r\n                            }\r\n                            catch (XmlException)\r\n                            {\r\n                                throw new ApplicationException(\"An unexpected error was encountered parsing a malformed nuspec file. This may occur if corrupt files are present in the package's install directory.\");\r\n                            }\r\n                        }\r\n\r\n                        var installedPath = nugetProject.GetInstalledPath(packageDependencyInfo);\r\n                        NormalizeNuspecCasing(packageRemoteMetadata, installedPath);\r\n\r\n                        RemovePackageFromNugetCache(packageRemoteMetadata);\r\n\r\n                        var manifestPath = nugetProject.GetInstalledManifestFilePath(packageDependencyInfo);\r\n                        var packageMetadata = new ChocolateyPackageMetadata(manifestPath, _fileSystem);\r\n\r\n                        this.Log().Info(ChocolateyLoggers.Important, \"{0}{1} v{2}{3}{4}{5}\".FormatWith(\r\n                            System.Environment.NewLine,\r\n                            packageMetadata.Id,\r\n                            packageMetadata.Version.ToFullStringChecked(),\r\n                            config.Force ? \" (forced)\" : string.Empty,\r\n                            packageRemoteMetadata.IsApproved ? \" [Approved]\" : string.Empty,\r\n                            packageRemoteMetadata.PackageTestResultStatus == \"Failing\" && packageRemoteMetadata.IsDownloadCacheAvailable ? \" - Likely broken for FOSS users (due to download location changes)\" : packageRemoteMetadata.PackageTestResultStatus == \"Failing\" ? \" - Possibly broken\" : string.Empty\r","sourceCodeStart":970,"sourceCodeEnd":1006,"githubUrl":"https://github.com/chocolatey/choco/blob/0d5abdd10cc177a141e69547cad6935b419b6c17/src/chocolatey/infrastructure.app/services/NugetService.cs#L970-L1006","documentation":"Wraps an XmlException raised by NuGet's InstallPackageAsync into an ApplicationException, signaling that the package's .nuspec file could not be parsed. Per the message this typically happens when corrupt files are left in the package's install directory from a previous failed install.","triggerScenarios":"InstallPackageAsync reads the package's .nuspec and throws XmlException because the XML is malformed or truncated. Only XmlException is caught and rewrapped; any other exception type propagates unwrapped.","commonSituations":"An interrupted previous install left a partial nuspec in the lib directory; a manually edited nuspec is invalid XML; antivirus quarantined part of the package files; a disk-full condition wrote a truncated nuspec.","solutions":["Force reinstall the package (choco install <pkg> --force) so Chocolatey re-downloads and overwrites corrupt files","Delete the package folder under the install lib directory and reinstall","Clear the NuGet/Chocolatey cache (choco cache remove) and retry","Re-download the .nupkg from the source and verify its .nuspec opens as valid XML"],"exampleFix":"// before\nchoco install mypkg\n// after\nchoco install mypkg --force","handlingStrategy":"try-catch","validationCode":"// Validate the on-disk nuspec before relying on it.\nvar nuspecPath = Path.Combine(installDir, pkg.Id + \".nuspec\");\nif (File.Exists(nuspecPath)) {\n    try { var _ = XDocument.Load(nuspecPath); }\n    catch (XmlException) { fileSystem.DeleteDirectoryChecked(installDir, recursive: true); } // force clean reinstall\n}","typeGuard":null,"tryCatchPattern":"try { nugetService.InstallPackage(...); }\ncatch (ApplicationException ex) when (ex.Message.Contains(\"malformed nuspec\")) {\n    // corrupt nuspec in the install dir -> force reinstall\n    fileSystem.DeleteDirectoryChecked(installDir, recursive: true);\n    choco.Install(pkg, --force);\n}","preventionTips":["Run with --force after a failed/interrupted install to overwrite corrupt files","Free up disk and disable over-eager AV that truncates package files","Validate a nuspec loads as XML before packaging/pushing to a feed"],"tags":["nuspec","xml","install","corruption"],"backgroundTag":null,"analyzedSha":"0d5abdd10cc177a141e69547cad6935b419b6c17","analyzedAt":"2026-08-13T18:33:03.301Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}