Repository navigation
report savehistory() / loadhistory() failures as errors on windows - #19689
Conversation
RStudio E2E - all platforms1 test(s) failed
All platforms - commit |
gtritchie
left a comment
There was a problem hiding this comment.
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.
| else | ||
| try | ||
| { | ||
| std::string file = R_ExpandFileName(r::sexp::asString(sFile).c_str()); |
There was a problem hiding this comment.
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:
- Keep
main'sFilePath file(r::sexp::asString(sFile));here and at line 222 instead ofR_ExpandFileName(). Otherwise savehistory() / loadhistory() mis-decode "~" paths on Windows R < 4.2 when the home directory is non-ASCII #19684 comes back, now as an error instead of a silent failure. - Keep expand ~ in savehistory() / loadhistory() paths with path.expand() #19685's comment above
rs_loadHistoryas well as the new one.src/cpp/r/R/Tools.R:1027points to it (see rs_loadHistory() in RSession.cpp). main's.rs.loadhistory() expands '~'test already loads a file, saves it and compares. The round-trip test here could be folded into it rather than kept alongside it.
There was a problem hiding this comment.
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.
|
|
||
| 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)) |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
#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()); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
gtritchie
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
Agreed it's worth its own issue: filed #19745. One addition to the caveat: the 11 |
Intent
On Windows,
savehistory()andloadhistory()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()andutils::loadhistory()with.rs.savehistory()/.rs.loadhistory(), which callrs_saveHistory/rs_loadHistoryinRSession.cpp.rs_saveHistoryignored theErrorfromConsoleHistory::saveToHistoryFile(), andrs_loadHistoryonly logged its error.Both now raise an R error with the error's message, as the POSIX
Rsavehistory/Rloadhistorycallbacks do. The message is the system's, so it differs by platform:No such file or directoryon macOS and Linux, and the Win32 text on Windows, e.g.The system cannot find the path specifiedfor a save into a missing folder andThe system cannot find the file specifiedfor a load of a missing file. They use theRErrorException/r::exec::error()pattern thatrs_editFile()andrs_showFile()use in the same file.rs_loadHistorystill 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()passedboost::system::errc::is_a_directorytosystemError(), which takes a bare errc value as a system error code, and on Windows 21 is the Win32ERROR_NOT_READY. It now passes astd::errcerror 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 bypath.expand()), #19688 (a failed load keeps the session's history), and #19691 (saveToHistoryFile()saves the lastR_HISTSIZEcommands).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:.rs.savehistory()to a path in a folder that doesn't exist raises the system's error and creates no file..rs.loadhistory()of a missing file raises the system's error..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 directoryand.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:
savehistory("C:/no-such-folder/history.Rhistory")should report an error:The system cannot find the path specified.loadhistory("no-such-file")should report an error:The system cannot find the file specified.loadhistory(tempdir())should report an error:is a directory.savehistory(tmp <- tempfile())followed byloadhistory(tmp)should still work without an error.Documentation
None needed.
Checklist
NEWS.md