infigraph-core 1.5.4

AST-powered code analysis framework — parser, graph, diff, and analysis engine
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
use anyhow::Result;
use serde::Serialize;
use std::path::Path;

use crate::graph::GraphStore;

#[derive(Debug, Clone, Serialize)]
pub struct ConfigBinding {
    pub symbol_id: String,
    pub kind: &'static str,
    pub key: String,
    pub value: String,
    pub profile: String,
    pub source_file: String,
}

struct ConditionalPattern {
    kind: &'static str,
    patterns: &'static [&'static str],
}

static CONDITIONAL_PATTERNS: &[ConditionalPattern] = &[
    // Spring profiles
    ConditionalPattern {
        kind: "Profile",
        patterns: &[
            "@Profile(",
            "@ConditionalOnProperty(",
            "@ConditionalOnBean(",
            "@ConditionalOnMissingBean(",
            "@ConditionalOnClass(",
            "@ConditionalOnExpression(",
        ],
    },
    // Spring qualifiers
    ConditionalPattern {
        kind: "Qualifier",
        patterns: &["@Qualifier(", "@Primary", "@Named("],
    },
    // .NET environment
    ConditionalPattern {
        kind: "Environment",
        patterns: &[
            "[Environment(",
            "IsDevelopment()",
            "IsProduction()",
            "IsStaging()",
            "ASPNETCORE_ENVIRONMENT",
        ],
    },
    // Python/Django
    ConditionalPattern {
        kind: "DjangoSetting",
        patterns: &[
            "settings.DEBUG",
            "settings.DATABASES",
            "settings.INSTALLED_APPS",
            "settings.MIDDLEWARE",
            "getattr(settings,",
            "os.environ.get(",
            "os.getenv(",
        ],
    },
    // Rails
    ConditionalPattern {
        kind: "RailsEnv",
        patterns: &[
            "Rails.env.production?",
            "Rails.env.development?",
            "Rails.env.test?",
            "Rails.env.staging?",
            "Rails.application.config.",
        ],
    },
    // Go build tags
    ConditionalPattern {
        kind: "BuildTag",
        patterns: &["//go:build ", "// +build "],
    },
    // Rust feature gates
    ConditionalPattern {
        kind: "FeatureGate",
        patterns: &[
            "#[cfg(feature",
            "#[cfg(target_os",
            "#[cfg(test)]",
            "#[cfg(not(",
            "#[cfg_attr(",
        ],
    },
    // Node.js / NestJS
    ConditionalPattern {
        kind: "EnvConfig",
        patterns: &[
            "process.env.",
            "ConfigService.get(",
            "ConfigService.getOrThrow(",
            "@Optional()",
        ],
    },
];

pub fn detect_config_bindings(store: &GraphStore) -> Result<Vec<ConfigBinding>> {
    let _lock = store.write_lock()?;
    let conn = store.connection()?;

    let result = conn
        .query("MATCH (s:Symbol) WHERE s.docstring IS NOT NULL AND s.docstring <> '' RETURN s.id, s.docstring, s.file")
        .map_err(|e| anyhow::anyhow!("query failed: {e}"))?;

    let mut bindings = Vec::new();

    for row in result {
        if row.len() < 3 {
            continue;
        }
        let symbol_id = row[0].to_string();
        let docstring = row[1].to_string();
        let file = row[2].to_string();

        for cp in CONDITIONAL_PATTERNS {
            for &pattern in cp.patterns {
                if docstring.contains(pattern) {
                    let detail = extract_config_detail(&docstring, pattern);
                    let (key, value) = parse_config_kv(&detail, pattern);
                    bindings.push(ConfigBinding {
                        symbol_id: symbol_id.clone(),
                        kind: cp.kind,
                        key,
                        value,
                        profile: extract_profile(&detail, cp.kind),
                        source_file: file.clone(),
                    });
                    break;
                }
            }
        }
    }

    if !bindings.is_empty() {
        write_config_bindings(store, &bindings)?;
    }

    Ok(bindings)
}

fn extract_config_detail(docstring: &str, pattern: &str) -> String {
    for line in docstring.lines() {
        if line.contains(pattern) {
            return line.trim().to_string();
        }
    }
    pattern.to_string()
}

fn parse_config_kv(detail: &str, pattern: &str) -> (String, String) {
    if let Some(start) = detail.find(pattern) {
        let after = &detail[start + pattern.len()..];
        let inner = after
            .trim_start_matches(['(', '"', '\''])
            .split([')', '"', '\''])
            .next()
            .unwrap_or("");
        if inner.contains('=') {
            let parts: Vec<&str> = inner.splitn(2, '=').collect();
            return (
                parts[0].trim().to_string(),
                parts
                    .get(1)
                    .map(|s| s.trim().to_string())
                    .unwrap_or_default(),
            );
        }
        return (inner.to_string(), String::new());
    }
    (detail.to_string(), String::new())
}

fn extract_profile(detail: &str, kind: &str) -> String {
    match kind {
        "Profile" => {
            if let Some(start) = detail.find("@Profile(") {
                let after = &detail[start + 9..];
                let inner = after
                    .trim_start_matches(['"', '\''])
                    .split(['"', '\'', ')'])
                    .next()
                    .unwrap_or("default");
                return inner.to_string();
            }
            "default".to_string()
        }
        "RailsEnv" => {
            if detail.contains("production") {
                "production".to_string()
            } else if detail.contains("development") {
                "development".to_string()
            } else if detail.contains("staging") {
                "staging".to_string()
            } else if detail.contains("test") {
                "test".to_string()
            } else {
                "default".to_string()
            }
        }
        "Environment" => {
            if detail.contains("Production") {
                "production".to_string()
            } else if detail.contains("Development") {
                "development".to_string()
            } else if detail.contains("Staging") {
                "staging".to_string()
            } else {
                "default".to_string()
            }
        }
        _ => "default".to_string(),
    }
}

fn write_config_bindings(store: &GraphStore, bindings: &[ConfigBinding]) -> Result<()> {
    let conn = store.connection()?;

    conn.query("BEGIN TRANSACTION")
        .map_err(|e| anyhow::anyhow!("begin txn: {e}"))?;

    let _ = conn.query("MATCH (c:ConfigBinding) DETACH DELETE c");

    for b in bindings {
        let id = format!("{}::{}::{}", b.symbol_id, b.kind, b.key);
        let id_esc = crate::escape_str(&id);
        let kind_esc = crate::escape_str(b.kind);
        let key_esc = crate::escape_str(&b.key);
        let val_esc = crate::escape_str(&b.value);
        let profile_esc = crate::escape_str(&b.profile);
        let src_esc = crate::escape_str(&b.source_file);
        let sym_esc = crate::escape_str(&b.symbol_id);

        let _ = conn.query(&format!(
            "CREATE (c:ConfigBinding {{id: '{id_esc}', kind: '{kind_esc}', key: '{key_esc}', value: '{val_esc}', `profile`: '{profile_esc}', source_file: '{src_esc}'}})"
        ));
        let _ = conn.query(&format!(
            "MATCH (s:Symbol), (c:ConfigBinding) WHERE s.id = '{sym_esc}' AND c.id = '{id_esc}' CREATE (s)-[:HAS_CONFIG]->(c)"
        ));
    }

    conn.query("COMMIT")
        .map_err(|e| anyhow::anyhow!("commit txn: {e}"))?;

    Ok(())
}

pub fn detect_config_files(root: &Path) -> Vec<ConfigFileInfo> {
    let mut configs = Vec::new();
    let patterns = [
        ("application.yml", "Spring"),
        ("application.yaml", "Spring"),
        ("application.properties", "Spring"),
        ("application-*.yml", "Spring"),
        ("application-*.yaml", "Spring"),
        ("application-*.properties", "Spring"),
        ("settings.py", "Django"),
        ("appsettings.json", "DotNet"),
        ("appsettings.*.json", "DotNet"),
        ("config/database.yml", "Rails"),
        ("config/environments/", "Rails"),
        (".env", "Generic"),
        (".env.*", "Generic"),
    ];

    if let Ok(walker) = glob_walk(root) {
        for entry in walker {
            let rel = entry.strip_prefix(root).unwrap_or(&entry);
            let name = rel.file_name().unwrap_or_default().to_string_lossy();
            for (pat, framework) in &patterns {
                if matches_config_pattern(&name, &rel.to_string_lossy(), pat) {
                    let profile = extract_profile_from_filename(&name, framework);
                    configs.push(ConfigFileInfo {
                        path: rel.to_string_lossy().to_string(),
                        framework: framework.to_string(),
                        profile,
                    });
                    break;
                }
            }
        }
    }
    configs
}

#[derive(Debug, Clone, Serialize)]
pub struct ConfigFileInfo {
    pub path: String,
    pub framework: String,
    pub profile: String,
}

fn glob_walk(root: &Path) -> Result<Vec<std::path::PathBuf>> {
    let mut files = Vec::new();
    walk_config_dir(root, root, &mut files, 0)?;
    Ok(files)
}

#[allow(clippy::only_used_in_recursion)]
fn walk_config_dir(
    root: &Path,
    dir: &Path,
    files: &mut Vec<std::path::PathBuf>,
    depth: usize,
) -> Result<()> {
    if depth > 5 {
        return Ok(());
    }
    let entries = match std::fs::read_dir(dir) {
        Ok(e) => e,
        Err(_) => return Ok(()),
    };
    for entry in entries {
        let entry = entry?;
        let path = entry.path();
        let name = entry.file_name().to_string_lossy().to_string();
        if name.starts_with('.') && name != ".env" && !name.starts_with(".env.") {
            continue;
        }
        if path.is_dir() {
            let skip = [
                "node_modules",
                "target",
                "build",
                "dist",
                ".git",
                "__pycache__",
                "venv",
                ".venv",
            ];
            if !skip.contains(&name.as_str()) {
                walk_config_dir(root, &path, files, depth + 1)?;
            }
        } else {
            files.push(path);
        }
    }
    Ok(())
}

fn matches_config_pattern(filename: &str, rel_path: &str, pattern: &str) -> bool {
    if pattern.contains('*') {
        let parts: Vec<&str> = pattern.split('*').collect();
        if parts.len() == 2 {
            return filename.starts_with(parts[0]) && filename.ends_with(parts[1]);
        }
    }
    if pattern.contains('/') {
        return rel_path.contains(pattern);
    }
    filename == pattern
}

fn extract_profile_from_filename(filename: &str, framework: &str) -> String {
    match framework {
        "Spring" => {
            if filename.starts_with("application-") {
                let name = filename.strip_prefix("application-").unwrap_or("");
                let profile = name.split('.').next().unwrap_or("default");
                return profile.to_string();
            }
            "default".to_string()
        }
        "DotNet" => {
            if filename.starts_with("appsettings.") && filename != "appsettings.json" {
                let name = filename.strip_prefix("appsettings.").unwrap_or("");
                let profile = name.strip_suffix(".json").unwrap_or(name);
                return profile.to_string();
            }
            "default".to_string()
        }
        "Generic" => {
            if filename.starts_with(".env.") {
                return filename
                    .strip_prefix(".env.")
                    .unwrap_or("default")
                    .to_string();
            }
            "default".to_string()
        }
        _ => "default".to_string(),
    }
}

pub fn format_config_bindings(
    bindings: &[ConfigBinding],
    config_files: &[ConfigFileInfo],
) -> String {
    if bindings.is_empty() && config_files.is_empty() {
        return "No configuration bindings or config files detected.".to_string();
    }

    let mut out = String::new();

    if !config_files.is_empty() {
        out.push_str(&format!(
            "Config files detected: {}\n\n",
            config_files.len()
        ));
        let mut by_fw: std::collections::BTreeMap<&str, Vec<&ConfigFileInfo>> =
            std::collections::BTreeMap::new();
        for cf in config_files {
            by_fw.entry(&cf.framework).or_default().push(cf);
        }
        for (fw, files) in &by_fw {
            out.push_str(&format!("## {} ({})\n", fw, files.len()));
            for f in files {
                out.push_str(&format!("  {} [profile: {}]\n", f.path, f.profile));
            }
            out.push('\n');
        }
    }

    if !bindings.is_empty() {
        out.push_str(&format!("Config bindings: {} total\n\n", bindings.len()));
        let mut by_kind: std::collections::BTreeMap<&str, Vec<&ConfigBinding>> =
            std::collections::BTreeMap::new();
        for b in bindings {
            by_kind.entry(b.kind).or_default().push(b);
        }
        for (kind, items) in &by_kind {
            out.push_str(&format!("## {} ({} symbols)\n", kind, items.len()));
            for item in items {
                if item.value.is_empty() {
                    out.push_str(&format!(
                        "  {}{} [profile: {}]\n",
                        item.symbol_id, item.key, item.profile
                    ));
                } else {
                    out.push_str(&format!(
                        "  {}{}={} [profile: {}]\n",
                        item.symbol_id, item.key, item.value, item.profile
                    ));
                }
            }
            out.push('\n');
        }
    }

    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_detect_spring_profile() {
        let docstring = "@Profile(\"production\")\n@Component\npublic class ProdDataSource {}";
        let mut found = Vec::new();
        for cp in CONDITIONAL_PATTERNS {
            for &pattern in cp.patterns {
                if docstring.contains(pattern) {
                    found.push(cp.kind);
                    break;
                }
            }
        }
        assert!(found.contains(&"Profile"), "should detect @Profile");
    }

    #[test]
    fn test_detect_spring_qualifier() {
        let docstring = "@Qualifier(\"primaryDB\")\n@Autowired\nprivate DataSource ds;";
        let mut found = Vec::new();
        for cp in CONDITIONAL_PATTERNS {
            for &pattern in cp.patterns {
                if docstring.contains(pattern) {
                    found.push(cp.kind);
                    break;
                }
            }
        }
        assert!(found.contains(&"Qualifier"), "should detect @Qualifier");
    }

    #[test]
    fn test_detect_dotnet_environment() {
        let docstring = "if (env.IsDevelopment())\n{\n    app.UseDeveloperExceptionPage();\n}";
        let mut found = Vec::new();
        for cp in CONDITIONAL_PATTERNS {
            for &pattern in cp.patterns {
                if docstring.contains(pattern) {
                    found.push(cp.kind);
                    break;
                }
            }
        }
        assert!(
            found.contains(&"Environment"),
            "should detect IsDevelopment()"
        );
    }

    #[test]
    fn test_detect_django_settings() {
        let docstring = "if settings.DEBUG:\n    print('debug mode')";
        let mut found = Vec::new();
        for cp in CONDITIONAL_PATTERNS {
            for &pattern in cp.patterns {
                if docstring.contains(pattern) {
                    found.push(cp.kind);
                    break;
                }
            }
        }
        assert!(
            found.contains(&"DjangoSetting"),
            "should detect settings.DEBUG"
        );
    }

    #[test]
    fn test_detect_rails_env() {
        let docstring = "if Rails.env.production?\n  config.force_ssl = true\nend";
        let mut found = Vec::new();
        for cp in CONDITIONAL_PATTERNS {
            for &pattern in cp.patterns {
                if docstring.contains(pattern) {
                    found.push(cp.kind);
                    break;
                }
            }
        }
        assert!(
            found.contains(&"RailsEnv"),
            "should detect Rails.env.production?"
        );
    }

    #[test]
    fn test_detect_go_build_tag() {
        let docstring = "//go:build linux && amd64\npackage main";
        let mut found = Vec::new();
        for cp in CONDITIONAL_PATTERNS {
            for &pattern in cp.patterns {
                if docstring.contains(pattern) {
                    found.push(cp.kind);
                    break;
                }
            }
        }
        assert!(found.contains(&"BuildTag"), "should detect //go:build");
    }

    #[test]
    fn test_detect_rust_cfg() {
        let docstring = "#[cfg(feature = \"postgres\")]\nmod postgres_backend {}";
        let mut found = Vec::new();
        for cp in CONDITIONAL_PATTERNS {
            for &pattern in cp.patterns {
                if docstring.contains(pattern) {
                    found.push(cp.kind);
                    break;
                }
            }
        }
        assert!(
            found.contains(&"FeatureGate"),
            "should detect #[cfg(feature"
        );
    }

    #[test]
    fn test_detect_node_env() {
        let docstring = "const port = process.env.PORT || 3000;";
        let mut found = Vec::new();
        for cp in CONDITIONAL_PATTERNS {
            for &pattern in cp.patterns {
                if docstring.contains(pattern) {
                    found.push(cp.kind);
                    break;
                }
            }
        }
        assert!(found.contains(&"EnvConfig"), "should detect process.env.");
    }

    #[test]
    fn test_extract_profile_spring() {
        let detail = "@Profile(\"production\")";
        let profile = extract_profile(detail, "Profile");
        assert_eq!(profile, "production");
    }

    #[test]
    fn test_extract_profile_rails() {
        let detail = "Rails.env.production?";
        let profile = extract_profile(detail, "RailsEnv");
        assert_eq!(profile, "production");
    }

    #[test]
    fn test_extract_profile_dotnet() {
        let detail = "env.IsProduction()";
        let profile = extract_profile(detail, "Environment");
        assert_eq!(profile, "production");
    }

    #[test]
    fn test_config_file_spring_profile() {
        assert_eq!(
            extract_profile_from_filename("application-prod.yml", "Spring"),
            "prod"
        );
        assert_eq!(
            extract_profile_from_filename("application.yml", "Spring"),
            "default"
        );
    }

    #[test]
    fn test_config_file_dotnet_profile() {
        assert_eq!(
            extract_profile_from_filename("appsettings.Production.json", "DotNet"),
            "Production"
        );
        assert_eq!(
            extract_profile_from_filename("appsettings.json", "DotNet"),
            "default"
        );
    }

    #[test]
    fn test_config_file_env_profile() {
        assert_eq!(
            extract_profile_from_filename(".env.production", "Generic"),
            "production"
        );
        assert_eq!(extract_profile_from_filename(".env", "Generic"), "default");
    }

    #[test]
    fn test_matches_config_pattern() {
        assert!(matches_config_pattern(
            "application.yml",
            "application.yml",
            "application.yml"
        ));
        assert!(matches_config_pattern(
            "application-prod.yml",
            "application-prod.yml",
            "application-*.yml"
        ));
        assert!(!matches_config_pattern(
            "other.yml",
            "other.yml",
            "application-*.yml"
        ));
        assert!(matches_config_pattern(
            ".env.production",
            ".env.production",
            ".env.*"
        ));
    }

    #[test]
    fn test_no_false_positive_on_plain_text() {
        let docstring = "This configures the production database settings.";
        let mut found = Vec::new();
        for cp in CONDITIONAL_PATTERNS {
            for &pattern in cp.patterns {
                if docstring.contains(pattern) {
                    found.push(cp.kind);
                    break;
                }
            }
        }
        assert!(found.is_empty(), "plain text should not match: {:?}", found);
    }

    #[test]
    fn test_parse_config_kv_spring_profile() {
        let detail = "@Profile(\"production\")";
        let (key, _value) = parse_config_kv(detail, "@Profile(");
        assert_eq!(key, "production");
    }

    #[test]
    fn test_parse_config_kv_conditional() {
        let detail = "@ConditionalOnProperty(name=\"feature.enabled\", havingValue=\"true\")";
        let (key, _val) = parse_config_kv(detail, "@ConditionalOnProperty(");
        assert!(
            key.contains("feature.enabled") || key.contains("name"),
            "key={}",
            key
        );
    }
}