Skip to content

Commit 109df46

Browse files
Suppress confusing logging messages re: namespace completions (#765)
* Logging straggler that escaped earlier reaping * Make return value of completion_item_from_symbol() more conventional * Let the return value just fall through Co-authored-by: Davis Vaughan <[email protected]> * Don't bail Co-authored-by: Davis Vaughan <[email protected]> * Prefer inlining Co-authored-by: Davis Vaughan <[email protected]> * Take @DavisVaughan's suggestion * Prefer inlining * Replace more uses of `bail!` --------- Co-authored-by: Davis Vaughan <[email protected]>
1 parent 3851f24 commit 109df46

File tree

3 files changed

+44
-35
lines changed

3 files changed

+44
-35
lines changed

crates/ark/src/lsp/completions/completion_item.rs

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use anyhow::bail;
1111
use harp::r_symbol;
1212
use harp::utils::is_symbol_valid;
1313
use harp::utils::r_env_binding_is_active;
14-
use harp::utils::r_envir_name;
1514
use harp::utils::r_promise_force_with_rollback;
1615
use harp::utils::r_promise_is_forced;
1716
use harp::utils::r_promise_is_lazy_load_binding;
@@ -374,35 +373,44 @@ pub(super) unsafe fn completion_item_from_namespace(
374373
package: &str,
375374
parameter_hints: &ParameterHints,
376375
) -> anyhow::Result<CompletionItem> {
376+
// We perform two passes to locate the object. It is normal for the first pass to
377+
// error when the `namespace` doesn't have a binding for `name` because the associated
378+
// object has been imported and re-exported. For example, the way dplyr imports and
379+
// re-exports `rlang::.data` or `tidyselect::all_of()`. In such a case, we'll succeed
380+
// in the second pass, when we try again in the imports environment. If both fail,
381+
// something is seriously wrong.
382+
377383
// First, look in the namespace itself.
378-
if let Some(item) = completion_item_from_symbol(
384+
let error_namespace = match completion_item_from_symbol(
379385
name,
380386
namespace,
381387
Some(package),
382388
PromiseStrategy::Force,
383389
parameter_hints,
384390
) {
385-
return item;
386-
}
391+
Ok(item) => return Ok(item),
392+
Err(error) => error,
393+
};
387394

388395
// Otherwise, try the imports environment.
389396
let imports = ENCLOS(namespace);
390-
if let Some(item) = completion_item_from_symbol(
397+
let error_imports = match completion_item_from_symbol(
391398
name,
392399
imports,
393400
Some(package),
394401
PromiseStrategy::Force,
395402
parameter_hints,
396403
) {
397-
return item;
398-
}
404+
Ok(item) => return Ok(item),
405+
Err(error) => error,
406+
};
399407

400-
// If still not found, something is wrong.
401-
bail!(
402-
"Object '{}' not defined in namespace {:?}",
403-
name,
404-
r_envir_name(namespace)?
405-
)
408+
// This is really unexpected.
409+
Err(anyhow::anyhow!(
410+
"Failed to form completion item for '{name}' in namespace '{package}':
411+
Namespace environment error: {error_namespace}
412+
Imports environment error: {error_imports}"
413+
))
406414
}
407415

408416
pub(super) unsafe fn completion_item_from_lazydata(
@@ -416,14 +424,14 @@ pub(super) unsafe fn completion_item_from_lazydata(
416424
let promise_strategy = PromiseStrategy::Simple;
417425

418426
// Lazydata objects are never functions, so this doesn't really matter
419-
let parameter_hints = ParameterHints::Enabled;
427+
let parameter_hints = ParameterHints::Disabled;
420428

421429
match completion_item_from_symbol(name, env, Some(package), promise_strategy, &parameter_hints)
422430
{
423-
Some(item) => item,
424-
None => {
431+
Ok(item) => Ok(item),
432+
Err(err) => {
425433
// Should be impossible, but we'll be extra safe
426-
bail!("Object '{name}' not defined in lazydata environment for namespace {package}")
434+
Err(anyhow::anyhow!("Object '{name}' not defined in lazydata environment for namespace {package}: {err}"))
427435
},
428436
}
429437
}
@@ -434,7 +442,7 @@ pub(super) unsafe fn completion_item_from_symbol(
434442
package: Option<&str>,
435443
promise_strategy: PromiseStrategy,
436444
parameter_hints: &ParameterHints,
437-
) -> Option<anyhow::Result<CompletionItem>> {
445+
) -> anyhow::Result<CompletionItem> {
438446
let symbol = r_symbol!(name);
439447

440448
match r_env_binding_is_active(envir, symbol) {
@@ -445,29 +453,33 @@ pub(super) unsafe fn completion_item_from_symbol(
445453
Ok(true) => {
446454
// We can't even extract out the object for active bindings so they
447455
// are handled extremely specially.
448-
return Some(completion_item_from_active_binding(name));
456+
return completion_item_from_active_binding(name);
449457
},
450458
Err(err) => {
451-
log::error!("Can't determine if binding is active: {err:?}");
452-
return None;
459+
// The only error we anticipate is the case where `envir` doesn't
460+
// have a binding for `name`.
461+
return Err(anyhow::anyhow!(
462+
"Failed to check if binding is active: {err}"
463+
));
453464
},
454465
}
455466

456467
let object = Rf_findVarInFrame(envir, symbol);
457468

458469
if object == R_UnboundValue {
459-
log::error!("Symbol '{name}' should have been found.");
460-
return None;
470+
return Err(anyhow::anyhow!(
471+
"Symbol '{name}' should have been found but wasn't"
472+
));
461473
}
462474

463-
Some(completion_item_from_object(
475+
completion_item_from_object(
464476
name,
465477
object,
466478
envir,
467479
package,
468480
promise_strategy,
469481
parameter_hints,
470-
))
482+
)
471483
}
472484

473485
// This is used when providing completions for a parameter in a document

crates/ark/src/lsp/completions/sources/composite/search_path.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,19 @@ fn completions_from_search_path(
101101
}
102102

103103
// Add the completion item.
104-
let Some(item) = completion_item_from_symbol(
104+
match completion_item_from_symbol(
105105
symbol,
106106
envir,
107107
name,
108108
promise_strategy.clone(),
109109
parameter_hints,
110-
) else {
111-
log::error!("Completion symbol '{symbol}' was unexpectedly not found.");
112-
continue;
113-
};
114-
115-
match item {
110+
) {
116111
Ok(item) => completions.push(item),
117-
Err(error) => log::error!("{:?}", error),
112+
Err(err) => {
113+
// Log the error but continue processing other symbols
114+
log::error!("Failed to get completion item for symbol '{symbol}': {err}");
115+
continue;
116+
},
118117
};
119118
}
120119

crates/ark/src/lsp/completions/sources/unique/namespace.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,6 @@ fn completions_from_namespace_lazydata(
186186
namespace: SEXP,
187187
package: &str,
188188
) -> anyhow::Result<Option<Vec<CompletionItem>>> {
189-
log::info!("completions_from_namespace_lazydata()");
190-
191189
unsafe {
192190
let ns = Rf_findVarInFrame(namespace, r_symbol!(".__NAMESPACE__."));
193191
if ns == R_UnboundValue {

0 commit comments

Comments
 (0)