Skip to content

savehistory() / loadhistory() mis-decode "~" paths on Windows R < 4.2 when the home directory is non-ASCII #19684

Closed

Description

@kevinushey

System details

RStudio Edition : Desktop
RStudio Version : main (2026.12 development)
OS Version      : Windows
R Version       : < 4.2 (native encoding is the ANSI code page, not UTF-8)

Steps to reproduce the problem

  1. On Windows, use an account whose R home directory (path.expand("~")) contains a non-ASCII character that the ANSI code page can represent, e.g. C:\Users\José\Documents.
  2. Start RStudio with R 4.1 or earlier.
  3. Run savehistory("~/test.Rhistory"), then file.exists("~/test.Rhistory").

Describe the problem in detail

Found by reading the code while reviewing #19683; not yet reproduced on Windows.

On Windows, RStudio replaces utils::savehistory() and utils::loadhistory() with .rs.savehistory() / .rs.loadhistory(), which call rs_saveHistory / rs_loadHistory (src/cpp/r/session/RSession.cpp). Both do:

std::string file = R_ExpandFileName(r::sexp::asString(sFile).c_str());

Since #19640, asString() returns UTF-8. On Windows, R_ExpandFileName() expands ~ by prepending the home directory as getenv() returns it, in the native code page. So the result mixes a native-encoded home directory with a UTF-8 remainder, and FilePath then decodes the whole string as UTF-8. The history is written to (or read from) the wrong path, or the call fails.

Before #19640 the whole string was native, so any non-ASCII history path was mis-decoded. #19640 fixed the paths that don't start with ~. This is the leftover case.

R's own path.expand() handles this case: on Windows it expands UTF-8 input with R_ExpandFileNameUTF8() and marks the result.

Describe the behavior you expected

savehistory("~/test.Rhistory") writes the history to path.expand("~/test.Rhistory"), and loadhistory() reads it back from there.

  • 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