cargo-telar 0.2.3

Cargo subcommand for Telar apps: dev server with hot reload, and packaging into native installers.
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
use super::*;

/// The `help:` line is where rustc puts what to actually do, and it used to be read off the wire and dropped — leaving the half of a type error that says there is a problem without the half that says what the fix is.
#[test]
fn help_and_note_children_survive_the_remap() {
    let message = serde_json::json!({
        "level": "error",
        "message": "mismatched types",
        "children": [
            { "level": "help", "message": "consider borrowing here" },
            { "level": "note", "message": "expected `&str`, found `String`" },
            { "level": "error", "message": "not a child worth repeating" },
        ],
    });
    let notes = notes_of(&message);
    assert_eq!(notes.len(), 2);
    assert_eq!(notes[0].level, "help");
    assert_eq!(notes[1].message, "expected `&str`, found `String`");
}

#[test]
fn ansi_is_stripped_for_the_in_window_banner() {
    assert_eq!(
        strip_ansi("\x1b[1;31merror\x1b[0m: mismatched types"),
        "error: mismatched types"
    );
}

/// Whether the report points at `suffix`, written with `/` whatever the platform spells it with. A frame carries a real mapped path, so on Windows it arrives with backslashes and a literal `src/home.rsx` never matches — which is a fact about the separator and not about the mapping under test.
fn points_at(text: &str, suffix: &str) -> bool {
    text.replace('\\', "/").contains(suffix)
}

/// Lays out a package the way a hot-reload build leaves one: the `.rsx` the author wrote, the generated `.rs` nobody wrote, and the map between them. Returns the package root.
fn fake_package(tag: &str, rsx: &str, generated: &str, map: SourceMap) -> PathBuf {
    let root = std::env::temp_dir().join(format!("telar_diag_{tag}_{}", std::process::id()));
    let build = root.join(".telar/build-hot");
    std::fs::create_dir_all(&build).unwrap();
    std::fs::create_dir_all(root.join("src")).unwrap();
    std::fs::write(root.join("src/home.rsx"), rsx).unwrap();
    std::fs::write(build.join("home.rs"), generated).unwrap();
    std::fs::write(build.join("home.rs.map"), map.to_json()).unwrap();
    root
}

fn message_line(root: &Path, level: &str, message: &str, line: u64) -> String {
    message_span(root, level, message, line, None)
}

/// `bytes` is what rustc puts in every real span; `None` stands for the spans that arrive without one, which fall back to the line.
fn message_span(
    root: &Path,
    level: &str,
    message: &str,
    line: u64,
    bytes: Option<(u32, u32)>,
) -> String {
    let mut span = serde_json::json!({
        "file_name": root.join(".telar/build-hot/home.rs").to_str().unwrap(),
        "line_start": line,
        "is_primary": true,
    });
    if let Some((start, end)) = bytes {
        span["byte_start"] = start.into();
        span["byte_end"] = end.into();
    }
    serde_json::json!({
        "reason": "compiler-message",
        "message": {
            "level": level,
            "message": message,
            "rendered": format!("{level}: {message}\n --> {}/.telar/build-hot/home.rs:{line}\n", root.display()),
            "spans": [span],
        },
    })
    .to_string()
}

/// The whole point. A type error in a `.rsx` used to be reported against `.telar/build-hot/….rs:LINE` — a file the author never opened, at a line they never typed — for the entire length of a `cargo telar dev` session, while the mapping that fixes it sat in a command almost nobody runs.
#[test]
fn a_type_error_is_reported_against_the_rsx_line_not_the_generated_one() {
    let root = fake_package(
        "err",
        "[logic]\nlet n = signal(0);\n\n[view]\ntext \"{n}\"\n",
        "1\n2\n3\n4\n5\n    let n = signal(0);\n",
        SourceMap::new(vec![None, None, None, None, None, Some(1)], vec![]),
    );
    let report = collect(message_line(&root, "error", "mismatched types", 6).as_bytes());
    let text = report.render(false);

    assert!(text.contains("error: mismatched types"), "{text}");
    assert!(points_at(&text, "src/home.rsx:2"), "{text}");
    assert!(
        text.contains("let n = signal(0);"),
        "the frame quotes the line the author wrote:\n{text}"
    );
    assert!(
        !text.contains(".telar"),
        "and nothing points into the build directory:\n{text}"
    );
    std::fs::remove_dir_all(&root).ok();
}

/// Warnings used to be captured into a `String` that only the failure path ever read, so a whole development session could pass without one ever reaching the terminal. They travel the same route as errors now, which is what makes them visible on the builds that succeed.
#[test]
fn a_warning_is_not_silently_swallowed() {
    let root = fake_package(
        "warn",
        "[logic]\nlet unused = 1;\n\n[view]\ncolumn\n",
        "boilerplate\n    let unused = 1;\n",
        SourceMap::new(vec![None, Some(1)], vec![]),
    );
    let report = collect(message_line(&root, "warning", "unused variable: `unused`", 2).as_bytes());

    assert!(
        report.has_diagnostics(),
        "a warning must reach the report, not be swallowed"
    );
    assert!(!report.has_errors(), "a warning does not fail the build");
    let text = report.render(false);
    assert!(text.contains("warning: unused variable"), "{text}");
    assert!(points_at(&text, "src/home.rsx:2"), "{text}");
    std::fs::remove_dir_all(&root).ok();
}

/// A diagnostic about hand-written Rust keeps rustc's own rendering, which is already pointing at a file the author can open.
#[test]
fn a_diagnostic_about_hand_written_rust_is_passed_through_untouched() {
    let message = serde_json::json!({
        "reason": "compiler-message",
        "message": {
            "level": "error",
            "message": "cannot find value `x`",
            "rendered": "error: cannot find value `x`\n --> src/state.rs:9\n",
            "spans": [{ "file_name": "src/state.rs", "line_start": 9, "is_primary": true }],
        },
    })
    .to_string();
    let text = collect(message.as_bytes()).render(false);
    assert_eq!(text, "error: cannot find value `x`\n --> src/state.rs:9\n");
}

/// A `.rsx` that cannot be read still reports the diagnostic — losing the quoted line is a worse frame, not a lost error.
#[test]
fn a_missing_source_file_still_reports_the_diagnostic() {
    let report = Report {
        projected: vec![Projected {
            source: PathBuf::from("/nowhere/home.rsx"),
            line: 4,
            underline: None,
            level: "error".to_string(),
            message: "mismatched types".to_string(),
            notes: vec![],
        }],
        passthrough: vec![],
        artifacts: vec![],
    };
    let text = report.render(false);
    assert!(text.contains("error: mismatched types"), "{text}");
    assert!(text.contains("/nowhere/home.rsx:4"), "{text}");
}

/// A `[logic]` line is transpiled 1:1 under a fixed indent, so rustc's columns come back by subtracting it — and the frame can point at the identifier rather than at the line it happens to sit on.
#[test]
fn a_logic_error_underlines_the_columns_rustc_gave_it() {
    let generated = "fn home() {\n    let count = signal(0);\n}\n";
    let at = generated.find("count").unwrap() as u32;
    let root = fake_package(
        "cols",
        "[logic]\nlet count = signal(0);\n\n[view]\ncolumn\n",
        generated,
        SourceMap::new(vec![None, Some(1), None], vec![]),
    );

    let text =
        collect(message_span(&root, "error", "mismatched types", 2, Some((at, at + 5))).as_bytes())
            .render(false);

    assert!(points_at(&text, "src/home.rsx:2"), "{text}");
    assert!(
        text.contains("    ^^^^^"),
        "the carets sit under `count`, four columns in:\n{text}"
    );
    std::fs::remove_dir_all(&root).ok();
}

/// The case that was unreachable until the expression spans reached the sidecar: a verbatim `[view]` fragment is the same bytes in both files, so an error inside it underlines exactly it. With only the line map on disk, the CLI could say nothing narrower than the whole `text "{…}"` line.
#[test]
fn a_verbatim_view_expression_is_underlined_exactly() {
    let rsx = "[view]\ncolumn\n    text \"{missing}\"\n";
    let generated = "fn home() {\n    text(format!(\"{}\", missing))\n}\n";
    let gen_at = generated.find("missing").unwrap() as u32;
    let root = fake_package(
        "verbatim",
        rsx,
        generated,
        SourceMap::new(
            vec![None, Some(2), None],
            vec![telar_transpiler::ExprSpan {
                rsx_start: rsx.find("missing").unwrap() as u32,
                len: 7,
                gen_start: gen_at,
            }],
        ),
    );

    let text = collect(
        message_span(
            &root,
            "error",
            "cannot find value `missing`",
            2,
            Some((gen_at, gen_at + 7)),
        )
        .as_bytes(),
    )
    .render(false);

    assert!(points_at(&text, "src/home.rsx:3"), "{text}");
    assert!(
        text.contains(&format!("{}^^^^^^^", " ".repeat(11))),
        "the carets sit under `missing`, past `    text \"{{`:\n{text}"
    );
    std::fs::remove_dir_all(&root).ok();
}

/// And the case that has to stay wide. A `[view]` line the transpiler rewrote into something else has no column correspondence, so underlining *something* would point at text unrelated to the error.
#[test]
fn a_rewritten_view_line_is_not_underlined_at_all() {
    let generated = "fn home() {\n    Text::new(\"hi\")\n}\n";
    let at = generated.find("Text").unwrap() as u32;
    let root = fake_package(
        "wide",
        "[logic]\nlet x = 1;\n\n[view]\ntext \"hi\"\n",
        generated,
        SourceMap::new(vec![None, Some(4), None], vec![]),
    );

    let text =
        collect(message_span(&root, "error", "no method `new`", 2, Some((at, at + 4))).as_bytes())
            .render(false);

    assert!(points_at(&text, "src/home.rsx:5"), "{text}");
    assert!(
        !text.contains('^'),
        "nothing is underlined when the columns mean nothing:\n{text}"
    );
    std::fs::remove_dir_all(&root).ok();
}

/// A package as `cargo telar` leaves one after a hot build: its manifests, the `.rsx`, and the transpiler's real output and map for it — so the fixtures below point at the Rust the emitter actually writes.
fn transpiled_package(tag: &str, cargo: &str, telar: &str, rsx: &str) -> (PathBuf, String) {
    let root = std::env::temp_dir().join(format!("telar_diag_{tag}_{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&root);
    std::fs::create_dir_all(root.join("src")).unwrap();
    std::fs::write(root.join("Cargo.toml"), cargo).unwrap();
    std::fs::write(root.join("telar.toml"), telar).unwrap();
    let prelude = telar_project::resolve_prelude(&root).unwrap();
    let out = telar_transpiler::transpile_buffer(
        &root.join("src/home.rsx"),
        rsx,
        &telar_transpiler::PackageOptions {
            src_dir: &root.join("src"),
            theme_type: None,
            assets: None,
            prelude: &prelude,
            library: false,
            flavour: telar_project::BuildFlavour::Plain,
        },
    )
    .unwrap();
    let package = fake_package(
        tag,
        rsx,
        &out.rust_code,
        SourceMap::new(out.source_map, out.expr_spans),
    );
    assert_eq!(package, root);
    (root, out.rust_code)
}

/// One rustc `compiler-message`, shaped as rustc 1.99 writes it, whose primary span covers the first `needle` in the generated file — without a trailing `(`, which only picks the call out of the names around it.
fn rustc_error(
    root: &Path,
    generated: &str,
    code: &str,
    message: &str,
    needle: &str,
    children: serde_json::Value,
) -> String {
    let start = generated
        .find(needle)
        .unwrap_or_else(|| panic!("`{needle}` is not in:\n{generated}"));
    let line = generated[..start].matches('\n').count() + 1;
    serde_json::json!({
        "reason": "compiler-message",
        "message": {
            "level": "error",
            "message": message,
            "code": { "code": code, "explanation": null },
            "rendered": format!("error[{code}]: {message}\n --> .telar/build-hot/home.rs:{line}\n"),
            "spans": [{
                "file_name": root.join(".telar/build-hot/home.rs").to_str().unwrap(),
                "byte_start": start,
                "byte_end": start + needle.trim_end_matches('(').len(),
                "line_start": line,
                "is_primary": true,
            }],
            "children": children,
        },
    })
    .to_string()
}

/// A help child carrying rustc's one suggested replacement.
fn suggestion(message: &str, replacement: &str) -> serde_json::Value {
    serde_json::json!({
        "level": "help",
        "message": message,
        "spans": [{ "suggested_replacement": replacement, "is_primary": true }],
        "children": [],
    })
}

/// The `could refer to … imported here` note rustc hangs off an E0659, underlining the glob import `path::*` on its line.
fn glob_note(message: &str, path: &str) -> serde_json::Value {
    let line = format!("#[allow(unused_imports)] use {path}::*;");
    let at = line.find(&format!("{path}::*")).unwrap() + 1;
    serde_json::json!({
        "level": "note",
        "message": message,
        "spans": [{
            "is_primary": true,
            "text": [{
                "text": line,
                "highlight_start": at,
                "highlight_end": at + path.len() + 3,
            }],
        }],
        "children": [],
    })
}

const DEPENDS_ON_COMPONENTS: &str =
    "[package]\nname = \"app\"\n\n[dependencies]\ntelar = \"0.2\"\ntelar-components = \"0.2\"\n";

/// An unknown tag reaches rustc as two unresolved names, the function and its `Props`, in a file the author never wrote. It is one mistake in the markup, so it is one error there, said in the markup's terms and underlining the tag.
#[test]
fn an_unknown_tag_is_one_error_on_the_tag() {
    let (root, generated) = transpiled_package(
        "unknown_tag",
        DEPENDS_ON_COMPONENTS,
        "[telar]\nprelude = [\"telar-components\"]\n",
        "[view]\ncolumn\n    buton label:\"Go\"\n",
    );
    let stream = [
        rustc_error(
            &root,
            &generated,
            "E0433",
            "cannot find type `ButonProps` in this scope",
            "ButonProps",
            serde_json::json!([suggestion(
                "a struct with a similar name exists",
                "ButtonProps"
            )]),
        ),
        rustc_error(
            &root,
            &generated,
            "E0425",
            "cannot find function `buton` in this scope",
            "buton(",
            serde_json::json!([suggestion(
                "a function with a similar name exists",
                "button"
            )]),
        ),
    ]
    .join("\n");

    let text = collect(stream.as_bytes()).render(false);
    std::fs::remove_dir_all(&root).ok();

    assert_eq!(
        text.matches("unknown tag").count(),
        1,
        "two rustc errors, one mistake:\n{text}"
    );
    assert!(
        text.contains("error: unknown tag `buton`: not a built-in, not a `.rsx` in this package, and not exported by any `[telar] prelude` entry (`telar_components`)"),
        "{text}"
    );
    assert!(points_at(&text, "src/home.rsx:3"), "{text}");
    assert!(
        text.contains("\n  |     ^^^^^\n"),
        "the tag is underlined:\n{text}"
    );
    assert!(
        text.contains("help: a function with a similar name exists: `button`"),
        "{text}"
    );
    assert!(
        !text.contains("ButtonProps") && !text.contains(".telar") && !text.contains("cannot find"),
        "nothing about the Rust the tag became:\n{text}"
    );
}

/// Two prelude crates exporting one tag is E0659 twice — on the function and on its `Props` — pointing at glob imports the author never wrote.
#[test]
fn a_tag_two_crates_export_names_both_and_the_use_that_settles_it() {
    let (root, generated) = transpiled_package(
        "clash",
        DEPENDS_ON_COMPONENTS,
        "[telar]\nprelude = [\"telar-components\"]\n",
        "[view]\nbutton\n",
    );
    let ambiguous = |name: &str, kind: &str, needle: &str| {
        rustc_error(
            &root,
            &generated,
            "E0659",
            &format!("`{name}` is ambiguous"),
            needle,
            serde_json::json!([
                { "level": "note", "message": "ambiguous because of multiple glob imports of a name in the same module", "spans": [], "children": [] },
                glob_note(&format!("`{name}` could refer to the {kind} imported here"), "telar"),
                { "level": "help", "message": format!("consider adding an explicit import of `{name}` to disambiguate"), "spans": [], "children": [] },
                glob_note(&format!("`{name}` could also refer to the {kind} imported here"), "telar_components"),
                { "level": "help", "message": format!("consider adding an explicit import of `{name}` to disambiguate"), "spans": [], "children": [] },
            ]),
        )
    };
    let stream = [
        ambiguous("button", "function", "button("),
        ambiguous("ButtonProps", "struct", "ButtonProps"),
    ]
    .join("\n");

    let text = collect(stream.as_bytes()).render(false);
    std::fs::remove_dir_all(&root).ok();

    assert_eq!(text.matches("error:").count(), 1, "{text}");
    assert!(
        text.contains("error: tag `button` is exported by both `telar` and `telar_components`; pick one with `use telar::{button, ButtonProps};` in `[logic]`"),
        "{text}"
    );
    assert!(points_at(&text, "src/home.rsx:2"), "{text}");
    assert!(
        !text.contains("could refer to") && !text.contains("explicit import"),
        "rustc's notes point at generated imports:\n{text}"
    );
}

/// A prelude crate the package does not depend on fails the glob import in every generated file. The check reads `Cargo.toml` and says so on the `telar.toml` line; rustc's copies of it land on the same line in the same words, so there is one error.
#[test]
fn a_prelude_crate_that_is_not_a_dependency_is_one_error_on_its_telar_toml_line() {
    let (root, generated) = transpiled_package(
        "undeclared",
        "[package]\nname = \"app\"\n\n[dependencies]\ntelar = \"0.2\"\n",
        "[telar]\nprelude = [\"telar-components\"]\n",
        "[view]\ntext \"hi\"\n",
    );
    let stream = rustc_error(
        &root,
        &generated,
        "E0432",
        "unresolved import `telar_components`",
        "telar_components",
        serde_json::json!([{ "level": "help", "message": "you might be missing a crate named `telar_components`", "spans": [], "children": [] }]),
    );

    let mut report = collect(stream.as_bytes());
    report.add_prelude_problems(&telar_project::prelude_problems(&root).unwrap());
    let text = report.render(false);
    std::fs::remove_dir_all(&root).ok();

    assert_eq!(text.matches("error:").count(), 1, "{text}");
    assert!(
        text.contains("error: `[telar] prelude` entry `telar_components` names `telar_components`, which is not a dependency of `app`"),
        "{text}"
    );
    assert!(points_at(&text, "telar.toml:2"), "{text}");
    assert!(
        text.contains("\n  |            ^^^^^^^^^^^^^^^^^^\n"),
        "the entry is underlined:\n{text}"
    );
    assert!(text.contains("cargo add -p app telar-components"), "{text}");
    assert!(!text.contains(".telar/"), "{text}");
}

/// An included crate the application does not depend on is said on the `telar.toml` line, rather than as an unresolved path inside what `app!` expands to.
#[test]
fn a_previews_include_that_is_not_a_dependency_is_an_error_on_its_telar_toml_line() {
    let root = std::env::temp_dir().join(format!("telar_diag_include_{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&root);
    std::fs::create_dir_all(&root).unwrap();
    std::fs::write(
        root.join("Cargo.toml"),
        "[package]\nname = \"app\"\n\n[dependencies]\ntelar = \"0.2\"\n",
    )
    .unwrap();
    std::fs::write(
        root.join("telar.toml"),
        "[telar.previews]\ninclude = [\"telar-components\"]\n",
    )
    .unwrap();

    let mut report = Report::default();
    report.add_previews_include_problems(&telar_project::previews_include_problems(&root).unwrap());
    let text = report.render(false);
    std::fs::remove_dir_all(&root).ok();

    assert!(report.has_errors(), "{text}");
    assert!(
        text.contains("error: `[telar.previews] include` entry `telar_components` is not a dependency of `app`"),
        "{text}"
    );
    assert!(points_at(&text, "telar.toml:2"), "{text}");
    assert!(text.contains("cargo add -p app telar-components"), "{text}");
}

/// A crate the package does depend on, and a path inside it that does not exist: only rustc can tell, and its answer still belongs on the key.
#[test]
fn a_prelude_path_that_does_not_resolve_is_reported_on_its_telar_toml_line() {
    let (root, generated) = transpiled_package(
        "unresolved_path",
        DEPENDS_ON_COMPONENTS,
        "[telar]\nprelude = [\"telar_components::prelud\"]\n",
        "[view]\ntext \"hi\"\n",
    );
    let stream = rustc_error(
        &root,
        &generated,
        "E0432",
        "unresolved import `telar_components::prelud`",
        "prelud::*",
        serde_json::json!([]),
    );

    let text = collect(stream.as_bytes()).render(false);
    std::fs::remove_dir_all(&root).ok();

    assert!(
        text.contains("error: `[telar] prelude` entry `telar_components::prelud` does not resolve: unresolved import `telar_components::prelud`"),
        "{text}"
    );
    assert!(points_at(&text, "telar.toml:2"), "{text}");
}

/// The map `cargo telar check` projects through is the transpiler's, beside the build file; the analyzer keeps its live map in memory, so nothing else on disk is consulted.
#[test]
fn origin_reads_the_transpilers_map_beside_the_build_file() {
    let built = SourceMap::new(vec![Some(0), Some(1)], Vec::new());
    let root = fake_package("origin_map", "a\nb\n", "x\ny\n", built.clone());

    let origin = Origin::read(&root.join(".telar/build-hot/home.rs")).unwrap();

    assert_eq!(origin.map.to_json(), built.to_json());
    assert!(!root.join(".telar/live").exists());
    let _ = std::fs::remove_dir_all(&root);
}