Skip to content

report savehistory() / loadhistory() failures as errors on windows - #19689

Merged
kevinushey merged 8 commits into
mainfrom
bugfix/19687-history-errors-on-windows
Nov 20, 2026
Merged

kevinushey merged 8 commits into
mainfrom
bugfix/19687-history-errors-on-windows

Conversation

@kevinushey

@kevinushey kevinushey commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Intent

On Windows, savehistory() and loadhistory() returned normally when they couldn't save or load the history file, for example because its folder didn't exist. On macOS and Linux the same calls report an R error. This silence is also what hid #19684.

Fixes #19687.

Approach

On Windows, RStudio replaces utils::savehistory() and utils::loadhistory() with .rs.savehistory() / .rs.loadhistory(), which call rs_saveHistory / rs_loadHistory in RSession.cpp. rs_saveHistory ignored the Error from ConsoleHistory::saveToHistoryFile(), and rs_loadHistory only logged its error.

Both now raise an R error with the error's message, as the POSIX Rsavehistory / Rloadhistory callbacks do. The message is the system's, so it differs by platform: No such file or directory on macOS and Linux, and the Win32 text on Windows, e.g. The system cannot find the path specified for a save into a missing folder and The system cannot find the file specified for a load of a missing file. They use the RErrorException / r::exec::error() pattern that rs_editFile() and rs_showFile() use in the same file. rs_loadHistory still sends the client its history reset only after a successful load.

That made one more message visible on Windows: loading a directory reported "The device is not ready". ConsoleHistory::loadFromFile() passed boost::system::errc::is_a_directory to systemError(), which takes a bare errc value as a system error code, and on Windows 21 is the Win32 ERROR_NOT_READY. It now passes a std::errc error code, whose message is "is a directory" on every platform.

This branch merges main, so it builds on #19685 (both routines take the path already expanded by path.expand()), #19688 (a failed load keeps the session's history), and #19691 (saveToHistoryFile() saves the last R_HISTSIZE commands).

Automated Tests

In src/cpp/tests/testthat/test-history.R (R testthat, --scope r --filter history). .rs.savehistory() and .rs.loadhistory() are defined on every platform, so the tests call them directly:

  • Save failure: .rs.savehistory() to a path in a folder that doesn't exist raises the system's error and creates no file.
  • Load failure: .rs.loadhistory() of a missing file raises the system's error.
  • Directory load: .rs.loadhistory() of a directory raises "is a directory" (in any case: MSVC's message is lower case).

Each test matches either the POSIX or the Win32 message, so it fails on an unrelated error. The existing .rs.loadhistory() expands '~' test already shows that the wrappers load and save a file, and it runs before the failure tests.

Without the fix, both failure checks fail with "did not throw". The full R testthat suite passes locally (macOS, R 4.6.1). I also called both wrappers in a macOS rsession: they now report .rs.savehistory(...): No such file or directory and .rs.loadhistory(...): No such file or directory. I haven't run this on Windows; the change has no platform-specific code.

QA Notes

On Windows:

  1. savehistory("C:/no-such-folder/history.Rhistory") should report an error: The system cannot find the path specified.
  2. loadhistory("no-such-file") should report an error: The system cannot find the file specified.
  3. loadhistory(tempdir()) should report an error: is a directory.
  4. savehistory(tmp <- tempfile()) followed by loadhistory(tmp) should still work without an error.

Documentation

None needed.

Checklist

  • If this PR adds a new feature, or fixes a bug in a previously released version, it includes an entry in NEWS.md
  • If this PR adds or changes UI, the updated UI meets accessibility standards (n/a: no UI)
  • A reviewer is assigned to this PR (if unsure who to assign, check Area Owners list)
  • This PR passes all local unit tests

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

RStudio E2E - all platforms

1 test(s) failed

###################- 99% pass rate

Passed Failed Skipped Flaky
5796 1 265 1
Platform ✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
✅ auth-teardown 4 0 16 0
✅ desktop-linux 1453 0 49 0
❌ desktop-macos 1448 1 54 0
✅ desktop-windows 1448 0 60 0
✅ server-linux 1395 0 54 1
✅ setup 48 0 32 0

View report online


All platforms - commit 3d25f98f892c6930054c4ec2934c6a57a29ed2e7 - Run #2411

@gtritchie gtritchie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at 4c208fd. The blocker is the merge conflict with #19685 (inline on RSession.cpp). The test-pattern comment is a small fix. The other inline comments are non-blocking.

One note on the PR description: the Approach section says both functions report the same text as the POSIX callbacks, e.g. No such file or directory. That holds for load, which uses the generic errc::no_such_file_or_directory. On Windows, though, a failed save comes from CreateFileW + LAST_SYSTEM_ERROR() in writeContentsToFile() (src/cpp/core/FileSerializer.cpp:127-140), so it reports the Win32 message for the error code, e.g. "The system cannot find the path specified". QA step 1 should expect that wording rather than the POSIX text.

Comment thread src/cpp/r/session/RSession.cpp Outdated
else
try
{
std::string file = R_ExpandFileName(r::sexp::asString(sFile).c_str());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now conflicts with #19685, which is on main (b7ebf96). Both files conflict: RSession.cpp, and test-history.R as an add/add conflict, since #19685 created the same file. When resolving:

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merged main in e617665. Both routines keep FilePath file(r::sexp::asString(sFile));, rs_saveHistory now checks the error from #19691's saveToHistoryFile(), and #19685's comment stays above rs_loadHistory with the new one after it. I dropped the round-trip test: .rs.loadhistory() expands '~' already loads, saves and compares, and it runs before the failure tests.

Comment thread src/cpp/tests/testthat/test-history.R Outdated

test_that(".rs.savehistory() reports a file it can't write", {
path <- file.path(tempfile("no-such-dir-"), "history.Rhistory")
expect_error(.rs.savehistory(path))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without a pattern, this and the expect_error() on line 50 pass on any error, e.g. if rs_saveHistory weren't registered. So they don't show that the history error itself reaches R. The load case could match "No such file or directory", which is the generic errc text on every platform. Save needs a looser pattern, because on Windows its message is the Win32 text (see the review summary).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 8a638f7. One correction: load isn't No such file or directory on Windows either. systemError(boost::system::errc::no_such_file_or_directory, ...) binds to systemError(int, ...) (shared_core/Error.cpp:503), since boost's errc_t is a condition enum rather than a code enum, so it gets system_category(); on Windows code 2 is ERROR_FILE_NOT_FOUND, "The system cannot find the file specified". So each pattern takes the POSIX or the Win32 text: No such file or directory|cannot find the file specified for load, ...|cannot find the path specified for save. I've updated the description and QA steps to match.

The same overload made #19688's errc::is_a_directory read as Win32 code 21 on Windows, "The device is not ready", which this PR would now show from loadhistory(<a directory>). 9792a11 passes a std::errc error code there instead, with a test.

std::string file = R_ExpandFileName(r::sexp::asString(sFile).c_str());
Error error = consoleHistory().loadFromFile(FilePath(file), true);
if (error)
throw r::exec::RErrorException(error.getMessage());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking, and not introduced here: loadFromFile() clears historyBuffer_ before it checks the file (RConsoleHistory.cpp:154), and the clear fires no signal. On this error path the History pane and Ctrl+Up still show the old entries, since the pane only refreshes on ConsoleResetHistoryEvent, while the backend holds none, and the exit save then writes the empty buffer. This is tracked in #19686, so it's fine to leave it for that. I'm flagging it because the new error doesn't tell the user their history was cleared.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#19686 is fixed on main by #19688, now merged in here: a missing file returns its error before loadFromFile() touches the buffer, so the history and the pane stay as they were. That also meant the load-failure test no longer changes the session's history, so it no longer skips outside rstudio-tests.

}
catch(r::exec::RErrorException& e)
{
r::exec::error(e.message());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, and the same as rs_editFile / rs_showFile and the POSIX history callbacks: r::exec::error() longjmps out of this catch handler, so the RErrorException and its string are never destroyed. I'm not asking for a change unless you want to address the pattern in general.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Leaving it to match rs_editFile / rs_showFile and the POSIX callbacks; it's better fixed for the pattern as a whole.


// failures are R errors, as from the Rloadhistory / Rsavehistory callbacks
// that implement these functions on POSIX
SEXP rs_loadHistory(SEXP sFile)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: this mirrors doHistoryFileOperation() and Rloadhistory / Rsavehistory in RStdCallbacks.cpp:146-161 and 666-692 (map the error to RErrorException, reset only on success). That helper can't be called as it is, since it takes the R argument list and still uses R_ExpandFileName(), so sharing it would take a small refactor. Fine to skip.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Skipping, as you suggest: sharing it would mean reworking doHistoryFileOperation()'s argument and path handling on the POSIX side, which this PR doesn't otherwise touch.

@kevinushey
kevinushey requested a review from gtritchie November 19, 2026 23:06

@gtritchie gtritchie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed at 9792a11. Everything from the earlier review is addressed: the merge with #19685 keeps its path handling and comment, #19688 covers the failed-load history wipe, the failure tests match the message on each platform, and the description gives the Windows wording. The two non-blocking items (the catch-handler longjmp and the duplication with RStdCallbacks.cpp) are fine as they are.

One broader note, not for this PR: the "device is not ready" message comes from boost::system::errc values converting implicitly to systemError(int), which uses system_category() and so decodes them as Win32 codes on Windows. git grep 'systemError(boost::system::errc::' finds 365 such calls, so this PR fixes the one it touches. A systemError overload for boost::system::errc::errc_t that uses generic_category() would fix them all, but it needs care, since some Windows not-found checks may rely on the current system-category codes (ENOENT and ERROR_FILE_NOT_FOUND are both 2). That seems worth a separate issue.


// libc++ opens a directory and reads it as an empty file, which would
// replace the history with nothing
// replace the history with nothing. a bare errc value would go to

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: line 181 in this function still passes a bare boost::system::errc::no_such_file_or_directory to systemError(), the case this comment describes. On Windows its message is only right because ENOENT and ERROR_FILE_NOT_FOUND are both 2, and the load test's cannot find the file specified alternative relies on that. std::make_error_code(std::errc::no_such_file_or_directory) would match the directory case and give the same message on every platform (lower case under MSVC, so the test pattern would need ignore.case). Fine as a follow-up if you'd rather not touch it here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 3d25f98: the missing-file case now uses std::make_error_code(std::errc::no_such_file_or_directory), and the load test matches no such file or directory with ignore.case = TRUE instead of the Win32 alternative. Nothing classifies this error (rs_loadHistory, Rloadhistory, and the SessionRTests check only test that the load failed), so moving it to the generic category is safe.

@kevinushey

Copy link
Copy Markdown
Collaborator Author

Agreed it's worth its own issue: filed #19745. One addition to the caveat: the 11 error == systemError(boost::system::errc::X, ErrorLocation()) checks (and POSIX isNotFoundError()) build their reference the same way, so the overload would move those to the generic category too. Since Error::operator== compares category names (#18946), they'd then stop matching errors from systemError(errno) on every platform, not just Windows. So the overload probably has to wait for #18946's equivalence comparison.

@kevinushey
kevinushey merged commit 72c49db into main Nov 20, 2026
46 of 47 checks passed
@kevinushey
kevinushey deleted the bugfix/19687-history-errors-on-windows branch November 20, 2026 01:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

savehistory() and loadhistory() fail without an error on Windows

2 participants