Fix container logs -n truncating the oldest line past a read chunk#1968
Open
OrtegaMatias wants to merge 1 commit into
Open
Fix container logs -n truncating the oldest line past a read chunk#1968OrtegaMatias wants to merge 1 commit into
container logs -n truncating the oldest line past a read chunk#1968OrtegaMatias wants to merge 1 commit into
Conversation
`container logs -n N` read the log file backwards in 1024-byte chunks and stopped at `lines.count < n`. When the last N lines spanned more than one chunk, the oldest line in the buffer was a fragment truncated at the chunk boundary and was returned as-is — e.g. a single log line longer than 1024 bytes with `-n 1` returned only its last 1024 bytes. Extract the backward-read into a testable `lastLines(fh:n:)` and read one line past N (`<= n`) so the partial leading line is dropped by `suffix(n)`, or stop at the start of the file. The full-log path is unchanged.
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Type of Change
Motivation and Context
container logs -n Ntruncated the oldest returned line whenever the last N log lines spanned more than one 1024-byte backward-read chunk. The clearest case: a single log line longer than 1024 bytes with-n 1returns only its last 1024 bytes — the beginning of the line is cut off. The full-log path (container logswithout-n) is unaffected.Fixes #1967.
Description
ContainerLogs.tailreads the log file backwards in 1024-byte chunks and stopped atlines.count < n. When a read landed on exactly N non-empty segments whose first was a fragment (buffer starting mid-line,offset > 0), that fragment was returned as the oldest line.lastLines(fh:n:) -> [String].lines.count < ntolines.count <= n, so it always reads one line past N (or to the start of the file). The extra, possibly-partial leading line is then dropped bysuffix(n), guaranteeing the oldest returned line is complete.No behavior change for the common case (last N lines within one chunk); the full-log path is untouched.
Testing
Added
Tests/ContainerCommandsTests/ContainerLogsTailTests.swiftcovering:-n 1),-n 2),ngreater than the number of lines.Transparency note: I was unable to run the in-repo test suite locally — this machine only has an Xcode 27 beta toolchain (which fails building a transitive dependency under strict concurrency) and the Command Line Tools, which lack the test frameworks. I verified the fix logic in isolation (old logic truncates the oldest line; new logic returns it in full for all four cases above) and confirmed the change compiles as part of the
ContainerCommandstarget. CI runs the added tests.