Skip to content

initialize COM at rsession startup on Windows - #18237

Merged
gtritchie merged 1 commit into
mainfrom
bugfix/13078-choose-dir-com-init
Jul 15, 2026
Merged

gtritchie merged 1 commit into
mainfrom
bugfix/13078-choose-dir-com-init

Conversation

@kevinushey

Copy link
Copy Markdown
Collaborator

Since R 4.3.0, utils::choose.dir() returns NA without showing a dialog whenever R runs outside of Rgui (rsession, Rterm, Rscript). R 4.3 rewrote askcdstring() in src/extra/graphapp/dialogs.c to use the COM-based IFileOpenDialog, but it never initializes COM on the calling thread, so CoCreateInstance fails with CO_E_NOTINITIALIZED and the failure is silently mapped to NA -- unless some other API happened to initialize COM on the thread first. That's why the known workaround (calling file.choose() first) works: GetOpenFileNameW initializes COM as a side effect. It's also why the bug never reproduced on some machines: detectGitBinDirFromShortcut() in SessionGit.cpp calls CoInitialize() when git isn't found on the PATH.

This PR initializes COM (STA, the model used by the common dialogs and Rgui) eagerly at rsession startup on Windows, so the main thread's apartment state is deterministic instead of depending on which code path runs first. Per COM semantics this is safe with respect to later initializers: they receive S_FALSE (already initialized) and proceed normally. The initialization is intentionally process-lifetime, so there is no matching CoUninitialize.

The root cause has also been reported upstream to R (an askcdstring() fix would only land in future R versions; this change fixes the behavior for all R >= 4.3.0).

Verification: requires a Windows build; running utils::choose.dir() in a fresh session should show the folder picker instead of returning NA immediately. I've verified the equivalent state manually via the file.choose() workaround (which produces exactly the COM state this change sets up eagerly), but the change itself still needs a Windows smoke test.

Addresses #13078.

Since R 4.3.0, askcdstring() (behind utils::choose.dir()) uses the
COM-based IFileOpenDialog but does not initialize COM itself, so
CoCreateInstance fails with CO_E_NOTINITIALIZED and choose.dir()
silently returns NA unless some other API (e.g. file.choose()'s
GetOpenFileNameW) happened to initialize COM on the thread first.

Initialize COM (STA) eagerly at startup so the apartment state is
deterministic and choose.dir() works out of the box.

Addresses #13078.
@kevinushey kevinushey added this to the 2026.08 (Yellow Yarrow) milestone Jul 15, 2026
@posit-snyk-bot

posit-snyk-bot commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

✅ Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues
✅ Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@kevinushey
kevinushey requested a review from gtritchie July 15, 2026 16:53

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

Nice! LGTM

@github-actions

github-actions Bot commented Jul 15, 2026 •

Copy link
Copy Markdown

RStudio E2E - all platforms

All tests passed!

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

Passed Failed Skipped Flaky
1832 0 67 1
Platform ✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
✅ desktop-linux 461 0 14 0
✅ desktop-macos 464 0 10 1
✅ desktop-windows 461 0 16 0
✅ server-linux 446 0 27 0
View / download the full report

View report online -- opens in the browser, no download needed. Available for 14 days.

Download playwright-report.zip -- extract and open index.html

Includes screenshots, traces, and step-by-step details for every test. Expires in 15 days.


All platforms - commit fa56e5de4e5f30870ce943be0065512b1a4c6bef - Run #191

@gtritchie
gtritchie merged commit 3da1da5 into main Jul 15, 2026
40 of 41 checks passed
@gtritchie
gtritchie deleted the bugfix/13078-choose-dir-com-init branch July 15, 2026 21:03
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.

3 participants