Skip to content

expand ~ to a utf-8 home directory on windows with r < 4.2 - #19899

Merged
kevinushey merged 3 commits into
mainfrom
bugfix/19724-expand-file-name-utf8
Dec 3, 2026
Merged

kevinushey merged 3 commits into
mainfrom
bugfix/19724-expand-file-name-utf8

Conversation

@kevinushey

@kevinushey kevinushey commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Intent

On Windows with R 4.1 and earlier, list.files("~"), list.dirs("~") and dir("~") 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 passed R_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() in SessionFiles.cpp, behind RStudio's list.files() / list.dirs() / dir() replacements, which are on by default on Windows with R < 4.2
  • r::sexp::extract(SEXP, FilePath*)
  • rs_browseURL(), for paths with no URL scheme

The 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 own R_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 of R_ExpandFileName(). The helper reads the R version from the R DLL (getDLLVersion()), the same check REmbeddedWin32.cpp makes 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 ~ with path.expand() (#19685). Nothing changes on POSIX.

Automated Tests

  • New test in test-script-startup.R (--scope r --filter script-startup): starts a child session whose home directory is named café, 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.
  • New 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 core and rsession scopes, 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_USER set to a directory named Crème:

  • Before (rsession from a CI installer of main, R 3.6.3): with the replacements installed, list.files("~"), list.dirs("~") and dir("~") return character(0). .rs.listFiles(path.expand("~")) lists the files, and base R's file.exists("~/alpha.txt") is TRUE.
  • After (rsession from this PR's CI installer, built from 3793e57, R 3.6.3): the same calls list alpha.txt, beta.txt and sub, and the full names are UTF-8 paths under Crème. SessionRTest.ExpandFileNameTakesAndReturnsUtf8 passes in that session.
  • R 4.5.3, under both rsession-utf8.exe and rsession.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 the R_USER environment variable to such a directory, with a few files in it, before starting RStudio is enough.

  1. In the Console, run list.files("~"), list.dirs("~") and dir("~"). Each should list the directory's contents, as list.files(path.expand("~")) does.
  2. Put an HTML file in that directory and run 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

  • 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

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.
@kevinushey kevinushey added this to the 2026.12 (Parrot Balsam) milestone Dec 2, 2026
@kevinushey
kevinushey requested a review from gtritchie December 2, 2026 22:25

@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.

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.

Comment thread src/cpp/r/RUtil.cpp Outdated
// 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");

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.

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.

@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

RStudio E2E - all platforms

All tests passed!

#################### 100% pass rate

Passed Failed Skipped Flaky
6008 0 286 2
Platform ✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
✅ auth-teardown 4 0 16 0
✅ desktop-linux 1509 0 49 0
✅ desktop-macos 1484 0 75 0
✅ desktop-windows 1503 0 60 1
✅ server-linux 1460 0 54 1
✅ setup 48 0 32 0

View report online


All platforms - commit 9d31173bd06666d03884013df20d7429f19949f7 - Run #2628

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.
@kevinushey
kevinushey merged commit 9e7c293 into main Dec 3, 2026
13 of 17 checks passed
@kevinushey
kevinushey deleted the bugfix/19724-expand-file-name-utf8 branch December 3, 2026 00:53
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.

~ paths mis-decoded by RStudio's list.files(), browseURL(), and other R_ExpandFileName() callers on Windows R < 4.2 with a non-ASCII home directory

2 participants