Skip to content

a failed loadhistory() discards the session's console history #19686

Closed

Description

@kevinushey

System details

RStudio Edition : Desktop and Server
RStudio Version : main (2026.12 development)
OS Version      : all
R Version       : all

Steps to reproduce the problem

  1. Run a few commands in the Console.
  2. Run loadhistory("no-such-file"). On macOS and Linux this reports No such file or directory; on Windows it reports nothing.
  3. Run savehistory(tmp <- tempfile()), then readLines(tmp).

Describe the problem in detail

readLines(tmp) returns character(0): the commands from step 1 are gone. The failed loadhistory() emptied the session's console history.

ConsoleHistory::loadFromFile() (src/cpp/r/session/RConsoleHistory.cpp) clears the history before it checks that the file exists, and reads straight into it, so any failure -- a mistyped path, a file that can't be read, a read error part-way through -- leaves the history empty or partial. The Console's own command list still looks unchanged, because the reset is only sent to the client after a successful load, but the session's copy is what gets written out. With "Always save history" on (the default), quitting the session then overwrites .Rhistory with only the commands run after the failed load. The emptied history also carries across a session suspend and resume.

Found while working on #19684.

Describe the behavior you expected

A loadhistory() that fails leaves the current history as it was.

  • I have read the guide for submitting good bug reports.
  • I have installed the latest version of RStudio, and confirmed that the issue still persists.
  • If I am reporting an RStudio crash, I have included a diagnostics report.
  • I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions