dotnet/maui · error · Exception

Failed to download the package.

Error message

Failed to download the package.

What it means

Thrown by the NuGet package download helper when CopyNupkgToStreamAsync returns false, meaning the server/protocol could not fulfill the download for the requested packageId/version. The file stream has already been created (possibly empty/partial) at packagePath, but the download is treated as failed and aborted before reporting success.

Source

Thrown at eng/cake/helpers.cake:148

    var packageVersion = NuGetVersion.Parse(version);
    var cacheContext = new SourceCacheContext();
    
    // Set up logging (optional, use NullLogger if you don't need logging)
    ILogger logger = NullLogger.Instance;

    // Download the package to the output directory
    EnsureDirectoryExists(outputDirectory);
    var packagePath = System.IO.Path.Combine(outputDirectory, $"{packageId}.{version}.nupkg");
    
    using (var fileStream = new FileStream(packagePath, FileMode.Create, FileAccess.Write, FileShare.None))
    {
        // Download package
        var success = await resource.CopyNupkgToStreamAsync(
            packageId, packageVersion, fileStream, cacheContext, logger, default);

        if (!success)
        {
            throw new Exception("Failed to download the package.");
        }

        Information("Package '{0} v{1}' downloaded successfully to {2}", packageId, version, packagePath);
    }
}

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Verify the packageId and version exist on the feed: `dotnet nuget list <packageId>` or browse the feed UI.
  2. Check NuGet.config source URLs and credentials/PAT validity used by the script.
  3. Clear the local NuGet HTTP cache (`dotnet nuget locals http-cache --clear`) and retry in case of a corrupt cached response.
  4. For transient errors, retry the cake target; if it persists, capture verbose logger output to see the underlying NuGet error.
Defensive patterns

Strategy: retry

Validate before calling

// Validate package existence on the feed before downloading
var found = await resource.DoesPackageExistAsync(packageId, packageVersion, cacheContext, logger, default);
if (!found)
    throw new Exception($"Package {packageId} {packageVersion} not found on configured feeds.");

Try / catch

for (int attempt = 1; attempt <= 3; attempt++) {
    using (var fs = new FileStream(packagePath, FileMode.Create, FileAccess.Write, FileShare.None)) {
        if (await resource.CopyNupkgToStreamAsync(packageId, packageVersion, fs, cacheContext, logger, default)) {
            Information("Downloaded on attempt {0}", attempt);
            return;
        }
    }
    await Task.Delay(TimeSpan.FromSeconds(2 * attempt));
}
throw new Exception("Failed to download the package after retries.");

Prevention

When it happens

Trigger: resource.CopyNupkgToStreamAsync(packageId, packageVersion, fileStream, cacheContext, logger, default) returns false for the requested packageId and version against the configured NuGet source.

Common situations: The package id/version does not exist on the configured feed; the feed URL/auth is wrong or the PAT has expired; a transient network failure or HTTP 429; a typo in the version string; the local HTTP cache is corrupt.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/e9dea08b9a64f579. Report an issue: GitHub.