Skip to content

Conversation

rhizoome
Copy link

@rhizoome rhizoome commented Aug 31, 2025

commit 1: seekBy should use the logical position, but it used the file position which includes the buffered data. the test did not catch this because it used an empty buffer. fixes #25087 #25020

commit 2: streaming seekBy completely ignored the buffer. the new logic checks if the seek offset is smaller than the buffer and adds the offset to r.interface.seek. otherwise the old logic is invoked.

@rhizoome rhizoome force-pushed the f/positional-seekBy branch 2 times, most recently from 8c44618 to 8e85bf2 Compare August 31, 2025 08:32
@rhizoome rhizoome marked this pull request as draft August 31, 2025 10:33
@rhizoome rhizoome force-pushed the f/positional-seekBy branch from 8e85bf2 to 10ddf27 Compare August 31, 2025 18:13
@rhizoome rhizoome changed the title std.fs.File: fix seekBy using wrong position std.fs.File: fix positional/streaming seekBy Aug 31, 2025
@rhizoome rhizoome force-pushed the f/positional-seekBy branch from 10ddf27 to d967f94 Compare September 1, 2025 12:10
@rhizoome rhizoome marked this pull request as ready for review September 1, 2025 12:10
@rhizoome rhizoome force-pushed the f/positional-seekBy branch 2 times, most recently from 28824c4 to 109199d Compare September 1, 2025 12:14
@rhizoome rhizoome marked this pull request as draft September 1, 2025 12:20
@rhizoome rhizoome force-pushed the f/positional-seekBy branch from 109199d to 1d5637d Compare September 1, 2025 12:33
@rhizoome rhizoome marked this pull request as ready for review September 1, 2025 12:33
@rhizoome rhizoome force-pushed the f/positional-seekBy branch 4 times, most recently from 77a916a to 401c6f5 Compare September 3, 2025 16:24
@rhizoome rhizoome force-pushed the f/positional-seekBy branch 2 times, most recently from 311477a to 47b1c31 Compare September 24, 2025 08:39
@moosichu
Copy link
Contributor

The main thing I would say reading through the PR, is that the tests seem to be testing the internal behaviour of the reader implementation (how the buffering works etc.) depending on modes and I don't think that's super important.

Ideally they would be testing that actual API boundary/contract. Which is that when seeking, the logical pos in the file changes as expected and that the data read through calls to the reader API return the data that is expected.

So instead of tests like:

    // seek within bufferedLen
    const pos_within = reader.pos;
    try reader.seekBy(2);
    // wasi does not buffer in this case
    if (builtin.target.os.tag != .wasi) {
        try testing.expectEqual(pos_within, reader.pos);
    }

I personally would test for:

    const logical_pos_before = reader.logicalPos();
    try reader.seekBy(2);
    try testing.expectEqual(logical_pos_before + 2, reader.logicalPos());

(and then that the buffer returns the correct data, but that is already being done)

Maybe testing that these all work based on seeking within and beyond the buffer could still be useful as they invoke different internal logic. But I don't think checking that we get specific values into the inteferace seek and end fields is super useful (for example).

Also adding a negative seekBy to the test could be useful as well for: #25020

This is all just my opinion though so take it with a grain of salt (as others might have different thoughts)! And I hope it makes sense as I went on a couple tangents.

@rhizoome rhizoome force-pushed the f/positional-seekBy branch 3 times, most recently from b3ceec7 to 9700149 Compare October 1, 2025 22:00
@rhizoome
Copy link
Author

rhizoome commented Oct 1, 2025

Updated tests to not test internals, just the interface. I also added a negative seekBy. This was the version of the tests that made sure, that the buffer is discarded. I am now pretty sure that testing internals was a bad idea.

@rhizoome rhizoome force-pushed the f/positional-seekBy branch from 9700149 to 2d6bd3e Compare October 1, 2025 22:37
positional seekBy should use the logical position, but it used the file
position which includes the buffered data. the test did not catch this
because it used an empty buffer. fixes ziglang#25087 ziglang#25020
streaming seekBy completely ignored the buffer. the new logic checks
if the seek offset is smaller than the buffer and adds the offset to
r.interface.seek. otherwise the old logic is invoked.
@rhizoome rhizoome force-pushed the f/positional-seekBy branch from 2d6bd3e to 48cfe69 Compare October 2, 2025 13:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fs.File.reader seekBy broken
2 participants