Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions crates/ark/src/modules/positron/hooks_source.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ make_ark_source <- function(original_source) {
# fallback calls.
use_file <- missing(exprs)

# Capture environment early if local evaluation is requested
if (isTRUE(local)) {
local <- parent.frame()
}

# DRY: Promise for calling `original_source` with all arguments.
# Evaluated lazily only when needed for fallback paths.
delayedAssign(
Expand Down Expand Up @@ -87,6 +92,9 @@ make_ark_source <- function(original_source) {
}

env <- if (isTRUE(local)) {
# That would be very unexpected since we captured the local
# environment in `local` when set to `TRUE`. But just for peace of
# mind we handle it here.
parent.frame()
} else if (isFALSE(local)) {
.GlobalEnv
Expand Down
58 changes: 58 additions & 0 deletions crates/ark/tests/kernel-hooks-source.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use std::io::Write;

use ark::fixtures::DummyArkFrontend;

#[test]
fn test_source_local() {
let frontend = DummyArkFrontend::lock();

let mut file = tempfile::NamedTempFile::new().unwrap();
write!(file, "foobar\n").unwrap();

let path = file.path().to_str().unwrap();

// Breakpoint injection path
let code = format!(
r#"local({{
foobar <- "worked"
source("{path}", local = TRUE)$value
}})"#
);

frontend.execute_request(&code, |result| {
assert_eq!(result, "[1] \"worked\"");
});

// Fallback path (because we supply `encoding`)
let code = format!(
r#"local({{
foobar <- "worked"
source("{path}", local = TRUE, encoding = "UTF-8")$value
}})"#
);

frontend.execute_request(&code, |result| {
assert_eq!(result, "[1] \"worked\"");
});
}

#[test]
fn test_source_local_fallback() {
let frontend = DummyArkFrontend::lock();

let mut file = tempfile::NamedTempFile::new().unwrap();
write!(file, "foobar\n").unwrap();

let path = file.path().to_str().unwrap();

let code = format!(
r#"local({{
foobar <- "worked"
source("{path}", local = TRUE, encoding = "UTF-8")$value
}})"#
);

frontend.execute_request(&code, |result| {
assert_eq!(result, "[1] \"worked\"");
});
}
Loading