Skip to content

Improve Playwright state isolation #17584

Closed
Closed

Description

@gtritchie

Playwright vs BRAT: State Isolation in RStudio Tests

Comparison of how the two RStudio test setups isolate per-user state, and why
Playwright runs leave leftovers (open documents, settings) visible in a normal
RStudio Desktop launch afterward, while BRAT runs do not.

Where the state actually lives

RStudio scatters per-user state across three roots, all overridable:

Env var XDG fallback Default on macOS What's in it
RSTUDIO_CONFIG_HOME XDG_CONFIG_HOME ~/.config/rstudio rstudio-prefs.json (user prefs)
RSTUDIO_DATA_HOME XDG_DATA_HOME ~/.local/share/rstudio session DB, source database (open docs), monitored user-state, Posit Assistant
Electron userData (controlled by --user-data-dir) n/a ~/Library/Application Support/RStudio Chromium profile: window state, recent files, cookies, localStorage

src/cpp/core/system/Xdg.cpp and src/node/desktop/src/core/xdg.ts are where
the lookups happen.

What BRAT does (src/cpp/session/modules/automation/SessionAutomation.R:421-547)

  1. Wipes the env first — strips every RS_* / RSTUDIO_* and
    R_SESSION_TMPDIR from the child's environment so the inner RStudio looks
    brand-new (lines 433-439).
  2. Creates tempfile("rstudio-automation-state-") and points all four at
    subdirs:
    • RSTUDIO_CONFIG_ROOT, RSTUDIO_CONFIG_HOME, RSTUDIO_CONFIG_DIR,
      RSTUDIO_DATA_HOME (line 461)
  3. Passes --user-data-dir=<browserDataDir> to Electron (line 526) —
    isolates the Chromium profile.
  4. Adds RS_NO_SPLASH, RS_CRASH_HANDLER_PROMPT,
    RSTUDIO_DISABLE_CHECK_FOR_UPDATES (479-481).

End result: every disk surface RStudio writes to is under tempfile(...). The
real user profile is never touched.

What Playwright does (e2e/rstudio/fixtures/desktop.fixture.ts:70-164)

  1. Inherits process.env as-is — no RS_* / RSTUDIO_* wipe.
  2. Creates mkdtempSync(.../pw_rstudio_config_) and sets only three vars:
    • RSTUDIO_CONFIG_ROOT, RSTUDIO_CONFIG_HOME, RSTUDIO_CONFIG_DIR
  3. RSTUDIO_DATA_HOME is intentionally not set — see the comment at line
    162: "intentionally not overridden -- share local Posit Assistant
    install/login"
    .
  4. Does not pass --user-data-dir to Electron at all.

Why leftovers appear after Playwright

The two unisolated surfaces are exactly what shows up:

  • Open documents / session-suspended state / monitored user-state →
    RSTUDIO_DATA_HOME (~/.local/share/rstudio). Source database lives here;
    that's why last-open files come back.
  • Window geometry, recent files, Electron-managed UI bits → Electron
    userData (~/Library/Application Support/RStudio on macOS). No
    --user-data-dir, so it writes to the real profile.

The comment in desktop.fixture.ts is honest about the RSTUDIO_DATA_HOME
tradeoff (Posit Assistant install/login persistence is convenient for dev). The
--user-data-dir omission appears to be a plain gap rather than a deliberate
choice — there's no comment defending it, and BRAT shows it's safe to use.

e2e/rstudio/README.md:118 references issue #17520 for tracking server-mode
parity, and useSuiteSandbox() in utils/sandbox.ts redirects R-side cwd
writes — but neither addresses the data-home or Electron-userData gap.

Minimal changes to match BRAT's isolation

  1. Set RSTUDIO_DATA_HOME to a temp subdir (and accept losing Posit Assistant
    persistence, or carry it over selectively).
  2. Pass --user-data-dir=<temp>/browser-data in the spawn args.

Activity

  1. added
    new
    on May 8, 2026
  2. added theissue type on May 8, 2026
  3. ronblum commented on May 8, 2026

    @ronblum
    Contributor

    Environment variables pointing at a sandbox:

    • RSTUDIO_CONFIG_ROOT → <os.tmpdir()>\pw_rstudio_config_ (desktop.fixture.ts:159)
    • RSTUDIO_CONFIG_HOME → \config-home (desktop.fixture.ts:160)
    • RSTUDIO_CONFIG_DIR → \config-dir (desktop.fixture.ts:161)

    Environment variables not set or not redirected:

    • RSTUDIO_DATA_HOME -- explicitly left alone, to share local Posit Assistant install/login (temporary until I set up automated authentication)
    • R_HOME, R_LIBS_USER, R_LIBS_SITE
    • R_PROFILE, R_PROFILE_USER
    • R_ENVIRON, R_ENVIRON_USER
    • R_USER, R_HISTFILE
    • HOME / USERPROFILE
    • TMPDIR / TMP / TEMP -- read indirectly (these are what determine where os.tmpdir() resolves), but not redirected

    I can't touch RSTUDIO_DATA_HOME because of the Posit Assistant tests, until I set up automated authentication. The tests rely on the user authenticating manually (so they currently all fail in GitHub Actions runners). RSTUDIO_DATA_HOME is at the top of the list to address. Any others?

  4. ronblum commented on May 8, 2026

    @ronblum
    Contributor

    A sandbox gets created where any file writing and project creation. I had RSTUDIO_DATA_HOME directed at the sandbox, but it causes a problem with Posit Assistant, so I took it out temporarily, continuing to rely on the user's local info. I think that once I get automated authentication going, I can put it back.

    Another option: I think that I can set it up so that by default, RSTUDIO_DATA_HOME points at a temporary directory, but you can override it and set to a fixed directory.  So

    • By default, RSTUDIO_DATA_HOME points to a temporary directory, but the Posit Assistant tests will fail.
    • With an override, RSTUDIO_DATA_HOME points to the usual place, so Posit Assistant tests will run, but you might get stuff that you don't want sticking around in your environment.

    Would this be useful as a temporary measure?

  5. gtritchie commented on May 8, 2026

    @gtritchie
    MemberAuthor

    Would this be useful as a temporary measure?

    Sounds reasonable (default to isolation, allow an override to unblock posit assistant tests).

    Also wonder if we could symlink just the part needed by Posit AI to somewhere else, and leave the rest isolated.

    In addition, also need the mentioned Electron userdata isolation (--user-data-dir ).

  6. ronblum commented on May 8, 2026

    @ronblum
    Contributor

    For the last one, you can pass RStudio options if you include them in the environment variable PW_RSTUDIO_EXTRA_ARGS.

  7. ronblum commented on May 11, 2026

    @ronblum
    Contributor

    There's a sandbox already implemented, but it includes some itmes. Here's the full plan. What are you thoughts?

    In addition to the sandbox, there are some files and directories that can be controlled by the user, including the location of the sandbox itself (a temporary directory by default).
    Also, there's a default rstudio-pres.json; it's needed, for example, for setting some dialog boxes to web-based instead of native. There's no default Rprofile and Renviron. These can be overlaid with tester-provided files (rstudio-pref.json is implemented, the other two WIP).

    Let me know what you think--if there's anything more that would be useful, if you think most of these are unnecessary, or anything else.

    Directory tree

    <sandbox>/                                    <- umbrella (PW_SANDBOX, or auto-created <os.tmpdir()>/pw_<runid>/)
    |
    +-- pw_rstudio_config_<rand>/                 <- per-worker
    |   |
    |   +-- config-home/                          <- RSTUDIO_CONFIG_HOME
    |   |   `-- rstudio-prefs.json                  (merged: base-prefs.jsonc + PW_RSTUDIO_PREFS_OVERLAY)
    |   |
    |   +-- config-dir/                           <- RSTUDIO_CONFIG_DIR
    |   |
    |   +-- data-home/                            <- RSTUDIO_DATA_HOME
    |   |
    |   +-- electron-userdata/                    <- --user-data-dir=<this>
    |   |     Cookies, IndexedDB, GPU cache,
    |   |     devtools state, rstudio.socket
    |   |
    |   +-- home/                                 <- HOME (POSIX) / USERPROFILE (Windows)
    |   |   `-- .positai/                           Posit Assistant auth + settings + workspaces
    |   |
    |   `-- r-home/                               <- per-worker R user files
    |       +-- Rprofile                          <- R_PROFILE_USER  (base + PW_R_PROFILE_OVERLAY)
    |       +-- Renviron                          <- R_ENVIRON_USER  (base + PW_R_ENVIRON_OVERLAY)
    |       +-- Rhistory                          <- R_HISTFILE
    |       `-- r-libs/                           <- R_LIBS_USER  (optionally pre-seeded via PW_R_LIBS_SEED)
    |
    +-- pw_r_workdir_<rand>/                      <- per-spec (R cwd)
    |   +-- .RData
    |   `-- <test files, projects, etc.>
    |
    `-- Rtmp<x>/                                  <- R session tempdir (auto, via TMPDIR=<sandbox>)
    

    Variables -- user-controlled

    Dials a user or CI explicitly sets in the shell, .env, or workflow YAML. All PW_* vars are optional; defaults apply when unset.

    Variable Value Notes
    PW_SANDBOX <umbrella> If unset, auto-create <os.tmpdir()>/pw_<runid>/ and remove in globalTeardown
    PW_SANDBOX_CREATE 1 to enable Allow auto-create when PW_SANDBOX points at a missing dir (default off, fail loud on typos)
    PW_RSTUDIO_PREFS_OVERLAY <file> JSONC file object-merged on top of base-prefs.jsonc
    PW_R_PROFILE_OVERLAY <file> Contents concatenated after base Rprofile
    PW_R_ENVIRON_OVERLAY <file> Contents concatenated after base Renviron
    PW_R_LIBS_SEED <dir> Pre-seeded R packages dir, copied into r-home/r-libs/
    PW_USE_REAL_HOME 1 1 = rstudio inherits the real user HOME / USERPROFILE (debug aid). Unset or 0 = sandboxed (default).
    PW_HOME_OVERRIDE <path> Use this path for HOME / USERPROFILE instead of <per-worker>/home

    Variables -- set automatically by the fixture

    Set on rstudio spawn by desktop.fixture.ts. The user does not need to set these; the fixture computes the values from the umbrella and per-worker root and passes them in the spawn env (or as a CLI arg for --user-data-dir). Values in the user's own shell are overridden for the rstudio child process.

    Variable Value Type Notes
    RSTUDIO_CONFIG_ROOT <per-worker>/ spawn env Existing. Not consumed by core RStudio today; only SessionAutomation.R writes it for BRAT child processes. Kept in anticipation of porting BRAT tests to Playwright.
    RSTUDIO_CONFIG_HOME <per-worker>/config-home spawn env Existing
    RSTUDIO_CONFIG_DIR <per-worker>/config-dir spawn env Existing
    RSTUDIO_DATA_HOME <per-worker>/data-home spawn env new
    R_LIBS_USER <per-worker>/r-home/r-libs spawn env new
    R_PROFILE_USER <per-worker>/r-home/Rprofile spawn env new
    R_ENVIRON_USER <per-worker>/r-home/Renviron spawn env new
    R_HISTFILE <per-worker>/r-home/Rhistory spawn env new. Redundant with HOME redirection (would otherwise default to <per-worker>/home/.Rhistory); kept for tidier grouping under r-home/.
    TMPDIR / TMP / TEMP <umbrella>/ spawn env new -- R tempdir and Node os.tmpdir() both flow here
    HOME (POSIX) <per-worker>/home spawn env new -- Tier 1 Posit Assistant isolation
    USERPROFILE (Windows) <per-worker>/home spawn env new -- Tier 1 Posit Assistant isolation
    HOMEDRIVE / HOMEPATH (Windows) derived from <per-worker>/home spawn env new -- completeness for older code that does %HOMEDRIVE%%HOMEPATH% concatenation
    --user-data-dir=<per-worker>/electron-userdata (Chromium switch, passed via spawn args) spawn arg new -- Electron browser-side state. Can also be passed via RSTUDIO_EXTRA_ARGS (user value wins, last-wins switch parsing)

    Base file locations (in repo)

    File Purpose
    e2e/rstudio/fixtures/base-prefs.jsonc Base RStudio prefs, merged with PW_RSTUDIO_PREFS_OVERLAY per worker
    e2e/rstudio/fixtures/base.Rprofile Base R startup profile, concatenated with PW_R_PROFILE_OVERLAY
    e2e/rstudio/fixtures/base.Renviron Base R environment vars, concatenated with PW_R_ENVIRON_OVERLAY

    base-prefs.jsonc exists today; base.Rprofile and base.Renviron are to be added as part of the plan implementation.

  8. self-assigned this
    on May 11, 2026
  9. removed
    new
    on May 13, 2026
  10. ronblum commented on May 17, 2026

    @ronblum
    Contributor

    Sandbox updated to include RSTUDIO_DATA_HOME and user-data-dir to the sandbox.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions