[Bug]: `container machine logs -f` exits immediately when the log is empty

Abierto Apto para principiantes
#2,492 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

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, testing

Línea de trabajo

Empieza en Sources/ContainerCommands/Machine/MachineLogs.swift, alrededor de las líneas 101–119 de tail(), y luego compara el comportamiento con un archivo vacío con la corrección de container logs -f en #2377 y revisa el trabajo del tailer compartido en #2023. Confirma que el seguimiento comienza cuando el log está vacío, mientras que el comportamiento sin seguimiento permanece sin cambios; el issue pide orientación sobre si corregir este archivo o el tailer compartido, así que comprueba qué dirección prefieren los maintainers antes de editar.

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

Hey folks, hope you're doing well!

Quick summary: container machine logs -f (without -n) exits right away with no output when the log file is empty, instead of waiting for new lines.

For context, container machine logs shows a machine VM's stdio log, or its boot log with --boot, and -f is meant to keep following it as new lines arrive.

What happens today, traced on main (d5416e63):

  1. Run container machine logs -f <machine> (or container machine logs --boot -f <machine>) while that log file is still empty, for example right after the machine starts.
  2. Without -n, tail() in Sources/ContainerCommands/Machine/MachineLogs.swift (around lines 101 to 105) does guard let data = try fh.readToEnd() else { return }.
  3. FileHandle.readToEnd() returns nil for a zero byte file (I checked this with a tiny Swift snippet on macOS).
  4. So tail() returns before it reaches if follow { followFile(fh:) } (around lines 116 to 119).
  5. The command exits immediately with no output and never follows the log.

Minimal repro of the Foundation part:

import Foundation

let path = NSTemporaryDirectory() + "empty.log"
FileManager.default.createFile(atPath: path, contents: Data())
let fh = try FileHandle(forReadingFrom: URL(fileURLWithPath: path))
print(try fh.readToEnd() as Any) // prints nil

Honest note: I haven't run this on a machine VM yet, I traced it in the code and confirmed the readToEnd() behavior with the snippet above.

Problem description

Expected: with -f, an empty log should just start following and print lines as they show up, same as container logs -f does now. Without -f, returning early is totally fine. The -n path (around lines 75 to 98) looks fine too.

This is the sibling of #2374, which was fixed for container logs in #2377. Big thanks to @i098 for that fix! The PR description mentions it deliberately left machine logs alone since they have a separate implementation, so I figured it was worth a dedicated issue.

A small heads up, in case it's useful: #2023 by @gnavadev (nice work on the tailing fixes there) moves both commands into a shared LogTailer.swift. As far as I can tell the shared tailer still has the guard ... else { return } form, so as written it wouldn't cover machine logs and might bring the empty log case back for container logs after it lands. I may well be missing something there, so happy to be corrected.

Which direction would you prefer?

  1. Mirror #2377 in MachineLogs.swift with } else if let data = try fh.readToEnd() {, so following still starts on an empty file.
  2. Fold the fix into the shared tailer from #2023, so both commands get it in one place.

I'd be glad to send a small PR for whichever you like, and any feedback or tips on how you'd want it tested would be really appreciated. Others are very welcome to weigh in too!

Environment
- OS: macOS 26.6.2 (25G83)
- Xcode: n/a (traced in source, not built locally)
- Container: main at d5416e63
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.