Skip to content

File blob is not invalidated if the file was initially empty then written to #47161

@Jamesernator

Description

@Jamesernator

Version

v19.8.1

Platform

Linux 5.19.0-35-generic #36~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Feb 17 15:17:25 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

fs

What steps will reproduce the bug?

The following should produce an exception when reading the blob, but currently doesn't:

import fs from "node:fs";

// Create an empty file
fs.writeFileSync("./test.txt", "");

const blob = await fs.openAsBlob("./test.txt");

// write to the file, which should invalidate the blob
fs.writeFileSync("./test.txt", "foobar");

// This should throw an error, but returns the empty string instead
const result = await blob.text();

console.log(JSON.stringify(result)); 

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

This happens consistently.

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

It should throw a DOMException, this works fine if the file was initially not empty, i.e. this throws an error as expected:

import fs from "node:fs";

// Create an empty file
fs.writeFileSync("./test.txt", "NOT_EMPTY");

const blob = await fs.openAsBlob("./test.txt");

// write to the file, which should invalidate the blob
fs.writeFileSync("./test.txt", "foobar");

// This should throw an error, but returns the empty string instead
const result = await blob.text();

console.log(JSON.stringify(result)); 

What do you see instead?

No exception is thrown.

Additional information

The Chrome behaviour for empty files being read after modification is the same as for non-empty files — i.e. an error is thrown in all cases.

(Curiously the Firefox behaviour allows reading the latest contents of the file regardless of modification status, however this seems buggy as the blob .size never actually changes).

Metadata

Metadata

Assignees

No one assigned

    Labels

    fsIssues and PRs related to the fs subsystem / file system.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions