Repository navigation
expand ~ to a utf-8 home directory on windows with r < 4.2 - #19899
Conversation
On Windows, R_ExpandFileName() prepends the home directory in the native encoding. The callers pass UTF-8 and read the result as UTF-8, so under R < 4.2 a home directory with non-ASCII characters gave a path that doesn't exist. initializePaths(), behind RStudio's list.files() / list.dirs() / dir() replacements, now uses the helper too. Addresses #19724.
…oding RStudio starts R >= 4.2 with a UTF-8 home directory, so converting it from the code page under the non-UTF-8 rsession.exe would have corrupted it. Adds a test that starts a session whose home directory isn't ASCII. Addresses #19724.
gtritchie
left a comment
There was a problem hiding this comment.
Looks good. One inline suggestion about the cached version check. It follows the pattern synchronizeLocale() already uses and is rarely triggered, so it isn't blocking.
| // REmbeddedWin32.cpp): UTF-8 for R >= 4.2, and the native encoding before | ||
| // that. asked before R_ExpandFileName() is called, as evaluating R code | ||
| // could reuse the buffer its result is in | ||
| static const bool s_nativeHome = versionTest("<", "4.2.0"); |
There was a problem hiding this comment.
If versionTest() fails on this first call, s_nativeHome is cached as false for the rest of the session, which turns this fix off on R < 4.2. It evaluates R code through R_tryEval and returns false on any error. A pending interrupt is one such error, e.g. Esc pressed while the session's first list.files() call reaches this point.
synchronizeLocale() caches versionTest() the same way, and the first call is likely to come from RStudio's own R code, so this is unlikely in practice. Getting the version without evaluating R code (as REmbeddedWin32.cpp does from the R DLL), or not caching a failed check, would avoid it.
RStudio E2E - all platformsAll tests passed!
All platforms - commit |
versionTest() evaluates R code and returns false on any error, a pending interrupt included, and the result was cached for the session. A failed first check would have left the home directory unconverted on R < 4.2. Addresses #19724.
Intent
On Windows with R 4.1 and earlier,
list.files("~"),list.dirs("~")anddir("~")returned nothing when the R home directory contained a non-ASCII character such asé.browseURL("~/report.html")and other~paths that RStudio expands itself named a folder that doesn't exist.Fixes #19724.
Approach
On Windows,
R_ExpandFileName()expands~by prepending the home directory R was started with. RStudio gives R that directory in the native encoding for R < 4.2, and as UTF-8 for R >= 4.2 (REmbeddedWin32.cpp). Three callers passedR_ExpandFileName()a UTF-8 string and read the result as UTF-8, so under R < 4.2 the path was a native home directory followed by a UTF-8 remainder:initializePaths()inSessionFiles.cpp, behind RStudio'slist.files()/list.dirs()/dir()replacements, which are on by default on Windows with R < 4.2r::sexp::extract(SEXP, FilePath*)rs_browseURL(), for paths with no URL schemeThe last two already went through
r::util::expandFileName(). On Windows with R < 4.2 it now converts the home directory to UTF-8 before joining it to the rest of the name, as R's ownR_ExpandFileNameUTF8()does (path.expand()uses it for UTF-8 input). R still decides whether a name is expanded, and what the home directory is.initializePaths()now calls the helper instead ofR_ExpandFileName(). The helper reads the R version from the R DLL (getDLLVersion()), the same checkREmbeddedWin32.cppmakes when it picks the encoding, so it evaluates no R code and can't be thrown off by a pending interrupt.For R >= 4.2 the home directory is already UTF-8, so the result is returned as before. That matters under the non-UTF-8
rsession.exe, where converting from the code page would corrupt it.I didn't call
R_ExpandFileNameUTF8()directly, as R's headers mark it "not API". Converting the whole name to the native encoding and back would work for the home directory, but would lose characters in the rest of the name that the code page can't represent.savehistory()/loadhistory()keep expanding~withpath.expand()(#19685). Nothing changes on POSIX.Automated Tests
test-script-startup.R(--scope r --filter script-startup): starts a child session whose home directory is namedcafé, and checks that.rs.listFiles("~"), its full names, and.rs.listDirs("~")find the files there. R reads its home directory once per process, so only a session started for the test can have a different one. It skips when the native encoding can't represent the name.SessionRTest.ExpandFileNameTakesAndReturnsUtf8(--scope rsession):r::util::expandFileName("~")names a directory that exists when read as UTF-8, a non-ASCII remainder is kept as it is, and names without a leading~are left alone.PR CI runs these on Linux only, where both pass with or without the fix, and the scheduled Windows Jenkins job runs only the
coreandrsessionscopes, so no CI job runs the child-session test on Windows. Both new tests were run on Windows (code page 1252, R 4.1.3) from a local build of 9d31173: they pass, and with the home directory conversion turned off both fail (expandFileName("~")doesn't name a directory, and the three listings come back empty).The behavior was also checked in a Windows VM (code page 1252) with
R_USERset to a directory namedCrème:main, R 3.6.3): with the replacements installed,list.files("~"),list.dirs("~")anddir("~")returncharacter(0)..rs.listFiles(path.expand("~"))lists the files, and base R'sfile.exists("~/alpha.txt")isTRUE.alpha.txt,beta.txtandsub, and the full names are UTF-8 paths underCrème.SessionRTest.ExpandFileNameTakesAndReturnsUtf8passes in that session.rsession-utf8.exeandrsession.exe:.rs.listFiles("~")and.rs.listDirs("~")list the directory, and the output is identical before and after.The VM results are from 3793e57, before the version check moved to
getDLLVersion().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, with a few files in it, before starting RStudio is enough.list.files("~"),list.dirs("~")anddir("~"). Each should list the directory's contents, aslist.files(path.expand("~"))does.browseURL("~/<file>.html"). It should open.With R 4.2 or later, the same calls should work as they did before.
Documentation
None needed.
Checklist
NEWS.md