Skip to main content

rolldown_error/build_diagnostic/events/
unloadable_dependency.rs

1use arcstr::ArcStr;
2use oxc::span::Span;
3
4use crate::types::diagnostic_options::DiagnosticOptions;
5
6use super::BuildEvent;
7
8#[derive(Debug)]
9pub struct UnloadableDependencyContext {
10  pub source: ArcStr,
11  pub importer_id: ArcStr,
12  pub importee_span: Span,
13}
14
15#[derive(Debug)]
16pub struct UnloadableDependency {
17  pub(crate) reason: ArcStr,
18  pub(crate) resolved: ArcStr,
19  pub(crate) context: Option<UnloadableDependencyContext>,
20}
21
22impl BuildEvent for UnloadableDependency {
23  fn kind(&self) -> crate::types::event_kind::EventKind {
24    crate::types::event_kind::EventKind::UnloadableDependencyError
25  }
26
27  fn id(&self) -> Option<String> {
28    self.context.as_ref().map(|context| context.importer_id.to_string())
29  }
30
31  fn message(&self, _opts: &DiagnosticOptions) -> String {
32    format!(
33      "Could not load {}{} - {}.",
34      self.resolved,
35      self
36        .context
37        .as_ref()
38        .map(|i| format!(" (imported by {})", i.importer_id))
39        .unwrap_or_default(),
40      self.reason
41    )
42  }
43
44  fn on_diagnostic(
45    &self,
46    diagnostic: &mut crate::build_diagnostic::diagnostic::Diagnostic,
47    opts: &DiagnosticOptions,
48  ) {
49    match &self.context {
50      Some(context) => {
51        let importer_file =
52          diagnostic.add_file(context.importer_id.clone(), context.source.clone());
53
54        diagnostic.title = format!("Could not load {}", self.resolved);
55
56        diagnostic.add_label(
57          &importer_file,
58          context.importee_span.start..context.importee_span.end,
59          self.reason.to_string(),
60        );
61      }
62      _ => {
63        diagnostic.title = self.message(opts);
64      }
65    }
66
67    if self.resolved.starts_with("\\0") {
68      diagnostic.add_help(String::from(
69        "This module seems to be a virtual module, but no plugin handled it via the load hook.",
70      ));
71    }
72  }
73}