Skip to content

fileHandle.read argument defaults don’t seem to work #39034

@lydell

Description

@lydell
  • Version: v16.1.0
  • Platform: Darwin 20.5.0 x86_64
  • Subsystem: fs/promises

What steps will reproduce the bug?

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

async function run() {
  const fileHandle = await fsPromises.open(__filename);
  const buffer = Buffer.alloc(16);

  // Logs `{ bytesRead: 0, buffer: ... }`:
  console.log(await fileHandle.read(buffer));

  // Logs `{ bytesRead: 16, buffer: ... }`:
  console.log(await fileHandle.read(buffer, 0, buffer.byteLength));

  // According to the docs, the first call should read 16 bytes too, because
  // the parameters left out default to the values in the second call.
  // `fs.readSync` works that way:

  // Logs `16`.
  console.log(fs.readSync(fs.openSync(__filename), buffer));
}

run().catch(console.error);

Save the above as test.cjs and run node test.cjs.

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

Always.

What is the expected behavior?

https://nodejs.org/api/fs.html#fs_filehandle_read_buffer_offset_length_position

filehandle.read(buffer, offset, length, position)

  • buffer <Buffer> | <TypedArray> | <DataView> A buffer that will be filled with the file data read.
  • offset <integer> The location in the buffer at which to start filling. Default: 0
  • length <integer> The number of bytes to read. Default: buffer.byteLength
  • position <integer> The location where to begin reading data from the file. If null, data will be read from the current file position, and the position will be updated. If position is an integer, the current file position will remain unchanged.

Calling fileHandle.read(buffer) should work like fileHandle.read(buffer, 0, buffer.byteLength).

What do you see instead?

0 bytes are read.

Additional information

fs.readSync has similar documentation and works as expected.

Potential problems:

  • I’m misunderstanding the docs.
  • The docs are wrong.
  • There’s a bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to the documentations.fsIssues and PRs related to the fs subsystem / file system.good first issueIssues that are suitable for first-time contributors.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions