lisette_diagnostics/
module_graph.rs1use crate::LisetteDiagnostic;
2use syntax::ast::Span;
3
4pub fn module_not_found(
5 module_name: &str,
6 span: Span,
7 is_go_stdlib: bool,
8 standalone: bool,
9 src_prefix_hint: Option<String>,
10) -> LisetteDiagnostic {
11 let help = if let Some(stripped) = src_prefix_hint {
12 format!(
13 "Did you mean `import \"{}\"`? The `src/` prefix is not needed — imports are relative to the source directory.",
14 stripped
15 )
16 } else if is_go_stdlib {
17 format!(
18 "No `{}` module found in your local project. Did you mean `import \"go:{}\"` from Go's stdlib?",
19 module_name, module_name
20 )
21 } else if standalone {
22 "When executing `lis run` on an individual file, that file may import only from the Go standard library. To import modules normally, use `lis new` to create a project."
23 .to_string()
24 } else {
25 "Check the module path and ensure the file exists".to_string()
26 };
27
28 LisetteDiagnostic::error("Module not found")
29 .with_resolve_code("module_not_found")
30 .with_span_label(&span, "not found")
31 .with_help(help)
32}
33
34pub fn cannot_import_prelude(span: Span) -> LisetteDiagnostic {
35 LisetteDiagnostic::error("Invalid import")
36 .with_resolve_code("cannot_import_prelude")
37 .with_span_label(&span, "prelude is automatically available")
38 .with_help("Remove this import. Use e.g. `Option` or `prelude.Option` directly.")
39}
40
41pub fn test_file_not_supported(filename: &str) -> LisetteDiagnostic {
42 LisetteDiagnostic::error(format!("Test file `{}` is not yet supported", filename))
43 .with_resolve_code("test_file_not_supported")
44 .with_help("Files ending in `_test.lis` are reserved for future testing support. Rename this file to compile it.")
45}
46
47pub fn undeclared_go_import(go_pkg: &str, span: Span) -> LisetteDiagnostic {
48 LisetteDiagnostic::error("Undeclared Go dependency")
49 .with_resolve_code("undeclared_go_import")
50 .with_span_label(&span, "not in lisette.toml")
51 .with_help(format!(
52 "Run `lis add {}` to add this dependency, or add it manually to `[dependencies.go]` in `lisette.toml`",
53 go_pkg
54 ))
55}
56
57pub fn missing_go_typedef(
58 go_pkg: &str,
59 module: &str,
60 version: &str,
61 span: Span,
62) -> LisetteDiagnostic {
63 LisetteDiagnostic::error("Missing Go typedef")
64 .with_resolve_code("missing_go_typedef")
65 .with_span_label(&span, "no .d.lis file found")
66 .with_help(format!(
67 "Package `{}` is declared via `{}` {} but no typedef was found. Run `lis sync` to generate it.",
68 go_pkg, module, version
69 ))
70}
71
72pub fn unreadable_go_typedef(path: &std::path::Path, error: &str, span: Span) -> LisetteDiagnostic {
73 LisetteDiagnostic::error("Failed to read Go typedef")
74 .with_resolve_code("unreadable_go_typedef")
75 .with_span_label(&span, "typedef exists but could not be read")
76 .with_help(format!("Failed to read `{}`: {}", path.display(), error,))
77}
78
79pub fn import_cycle(path: &[String]) -> LisetteDiagnostic {
80 let modules: Vec<_> = path[..path.len() - 1].to_vec();
81
82 let is_self_import = modules.len() == 1;
83
84 let chain = if is_self_import {
85 format!("{} -> {}", modules[0], modules[0])
86 } else {
87 modules.join(" -> ")
88 };
89
90 let first_module = &modules[0];
91 let first_end = first_module.len();
92 let first_center = first_module.len() / 2;
93
94 let last_module = if is_self_import {
95 &modules[0]
96 } else {
97 modules.last().expect("cycle must have at least one module")
98 };
99 let last_start = chain.len() - last_module.len();
100 let last_end = chain.len();
101 let last_center = last_start + last_module.len() / 2;
102
103 let mut underline = String::new();
104 for i in 0..chain.len() {
105 if i < first_end {
106 if i == first_center {
107 underline.push('┬');
108 } else {
109 underline.push('─');
110 }
111 } else if i >= last_start && i < last_end {
112 if i == last_center {
113 underline.push('┬');
114 } else {
115 underline.push('─');
116 }
117 } else {
118 underline.push(' ');
119 }
120 }
121
122 let mut connect_line = String::new();
123 for i in 0..=last_center {
124 if i < first_center {
125 connect_line.push(' ');
126 } else if i == first_center {
127 connect_line.push('╰');
128 } else if i < last_center {
129 connect_line.push('─');
130 } else {
131 connect_line.push('╯');
132 }
133 }
134
135 let art = format!("{}\n{}\n{}", chain, underline, connect_line);
136
137 let help = if is_self_import {
138 "Remove the self-import"
139 } else {
140 "To break the cycle, remove one of imports or extract common dependencies into a separate module"
141 };
142
143 LisetteDiagnostic::error(format!("Import cycle detected\n\n{}", art))
144 .with_resolve_code("import_cycle")
145 .with_help(help)
146}