Repository navigation
expand ~ in savehistory() / loadhistory() paths with path.expand() - #19685
Conversation
RStudio E2E - all platformsAll tests passed!
All platforms - commit |
gtritchie
left a comment
There was a problem hiding this comment.
The path.expand() approach looks right. In R 4.1.3's do_pathexpand, UTF-8 input goes through R_ExpandFileNameUTF8 and other input is expanded natively, and asString() converts either result to UTF-8 correctly. The inline comments are about the error handling around the fix and a test.
One more that isn't on a changed line:
The same encoding mismatch is in other callers. r::util::expandFileName (src/cpp/r/RUtil.cpp:180) and one direct R_ExpandFileName call still pass UTF-8 strings to R_ExpandFileName, so on Windows with R < 4.2 and a non-ASCII R_USER they build the same mixed-encoding path:
initializePaths()(src/cpp/session/modules/SessionFiles.cpp:1955) runsRf_translateCharUTF8thenR_ExpandFileName. RStudio'slist.files()/list.dirs()replacements use it, andSessionPatches.R:64enables them by default on R < 4.2. Solist.files("~")is affected in exactly the setup from #19684.r::sexp::extract(SEXP, FilePath*)(src/cpp/r/RSexp.cpp:1173) extracts as UTF-8, then expands.rs_browseURLfor bare file paths (src/cpp/r/session/RSession.cpp:175).
Making expandFileName handle the encoding (UTF-8 to native, expand, back to UTF-8) would fix all of these, and the history functions could use it as well. That may be better as a separate issue than in this PR.
| std::string file = R_ExpandFileName(r::sexp::asString(sFile).c_str()); | ||
| consoleHistory().saveToFile(FilePath(file), false /* atomic */); | ||
| FilePath file(r::sexp::asString(sFile)); | ||
| consoleHistory().saveToFile(file, false /* atomic */); |
There was a problem hiding this comment.
This still drops the error from saveToFile(), so on Windows a failed savehistory() stays silent, which is what kept #19684 hidden. It predates this PR, but the same thing happens for any other write failure, such as a directory that doesn't exist or isn't writable.
On POSIX, RStudio's Rsavehistory goes through doHistoryFileOperation() (RStdCallbacks.cpp:146), which raises the error in R (RStdCallbacks.cpp:160), and R's own savehistory() fails with "problem in saving the history file". Raising an R error here would make Windows match.
There was a problem hiding this comment.
This is #19687, fixed in #19689: rs_saveHistory and rs_loadHistory now raise an R error with the same message the POSIX callbacks give. I kept it out of this PR so this one stays on the ~ expansion. The two PRs conflict on these lines; whichever merges second keeps this PR's FilePath file(...) line inside #19689's try.
| FilePath file(r::sexp::asString(sFile)); | ||
| Error error = consoleHistory().loadFromFile(file, true); | ||
| if (error) | ||
| LOG_ERROR(error); |
There was a problem hiding this comment.
Same for load errors, with one more effect. ConsoleHistory::loadFromFile() clears historyBuffer_ before it finds the file is missing (RConsoleHistory.cpp:154), and consoleHistoryReset() runs only on success. So on Windows loadhistory("typo.Rhistory") empties the server-side history with no message, while the Console's Up arrow still offers the old commands. With "Always save history" on, quitting then writes the empty history to .Rhistory (RStdCallbacks.cpp:793).
R's own loadhistory() also clears first, so the clear itself is expected. What's missing is the error, and a reset event so the client matches the server. POSIX Rloadhistory (RStdCallbacks.cpp:666) raises the error but also skips the reset.
There was a problem hiding this comment.
The missing error is #19689, as above. The clearing is #19686, fixed in #19688: loadFromFile() reads into a separate buffer and swaps it in only once the read succeeds, and a missing file with verifyFile set returns the error before touching the history. A failed load now leaves the server's history as it was, so the client's list still matches and no reset event is needed. The reset after a successful load is unchanged. That covers POSIX Rloadhistory too.
| .rs.addFunction("savehistory", function(file = ".Rhistory") | ||
| { | ||
| invisible(.Call("rs_saveHistory", file, PACKAGE = "(embedding)")) | ||
| invisible(.Call("rs_saveHistory", path.expand(file), PACKAGE = "(embedding)")) |
There was a problem hiding this comment.
Could this get a testthat test? It wouldn't reproduce #19684, as the description says, but it would cover the new split where these wrappers expand ~ and the C++ side doesn't. FilePath's string constructor doesn't expand ~, so if path.expand() were dropped later, "~/x" would be treated as a relative path and nothing would catch it. No existing test calls savehistory or loadhistory.
Something like .rs.savehistory("~/<temp name>") followed by expect_true(file.exists(path.expand(...))), with cleanup, would work on any platform.
There was a problem hiding this comment.
Added in 202a3a9. test-history.R checks that .rs.savehistory("~/<temp name>") writes the file under path.expand("~"). It also checks that .rs.loadhistory("~/<temp name>") loads that file, by saving the history to a temp file and reading it back. The load test changes the session's history, so it skips outside the rstudio-tests runner, as #19689's tests do. With path.expand() removed from the wrappers, both tests fail. #19689 adds a file with the same name, so whichever merges second keeps both sets of tests.
|
Agreed on the other |
Intent
On Windows with R 4.1 and earlier,
savehistory()andloadhistory()didn't save or load the history file when given a path starting with~and the R home directory contained a non-ASCII character such asé. Neither showed an error.Fixes #19684.
Approach
On Windows, RStudio replaces
utils::savehistory()andutils::loadhistory()with.rs.savehistory()and.rs.loadhistory(), which callrs_saveHistoryandrs_loadHistoryinRSession.cpp. Those passed the path throughR_ExpandFileName(r::sexp::asString(...)). Since #19640,asString()returns UTF-8. On Windows, though,R_ExpandFileName()expands~by prepending the home directory asgetenv()returns it, which is in the native code page.FilePaththen decoded the mixed string as UTF-8 and named a directory that doesn't exist.rs_saveHistorydiscards the write error, andrs_loadHistoryonly logs it, so nothing reached the user.The wrappers now expand
~in R withpath.expand(), and the C++ side takes the expanded path as given.path.expand()marks the result's encoding, soasString()converts it to UTF-8 correctly whatever the native encoding. Relative paths and paths without~behave as before.Before #19640 the whole string was native, so any non-ASCII history path failed. #19640 fixed paths that don't start with
~, and this fixes the rest.The POSIX
loadhistory()/savehistory()callbacks (doHistoryFileOperation()inRStdCallbacks.cpp) aren't affected. They read the path as native, which is whatR_ExpandFileName()expects, andFilePathon POSIX uses the bytes as they are.Automated Tests
New
src/cpp/tests/testthat/test-history.R(--scope r --filter history) covers the split where the wrappers expand~and the C++ side doesn't..rs.savehistory("~/<temp name>")should write the file underpath.expand("~"), and.rs.loadhistory("~/<temp name>")should load it, checked by saving the history to a temp file and reading it back. The load test changes the session's history, so it skips unlessRSTUDIO_TESTS_HELPERis set, as therstudio-testsrunners do. Withpath.expand()removed from the wrappers, both tests fail.They don't reproduce #19684: that needs Windows, R < 4.2 and a non-ASCII home directory, and R caches the home directory for the life of the process. Verified manually instead:
R_USERset to a directory namedCrème.savehistory("~/hist.Rhistory")wrote no file at all, andloadhistory()on the same path loggedsystem error 2 (The system cannot find the file specified)fromrs_loadHistory. Calling the same C++ functions withpath.expand(file), as the wrappers now do, wrote the file intoCrèmeand loaded it back without error. (In that buildR_ExpandFileName()leaves an already-expanded path unchanged, so this exercises the fixed behavior.)HOMEset to a directory namedCrème..rs.savehistory("~/...")and.rs.loadhistory("~/...")work, and a relative path still saves into the working directory.QA Notes
Needs Windows with R 4.1 or earlier and an R home directory containing a non-ASCII character that the system code page can represent (e.g.
éon an English-language system). Setting theR_USERenvironment variable to such a directory before starting RStudio is enough.savehistory("~/test.Rhistory").file.exists("~/test.Rhistory")should beTRUE, and the file should contain those commands.loadhistory("~/test.Rhistory")should restore them (check with the Up arrow in the Console).Documentation
None needed.
Checklist
NEWS.md