Skip to content

Posit Assistant's R code checks stay off for the session after a hooked function fails to restore #19806

Description

@kevinushey

System details

RStudio Edition : Desktop and Server
RStudio Version : main
OS Version      : any (seen on macOS 27)
R Version       : any (seen with R 4.6)

Steps to reproduce the problem

Run this in the Console, with fs installed in a user library rather than in .Library:

loadNamespace("fs")
libs <- .libPaths()
outside <- tempfile(tmpdir = dirname(tempdir()))
dir.create(outside)

# 1. code run by Posit Assistant unloads fs, and fs can't be loaded again
.rs.chat.safeEval(quote({ unloadNamespace("fs"); .libPaths(.Library) }))
.libPaths(libs)

# 2. later code run by Posit Assistant writes outside the allowed locations
.rs.chat.safeEval(quote(writeLines("x", file.path(outside, "file.txt"))))
file.exists(file.path(outside, "file.txt"))

Describe the problem in detail

Step 1 warns failed to restore binding 'file_create' in 'fs': Failed to load namespace for package 'fs' once for each hooked fs function. Step 2 then writes the file, and file.exists() returns TRUE. Before step 1, the same write is refused.

chat.restoreBindings() leaves chat.bindingsInjected set to TRUE when any binding fails to restore. chat.injectBindings() returns early whenever that flag is TRUE, which is meant as a guard against reentrant calls. The base and utils functions were restored, so every later evaluation runs with none of the checks for the rest of the session.

A restore fails when .rs.replaceBinding() errors. Here, its requireNamespace() can't load a package that was unloaded during the evaluation.

Describe the behavior you expected

A failed restore doesn't turn off the checks for later evaluations: the next evaluation hooks whatever isn't hooked. Functions in a namespace that was unloaded during the evaluation don't need restoring.

Found while reviewing #19795.

  • 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions