Skip to content

Commit d19da05

Browse files
committed
Give precedence to namespace of debugged function
1 parent 7668187 commit d19da05

File tree

3 files changed

+47
-32
lines changed

3 files changed

+47
-32
lines changed

crates/ark/src/interface.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ impl RMain {
453453
// Namespaces of future loaded packages will be populated on load.
454454
// (after r_task initialization)
455455
if do_resource_namespaces() {
456-
if let Err(err) = resource_loaded_namespaces() {
456+
if let Err(err) = resource_loaded_namespaces(None) {
457457
log::error!("Can't populate srcrefs for loaded packages: {err:?}");
458458
}
459459
}

crates/ark/src/modules/positron/srcref.R

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,33 +46,40 @@ zap_srcref <- function(x) {
4646
}
4747

4848
new_ark_debug <- function(fn) {
49-
# Signature of `debug()`:
50-
# function(fun, text = "", condition = NULL, signature = NULL)
51-
52-
body(fn) <- bquote({
53-
local({
54-
# Don't do anything if user has explicitly disabled namespace resourcing
55-
if (!.ps.internal(do_resource_namespaces(default = TRUE))) {
56-
return() # from local()
57-
}
58-
59-
# Enable namespace resourcing for all future loaded namespaces and
60-
# resource already loaded namespaces so we get virtual documents for
61-
# step-debugging.
62-
options(ark.resource_namespaces = TRUE)
63-
.ps.internal(resource_loaded_namespaces())
49+
# Signature of `debug()`:
50+
# function(fun, text = "", condition = NULL, signature = NULL)
51+
52+
body(fn) <- bquote({
53+
local({
54+
if (!.ps.internal(do_resource_namespaces(default = TRUE))) {
55+
return() # from local()
56+
}
57+
58+
pkgs <- loadedNamespaces()
59+
60+
# Give priority to the namespace of the debugged function
61+
env <- topenv(environment(fun))
62+
if (isNamespace(env)) {
63+
pkgs <- unique(c(getNamespaceName(env), pkgs))
64+
}
65+
66+
# Enable namespace resourcing for all future loaded namespaces and
67+
# resource already loaded namespaces so we get virtual documents for
68+
# step-debugging.
69+
options(ark.resource_namespaces = TRUE)
70+
.ps.internal(resource_loaded_namespaces(pkgs))
71+
})
72+
73+
.(body(fn))
6474
})
6575

66-
.(body(fn))
67-
})
68-
69-
fn
76+
fn
7077
}
7178

7279
do_resource_namespaces <- function(default) {
7380
getOption("ark.resource_namespaces", default = default)
7481
}
7582

76-
resource_loaded_namespaces <- function() {
77-
.ps.Call("ps_resource_loaded_namespaces")
83+
resource_loaded_namespaces <- function(pkgs) {
84+
.ps.Call("ps_resource_loaded_namespaces", pkgs)
7885
}

crates/ark/src/srcref.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,32 @@ use crate::variables::variable::is_binding_fancy;
1616
use crate::variables::variable::plain_binding_force_with_rollback;
1717

1818
#[tracing::instrument(level = "trace")]
19-
pub(crate) fn resource_loaded_namespaces() -> anyhow::Result<()> {
20-
let loaded = RFunction::new("base", "loadedNamespaces").call()?;
21-
let loaded: Vec<String> = loaded.try_into()?;
22-
23-
for pkg in loaded.into_iter() {
24-
r_task::spawn_idle(|| async move {
19+
pub(crate) fn resource_loaded_namespaces(pkgs: Option<Vec<String>>) -> anyhow::Result<()> {
20+
let pkgs = match pkgs {
21+
Some(inner) => inner,
22+
None => {
23+
let loaded = RFunction::new("base", "loadedNamespaces").call()?;
24+
let loaded: Vec<String> = loaded.try_into()?;
25+
loaded
26+
},
27+
};
28+
29+
// Generate only one task and loop inside it to preserve the order of `pkgs`
30+
r_task::spawn_idle(|| async move {
31+
for pkg in pkgs.into_iter() {
2532
if let Err(err) = ns_populate_srcref(pkg.clone()).await {
2633
log::error!("Can't populate srcrefs for `{pkg}`: {err:?}");
2734
}
28-
});
29-
}
35+
}
36+
});
3037

3138
Ok(())
3239
}
3340

3441
#[harp::register]
35-
unsafe extern "C" fn ps_resource_loaded_namespaces() -> anyhow::Result<SEXP> {
36-
resource_loaded_namespaces()?;
42+
unsafe extern "C" fn ps_resource_loaded_namespaces(pkgs: SEXP) -> anyhow::Result<SEXP> {
43+
let pkgs: Vec<String> = RObject::view(pkgs).try_into()?;
44+
resource_loaded_namespaces(Some(pkgs))?;
3745
Ok(harp::r_null())
3846
}
3947

0 commit comments

Comments
 (0)