1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//! `cargo telar check` — cargo's own diagnostics, re-pointed at the `.rsx` that produced them.
//!
//! The mapping itself lives in [`super::diagnostics`], which the `cargo telar dev` rebuild loop shares.
use std::io::BufReader;
use std::process::{Command, Stdio};
use super::cli::{CheckArgs, Target};
use super::diagnostics;
use super::frontend_args;
use super::package::WASM_TARGET;
pub(crate) fn run_check_cmd(args: CheckArgs) {
let CheckArgs {
common,
all_targets,
} = args;
let mut cargo_args = vec![
"check".to_string(),
"--message-format=json".to_string(),
"--color=always".to_string(),
];
if let Some(package) = &common.package {
cargo_args.push("-p".to_string());
cargo_args.push(package.clone());
}
if let Some(features) = &common.features {
cargo_args.push("--features".to_string());
cargo_args.push(features.clone());
}
// `--target` used to reach nothing here, so a check answered for the package's `default` whatever it was asked about: `--target tui` on a machine that cannot build a desktop stack failed before it read a line of the project, and one that could reported on a frontend the author was not working on.
if let Some(target) = common.target {
let selected = frontend_args(target, common.renderer, &cargo_args);
cargo_args.extend(selected);
// The browser frontends are target-gated, so naming one for a host check checks nothing at all. `--lib` because a browser loads a module, and the binary beside it need not compile for wasm.
if target == Target::Web {
cargo_args.push("--target".to_string());
cargo_args.push(WASM_TARGET.to_string());
if !all_targets {
cargo_args.push("--lib".to_string());
}
}
}
super::config::warn_if_tooling_unlocked(&cargo_args, &["telar/previews"]);
super::config::warn_if_features_moved(&cargo_args);
// A `[preview]` is markup the author wrote, so a check that skipped it would report nothing about the one block most likely to be half-finished. It is the only command that asks for previews without going on to render them.
cargo_args.push("--features".to_string());
cargo_args.push("telar/previews".to_string());
if all_targets {
cargo_args.push("--all-targets".to_string());
}
cargo_args.extend(common.cargo_args);
let mut cmd = Command::new("cargo");
cmd.args(&cargo_args);
// cargo writes its JSON stream to stdout and its human progress to stderr; letting stderr through keeps the familiar "Checking foo v0.1.0" output while the machine-readable half is consumed here.
cmd.stdout(Stdio::piped()).stderr(Stdio::inherit());
let mut child = match cmd.spawn() {
Ok(child) => child,
Err(e) => {
eprintln!("[cargo-telar] could not run cargo check: {e}");
std::process::exit(1);
}
};
let mut report = match child.stdout.take() {
Some(stdout) => diagnostics::collect(BufReader::new(stdout)),
None => diagnostics::Report::default(),
};
let status = child.wait();
report.add_all_semantic();
report.add_all_prelude_problems();
report.add_all_previews_include_problems();
if !report.is_empty() {
eprintln!();
eprint!("{}", report.render(true));
}
std::process::exit(exit_code(
status.ok().and_then(|status| status.code()),
report.has_errors(),
));
}
fn exit_code(cargo_code: Option<i32>, report_has_errors: bool) -> i32 {
match cargo_code {
Some(0) if !report_has_errors => 0,
Some(code) if code != 0 => code,
_ => 1,
}
}
impl diagnostics::Report {
/// The `.rsx` checks the language server runs, over every source in the workspace.
///
/// Here rather than only in the editor because the two disagreed: an undeclared style class or an i18n key the catalogue does not hold was underlined in VS Code and compiled clean from a terminal, so a project could pass its own check and still be wrong on the screen of whoever opened it.
///
/// They are markup checks, not type checks — cargo answers for the Rust — so they run whether or not cargo found anything.
fn add_all_semantic(&mut self) {
let dir = super::config::find_package_dir(&[]);
let root = telar_project::find_workspace_root(&dir).unwrap_or(dir);
for member in telar_project::workspace_member_dirs(&root) {
let keys = catalog_keys(&member);
let catalog =
(!keys.is_empty()).then_some(telar_diagnostics::CatalogView { keys: &keys });
for rsx in telar_project::find_rsx_files(&member.join("src")) {
let Ok(source) = std::fs::read_to_string(&rsx) else {
continue;
};
// A file that does not parse is reported by the macro, on the line it broke, with the text the parser produced. Saying it twice from two processes only makes the second one look like a different problem.
let Ok(document) = telar_parser::parse(&source) else {
continue;
};
let imported = telar_transpiler::resolve_style_imports(
&rsx,
&member.join("src"),
&document.style,
&|path| std::fs::read_to_string(path).ok(),
);
let names: Vec<&str> = imported
.classes
.iter()
.map(|class| class.class.name.as_str())
.collect();
self.add_semantic(
&rsx,
telar_diagnostics::semantic_diagnostics(&document, catalog.as_ref(), &names),
);
}
}
}
/// The `[telar] prelude` entries each package with `.rsx` sources cannot reach, on the `telar.toml` lines that declare them.
///
/// Checked against `Cargo.toml` rather than left to rustc, whose answer is an unresolved import in every generated file and no word about the key that caused it. An unreadable `telar.toml` is skipped: the transpile reports that, with the line serde names.
fn add_all_prelude_problems(&mut self) {
let dir = super::config::find_package_dir(&[]);
let root = telar_project::find_workspace_root(&dir).unwrap_or(dir);
for member in telar_project::workspace_member_dirs(&root) {
if telar_project::find_rsx_files(&member.join("src")).is_empty() {
continue;
}
if let Ok(problems) = telar_project::prelude_problems(&member) {
self.add_prelude_problems(&problems);
}
}
}
/// The `[telar.previews] include` entries each package cannot reach, on the `telar.toml` lines that declare them, rather than as the unresolved path inside what `app!` expands to.
fn add_all_previews_include_problems(&mut self) {
let dir = super::config::find_package_dir(&[]);
let root = telar_project::find_workspace_root(&dir).unwrap_or(dir);
for member in telar_project::workspace_member_dirs(&root) {
if let Ok(problems) = telar_project::previews_include_problems(&member) {
self.add_previews_include_problems(&problems);
}
}
}
}
/// Every key one package's catalog defines, or empty when it has none. A malformed catalog reads as empty: the bake reports that, and refusing to run the other checks over it would hide a class error behind a translation one.
fn catalog_keys(package_root: &std::path::Path) -> std::collections::HashSet<String> {
telar_baker::parse_catalog(package_root)
.ok()
.flatten()
.map(|c| c.keys().cloned().collect())
.unwrap_or_default()
}
#[cfg(test)]
#[path = "check_test.rs"]
mod tests;