devscout-rs 0.8.0

Fast code index for C# and TypeScript codebases -- map, find, refs, impact from the CLI.
Documentation
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
use super::*;
use std::collections::HashMap;
use std::fs;

// --- The v13 cache generation --------------------------

#[test]
fn rebuild_graph_writes_the_v13_caches_and_deletes_every_superseded_generation() {
    let dir = temp_dir("rebuild-v13");
    fs::create_dir_all(graph_dir(&dir)).unwrap();
    // The v9 pair joined this list when fragments gained def
    // `typeParams`/`baseGenericArgs` and the `ctor-param` ref kind, so a v9
    // fragment read back today carries none and every ctor-DI fact would be
    // missing from the graph. The v10 pair joined when a markup
    // fragment gained the `x:Class` def and its element/binding refs, so a
    // v10 markup fragment read back carries names only and every XAML
    // declaration and instantiation would be missing from the graph. The v11
    // pair joined when a `.ts/.tsx/.js/.jsx` file gained a reference
    // fragment where it previously had none at all, so a v11 cache read back
    // would leave every TS/JS rel looking like a file the worker never saw.
    // This side records no TS fragment yet, but the cache generation is
    // shared -- two writers putting two generations into one git dir would
    // have each delete the other's cache on every map. The v12 pair joined
    // when defs gained propertyTypes and refs
    // gained receiverPropertyOwner and the receiverCallOwner/
    // receiverCallMember pair, so a v12 fragment read back carries none and
    // every property hop and every var-from-invocation receiver would
    // silently stay unresolved.
    assert_eq!(SUPERSEDED_CACHE_FILES.len(), 44, "v1..v22 pairs");
    for stale in SUPERSEDED_CACHE_FILES {
        fs::write(graph_dir(&dir).join(stale), b"{}").unwrap();
    }

    let fragment = Fragment {
        defs: vec![FragDef {
            id: "App.A".into(),
            name: "A".into(),
            namespace: "App".into(),
            kind: "class".into(),
            line: 1,
            methods: vec![],
            properties: vec![],
            fields: vec![],
            method_returns: OrderedMap::new(),
            extension_methods: vec![],
            bases: vec![],
            type_params: vec![],
            base_generic_args: OrderedMap::new(),
            test_methods: vec![],
            property_types: OrderedMap::new(),
            field_types: OrderedMap::new(),
            method_return_args: OrderedMap::new(),
            non_public_methods: vec![],
            method_arities: OrderedMap::new(),
            method_params: OrderedMap::new(),
            override_methods: vec![],
            base_type_args: OrderedMap::new(),
            property_message_args: Vec::new(),
            array_message_bases: Vec::new(),
            end_line: 1,
        }],
        usings: vec![],
        refs: vec![],
        names: vec![],
        registrations: vec![],
        publishes: vec![],
        handler_registrations: vec![],
    };
    let mut fresh = HashMap::new();
    fresh.insert("src/A.cs".to_string(), AnyFragment::Cs(fragment));
    let graph_files = vec![GraphFile {
        rel: "src/A.cs".to_string(),
        mtime: 222,
    }];
    rebuild_graph(&dir, &graph_files, &fresh, true, None, None).unwrap();

    assert!(
        graph_dir(&dir).join("fragments-v23.json").exists(),
        "the v23 payload cache is what gets written"
    );
    assert!(
        graph_dir(&dir).join("fragments-index-v23.json").exists(),
        "and its mtime-only index alongside it"
    );
    for stale in SUPERSEDED_CACHE_FILES {
        assert!(
            !graph_dir(&dir).join(stale).exists(),
            "{stale} must be deleted -- rename IS the invalidation"
        );
    }
}

// --- The v23 cache generation --------------------------

#[test]
fn fragments_cache_v23_supersedes_v22() {
    let dir = temp_dir("fragments-cache-v23-paths");
    assert_eq!(
        fragments_cache_path(&dir),
        graph_dir(&dir).join("fragments-v23.json")
    );
    assert_eq!(
        fragments_index_path(&dir),
        graph_dir(&dir).join("fragments-index-v23.json")
    );
    assert!(
        SUPERSEDED_CACHE_FILES.contains(&"fragments-v22.json"),
        "v22 joined the superseded list when the v23 bump landed"
    );
    assert!(
        SUPERSEDED_CACHE_FILES.contains(&"fragments-index-v22.json"),
        "its index pairs with it, same as every other generation"
    );

    let dir = temp_dir("rebuild-v23");
    fs::create_dir_all(graph_dir(&dir)).unwrap();
    fs::write(graph_dir(&dir).join("fragments-v22.json"), b"{}").unwrap();
    fs::write(graph_dir(&dir).join("fragments-index-v22.json"), b"{}").unwrap();

    let fragment = Fragment {
        defs: vec![],
        usings: vec![],
        refs: vec![],
        names: vec![],
        registrations: vec![],
        publishes: vec![],
        handler_registrations: vec![],
    };
    let mut fresh = HashMap::new();
    fresh.insert("src/A.cs".to_string(), AnyFragment::Cs(fragment));
    let graph_files = vec![GraphFile {
        rel: "src/A.cs".to_string(),
        mtime: 1,
    }];
    rebuild_graph(&dir, &graph_files, &fresh, true, None, None).unwrap();

    assert!(graph_dir(&dir).join("fragments-v23.json").exists());
    assert!(graph_dir(&dir).join("fragments-index-v23.json").exists());
    assert!(
        !graph_dir(&dir).join("fragments-v22.json").exists(),
        "the v22 pair is deleted -- rename IS the invalidation"
    );
    assert!(!graph_dir(&dir).join("fragments-index-v16.json").exists());
}

// --- The rename as the bus-hop invalidation ------------------------------

// Cache payloads here are built as JSON rather than as struct literals
// because the entry under test is one an OLDER generation wrote: it has to
// be missing a field the current type has, which no literal can express.
fn cs_def(id: &str, name: &str) -> serde_json::Value {
    serde_json::json!({
        "id": id,
        "name": name,
        "namespace": "App.Bus",
        "kind": "class",
        "line": 1,
        "methods": [],
    })
}

fn message_and_consumer() -> Vec<(String, AnyFragment)> {
    let consumer = serde_json::json!({
        "id": "App.Bus.Handler",
        "name": "Handler",
        "namespace": "App.Bus",
        "kind": "class",
        "line": 1,
        "methods": [],
        "bases": ["IConsumer"],
        "baseGenericArgs": {"IConsumer": ["LoanRequested"]},
    });
    vec![
        (
            "Bus/Messages.cs".to_string(),
            cs_fragment(serde_json::json!({
                "defs": [cs_def("App.Bus.LoanRequested", "LoanRequested")],
                "usings": [],
                "refs": [],
            })),
        ),
        (
            "Bus/Consumers.cs".to_string(),
            cs_fragment(serde_json::json!({
                "defs": [consumer],
                "usings": [],
                "refs": [],
            })),
        ),
    ]
}

fn publisher_with_publish() -> serde_json::Value {
    serde_json::json!({
        "defs": [cs_def("App.Bus.Publisher", "Publisher")],
        "usings": [],
        "refs": [],
        "publishes": [{
            "verb": "Publish",
            "message": "LoanRequested",
            "namespace": "App.Bus",
            "line": 10,
        }],
    })
}

// The same file as the previous generation recorded it: parsed, so its
// mtime is current and the entry looks reusable, but carrying no publish
// key at all because the field did not exist when it was written.
fn publisher_without_publishes() -> serde_json::Value {
    serde_json::json!({
        "defs": [cs_def("App.Bus.Publisher", "Publisher")],
        "usings": [],
        "refs": [],
    })
}

fn cs_fragment(value: serde_json::Value) -> AnyFragment {
    serde_json::from_value(value).unwrap()
}

fn plant_cache_pair(dir: &std::path::Path, payload: &str, index: &str, generation: &str) {
    fs::write(
        graph_dir(dir).join(format!("fragments{generation}.json")),
        payload,
    )
    .unwrap();
    fs::write(
        graph_dir(dir).join(format!("fragments-index{generation}.json")),
        index,
    )
    .unwrap();
}

fn bus_files() -> Vec<GraphFile> {
    ["Bus/Messages.cs", "Bus/Consumers.cs", "Bus/Publisher.cs"]
        .iter()
        .map(|rel| GraphFile {
            rel: (*rel).to_string(),
            mtime: 7,
        })
        .collect()
}

fn fresh_bus_fragments() -> HashMap<String, AnyFragment> {
    let mut fresh: HashMap<String, AnyFragment> = message_and_consumer().into_iter().collect();
    fresh.insert(
        "Bus/Publisher.cs".to_string(),
        cs_fragment(publisher_with_publish()),
    );
    fresh
}

fn rebuilt(dir: &std::path::Path) -> Graph {
    match rebuild_graph(dir, &bus_files(), &fresh_bus_fragments(), true, None, None).unwrap() {
        RebuildOutcome::Rebuilt(g) => g,
        RebuildOutcome::NotRebuilt => unreachable!("changed is true"),
    }
}

fn hop_count(g: &Graph) -> usize {
    g.edges
        .iter()
        .filter(|e| matches!(e, Edge::BusHop { .. }))
        .count()
}

#[test]
fn a_pre_bump_fragment_cache_cannot_leave_a_supported_hop_unfound() {
    let stale = serde_json::to_string(&serde_json::json!({
        "Bus/Publisher.cs": {"mtime": 7, "fragment": publisher_without_publishes()},
    }))
    .unwrap();
    let index = r#"{"Bus/Publisher.cs":7}"#;

    let dir = temp_dir("bus-hop-pre-bump-cache");
    fs::create_dir_all(graph_dir(&dir)).unwrap();
    plant_cache_pair(&dir, &stale, index, "-v21");
    let g = rebuilt(&dir);
    assert_eq!(
        hop_count(&g),
        1,
        "the publish fact comes from this run's extraction, never from the older generation"
    );
    assert_eq!(g.stats.edges_by_kind.bus_hop, Some(1));
    assert!(!graph_dir(&dir).join("fragments-v21.json").exists());

    // The control: the identical entry sitting at the CURRENT generation is
    // reused on its matching mtime and the hop goes missing with nothing on
    // disk to show for it. That is the loss the rename prevents, so this
    // half fails the moment the rename is reverted.
    let reused = temp_dir("bus-hop-current-generation-cache");
    fs::create_dir_all(graph_dir(&reused)).unwrap();
    plant_cache_pair(&reused, &stale, index, "-v23");
    let g = rebuilt(&reused);
    assert_eq!(hop_count(&g), 0);
    assert_eq!(g.stats.edges_by_kind.bus_hop, None);
}

#[test]
fn a_rebuild_with_no_publish_site_writes_no_bus_hop_counter_at_all() {
    let dir = temp_dir("bus-hop-counter-absent");
    let files: Vec<GraphFile> = message_and_consumer()
        .iter()
        .map(|(rel, _)| GraphFile {
            rel: rel.clone(),
            mtime: 7,
        })
        .collect();
    let fresh: HashMap<String, AnyFragment> = message_and_consumer().into_iter().collect();
    let g = match rebuild_graph(&dir, &files, &fresh, true, None, None).unwrap() {
        RebuildOutcome::Rebuilt(g) => g,
        RebuildOutcome::NotRebuilt => unreachable!("changed is true"),
    };

    assert_eq!(hop_count(&g), 0);
    assert_eq!(g.stats.edges_by_kind.bus_hop, None);
    let written = fs::read_to_string(graph_dir(&dir).join("graph.json")).unwrap();
    assert!(
        !written.contains("bus-hop"),
        "an absent key, not a zero -- a zero would move every bus-free artifact's bytes"
    );
}

// --- The v22 generation carried no array fact ----------------------------

fn array_message_and_consumers() -> Vec<(String, AnyFragment)> {
    let array_consumer = serde_json::json!({
        "id": "App.Bus.ArrayHandler",
        "name": "ArrayHandler",
        "namespace": "App.Bus",
        "kind": "class",
        "line": 1,
        "methods": [],
        "bases": ["IConsumer"],
        "baseGenericArgs": {"IConsumer": ["Foo"]},
        "arrayMessageBases": ["IConsumer"],
    });
    let single_consumer = serde_json::json!({
        "id": "App.Bus.SingleHandler",
        "name": "SingleHandler",
        "namespace": "App.Bus",
        "kind": "class",
        "line": 1,
        "methods": [],
        "bases": ["IConsumer"],
        "baseGenericArgs": {"IConsumer": ["Foo"]},
    });
    vec![
        (
            "Bus/Messages.cs".to_string(),
            cs_fragment(serde_json::json!({
                "defs": [cs_def("App.Bus.Foo", "Foo")],
                "usings": [],
                "refs": [],
            })),
        ),
        (
            "Bus/Consumers.cs".to_string(),
            cs_fragment(serde_json::json!({
                "defs": [array_consumer, single_consumer],
                "usings": [],
                "refs": [],
            })),
        ),
    ]
}

fn array_publisher_with_publish() -> serde_json::Value {
    serde_json::json!({
        "defs": [cs_def("App.Bus.Publisher", "Publisher")],
        "usings": [],
        "refs": [],
        "publishes": [{
            "verb": "Publish",
            "message": "Foo[]",
            "namespace": "App.Bus",
            "line": 10,
        }],
    })
}

// The same consumer file as a v22 cache would have recorded it: parsed, so
// its mtime is current and the entry looks reusable, but with no
// `arrayMessageBases` key at all -- v22 never wrote one, so `ArrayHandler`
// reads back indistinguishable from `SingleHandler`.
fn array_consumers_without_array_message_bases() -> serde_json::Value {
    serde_json::json!({
        "defs": [
            {
                "id": "App.Bus.ArrayHandler",
                "name": "ArrayHandler",
                "namespace": "App.Bus",
                "kind": "class",
                "line": 1,
                "methods": [],
                "bases": ["IConsumer"],
                "baseGenericArgs": {"IConsumer": ["Foo"]},
            },
            {
                "id": "App.Bus.SingleHandler",
                "name": "SingleHandler",
                "namespace": "App.Bus",
                "kind": "class",
                "line": 1,
                "methods": [],
                "bases": ["IConsumer"],
                "baseGenericArgs": {"IConsumer": ["Foo"]},
            },
        ],
        "usings": [],
        "refs": [],
    })
}

fn array_bus_files() -> Vec<GraphFile> {
    ["Bus/Messages.cs", "Bus/Consumers.cs", "Bus/Publisher.cs"]
        .iter()
        .map(|rel| GraphFile {
            rel: (*rel).to_string(),
            mtime: 7,
        })
        .collect()
}

fn fresh_array_bus_fragments() -> HashMap<String, AnyFragment> {
    let mut fresh: HashMap<String, AnyFragment> =
        array_message_and_consumers().into_iter().collect();
    fresh.insert(
        "Bus/Publisher.cs".to_string(),
        cs_fragment(array_publisher_with_publish()),
    );
    fresh
}

fn array_handler_names(g: &Graph) -> Vec<String> {
    let mut names: Vec<String> = g
        .edges
        .iter()
        .filter_map(|e| match e {
            Edge::BusHop { to, .. } => Some(
                to.rsplit_once('.')
                    .map_or(to.clone(), |(_, t)| t.to_string()),
            ),
            _ => None,
        })
        .collect();
    names.sort();
    names
}

#[test]
fn a_warm_v22_cache_is_rebuilt_rather_than_read_without_array_facts() {
    let stale = serde_json::to_string(&serde_json::json!({
        "Bus/Consumers.cs": {"mtime": 7, "fragment": array_consumers_without_array_message_bases()},
    }))
    .unwrap();
    let index = r#"{"Bus/Consumers.cs":7}"#;

    let dir = temp_dir("bus-array-warm-v22-cache");
    fs::create_dir_all(graph_dir(&dir)).unwrap();
    plant_cache_pair(&dir, &stale, index, "-v22");
    let g = match rebuild_graph(
        &dir,
        &array_bus_files(),
        &fresh_array_bus_fragments(),
        true,
        None,
        None,
    )
    .unwrap()
    {
        RebuildOutcome::Rebuilt(g) => g,
        RebuildOutcome::NotRebuilt => unreachable!("changed is true"),
    };
    assert_eq!(
        array_handler_names(&g),
        vec!["ArrayHandler".to_string()],
        "a fresh v23 extraction, never the warm v22 payload, is what decides the array \
         consumer's own identity"
    );
    assert!(!graph_dir(&dir).join("fragments-v22.json").exists());

    // The control: the SAME degraded entry, planted at the CURRENT v23
    // generation and reused on its matching mtime, loses the array bit --
    // both handlers then read as plain `Foo` consumers, so the `Foo[]`
    // publish's own identity (rank 1) matches NEITHER of them and the
    // array route silently disappears. That is the loss the rename
    // prevents, so this half fails the moment the rename is reverted.
    let reused = temp_dir("bus-array-current-generation-cache");
    fs::create_dir_all(graph_dir(&reused)).unwrap();
    plant_cache_pair(&reused, &stale, index, "-v23");
    let g = match rebuild_graph(
        &reused,
        &array_bus_files(),
        &fresh_array_bus_fragments(),
        true,
        None,
        None,
    )
    .unwrap()
    {
        RebuildOutcome::Rebuilt(g) => g,
        RebuildOutcome::NotRebuilt => unreachable!("changed is true"),
    };
    assert_eq!(
        array_handler_names(&g),
        Vec::<String>::new(),
        "reusing a degraded entry at the current generation is exactly the loss the rename \
         prevents: the array bit is gone, so the array publish's own identity (rank 1) \
         matches neither handler's now-plain (rank 0) identity, and the route silently \
         disappears rather than reaching ArrayHandler"
    );
}