chocolatey/choco · error · AggregateException

An exception occurred while copying files to '{0}'

Error message

An exception occurred while copying files to '{0}'

What it means

Thrown by DotNetFileSystem's directory-copy routine as an AggregateException when more than one individual file copy failed during a recursive copy. The method catches each per-file exception into a list, waits to let I/O settle, then wraps all collected exceptions into a single AggregateException whose message names the destination directory.

Source

Thrown at src/chocolatey/infrastructure/filesystem/DotNetFileSystem.cs:790

                var destinationFile = file.Replace(sourceDirectoryPath, destinationDirectoryPath);
                EnsureDirectoryExists(GetDirectoryName(destinationFile), ignoreError: true);
                //this.Log().Debug(ChocolateyLoggers.Verbose, "Copying '{0}' {1} to '{2}'".FormatWith(file, Environment.NewLine, destinationFile));

                try
                {
                    CopyFile(file, destinationFile, overwriteExisting, isSilent);
                }
                catch (Exception ex)
                {
                    exceptions.Add(ex);
                }
            }

            Thread.Sleep(1500); // sleep for enough time to allow the folder to finish copying

            if (exceptions.Count > 1)
            {
                throw new AggregateException("An exception occurred while copying files to '{0}'".FormatWith(destinationDirectoryPath), exceptions);
            }
            else if (exceptions.Count == 1)
            {
                throw exceptions[0];
            }
        }

        public void EnsureDirectoryExists(string directoryPath)
        {
            EnsureDirectoryExists(directoryPath, false);
        }

        public bool IsLockedDirectory(string directoryPath)
        {
            try
            {
                var permissions = Directory.GetAccessControl(directoryPath);

View on GitHub (pinned to 0d5abdd10c)

Solutions

  1. Inspect AggregateException.InnerExceptions to identify the actual failing files and root causes (lock, access denied, path length).
  2. Close other processes/AV scanning the directory, grant the running account read access, and retry.
  3. Enable long-path support or shorten the destination path if path-too-long is among the inner exceptions.

Example fix

// before
try { fs.CopyDirectory(src, dest, true, false); }
catch (Exception ex) { log(ex.Message); } // loses inner failures

// after
try { fs.CopyDirectory(src, dest, true, false); }
catch (AggregateException ex) {
  foreach (var inner in ex.InnerExceptions) log(inner.GetType().Name + ": " + inner.Message);
  throw;
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Pre-check: ensure source files are readable and not locked before bulk copy.
var unreadable = srcFiles.Where(f => {
    try { using (File.Open(f, FileMode.Open, FileAccess.Read, FileShare.Read)) {} return false; }
    catch { return true; }
}).ToList();
if (unreadable.Count > 0)
    throw new IOException(unreadable.Count + " source file(s) not readable/locked: " + string.Join(", ", unreadable));

Try / catch

try
{
    fs.CopyDirectory(src, dest, overwriteExisting, isSilent);
}
catch (AggregateException ex)
{
    foreach (var inner in ex.InnerExceptions)
        logger.Error("Copy failed: " + inner.GetType().Name + ": " + inner.Message);
    throw;
}

Prevention

When it happens

Trigger: Copying a directory tree where two or more files throw (lock, permission, missing source, path-too-long). Each failure is appended to the exceptions list; when exceptions.Count > 1 the AggregateException is raised with the inner list preserved.

Common situations: Antivirus or another process holding files open mid-copy, insufficient NTFS permissions on a subset of files, very long paths exceeding MAX_PATH on Windows, or a source tree partially locked by a running Chocolatey operation.

Related errors


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