chocolatey/choco · error · ApplicationException

An unexpected error was encountered parsing a malformed nusp

Error message

An unexpected error was encountered parsing a malformed nuspec file. This may occur if corrupt files are present in the package's install directory.

What it means

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.

Source

Thrown at src/chocolatey/infrastructure.app/services/NugetService.cs:988

                        using (var downloadResult = downloadResource.GetDownloadResourceResultAsync(
                                   packageDependencyInfo,
                                   new PackageDownloadContext(sourceCacheContext),
                                   NuGetEnvironment.GetFolderPath(NuGetFolderPath.Temp),
                                   _nugetLogger, CancellationToken.None).GetAwaiter().GetResult())
                        {
                            ValidatePackageHash(config, packageDependencyInfo, downloadResult);
                            try
                            {
                                nugetProject.InstallPackageAsync(
                                    packageDependencyInfo,
                                    downloadResult,
                                    projectContext,
                                    CancellationToken.None).GetAwaiter().GetResult();
                            }
                            catch (XmlException)
                            {
                                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.");
                            }
                        }

                        var installedPath = nugetProject.GetInstalledPath(packageDependencyInfo);
                        NormalizeNuspecCasing(packageRemoteMetadata, installedPath);

                        RemovePackageFromNugetCache(packageRemoteMetadata);

                        var manifestPath = nugetProject.GetInstalledManifestFilePath(packageDependencyInfo);
                        var packageMetadata = new ChocolateyPackageMetadata(manifestPath, _fileSystem);

                        this.Log().Info(ChocolateyLoggers.Important, "{0}{1} v{2}{3}{4}{5}".FormatWith(
                            System.Environment.NewLine,
                            packageMetadata.Id,
                            packageMetadata.Version.ToFullStringChecked(),
                            config.Force ? " (forced)" : string.Empty,
                            packageRemoteMetadata.IsApproved ? " [Approved]" : string.Empty,
                            packageRemoteMetadata.PackageTestResultStatus == "Failing" && packageRemoteMetadata.IsDownloadCacheAvailable ? " - Likely broken for FOSS users (due to download location changes)" : packageRemoteMetadata.PackageTestResultStatus == "Failing" ? " - Possibly broken" : string.Empty

View on GitHub (pinned to 0d5abdd10c)

Solutions

  1. Force reinstall the package (choco install <pkg> --force) so Chocolatey re-downloads and overwrites corrupt files
  2. Delete the package folder under the install lib directory and reinstall
  3. Clear the NuGet/Chocolatey cache (choco cache remove) and retry
  4. Re-download the .nupkg from the source and verify its .nuspec opens as valid XML

Example fix

// before
choco install mypkg
// after
choco install mypkg --force
Defensive patterns

Strategy: try-catch

Validate before calling

// Validate the on-disk nuspec before relying on it.
var nuspecPath = Path.Combine(installDir, pkg.Id + ".nuspec");
if (File.Exists(nuspecPath)) {
    try { var _ = XDocument.Load(nuspecPath); }
    catch (XmlException) { fileSystem.DeleteDirectoryChecked(installDir, recursive: true); } // force clean reinstall
}

Try / catch

try { nugetService.InstallPackage(...); }
catch (ApplicationException ex) when (ex.Message.Contains("malformed nuspec")) {
    // corrupt nuspec in the install dir -> force reinstall
    fileSystem.DeleteDirectoryChecked(installDir, recursive: true);
    choco.Install(pkg, --force);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Related errors


AI-assisted analysis of chocolatey/choco@0d5abdd10c (2026-08-13). Data as JSON: /api/errors/15cdf2322fd3cb18. Report an issue: GitHub.