Skip to content

filehandle.read(buffer) can't read file when options are omitted #47183

Description

@H1Gdev

Version

v18.12.1

Platform

Linux ubuntu-focal 5.4.0-144-generic #161-Ubuntu SMP Fri Feb 3 14:49:04 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

const fs = require("fs/promises");

async function test() {
  let path = '/path/to/file';

  let buf = Buffer.alloc(4);
  let handle;
  try {
    handle = await fs.open(path);
    {
      let buf = Buffer.alloc(4);
      let { bytesRead, buffer } = await handle.read(buf);
      //let { bytesRead, buffer } = await handle.read(buf, {}); // It is behavior I would expect.
      console.log(bytesRead);
      console.log(buffer);
      console.log(buf);
    }
  } finally {
    await handle?.close();
  }
}
test();

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

always

What is the expected behavior? Why is that the expected behavior?

https://nodejs.org/dist/latest-v18.x/docs/api/fs.html#filehandlereadbuffer-options

Even if you omit options, it will be default values.

What do you see instead?

0
<Buffer 00 00 00 00>
<Buffer 00 00 00 00>

Additional information

No response

Activity

  1. H1Gdev commented on Mar 21, 2023

    @H1Gdev
    ContributorAuthor

    if (offset == null) {
    offset = 0;
    } else {

    It looks like the length is still zero here.
    Adding the following might work as expected.

    length = buffer.byteLength - offset
  2. added
    fsIssues and PRs related to file-system APIs and the fs module.
    on Mar 21, 2023
  3. changed the title [-]Can not read file.[/-] [+]filehandle.read(buffer) can't read file when options are ommitted[/+] on Mar 21, 2023
  4. changed the title [-]filehandle.read(buffer) can't read file when options are ommitted[/-] [+]filehandle.read(buffer) can't read file when options are omitted[/+] on Mar 21, 2023
  5. pulkit-30 commented on Dec 7, 2023

    @pulkit-30
    Contributor

    is this issue valid? please confirm.

  6. markrian commented on Jan 2, 2024

    @markrian

    Edit: Oh, I see #51087 probably fixes this. Sorry for the noise!


    @pulkit-30 I can still reproduce this on node 21.5.0 and 20.10.0 with this script:

    import { open } from 'fs/promises';
    
    async function main() {
      const bytes = new Uint8Array(8);
      const fileHandle = await open(process.argv[2]);
      console.debug(await fileHandle.read(bytes));
    }
    
    main();

    Invoked like this:

    node index.mjs some-file.txt
    

    Produces this output:

    [Object: null prototype] {
      bytesRead: 0,
      buffer: Uint8Array(8) [
        0, 0, 0, 0,
        0, 0, 0, 0
      ]
    }
    
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