Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/runtime/cli/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ impl History {
};

for line in strings::split(&content, b"\n") {
let line = line.strip_suffix(b"\r").unwrap_or(line);
if !line.is_empty() {
self.entries.push(Box::<[u8]>::from(line));
}
Expand Down
44 changes: 44 additions & 0 deletions test/js/bun/repl/repl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,50 @@ describe.skipIf(isWindows)("REPL history file permissions", () => {
});
});

describe("REPL history file", () => {
// A history file written on Windows (or edited in a CRLF editor) has CRLF
// line endings. Loading it must strip the trailing '\r' so entries don't
// carry a stray CR into recall, .history output, or the next save.
test("strips CRLF when loading existing history", async () => {
using dir = tempDir("repl-history-crlf", {
".bun_repl_history": "old_one\r\nold_two\r\n",
});
const home = String(dir);

await using proc = Bun.spawn({
cmd: [bunExe(), "repl"],
stdin: Buffer.from(".history\nnew_three\n.exit\n"),
stdout: "pipe",
stderr: "pipe",
env: {
...bunEnv,
TERM: "dumb",
NO_COLOR: "1",
HOME: home,
USERPROFILE: home,
},
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);

// .history prints the loaded entries verbatim; a kept '\r' would appear
// between the entry text and the line's trailing '\n'.
const historyLines = stripAnsi(stdout)
.split("\n")
.filter(l => l.includes("old_one") || l.includes("old_two"));
expect(historyLines).toHaveLength(2);
for (const line of historyLines) {
expect(line).not.toContain("\r");
}
Comment thread
robobun marked this conversation as resolved.
Comment thread
robobun marked this conversation as resolved.

const saved = await Bun.file(path.join(home, ".bun_repl_history")).text();
expect(saved).not.toContain("\r");
expect(saved.split("\n").filter(Boolean)).toEqual(["old_one", "old_two", "new_three"]);

expect(stderr).toBe("");
expect(exitCode).toBe(0);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
});
});

// `bun --interactive` boots the full node:repl + readline + acorn stack; on a
// debug+asan build that is ~4–5s per spawn, so the 5s default is too tight.
const interactiveTimeout = 20_000;
Expand Down