Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion crates/ark/src/modules/positron/errors.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ globalInterruptHandler <- function(cnd) {
format_traceback(traceback)
}

# Sources `file` with ark's global condition handlers re-registered.
# Intended to be called from a `top_level_exec()` context (which clears the
# handler stack), so that `globalCallingHandlers()` succeeds. This restores
# message/error/warning rendering to match interactive behavior during
# `.Rprofile` sourcing without putting calling-handlers on the stack that
# would block user `globalCallingHandlers()` calls.
.ps.errors.source_with_handlers <- function(file) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not exported so shouldn't use .ps. prefix.

initialize_errors()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need a comment in initialize_errors() to mention that particular calling context.

Are we certain that no ill side effect comes from reregistering the global handlers in that top-level context? Does R restore the handler stack to the previous state? (It might very well do that).

To make things clearer I think I'd prefer not using initialize_errors() which in my mind should only be called once per file. Probably best to split into initialize_global_handlers().

sys.source(file, envir = globalenv())
}

# If a sink is active (either on output or on messages) messages
# are always streamed to `stderr`. This follows rlang behaviour
# and ensures messages can be sinked from stderr consistently.
Expand Down Expand Up @@ -278,10 +289,21 @@ initialize_errors <- function() {
# Unregister all handlers and hold onto them
handlers <- globalCallingHandlers(NULL)

existing_ps_handler <- vapply(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ark rather than ps

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

registered_ark_handlers

"registered" is clearer to me, and plural makes it clearer this is a vector

handlers,
function(h) {
isTRUE(all.equal(h, .ps.errors.globalErrorHandler)) ||
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is preventing usage of identical() here?

isTRUE(all.equal(h, .ps.errors.globalWarningHandler)) ||
isTRUE(all.equal(h, .ps.errors.globalMessageHandler)) ||
isTRUE(all.equal(h, globalInterruptHandler))
},
logical(1)
)

# Inject our global error handler at the end.
# This allows other existing error handlers to run ahead of us.
handlers <- c(
handlers,
handlers[!existing_ps_handler],
list(
error = .ps.errors.globalErrorHandler,
warning = .ps.errors.globalWarningHandler,
Expand Down
11 changes: 6 additions & 5 deletions crates/ark/src/startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use std::str::FromStr;
use amalthea::socket::iopub::IOPubMessage;
use amalthea::wire::stream::Stream;
use amalthea::wire::stream::StreamOutput;
use harp::environment::R_ENVS;
use harp::exec::RFunction;
use harp::exec::RFunctionExt;
use libr::Rf_eval;
Expand Down Expand Up @@ -73,14 +72,16 @@ fn source_r_profile(path: &Path) {
// `globalCallingHandlers()` from being called within `.Rprofile`s (can't
// call it when there are handlers on the stack). That is a common place to
// register global calling handlers, including in Gabor's prompt package.
// Source in the global env to mimic R.
// Source in the global env to mimic R. We do still register our handlers
// using globalCallingHandlers so that messages, warnings and errors appear
// as expected
let positron_ns = crate::modules::ARK_ENVS.positron_ns;
let result = unsafe {
let call = RFunction::new("base", "sys.source")
let call = RFunction::from(".ps.errors.source_with_handlers")
.param("file", path)
.param("envir", R_ENVS.global)
.call
.build();
harp::top_level_exec(|| Rf_eval(call.sexp, R_ENVS.global))
harp::top_level_exec(|| Rf_eval(call.sexp, positron_ns))
};

let Err(err) = result else {
Expand Down
Loading