blast-radius 0.4.0

Analyze the transitive blast radius of code changes.
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
515
use std::collections::{BTreeMap, HashSet};
use std::fs;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use ignore::WalkBuilder;
use jsonc_parser::{ParseOptions, parse_to_serde_value};
use serde::Deserialize;

#[derive(Debug, Clone)]
pub struct RepoContext {
    pub repo_root: PathBuf,
    pub source_files: Vec<PathBuf>,
    pub tsconfigs: Vec<TsConfigPath>,
    pub package_jsons: Vec<PathBuf>,
    /// Import-specifier substrings the repo asks not to count as unresolved
    /// (generated/virtual modules its tooling produces). From `.blast-radius.json`.
    pub ignore_unresolved: Vec<String>,
    pub warnings: Vec<String>,
}

/// Optional per-repo configuration loaded from `.blast-radius.json` at the repo
/// root. Lets a repo declare tooling-specific quirks the language-neutral core
/// shouldn't hardcode.
#[derive(Debug, Default, Deserialize)]
struct ProjectConfig {
    #[serde(default)]
    unresolved: UnresolvedConfig,
}

#[derive(Debug, Default, Deserialize)]
struct UnresolvedConfig {
    /// Import specifiers whose substring matches are not counted as unresolved.
    #[serde(default)]
    ignore: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct TsConfigPath {
    pub path: PathBuf,
    pub compiler_options: TsCompilerOptions,
}

/// Compiler options after following `extends` chains. Directory-relative
/// fields are resolved against the config file that declared them, so the
/// merged result is anchored to absolute directories.
#[derive(Debug, Clone, Default)]
pub struct TsCompilerOptions {
    /// Absolute directory `baseUrl` points at, when declared.
    pub base_dir: Option<PathBuf>,
    pub paths: BTreeMap<String, Vec<String>>,
    /// Absolute directory relative `paths` targets resolve against when no
    /// `baseUrl` is in effect: the directory of the config declaring `paths`.
    pub paths_dir: Option<PathBuf>,
}

impl TsCompilerOptions {
    pub fn has_aliases(&self) -> bool {
        self.base_dir.is_some() || !self.paths.is_empty()
    }
}

#[derive(Debug, Default, Deserialize)]
struct TsConfigFile {
    #[serde(default)]
    extends: Option<serde_json::Value>,
    #[serde(default, rename = "compilerOptions")]
    compiler_options: RawCompilerOptions,
}

#[derive(Debug, Default, Deserialize)]
struct RawCompilerOptions {
    #[serde(default, rename = "baseUrl")]
    base_url: Option<String>,
    #[serde(default)]
    paths: Option<BTreeMap<String, Vec<String>>>,
}

impl RepoContext {
    pub fn discover(repo_root: &Path) -> Result<Self> {
        let repo_root = repo_root
            .canonicalize()
            .with_context(|| format!("failed to resolve repo root {}", repo_root.display()))?;

        let mut source_files = Vec::new();
        let mut tsconfigs = Vec::new();
        let mut package_jsons = Vec::new();
        let mut warnings = Vec::new();

        let ignore_unresolved = match load_project_config(&repo_root) {
            Ok(config) => config.unresolved.ignore,
            Err(error) => {
                warnings.push(format!("{error:#}"));
                Vec::new()
            }
        };

        let walker = WalkBuilder::new(&repo_root)
            .hidden(false)
            .git_ignore(true)
            .git_exclude(true)
            .git_global(true)
            .filter_entry(|entry| {
                let name = entry.file_name().to_string_lossy();
                !matches!(
                    name.as_ref(),
                    ".git" | "node_modules" | "dist" | "build" | "coverage" | ".next" | ".turbo"
                )
            })
            .build();

        for entry in walker {
            let entry = match entry {
                Ok(entry) => entry,
                Err(error) => {
                    warnings.push(format!("skipping unreadable path: {error}"));
                    continue;
                }
            };
            if !entry.file_type().is_some_and(|kind| kind.is_file()) {
                continue;
            }

            let path = entry.into_path();
            match path.file_name().and_then(|name| name.to_str()) {
                Some("tsconfig.json") => match load_tsconfig(&path) {
                    Ok(config) => {
                        // Nx/Vite scaffolds keep aliases in a sibling config the
                        // tsconfig.json only references; pull those in when the
                        // merged options carry no aliases of their own.
                        if !config.compiler_options.has_aliases() {
                            tsconfigs.extend(load_sibling_tsconfigs(&path, &mut warnings));
                        }
                        tsconfigs.push(config);
                    }
                    Err(error) => warnings.push(format!("{error:#}")),
                },
                Some("package.json") => package_jsons.push(path.clone()),
                _ => {}
            }

            if is_source_file(&path) {
                source_files.push(path);
            }
        }

        source_files.sort();
        tsconfigs.sort_by(|a, b| a.path.cmp(&b.path));
        package_jsons.sort();

        Ok(Self {
            repo_root,
            source_files,
            tsconfigs,
            package_jsons,
            ignore_unresolved,
            warnings,
        })
    }
}

fn load_project_config(repo_root: &Path) -> Result<ProjectConfig> {
    let path = repo_root.join(".blast-radius.json");
    let contents = match fs::read_to_string(&path) {
        Ok(contents) => contents,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
            return Ok(ProjectConfig::default());
        }
        Err(error) => {
            return Err(anyhow::Error::new(error)
                .context(format!("failed to read config {}", path.display())));
        }
    };

    // Parsed as JSONC (comments + trailing commas), matching tsconfig handling.
    let value: serde_json::Value = parse_to_serde_value(
        &contents,
        &ParseOptions {
            allow_comments: true,
            allow_loose_object_property_names: false,
            allow_trailing_commas: true,
            allow_missing_commas: false,
            allow_single_quoted_strings: false,
            allow_hexadecimal_numbers: false,
            allow_unary_plus_numbers: false,
        },
    )
    .with_context(|| format!("failed to parse config {}", path.display()))?;

    serde_json::from_value(value)
        .with_context(|| format!("failed to decode config {}", path.display()))
}

fn load_tsconfig(path: &Path) -> Result<TsConfigPath> {
    let mut visited = HashSet::new();
    Ok(TsConfigPath {
        path: path.to_path_buf(),
        compiler_options: load_tsconfig_options(path, &mut visited)?,
    })
}

/// Load a config's options after following its `extends` chain. Child fields
/// shallow-override parents; `paths` replaces wholesale when redeclared.
fn load_tsconfig_options(path: &Path, visited: &mut HashSet<PathBuf>) -> Result<TsCompilerOptions> {
    let canonical = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
    if !visited.insert(canonical) {
        // `extends` cycle: treat the revisited config as empty.
        return Ok(TsCompilerOptions::default());
    }

    let contents = fs::read_to_string(path)
        .with_context(|| format!("failed to read tsconfig {}", path.display()))?;
    let value: serde_json::Value = parse_to_serde_value(
        &contents,
        &ParseOptions {
            allow_comments: true,
            allow_loose_object_property_names: false,
            allow_trailing_commas: true,
            allow_missing_commas: false,
            allow_single_quoted_strings: false,
            allow_hexadecimal_numbers: false,
            allow_unary_plus_numbers: false,
        },
    )
    .with_context(|| format!("failed to parse tsconfig {}", path.display()))?;
    let parsed: TsConfigFile = serde_json::from_value(value)
        .with_context(|| format!("failed to decode tsconfig {}", path.display()))?;

    let dir = path.parent().unwrap_or(Path::new("."));
    let mut merged = TsCompilerOptions::default();
    for specifier in extends_specifiers(&parsed.extends) {
        // Bare package specifiers (e.g. @tsconfig/node18) live in node_modules,
        // which isn't indexed; skip them silently.
        let Some(parent_path) = resolve_extends_target(dir, &specifier) else {
            continue;
        };
        let parent = load_tsconfig_options(&parent_path, visited)
            .with_context(|| format!("failed to load extended tsconfig from {}", path.display()))?;
        if parent.base_dir.is_some() {
            merged.base_dir = parent.base_dir;
        }
        if parent.paths_dir.is_some() {
            merged.paths = parent.paths;
            merged.paths_dir = parent.paths_dir;
        }
    }

    if let Some(base_url) = parsed.compiler_options.base_url {
        merged.base_dir = Some(crate::resolve::clean_path(&dir.join(base_url)));
    }
    if let Some(paths) = parsed.compiler_options.paths {
        merged.paths = paths;
        merged.paths_dir = Some(dir.to_path_buf());
    }

    Ok(merged)
}

fn extends_specifiers(value: &Option<serde_json::Value>) -> Vec<String> {
    match value {
        Some(serde_json::Value::String(specifier)) => vec![specifier.clone()],
        Some(serde_json::Value::Array(items)) => items
            .iter()
            .filter_map(|item| item.as_str().map(str::to_string))
            .collect(),
        _ => Vec::new(),
    }
}

/// Map an `extends` specifier to a config file on disk: `./`/`../` specifiers
/// and plain sibling filenames resolve relative to `dir` (with `.json` appended
/// when missing, as TypeScript does); anything else is a package specifier.
fn resolve_extends_target(dir: &Path, specifier: &str) -> Option<PathBuf> {
    let candidate = dir.join(specifier);
    if candidate.is_file() {
        return Some(candidate);
    }
    if !specifier.ends_with(".json") {
        let mut with_json = candidate.into_os_string();
        with_json.push(".json");
        let with_json = PathBuf::from(with_json);
        if with_json.is_file() {
            return Some(with_json);
        }
    }
    None
}

/// Probe well-known sibling configs that hold path aliases in Nx and Vite
/// scaffold layouts, where tsconfig.json itself declares none.
fn load_sibling_tsconfigs(tsconfig: &Path, warnings: &mut Vec<String>) -> Vec<TsConfigPath> {
    let Some(dir) = tsconfig.parent() else {
        return Vec::new();
    };

    let mut configs = Vec::new();
    for name in ["tsconfig.base.json", "tsconfig.app.json"] {
        let path = dir.join(name);
        if !path.is_file() {
            continue;
        }
        match load_tsconfig(&path) {
            Ok(config) if config.compiler_options.has_aliases() => configs.push(config),
            Ok(_) => {}
            Err(error) => warnings.push(format!("{error:#}")),
        }
    }
    configs
}

fn is_source_file(path: &Path) -> bool {
    path.extension()
        .and_then(|ext| ext.to_str())
        .is_some_and(crate::language::is_source_extension)
}

#[cfg(test)]
mod tests {
    use std::fs;

    use tempfile::tempdir;

    use super::RepoContext;

    #[test]
    fn discovers_source_files_and_tsconfig() {
        let dir = tempdir().unwrap();
        fs::create_dir_all(dir.path().join("src")).unwrap();
        fs::write(
            dir.path().join("tsconfig.json"),
            r#"{"compilerOptions":{"baseUrl":".","paths":{"@ui/*":["packages/ui/*"]}}}"#,
        )
        .unwrap();
        fs::write(
            dir.path().join("src").join("Button.tsx"),
            "export const Button = () => null;",
        )
        .unwrap();
        fs::write(
            dir.path().join("src").join("legacy.mjs"),
            "export const legacy = true;",
        )
        .unwrap();
        fs::write(dir.path().join("src").join("server.cts"), "export = {};").unwrap();
        fs::write(
            dir.path().join("src").join("helper.py"),
            "def helper(): pass",
        )
        .unwrap();
        fs::write(dir.path().join("src").join("lib.rs"), "pub fn helper() {}").unwrap();
        fs::write(
            dir.path().join("src").join("Button.vue"),
            "<script setup>import x from './x'</script>",
        )
        .unwrap();
        fs::write(
            dir.path().join("src").join("Card.svelte"),
            "<script>import x from './x'</script>",
        )
        .unwrap();
        fs::write(dir.path().join("package.json"), r#"{"name":"fixture"}"#).unwrap();

        let repo = RepoContext::discover(dir.path()).unwrap();

        let mut expected = 3;
        if cfg!(feature = "python") {
            expected += 1;
        }
        if cfg!(feature = "rust") {
            expected += 1;
        }
        if cfg!(feature = "vue") {
            expected += 1;
        }
        if cfg!(feature = "svelte") {
            expected += 1;
        }
        assert_eq!(repo.source_files.len(), expected);
        assert_eq!(repo.tsconfigs.len(), 1);
        assert_eq!(repo.package_jsons.len(), 1);
    }

    #[test]
    fn loads_ignore_unresolved_from_project_config() {
        let dir = tempdir().unwrap();
        fs::create_dir_all(dir.path().join("src")).unwrap();
        fs::write(
            dir.path().join("src").join("App.tsx"),
            "export const App = () => null;",
        )
        .unwrap();
        fs::write(
            dir.path().join(".blast-radius.json"),
            r#"{ "unresolved": { "ignore": ["styled-system/css", ".velite"] } }"#,
        )
        .unwrap();

        let repo = RepoContext::discover(dir.path()).unwrap();

        assert_eq!(
            repo.ignore_unresolved,
            vec!["styled-system/css".to_string(), ".velite".to_string()]
        );
    }

    #[test]
    fn defaults_ignore_unresolved_when_config_absent() {
        let dir = tempdir().unwrap();
        fs::create_dir_all(dir.path().join("src")).unwrap();
        fs::write(
            dir.path().join("src").join("App.tsx"),
            "export const App = () => null;",
        )
        .unwrap();

        let repo = RepoContext::discover(dir.path()).unwrap();

        assert!(repo.ignore_unresolved.is_empty());
    }

    #[test]
    fn reports_invalid_project_config_as_warning() {
        let dir = tempdir().unwrap();
        fs::write(dir.path().join(".blast-radius.json"), "{ not valid json").unwrap();

        let repo = RepoContext::discover(dir.path()).unwrap();

        assert!(repo.ignore_unresolved.is_empty());
        assert!(
            repo.warnings
                .iter()
                .any(|warning| warning.contains("failed to parse config")),
            "invalid config should be reported as a discovery warning"
        );
    }

    #[test]
    fn reports_invalid_tsconfig_as_warning() {
        let dir = tempdir().unwrap();
        fs::create_dir_all(dir.path().join("src")).unwrap();
        fs::write(dir.path().join("tsconfig.json"), "{ invalid json").unwrap();
        fs::write(
            dir.path().join("src").join("Button.tsx"),
            "export const Button = () => null;",
        )
        .unwrap();

        let repo = RepoContext::discover(dir.path()).unwrap();

        assert_eq!(repo.source_files.len(), 1);
        assert!(repo.tsconfigs.is_empty());
        assert!(
            repo.warnings
                .iter()
                .any(|warning| warning.contains("failed to parse tsconfig")),
            "invalid tsconfig should be reported as a discovery warning"
        );
    }

    #[cfg(feature = "python")]
    #[test]
    fn discovers_python_sources_when_enabled() {
        let dir = tempdir().unwrap();
        fs::create_dir_all(dir.path().join("src")).unwrap();
        fs::write(
            dir.path().join("src").join("helper.py"),
            "def helper(): pass",
        )
        .unwrap();

        let repo = RepoContext::discover(dir.path()).unwrap();

        assert_eq!(repo.source_files.len(), 1);
    }

    #[cfg(feature = "rust")]
    #[test]
    fn discovers_rust_sources_when_enabled() {
        let dir = tempdir().unwrap();
        fs::create_dir_all(dir.path().join("src")).unwrap();
        fs::write(dir.path().join("src").join("lib.rs"), "pub fn helper() {}").unwrap();

        let repo = RepoContext::discover(dir.path()).unwrap();

        assert_eq!(repo.source_files.len(), 1);
    }

    #[cfg(feature = "vue")]
    #[test]
    fn discovers_vue_sources_when_enabled() {
        let dir = tempdir().unwrap();
        fs::create_dir_all(dir.path().join("src")).unwrap();
        fs::write(dir.path().join("src").join("Button.vue"), "<template />").unwrap();

        let repo = RepoContext::discover(dir.path()).unwrap();

        assert_eq!(repo.source_files.len(), 1);
    }

    #[cfg(feature = "svelte")]
    #[test]
    fn discovers_svelte_sources_when_enabled() {
        let dir = tempdir().unwrap();
        fs::create_dir_all(dir.path().join("src")).unwrap();
        fs::write(
            dir.path().join("src").join("Card.svelte"),
            "<script></script>",
        )
        .unwrap();

        let repo = RepoContext::discover(dir.path()).unwrap();

        assert_eq!(repo.source_files.len(), 1);
    }
}