Skip to content

std.Io.Reader.takeDelimiterExclusive() return error.StreamTooLong instead of error.EndOfStream #24950

Closed

Description

@truly-not-taken

Zig Version

0.15.1

Steps to Reproduce and Observed Behavior

test {
    var reader = std.Io.Reader.fixed("");
    try std.testing.expectError(
        error.EndOfStream,
        reader.takeDelimiterExclusive('\n'),
    );
}

failed: expected error.EndOfStream, found error.StreamTooLong

Expected Behavior

Docs say

Returned slice excludes the delimiter. End-of-stream is treated equivalent to a delimiter, unless it would result in a length 0 return value, in which case error.EndOfStream is returned instead.

Activity

  1. added
    bug
    on Aug 22, 2025
  2. changed the title [-]std.Io.Reader.takeDelimiterExclusive() return error.StreamTooLonginstead of error.EndOfStream[/-] [+]std.Io.Reader.takeDelimiterExclusive() return error.StreamTooLong instead of error.EndOfStream[/+] on Aug 22, 2025
  3. truly-not-taken commented on Aug 22, 2025

    @truly-not-taken
    Author
  4. truly-not-taken commented on Aug 22, 2025

    @truly-not-taken
    Author
  5. truly-not-taken commented on Aug 23, 2025

    @truly-not-taken
    Author

    The behavior of std.Io.Reader.fixed is different from a file reader. This test does not fail while it is basically the same:

    const std = @import("std");
    test {
        var file = try std.fs.cwd().openFile("/dev/null", .{});
        var buffer: [1024]u8 = undefined;
        var reader = file.readerStreaming(&buffer);
        try std.testing.expectError(
            error.EndOfStream,
            reader.interface.takeDelimiterExclusive('\n'),
        );
    }

    But how do I do unit tests now? I need to test my function behavior against empty input.

  6. truly-not-taken commented on Aug 23, 2025

    @truly-not-taken
    Author
  7. truly-not-taken commented on Aug 23, 2025

    @truly-not-taken
    Author

    This is ridiculous! It keeps gaslighting me about long streams while they are empty.

    test {
        try std.testing.expectError(
            error.EndOfStream,
            std.Io.Reader.ending.takeDelimiterExclusive('\n'),
        );
    }

    error: '1.test_0' failed: expected error.EndOfStream, found error.StreamTooLong

  8. added
    standard library
    on Aug 23, 2025
  9. added this to the 0.15.2 milestone on Aug 23, 2025
  10. truly-not-taken commented on Aug 23, 2025

    @truly-not-taken
    Author

    So here is a work around. I hope I have not done anything illegal.

    lib/std/Io/Reader.zig

    /// Constructs a `Reader` such that it will read from `buffer` and then end.
    /// Its `takeDelimiterExclusive()` will return `error.EndOfStream`
    /// even if the last byte is not the delimiter.
    pub fn fixedZ(bufferZ: [:0]const u8) Reader {
        var buffer:[]const u8 = bufferZ;
        buffer.len += 1;
        return .{
            .vtable = &.{
                .stream = endingStream,
                .discard = endingDiscard,
                .readVec = endingReadVec,
                .rebase = endingRebase,
            },
            // This cast is safe because all potential writes to it will instead
            // return `error.EndOfStream`.
            .buffer = @constCast(buffer),
            .end = bufferZ.len,
            .seek = 0,
        };
    }

    Now I can test my function against an empty input:

    test {
        var reader = std.Io.Reader.fixedZ("");
        try std.testing.expectError(
            error.EndOfStream,
            reader.takeDelimiterExclusive('\n'),
        );
    }
  11. added a commit that references this issue on Sep 6, 2025
    6c43ee6
  12. modified the milestones: 0.15.2, urgent on Sep 30, 2025
  13. added a commit that references this issue on Oct 9, 2025
    bf58b4e
  14. added a commit that references this issue on Oct 9, 2025
    f661ab6
  15. modified the milestones: urgent, 0.16.0 on Mar 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugstandard library

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions