tfparser-core 0.1.0

Library for parsing Terraform / Terragrunt source repositories into a queryable IR (Parquet-backed).
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
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//! [`FsDiscoverer`]: walks the real filesystem with `ignore::WalkBuilder`,
//! groups files by directory, runs the shallow probe + classifier from
//! [`super::classifier`], and produces a deterministic [`Discovered`].

use std::{
    collections::{BTreeMap, BTreeSet},
    path::{Path, PathBuf},
    sync::Arc,
};

use ignore::WalkBuilder;
use tracing::{debug, instrument, warn};

use super::{
    Discovered, DiscoveredDir, DiscoveredFile, Discoverer, DiscoveryOptions,
    classifier::{FileSignals, classify, probe_file},
    types::DirKind,
};
use crate::{
    Diagnostic, Error, LimitKind, Result, Severity,
    diagnostic::Diagnostic as Diag,
    ir::FileExt,
    util::paths::{self, SymlinkPolicy},
};

/// Default [`Discoverer`] implementation.
///
/// Stateless and `Send + Sync`; cheap to construct (no fields). The
/// per-walk caps and excludes are supplied via [`DiscoveryOptions`].
#[derive(Clone, Copy, Debug, Default)]
pub struct FsDiscoverer;

impl FsDiscoverer {
    /// Construct an [`FsDiscoverer`].
    #[must_use]
    pub const fn new() -> Self {
        Self
    }
}

impl Discoverer for FsDiscoverer {
    #[instrument(level = "debug", skip(self, opts), fields(root = %root.display()))]
    fn discover(&self, root: &Path, opts: &DiscoveryOptions) -> Result<Discovered> {
        run_discovery(root, opts)
    }
}

/// Internal entry point — separates the trait dispatch from the actual logic
/// so we can call it from tests without going through the trait.
fn run_discovery(root: &Path, opts: &DiscoveryOptions) -> Result<Discovered> {
    let canonical_root = canonicalize_root(root, opts)?;
    let WalkOutput {
        groups,
        seen_dirs,
        mut diagnostics,
    } = walk_workspace(&canonical_root, opts)?;
    let mut classified =
        classify_dirs(&canonical_root, opts, &groups, &seen_dirs, &mut diagnostics);
    classified
        .components
        .sort_by(|a, b| a.path.as_os_str().cmp(b.path.as_os_str()));
    classified
        .modules
        .sort_by(|a, b| a.path.as_os_str().cmp(b.path.as_os_str()));
    let root_hcl = find_root_hcl(&canonical_root);
    Ok(Discovered {
        root: Arc::from(canonical_root.as_path()),
        components: classified.components,
        modules: classified.modules,
        envs_dir: classified.envs_dir,
        root_hcl,
        diagnostics,
    })
}

struct WalkOutput {
    groups: BTreeMap<PathBuf, Vec<DiscoveredFile>>,
    seen_dirs: BTreeSet<PathBuf>,
    diagnostics: Vec<Diagnostic>,
}

fn walk_workspace(canonical_root: &Path, opts: &DiscoveryOptions) -> Result<WalkOutput> {
    let mut state = WalkState::new();
    let walker = build_walker(canonical_root, opts);
    for result in walker {
        match result {
            Ok(entry) => process_walk_entry(canonical_root, opts, &entry, &mut state)?,
            Err(err) => state.diagnostics.push(walk_error_to_diagnostic(&err)),
        }
    }
    Ok(WalkOutput {
        groups: state.groups,
        seen_dirs: state.seen_dirs,
        diagnostics: state.diagnostics,
    })
}

struct WalkState {
    groups: BTreeMap<PathBuf, Vec<DiscoveredFile>>,
    seen_dirs: BTreeSet<PathBuf>,
    diagnostics: Vec<Diagnostic>,
    total_files: u64,
}

impl WalkState {
    fn new() -> Self {
        let mut seen_dirs = BTreeSet::new();
        seen_dirs.insert(PathBuf::new());
        Self {
            groups: BTreeMap::new(),
            seen_dirs,
            diagnostics: Vec::new(),
            total_files: 0,
        }
    }
}

fn process_walk_entry(
    canonical_root: &Path,
    opts: &DiscoveryOptions,
    entry: &ignore::DirEntry,
    state: &mut WalkState,
) -> Result<()> {
    if entry.depth() == 0 {
        return Ok(());
    }
    let entry_path = entry.path().to_path_buf();
    let Ok(rel) = entry_path.strip_prefix(canonical_root) else {
        state.diagnostics.push(Diag::new(
            Severity::Warn,
            "TF1101",
            format!(
                "dropping entry outside workspace root: {}",
                entry_path.display()
            ),
        ));
        return Ok(());
    };
    let rel_path = rel.to_path_buf();
    if opts.exclude_globs.is_match(&rel_path) {
        return Ok(());
    }
    let Some(file_type) = entry.file_type() else {
        return Ok(());
    };
    if file_type.is_dir() {
        state.seen_dirs.insert(rel_path);
        return Ok(());
    }
    if !file_type.is_file() {
        if file_type.is_symlink() {
            state.diagnostics.push(Diag::new(
                Severity::Trace,
                "TF1110",
                format!("skipped symlink: {}", rel_path.display()),
            ));
        }
        return Ok(());
    }
    let metadata = match entry.metadata() {
        Ok(m) => m,
        Err(err) => {
            state.diagnostics.push(Diag::new(
                Severity::Warn,
                "TF1102",
                format!("metadata error for {}: {err}", rel_path.display()),
            ));
            return Ok(());
        }
    };
    let size = metadata.len();
    if size > opts.max_file_size_bytes {
        state.diagnostics.push(Diag::limit(
            LimitKind::FileSize,
            "TF1103",
            format!(
                "file exceeds size limit and was skipped: {} ({size} > {})",
                rel_path.display(),
                opts.max_file_size_bytes
            ),
        ));
        return Ok(());
    }
    let Some(ext) = FileExt::classify(&rel_path) else {
        return Ok(());
    };
    state.total_files = state.total_files.checked_add(1).ok_or(Error::Limit {
        kind: LimitKind::TotalFiles,
        observed: u64::MAX,
        limit: opts.max_total_files,
    })?;
    if state.total_files > opts.max_total_files {
        return Err(Error::Limit {
            kind: LimitKind::TotalFiles,
            observed: state.total_files,
            limit: opts.max_total_files,
        });
    }
    let parent_rel = rel_path
        .parent()
        .map_or_else(PathBuf::new, Path::to_path_buf);
    state.seen_dirs.insert(parent_rel.clone());
    let file = DiscoveredFile {
        path: Arc::from(rel_path.as_path()),
        ext,
        size,
    };
    state.groups.entry(parent_rel).or_default().push(file);
    Ok(())
}

struct Classified {
    components: Vec<DiscoveredDir>,
    modules: Vec<DiscoveredDir>,
    envs_dir: Option<Arc<Path>>,
}

fn classify_dirs(
    canonical_root: &Path,
    opts: &DiscoveryOptions,
    groups: &BTreeMap<PathBuf, Vec<DiscoveredFile>>,
    seen_dirs: &BTreeSet<PathBuf>,
    diagnostics: &mut Vec<Diagnostic>,
) -> Classified {
    let mut components: Vec<DiscoveredDir> = Vec::new();
    let mut modules: Vec<DiscoveredDir> = Vec::new();
    let mut envs_dir: Option<Arc<Path>> = None;
    for rel_dir in seen_dirs {
        let files = groups.get(rel_dir).cloned().unwrap_or_default();
        let signals = aggregate_signals(canonical_root, &files, opts.max_file_size_bytes);
        let is_workspace_envs_dir = is_workspace_environments_dir(rel_dir);
        let (kind, reason, ambiguous) =
            classify(rel_dir, signals, &files, opts, is_workspace_envs_dir);
        if ambiguous {
            diagnostics.push(Diag::new(
                Severity::Info,
                "TF1120",
                format!(
                    "directory is ambiguous (matches both component and module heuristics); \
                     component classification kept: {}",
                    rel_dir.display()
                ),
            ));
        }
        let dir_arc: Arc<Path> = Arc::from(rel_dir.as_path());
        match kind {
            DirKind::Component => {
                components.push(DiscoveredDir::new(dir_arc, kind, reason, files));
            }
            DirKind::Module => {
                modules.push(DiscoveredDir::new(dir_arc, kind, reason, files));
            }
            DirKind::Environments => {
                envs_dir = Some(dir_arc);
            }
            DirKind::Files | DirKind::Other => {
                debug!(?rel_dir, ?kind, "discovery: non-component dir");
            }
        }
    }
    Classified {
        components,
        modules,
        envs_dir,
    }
}

fn find_root_hcl(canonical_root: &Path) -> Option<Arc<Path>> {
    for candidate_rel in [Path::new("root.hcl"), Path::new("terraform/root.hcl")] {
        if canonical_root.join(candidate_rel).is_file() {
            return Some(Arc::from(candidate_rel));
        }
    }
    None
}

fn canonicalize_root(root: &Path, opts: &DiscoveryOptions) -> Result<PathBuf> {
    let policy = if opts.follow_symlinks {
        SymlinkPolicy::Follow
    } else {
        SymlinkPolicy::Reject
    };
    paths::reject_nul(root).map_err(|_| Error::Io {
        path: root.to_path_buf(),
        source: std::io::Error::new(std::io::ErrorKind::InvalidInput, "path contains NUL"),
    })?;

    if !root.exists() {
        return Err(Error::Io {
            path: root.to_path_buf(),
            source: std::io::Error::new(std::io::ErrorKind::NotFound, "workspace root not found"),
        });
    }
    let canonical = std::fs::canonicalize(root).map_err(|source| Error::Io {
        path: root.to_path_buf(),
        source,
    })?;

    // Final safety — running through the same canonicalizer the rest of the
    // pipeline uses ensures the helpers agree on what "inside" means.
    let resolved =
        paths::canonicalize_inside(&canonical, &canonical, policy).map_err(|err| match err {
            paths::PathSafetyError::Io { path, source } => Error::Io { path, source },
            other => Error::Io {
                path: canonical.clone(),
                source: std::io::Error::new(
                    std::io::ErrorKind::PermissionDenied,
                    other.to_string(),
                ),
            },
        })?;
    Ok(resolved)
}

fn build_walker(root: &Path, opts: &DiscoveryOptions) -> ignore::Walk {
    let mut builder = WalkBuilder::new(root);
    builder
        .follow_links(opts.follow_symlinks)
        .max_depth(Some(opts.max_depth as usize))
        // We do NOT call WalkBuilder::max_filesize because that drops the
        // entry silently. Discovery's contract is to *surface* over-cap
        // files as diagnostics so the operator knows what was skipped, so
        // the size check happens in the consumer loop below instead.
        .standard_filters(true)
        .git_ignore(true)
        .git_exclude(true)
        .git_global(true)
        .require_git(false)
        .hidden(true);

    // The `ignore` crate's WalkBuilder takes overrides (gitignore-style)
    // but not a `GlobSet` directly. The exclude_globs from
    // `DiscoveryOptions` are applied per-entry in the consumer loop.
    builder.build()
}

/// Probe every HCL-shaped file in `files`, capping per-file work at
/// `max_file_bytes` (so a 4 MiB `.tf` does not bloat discovery time).
fn aggregate_signals(root: &Path, files: &[DiscoveredFile], max_file_bytes: u64) -> FileSignals {
    let mut signals = FileSignals::default();
    for file in files {
        if !file.ext.is_hcl() {
            continue;
        }
        if file.size > max_file_bytes {
            // Already surfaced as a Diagnostic in the walk loop; skip the
            // probe to avoid re-tripping the cap.
            continue;
        }
        let abs = root.join(&*file.path);
        let bytes = match std::fs::read(&abs) {
            Ok(b) => b,
            Err(err) => {
                warn!(path = %abs.display(), error = %err, "discovery: probe read failed");
                continue;
            }
        };
        signals.merge(probe_file(&bytes));
    }
    signals
}

/// `<workspace_root>/environments` (no deeper). Spec § 3.2.
fn is_workspace_environments_dir(rel: &Path) -> bool {
    let mut comps = rel.components();
    let first = comps.next();
    let second = comps.next();
    matches!(first, Some(c) if c.as_os_str() == "environments") && second.is_none()
}

fn walk_error_to_diagnostic(err: &ignore::Error) -> Diagnostic {
    Diag::new(Severity::Warn, "TF1100", format!("walk error: {err}"))
}

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    clippy::expect_used,
    clippy::panic,
    clippy::indexing_slicing
)]
mod tests {
    use std::{fs, path::PathBuf};

    use tempfile::tempdir;

    use super::{super::classifier::ClassificationReason, *};
    use crate::ir::FileExt;

    fn write(root: &Path, rel: &str, content: &str) {
        let path = root.join(rel);
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).unwrap();
        }
        fs::write(path, content).unwrap();
    }

    #[test]
    fn test_should_discover_single_component() {
        let tmp = tempdir().unwrap();
        let root = tmp.path();
        write(
            root,
            "services/api/main.tf",
            "resource \"aws_iam_role\" \"r\" {\n  name = \"x\"\n}\n",
        );
        let opts = DiscoveryOptions::defaults();
        let d = FsDiscoverer.discover(root, &opts).unwrap();
        assert_eq!(d.components.len(), 1, "{d:?}");
        assert_eq!(&*d.components[0].path, Path::new("services/api"));
        assert_eq!(d.components[0].reason, ClassificationReason::HasResources);
        assert!(d.modules.is_empty());
    }

    #[test]
    fn test_should_discover_module_via_glob() {
        let tmp = tempdir().unwrap();
        let root = tmp.path();
        write(
            root,
            "modules/iam-role/variables.tf",
            "variable \"name\" {\n  type = string\n}\n",
        );
        let opts = DiscoveryOptions::defaults();
        let d = FsDiscoverer.discover(root, &opts).unwrap();
        assert_eq!(d.modules.len(), 1, "{d:?}");
        assert_eq!(d.modules[0].kind, DirKind::Module);
    }

    #[test]
    fn test_should_discover_terragrunt_component_with_include() {
        let tmp = tempdir().unwrap();
        let root = tmp.path();
        write(
            root,
            "services/api/terragrunt.hcl",
            "include \"root\" {\n  path = find_in_parent_folders()\n}\n",
        );
        let opts = DiscoveryOptions::defaults();
        let d = FsDiscoverer.discover(root, &opts).unwrap();
        assert_eq!(d.components.len(), 1);
        assert_eq!(
            d.components[0].reason,
            ClassificationReason::TerragruntInclude
        );
    }

    #[test]
    fn test_should_skip_files_larger_than_cap() {
        let tmp = tempdir().unwrap();
        let root = tmp.path();
        let big = "a".repeat(100);
        write(root, "svc/main.tf", &format!("{big}\n"));
        let opts: DiscoveryOptions = DiscoveryOptions::builder()
            .max_file_size_bytes(50_u64)
            .build();
        let d = FsDiscoverer.discover(root, &opts).unwrap();
        assert!(
            d.diagnostics.iter().any(|x| x.code.as_ref() == "TF1103"),
            "{d:?}"
        );
    }

    #[test]
    fn test_should_enforce_total_file_cap() {
        let tmp = tempdir().unwrap();
        let root = tmp.path();
        for i in 0..5 {
            write(root, &format!("svc/f{i}.tf"), "");
        }
        let opts: DiscoveryOptions = DiscoveryOptions::builder().max_total_files(3_u64).build();
        let err = FsDiscoverer.discover(root, &opts).unwrap_err();
        assert!(matches!(
            err,
            Error::Limit {
                kind: LimitKind::TotalFiles,
                ..
            }
        ));
    }

    #[test]
    fn test_should_detect_environments_dir() {
        let tmp = tempdir().unwrap();
        let root = tmp.path();
        write(root, "environments/staging.tfvars", "");
        let opts = DiscoveryOptions::defaults();
        let d = FsDiscoverer.discover(root, &opts).unwrap();
        assert!(d.envs_dir.is_some(), "{d:?}");
    }

    #[test]
    fn test_should_detect_root_hcl() {
        let tmp = tempdir().unwrap();
        let root = tmp.path();
        write(root, "root.hcl", "remote_state {}\n");
        let opts = DiscoveryOptions::defaults();
        let d = FsDiscoverer.discover(root, &opts).unwrap();
        assert_eq!(d.root_hcl.as_deref(), Some(Path::new("root.hcl")));
    }

    #[test]
    fn test_should_reject_missing_root() {
        let tmp = tempdir().unwrap();
        let missing = tmp.path().join("not-here");
        let err = FsDiscoverer
            .discover(&missing, &DiscoveryOptions::defaults())
            .unwrap_err();
        assert!(matches!(err, Error::Io { .. }));
    }

    #[test]
    fn test_should_classify_environments_dir_only_at_root_level() {
        let tmp = tempdir().unwrap();
        let root = tmp.path();
        write(root, "platform/environments/x.tfvars", "");
        let opts = DiscoveryOptions::defaults();
        let d = FsDiscoverer.discover(root, &opts).unwrap();
        assert!(d.envs_dir.is_none(), "nested env dir should not match");
    }

    #[test]
    fn test_should_emit_ambiguity_diag_for_component_in_modules_glob() {
        let tmp = tempdir().unwrap();
        let root = tmp.path();
        write(
            root,
            "modules/strange/terragrunt.hcl",
            "include \"root\" {\n  path = find_in_parent_folders()\n}\n",
        );
        let opts = DiscoveryOptions::defaults();
        let d = FsDiscoverer.discover(root, &opts).unwrap();
        assert!(
            d.components
                .iter()
                .any(|c| c.path.as_ref() == Path::new("modules/strange"))
        );
        assert!(
            d.diagnostics.iter().any(|x| x.code.as_ref() == "TF1120"),
            "{d:?}"
        );
    }

    #[test]
    fn test_should_be_deterministic_in_ordering() {
        let tmp = tempdir().unwrap();
        let root = tmp.path();
        for name in ["zeta", "alpha", "mu"] {
            write(
                root,
                &format!("services/{name}/main.tf"),
                "resource \"r\" \"r\" {}\n",
            );
        }
        let opts = DiscoveryOptions::defaults();
        let d = FsDiscoverer.discover(root, &opts).unwrap();
        let names: Vec<_> = d
            .components
            .iter()
            .map(|c| c.path.to_string_lossy().into_owned())
            .collect();
        let mut sorted = names.clone();
        sorted.sort();
        assert_eq!(names, sorted);
    }

    #[test]
    fn test_should_classify_files_only_dir() {
        let tmp = tempdir().unwrap();
        let root = tmp.path();
        write(root, "policies/foo.json", "{}");
        let opts = DiscoveryOptions::defaults();
        let d = FsDiscoverer.discover(root, &opts).unwrap();
        // `Files` directories don't surface in components/modules; they are
        // implicitly tracked via tracing::debug. The presence/absence here is
        // structural, not asserted by Discovery's public API. We just verify
        // discovery does not error.
        assert!(d.components.is_empty());
        assert!(d.modules.is_empty());
    }

    #[test]
    fn test_should_record_files_for_component() {
        let tmp = tempdir().unwrap();
        let root = tmp.path();
        write(root, "svc/main.tf", "resource \"r\" \"r\" {}\n");
        write(root, "svc/outputs.tf", "output \"o\" { value = 1 }\n");
        let opts = DiscoveryOptions::defaults();
        let d = FsDiscoverer.discover(root, &opts).unwrap();
        assert_eq!(d.components.len(), 1);
        let exts: Vec<_> = d.components[0].files.iter().map(|f| f.ext).collect();
        assert!(exts.iter().all(|e| *e == FileExt::Tf));
    }

    #[test]
    fn test_canonical_root_strips_dot_dot() {
        let tmp = tempdir().unwrap();
        let root = tmp.path();
        write(root, "svc/main.tf", "resource \"r\" \"r\" {}\n");
        let nested = root.join("svc/..");
        let d = FsDiscoverer
            .discover(&nested, &DiscoveryOptions::defaults())
            .unwrap();
        assert!(!d.root.to_string_lossy().contains(".."));
    }

    // Symlinks are unix-only.
    #[cfg(unix)]
    #[test]
    fn test_should_skip_symlinks_by_default() {
        let tmp = tempdir().unwrap();
        let root = tmp.path();
        write(root, "real/main.tf", "resource \"r\" \"r\" {}\n");
        std::os::unix::fs::symlink(root.join("real/main.tf"), root.join("real/main_link.tf"))
            .unwrap();
        let opts = DiscoveryOptions::defaults();
        let d = FsDiscoverer.discover(root, &opts).unwrap();
        // Component still classified (the real file is there), and the
        // symlink path emits a `Trace` diagnostic.
        assert_eq!(d.components.len(), 1);
        let _ = PathBuf::from(root); // keep tempdir alive
    }
}