[Bug]: container export -o deletes an existing directory

Abierto Apto para principiantes
#2,325 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

@devops-thiago ya está trabajando en esto.

Desde el 1/10/2026.

  • #2329 de @devops-thiago — abierto
  • #2361 de @dajiaohuang — cerrado sin fusionar

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
76/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
swift
Área
cli

Línea de trabajo

Lee Sources/ContainerCommands/Container/ContainerExport.swift alrededor de la limpieza y el traslado mencionados en ContainerExport.run y, después, inspecciona testExportCommandRunningContainerAndOverwrite para entender el comportamiento previsto al reemplazar archivos. Reproduce el problema con -o apuntando a un directorio que contenga un archivo; la tarea está hecha cuando la exportación rechaza ese destino antes de comenzar, mientras que el comportamiento de sobrescritura de archivos existentes sigue cubierto por el test.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

I have done the following
  • I have searched the existing issues
  • If possible, I've reproduced the issue using the 'main' branch of this project
Steps to reproduce

Point -o at a directory that already holds a file:

mkdir -p ~/work/out && echo "keep this" > ~/work/out/notes.txt
container run -d --name web alpine:latest sleep 900
container export -o ~/work/out web
ls -la ~/work
file ~/work/out

The export exits 0 and prints nothing. The directory has been replaced by the archive and notes.txt is gone (paths shortened):

total 17472
drwxr-xr-x@ 3 thiago  staff       96 Oct  1 14:27 .
drwxr-xr-x@ 6 thiago  staff      192 Oct  1 14:27 ..
-rw-r--r--@ 1 thiago  staff  8943104 Oct  1 14:27 out
~/work/out: POSIX tar archive

Before the export, out was a directory holding notes.txt. A debug build of main at 0a48a1b gives the same result.

With -o . the working directory is the one that goes:

mkdir -p ~/work/cwd && echo "keep this" > ~/work/cwd/notes.txt
cd ~/work/cwd && container export -o . web
ls -la ~/work

cwd is now a tar archive of 8943104 bytes and the directory with notes.txt no longer exists.

The removal also comes before the move, so when the move fails the previous output file is lost too. To see it, use a second volume with less free space than the archive (a 24 MB disk image here, filled to 4.3 MiB free):

$ echo old > /Volumes/exptest/out.tar
$ container export -o /Volumes/exptest/out.tar web
Error: “archive.tar” couldn’t be moved to “exptest” because there isn’t enough space.
$ cat /Volumes/exptest/out.tar
cat: /Volumes/exptest/out.tar: No such file or directory
Problem description

After the export has finished, ContainerExport.run clears the destination and moves the archive in:

https://github.com/apple/container/blob/0a48a1bd/Sources/ContainerCommands/Container/ContainerExport.swift#L70-L74

let outputURL = URL(fileURLWithPath: output!)
if FileManager.default.fileExists(atPath: outputURL.path(percentEncoded: false)) {
    try FileManager.default.removeItem(at: outputURL)
}
try FileManager.default.moveItem(at: archive, to: outputURL)

removeItem deletes a directory and everything under it. The option value is resolved against the current directory before this runs, so -o . becomes the working directory and -o ~/projects becomes that folder. Because the removal comes after the export, a long export finishes before the data is gone.

Because removeItem runs before moveItem, any failure of the move, a full volume for example, also leaves nothing at the path.

Replacing an existing file is intended: testExportCommandRunningContainerAndOverwrite exports twice to the same path. A directory should be refused instead, before the export starts.

Environment
  • OS: macOS 26.6.2 (25G83)
  • Xcode: 26.6 (17F113)
  • Container: 1.5.0 (Homebrew), and main at 0a48a1b
Code of Conduct
  • I agree to follow this project's Code of Conduct
Lenguaje dominante
Swift
Estrellas
51.1k
Forks
1.8k
Merge medio
2 d 10 h
PR fusionados (30 d)
37

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de apple/container

Todos los issues de apple/container

Issues similares

Más issues de Swift

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.