Skip to content

~ 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 #19724

Description

@kevinushey

System details

RStudio Edition : Desktop
RStudio Version : main (2026.12 development)
OS Version      : Windows
R Version       : < 4.2 (native encoding is the ANSI code page, not UTF-8)

Steps to reproduce the problem

  1. On Windows, set R_USER to a directory containing a non-ASCII character that the ANSI code page can represent (e.g. Crème), with a few files in it.
  2. Start RStudio with R 4.1 or earlier.
  3. Run list.files("~").

Describe the problem in detail

Found by @gtritchie while reviewing #19685; not yet reproduced on Windows.

This is the same encoding mismatch as #19684, in other callers. On Windows, R_ExpandFileName() expands ~ by prepending the home directory as getenv() returns it, in the native code page. These callers pass it a UTF-8 string, so the result mixes a native home directory with a UTF-8 remainder, and it is then decoded as UTF-8:

  • initializePaths() (src/cpp/session/modules/SessionFiles.cpp) runs Rf_translateCharUTF8(), then R_ExpandFileName(). RStudio's list.files() / list.dirs() / dir() replacements use it, and SessionPatches.R turns them on by default on Windows with R < 4.2. So list.files("~") names a directory that doesn't exist.
  • r::sexp::extract(SEXP, FilePath*) (src/cpp/r/RSexp.cpp) extracts the string as UTF-8, then calls r::util::expandFileName().
  • rs_browseURL() (src/cpp/r/session/RSession.cpp) calls r::util::expandFileName() on file paths with no URL scheme, e.g. browseURL("~/report.html").

r::util::expandFileName() (src/cpp/r/RUtil.cpp) is a thin wrapper around R_ExpandFileName(). Making it take and return UTF-8 (convert to native, expand, convert back) would fix the last two, and initializePaths() could call it instead of R_ExpandFileName(). #19685 fixed savehistory() / loadhistory() by expanding ~ with path.expand() in R; they could use the fixed helper as well.

The POSIX callbacks in RStdCallbacks.cpp aren't affected: they pass native strings to R_ExpandFileName(), and FilePath on POSIX uses the bytes as they are.

Describe the behavior you expected

list.files("~") lists the home directory's files, and ~ paths passed to the other callers name the home directory.

  • I have read the guide for submitting good bug reports.
  • I have installed the latest version of RStudio, and confirmed that the issue still persists.
  • If I am reporting an RStudio crash, I have included a diagnostics report.
  • I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions