Skip to content

Commit

Permalink
Fix ReadFileUV not reading to the end of a non-regular file on Windows (
Browse files Browse the repository at this point in the history
  • Loading branch information
190n authored Sep 12, 2024
1 parent f6841a0 commit b33d6b1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/bun.js/webcore/blob/ReadFile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,9 @@ pub const ReadFileUV = struct {
}

pub fn queueRead(this: *ReadFileUV) void {
if (this.remainingBuffer().len > 0 and this.errno == null and !this.read_eof) {
// if not a regular file, buffer capacity is arbitrary, and running out doesn't mean we're
// at the end of the file
if ((this.remainingBuffer().len > 0 or !this.is_regular_file) and this.errno == null and !this.read_eof) {
log("ReadFileUV.queueRead - this.remainingBuffer().len = {d}", .{this.remainingBuffer().len});

if (!this.is_regular_file) {
Expand Down
19 changes: 18 additions & 1 deletion test/regression/issue/07500/07500.fixture.js
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
console.write(await Bun.stdin.text());
const input = await Bun.stdin.text();

if (process.platform == "win32") {
// powershell unavoidably appends \r\n to text sent from a powershell command (like Get-Content)
// to an external program (like bun)
// https://github.com/PowerShell/PowerShell/issues/5974
// so we have to remove it
// for sanity check that it actually ends in \r\n
const CR = "\r".charCodeAt(0);
const LF = "\n".charCodeAt(0);
if (input.charCodeAt(input.length - 2) !== CR || input.charCodeAt(input.length - 1) !== LF) {
throw new Error(`input of ${input.length} bytes does not end in CRLF`);
}
const trimmed = input.substring(0, input.length - 2);
console.write(trimmed);
} else {
console.write(input);
}
5 changes: 3 additions & 2 deletions test/regression/issue/07500/07500.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ test("7500 - Bun.stdin.text() doesn't read all data", async () => {
.split(" ")
.join("\n");
await Bun.write(filename, text);
const cat = isWindows ? "Get-Content" : "cat";
// -Raw on windows makes it output a single string instead of an array of lines
const cat = isWindows ? "Get-Content -Raw" : "cat";
const bunCommand = `${bunExe()} ${join(import.meta.dir, "07500.fixture.js")}`;
const shellCommand = `${cat} ${filename} | ${bunCommand}`.replace(/\\/g, "\\\\");

Expand All @@ -27,7 +28,7 @@ test("7500 - Bun.stdin.text() doesn't read all data", async () => {
throw new Error(proc.stdout.toString());
}

const output = proc.stdout.toString().replaceAll("\r\n", "\n");
const output = proc.stdout.toString();
if (output !== text) {
expect(output).toHaveLength(text.length);
throw new Error("Output didn't match!\n");
Expand Down

0 comments on commit b33d6b1

Please sign in to comment.