Skip to content

Two types of filehandle.read return different values #41558

Description

@nujarum

Version

v14.18.3 / v16.13.2

Platform

Microsoft Windows NT 10.0.19044.0 x64

Subsystem

fs/promises

What steps will reproduce the bug?

import { open } from 'fs/promises';

const path = '/path/to/file';
const fh = await open(path, 'w+'); // reading & writing

const input = new Uint8Array([0, 1, 2, 3]);
await fh.writeFile(input);

const length = input.byteLength;
const buffer = new Uint8Array(length);
const offset = 0;
const position = 0;

const result1 = await fh.read(buffer, offset, length, position);     // 4 parameters
const result2 = await fh.read({ buffer, offset, length, position }); // 1 object parameter

console.log('result1:', result1);
console.log('result2:', result2);

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior?

I think the expected behavior is that these two return values are the same.

What do you see instead?

result1: { bytesRead: 4, buffer: Uint8Array(4) [ 0, 1, 2, 3 ] }
result2: { bytesRead: 0, buffer: Uint8Array(4) [ 0, 1, 2, 3 ] }

Additional information

No response

Activity

  1. added
    fsIssues and PRs related to file-system APIs and the fs module.
    on Jan 16, 2022
  2. nujarum commented on Jan 16, 2022

    @nujarum
    Author

    I get the same result even if I reverse the order of execution, but is it expected behavior?

    const result2 = await fh.read({ buffer, offset, length, position }); // => { bytesRead: 0, buffer: Uint8Array(4) [ 0, 1, 2, 3 ] }
    const result1 = await fh.read(buffer, offset, length, position);     // => { bytesRead: 4, buffer: Uint8Array(4) [ 0, 1, 2, 3 ] }
  3. Trott commented on Jan 16, 2022

    @Trott
    Member

    @nodejs/fs

  4. christian102094 commented on Jan 17, 2022

    @christian102094

    I think this was a bug (see #40715 and #40699). Already fixed in #40716. I guess it was released in Node version 17.2.x (#40983).

  5. nujarum commented on Jan 17, 2022

    @nujarum
    Author

    @christian102094 Thanks for the information.

    Sorry, I was not aware of those closed issues.

    Should I close this issue?

  6. benjamingr commented on Jan 17, 2022

    @benjamingr
    Member

    @nujarum if you can verify this works for you in latest node that'd be helpful - after that sure!

    Thanks for the report btw.

  7. nujarum commented on Jan 19, 2022

    @nujarum
    Author

    Sorry for late reply.

    I have confirmed that this has been fixed in the latest version v17.4.0.

    $ node -v
    v17.4.0
    
    const result2 = await fh.read({ buffer, offset, length, position });
    // => result2: { bytesRead: 4, buffer: Uint8Array(4) [ 0, 1, 2, 3 ] }

    Now that I understand the cause of this problem, I am going to close this issue.

    (I hope this will be fixed in the LTS versions, v14 and v16. 😉)

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

    fsIssues and PRs related to file-system APIs and the fs module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions