azdolint 0.3.0

CLI tool that validates Azure DevOps pipeline YAML files by checking that referenced variable groups and variables exist
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
695
696
697
698
699
700
701
702
703
704
705
706
//! YAML parser for Azure DevOps pipeline files

use anyhow::{Context, Result};
use regex::Regex;
use serde::Deserialize;
use std::fs;

/// Represents a variable group reference in the pipeline
#[derive(Debug, Deserialize)]
pub struct VariableGroup {
    /// Name of the variable group
    pub group: Option<String>,
    /// Individual variables (when not a group reference)
    #[serde(flatten)]
    pub variables: Option<std::collections::HashMap<String, String>>,
}

/// Represents an individual variable definition
#[derive(Debug, Deserialize)]
pub struct Variable {
    /// Variable name
    pub name: Option<String>,
    /// Variable value
    pub value: Option<String>,
}

/// Represents a variable entry in the variables section (list format)
/// Azure DevOps YAML supports multiple formats:
/// - group: 'GroupName' (variable group reference)
/// - name: 'VarName' + value: 'VarValue' (inline variable)
/// - template: 'path/to/template.yml' (template reference)
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum VariableEntry {
    /// Variable group reference: - group: 'GroupName'
    Group { group: String },
    /// Named variable: - name: 'VarName' value: 'VarValue'
    Named { name: String, value: Option<String> },
    /// Template reference: - template: 'path'
    Template { template: String },
    /// Catch-all for template expressions like ${{ if eq(...) }} and other compile-time constructs
    Conditional(serde_yaml::Value),
}

/// Variables section that can be either a list or a map
/// Azure DevOps supports two formats:
/// - List format: variables: [{ name: 'x', value: 'y' }, { group: 'z' }]
/// - Map format: variables: { varName: 'value', anotherVar: 'value2' }
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum Variables {
    /// List format with structured entries
    List(Vec<VariableEntry>),
    /// Map format with simple key-value pairs
    Map(std::collections::HashMap<String, serde_yaml::Value>),
}

impl Variables {
    /// Returns the number of entries in the variables section
    pub fn len(&self) -> usize {
        match self {
            Variables::List(entries) => entries.len(),
            Variables::Map(map) => map.len(),
        }
    }

    /// Returns true if the variables section is empty
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns an iterator over the variable entries (only works for List format)
    /// For Map format, returns an empty iterator
    pub fn iter(&self) -> std::slice::Iter<'_, VariableEntry> {
        match self {
            Variables::List(entries) => entries.iter(),
            Variables::Map(_) => [].iter(),
        }
    }
}

/// Represents a job in a stage
#[derive(Debug, Deserialize)]
pub struct Job {
    /// Job-level variables (supports both list and map formats)
    #[serde(default)]
    pub variables: Option<Variables>,
}

/// Represents a deployment job in a stage
#[derive(Debug, Deserialize)]
pub struct Deployment {
    /// Deployment-level variables (supports both list and map formats)
    #[serde(default)]
    pub variables: Option<Variables>,
}

/// Represents a stage in the pipeline
#[derive(Debug, Deserialize)]
pub struct Stage {
    /// Stage name
    #[serde(default)]
    pub stage: Option<String>,
    /// Stage-level variables (supports both list and map formats)
    #[serde(default)]
    pub variables: Option<Variables>,
    /// Jobs in the stage
    #[serde(default)]
    pub jobs: Option<Vec<Job>>,
}

/// Top-level pipeline structure
#[derive(Debug, Deserialize)]
pub struct Pipeline {
    /// Variables section containing both inline variables and group references
    /// (supports both list and map formats)
    #[serde(default)]
    pub variables: Option<Variables>,
    /// Stages in the pipeline
    #[serde(default)]
    pub stages: Option<Vec<Stage>>,
}

impl Pipeline {
    /// Extract all variable group names referenced in the pipeline
    /// Searches top-level variables, stage-level variables, and job-level variables
    ///
    /// # Returns
    /// * `Vec<String>` - Unique list of variable group names
    pub fn get_variable_groups(&self) -> Vec<String> {
        let mut groups = Vec::new();

        // Collect from top-level variables
        Self::collect_groups_from_variables(&self.variables, &mut groups);

        // Collect from stage-level variables
        if let Some(ref stages) = self.stages {
            for stage in stages {
                Self::collect_groups_from_variables(&stage.variables, &mut groups);

                // Collect from job-level variables
                if let Some(ref jobs) = stage.jobs {
                    for job in jobs {
                        Self::collect_groups_from_variables(&job.variables, &mut groups);
                    }
                }
            }
        }

        groups
    }

    /// Helper function to collect variable groups from a variables section
    fn collect_groups_from_variables(variables: &Option<Variables>, groups: &mut Vec<String>) {
        if let Some(ref vars) = variables {
            match vars {
                Variables::List(entries) => {
                    for entry in entries {
                        match entry {
                            VariableEntry::Group { group } => {
                                if !groups.contains(group) {
                                    groups.push(group.clone());
                                }
                            }
                            VariableEntry::Conditional(value) => {
                                Self::extract_groups_from_value(value, groups);
                            }
                            _ => {}
                        }
                    }
                }
                Variables::Map(_) => {
                    // Map format doesn't support variable groups
                }
            }
        }
    }

    /// Extract all inline variable names defined in the pipeline
    /// Searches top-level variables, stage-level variables, and job-level variables
    ///
    /// # Returns
    /// * `Vec<String>` - Unique list of inline variable names
    pub fn get_inline_variable_names(&self) -> Vec<String> {
        let mut names = Vec::new();

        // Collect from top-level variables
        Self::collect_inline_variables(&self.variables, &mut names);

        // Collect from stage-level variables
        if let Some(ref stages) = self.stages {
            for stage in stages {
                Self::collect_inline_variables(&stage.variables, &mut names);

                // Collect from job-level variables
                if let Some(ref jobs) = stage.jobs {
                    for job in jobs {
                        Self::collect_inline_variables(&job.variables, &mut names);
                    }
                }
            }
        }

        names
    }

    /// Helper function to collect inline variable names from a variables section
    fn collect_inline_variables(variables: &Option<Variables>, names: &mut Vec<String>) {
        if let Some(ref vars) = variables {
            match vars {
                Variables::List(entries) => {
                    for entry in entries {
                        match entry {
                            VariableEntry::Named { name, .. } => {
                                if !names.contains(name) {
                                    names.push(name.clone());
                                }
                            }
                            VariableEntry::Conditional(value) => {
                                Self::extract_inline_variables_from_value(value, names);
                            }
                            _ => {}
                        }
                    }
                }
                Variables::Map(map) => {
                    // Map format: each key is a variable name, unless it's a template conditional
                    for (key, value) in map {
                        // Skip template conditional keys - they're not variable names
                        // Instead, recursively extract variables from the nested structure
                        if key.starts_with("${{") {
                            Self::extract_inline_variables_from_value(value, names);
                        } else if !names.contains(key) {
                            names.push(key.clone());
                        }
                    }
                }
            }
        }
    }

    /// Recursively extract variable groups from a serde_yaml::Value
    /// This handles template conditionals like ${{ if eq(...) }} which contain nested variables
    fn extract_groups_from_value(value: &serde_yaml::Value, groups: &mut Vec<String>) {
        match value {
            serde_yaml::Value::Mapping(map) => {
                // Check if this mapping has a "group" key (direct variable group reference)
                if let Some(serde_yaml::Value::String(group_name)) =
                    map.get(serde_yaml::Value::String("group".to_string()))
                {
                    if !groups.contains(group_name) {
                        groups.push(group_name.clone());
                    }
                }
                // Recurse into all values in the mapping
                for (_key, val) in map {
                    Self::extract_groups_from_value(val, groups);
                }
            }
            serde_yaml::Value::Sequence(seq) => {
                // Recurse into each item in the sequence
                for item in seq {
                    Self::extract_groups_from_value(item, groups);
                }
            }
            _ => {}
        }
    }

    /// Recursively extract inline variable names from a serde_yaml::Value
    /// This handles template conditionals like ${{ if eq(...) }} which contain nested variables
    /// Supports both list format (name: 'x', value: 'y') and map format (varName: 'value')
    fn extract_inline_variables_from_value(value: &serde_yaml::Value, names: &mut Vec<String>) {
        match value {
            serde_yaml::Value::Mapping(map) => {
                // Check if this mapping has a "name" key (list format inline variable)
                if let Some(serde_yaml::Value::String(var_name)) =
                    map.get(serde_yaml::Value::String("name".to_string()))
                {
                    if !names.contains(var_name) {
                        names.push(var_name.clone());
                    }
                }

                // Also handle map format: each key could be a variable name
                // Skip special keys and template conditionals
                for (key, val) in map {
                    if let serde_yaml::Value::String(key_str) = key {
                        // Skip template conditionals
                        if key_str.starts_with("${{") {
                            // Recurse into the conditional's nested structure
                            Self::extract_inline_variables_from_value(val, names);
                        } else if !is_special_yaml_key(key_str) && !names.contains(key_str) {
                            // This is a variable name in map format
                            names.push(key_str.clone());
                        }
                    }
                    // Always recurse into values to find nested structures
                    Self::extract_inline_variables_from_value(val, names);
                }
            }
            serde_yaml::Value::Sequence(seq) => {
                // Recurse into each item in the sequence
                for item in seq {
                    Self::extract_inline_variables_from_value(item, names);
                }
            }
            _ => {}
        }
    }
}

/// Check if a key is a special YAML key that should not be treated as a variable name
fn is_special_yaml_key(key: &str) -> bool {
    const SPECIAL_KEYS: &[&str] = &[
        "name", "value", "group", "template", "readonly", "isSecret",
    ];
    SPECIAL_KEYS.contains(&key)
}

/// Parse a pipeline YAML file and return the Pipeline structure
///
/// # Arguments
/// * `path` - Path to the YAML pipeline file
///
/// # Returns
/// * `Result<Pipeline>` - Parsed pipeline or error
pub fn parse_pipeline_file(path: &str) -> Result<Pipeline> {
    let content = fs::read_to_string(path)
        .with_context(|| format!("Failed to read pipeline file: {path}"))?;

    let pipeline: Pipeline = serde_yaml::from_str(&content)
        .with_context(|| format!("Failed to parse YAML in pipeline file: {path}"))?;

    Ok(pipeline)
}

/// Extract all variable references from pipeline YAML content
///
/// Finds all occurrences of $(variableName) syntax in the YAML content
/// and returns a unique list of variable names.
///
/// # Arguments
/// * `path` - Path to the YAML pipeline file
///
/// # Returns
/// * `Result<Vec<String>>` - Unique list of variable names referenced
pub fn extract_variable_references(path: &str) -> Result<Vec<String>> {
    let content = fs::read_to_string(path)
        .with_context(|| format!("Failed to read pipeline file: {path}"))?;

    extract_variable_references_from_content(&content)
}

/// Azure DevOps system variable prefixes that should be skipped during validation
const SYSTEM_VARIABLE_PREFIXES: &[&str] = &[
    "Build.",
    "System.",
    "Agent.",
    "Pipeline.",
    "Environment.",
    "Checks.",
    "Release.",
    "Task.",
    "Resources.",
];

/// Check if a variable name is a system/predefined Azure DevOps variable
pub fn is_system_variable(name: &str) -> bool {
    SYSTEM_VARIABLE_PREFIXES
        .iter()
        .any(|prefix| name.starts_with(prefix))
}

/// Check if a variable name is a runtime output variable
/// These are set dynamically during pipeline execution and cannot be validated statically
/// Examples: outputs.registryName, agentIp.value, domains.domainId
fn is_runtime_output_variable(name: &str) -> bool {
    // Must contain a dot to be a potential runtime output
    if !name.contains('.') {
        return false;
    }

    let parts: Vec<&str> = name.split('.').collect();
    if parts.len() < 2 {
        return false;
    }

    // Skip known system variable prefixes (handled separately)
    if is_system_variable(name) {
        return false;
    }

    // Anything else with a dot is likely a runtime output
    // e.g., outputs.registryName, agentIp.value, domains.domainId
    true
}

/// Azure DevOps build number format specifiers that should be skipped
const BUILD_NUMBER_FORMAT_PREFIXES: &[&str] = &["Date:", "Rev:"];

/// Check if a variable pattern should be skipped during validation
fn should_skip_variable(name: &str) -> bool {
    // Skip PowerShell expressions: $($outputs.foo), $($env:VAR)
    if name.starts_with('$') {
        return true;
    }

    // Skip template expressions: $[ ... ]
    if name.starts_with('[') {
        return true;
    }

    // Skip system variables
    if is_system_variable(name) {
        return true;
    }

    // Skip build number format specifiers like $(Date:yyyyMMdd), $(Rev:r)
    if BUILD_NUMBER_FORMAT_PREFIXES
        .iter()
        .any(|prefix| name.starts_with(prefix))
    {
        return true;
    }

    // Skip runtime output variables
    if is_runtime_output_variable(name) {
        return true;
    }

    // Skip shell command substitution patterns
    // Valid Azure DevOps variable names don't contain spaces
    // Shell commands like "git merge-base" or "git rev-parse HEAD" do
    if looks_like_shell_command(name) {
        return true;
    }

    false
}

/// Check if a pattern looks like shell command substitution rather than a variable
/// Shell commands typically contain spaces (e.g., "git merge-base", "git rev-parse HEAD")
/// while Azure DevOps variable names are alphanumeric with underscores
fn looks_like_shell_command(name: &str) -> bool {
    name.contains(' ')
}

/// Information about whether a file is a template
#[derive(Debug)]
pub struct TemplateInfo {
    /// Whether the file appears to be a template
    pub is_template: bool,
    /// Names of template parameters (if any)
    pub parameter_names: Vec<String>,
}

/// Detect if a pipeline file is a template
///
/// Templates are characterized by:
/// - Having a top-level `parameters:` section
/// - NOT having a `trigger:` key (templates don't define triggers)
///
/// # Arguments
/// * `path` - Path to the YAML pipeline file
///
/// # Returns
/// * `Result<TemplateInfo>` - Information about whether the file is a template
pub fn detect_template(path: &str) -> Result<TemplateInfo> {
    let content = fs::read_to_string(path)
        .with_context(|| format!("Failed to read pipeline file: {path}"))?;

    let yaml: serde_yaml::Value = serde_yaml::from_str(&content)
        .with_context(|| format!("Failed to parse YAML in pipeline file: {path}"))?;

    let mapping = match yaml.as_mapping() {
        Some(m) => m,
        None => {
            return Ok(TemplateInfo {
                is_template: false,
                parameter_names: Vec::new(),
            })
        }
    };

    // Check for template indicators
    let has_parameters = mapping.contains_key(serde_yaml::Value::String("parameters".to_string()));
    let has_trigger = mapping.contains_key(serde_yaml::Value::String("trigger".to_string()));
    let has_pr = mapping.contains_key(serde_yaml::Value::String("pr".to_string()));

    // A template has parameters but no trigger/pr
    let is_template = has_parameters && !has_trigger && !has_pr;

    // Extract parameter names if this is a template
    let parameter_names = if is_template {
        extract_parameter_names(&yaml)
    } else {
        Vec::new()
    };

    Ok(TemplateInfo {
        is_template,
        parameter_names,
    })
}

/// Extract parameter names from the YAML parameters section
fn extract_parameter_names(yaml: &serde_yaml::Value) -> Vec<String> {
    let mut names = Vec::new();

    if let Some(mapping) = yaml.as_mapping() {
        if let Some(params) = mapping.get(serde_yaml::Value::String("parameters".to_string())) {
            if let Some(params_seq) = params.as_sequence() {
                for param in params_seq {
                    if let Some(param_map) = param.as_mapping() {
                        if let Some(serde_yaml::Value::String(name)) =
                            param_map.get(serde_yaml::Value::String("name".to_string()))
                        {
                            names.push(name.clone());
                        }
                    }
                }
            }
        }
    }

    names
}

/// A template reference found in a pipeline's jobs section
#[derive(Debug, Clone)]
pub struct TemplateReference {
    /// Path to the template file (as specified in YAML)
    pub template_path: String,
    /// Name of the stage containing this template reference
    pub stage_name: Option<String>,
    /// Variable groups available in this template's scope (top-level + stage-level)
    pub available_groups: Vec<String>,
    /// Inline variables available in this template's scope
    pub available_inline_vars: Vec<String>,
}

/// Extract template references from a pipeline file
///
/// Parses the raw YAML to find `- template: path` entries in jobs sections,
/// collecting the variable groups available at each template's scope.
///
/// # Arguments
/// * `path` - Path to the pipeline YAML file
///
/// # Returns
/// * `Result<Vec<TemplateReference>>` - List of template references with their available groups
pub fn extract_template_references(path: &str) -> Result<Vec<TemplateReference>> {
    let content = fs::read_to_string(path)
        .with_context(|| format!("Failed to read pipeline file: {path}"))?;

    let yaml: serde_yaml::Value = serde_yaml::from_str(&content)
        .with_context(|| format!("Failed to parse YAML in pipeline file: {path}"))?;

    let mut references = Vec::new();

    let mapping = match yaml.as_mapping() {
        Some(m) => m,
        None => return Ok(references),
    };

    // Collect top-level variable groups and inline variables
    let mut top_level_groups = Vec::new();
    let mut top_level_inline_vars = Vec::new();
    if let Some(vars) = mapping.get(serde_yaml::Value::String("variables".to_string())) {
        collect_groups_from_yaml_value(vars, &mut top_level_groups);
        collect_inline_vars_from_yaml_value(vars, &mut top_level_inline_vars);
    }

    // Process stages
    if let Some(stages) = mapping.get(serde_yaml::Value::String("stages".to_string())) {
        if let Some(stages_seq) = stages.as_sequence() {
            for stage in stages_seq {
                if let Some(stage_map) = stage.as_mapping() {
                    // Get stage name
                    let stage_name = stage_map
                        .get(serde_yaml::Value::String("stage".to_string()))
                        .and_then(|v| v.as_str())
                        .map(|s| s.to_string());

                    // Collect stage-level variable groups
                    let mut stage_groups = top_level_groups.clone();
                    let mut stage_inline_vars = top_level_inline_vars.clone();
                    if let Some(vars) = stage_map.get(serde_yaml::Value::String("variables".to_string())) {
                        collect_groups_from_yaml_value(vars, &mut stage_groups);
                        collect_inline_vars_from_yaml_value(vars, &mut stage_inline_vars);
                    }

                    // Process jobs in this stage
                    if let Some(jobs) = stage_map.get(serde_yaml::Value::String("jobs".to_string())) {
                        if let Some(jobs_seq) = jobs.as_sequence() {
                            for job in jobs_seq {
                                if let Some(job_map) = job.as_mapping() {
                                    // Check if this is a template reference
                                    if let Some(template_val) = job_map.get(serde_yaml::Value::String("template".to_string())) {
                                        if let Some(template_path) = template_val.as_str() {
                                            references.push(TemplateReference {
                                                template_path: template_path.to_string(),
                                                stage_name: stage_name.clone(),
                                                available_groups: stage_groups.clone(),
                                                available_inline_vars: stage_inline_vars.clone(),
                                            });
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    Ok(references)
}

/// Helper to collect variable groups from a YAML value
fn collect_groups_from_yaml_value(value: &serde_yaml::Value, groups: &mut Vec<String>) {
    if let Some(seq) = value.as_sequence() {
        for item in seq {
            if let Some(map) = item.as_mapping() {
                if let Some(serde_yaml::Value::String(group_name)) =
                    map.get(serde_yaml::Value::String("group".to_string()))
                {
                    if !groups.contains(group_name) {
                        groups.push(group_name.clone());
                    }
                }
            }
        }
    }
}

/// Helper to collect inline variable names from a YAML value
fn collect_inline_vars_from_yaml_value(value: &serde_yaml::Value, vars: &mut Vec<String>) {
    if let Some(seq) = value.as_sequence() {
        for item in seq {
            if let Some(map) = item.as_mapping() {
                if let Some(serde_yaml::Value::String(var_name)) =
                    map.get(serde_yaml::Value::String("name".to_string()))
                {
                    if !vars.contains(var_name) {
                        vars.push(var_name.clone());
                    }
                }
            }
        }
    }
}

/// Resolve a template path relative to the parent pipeline file
///
/// # Arguments
/// * `parent_path` - Path to the parent pipeline file
/// * `template_ref` - Template path as specified in YAML (may be relative)
///
/// # Returns
/// * Resolved absolute or relative path to the template file
pub fn resolve_template_path(parent_path: &str, template_ref: &str) -> String {
    use std::path::Path;

    let parent = Path::new(parent_path);
    let parent_dir = parent.parent().unwrap_or(Path::new("."));

    parent_dir.join(template_ref).to_string_lossy().to_string()
}

/// Extract variable references from raw YAML content string
/// Filters out PowerShell expressions, system variables, and runtime output variables
///
/// # Arguments
/// * `content` - Raw YAML content
///
/// # Returns
/// * `Result<Vec<String>>` - Unique list of variable names referenced (excluding system/runtime vars)
pub fn extract_variable_references_from_content(content: &str) -> Result<Vec<String>> {
    // Regex pattern to match $(variableName) syntax
    // Captures the variable name inside the parentheses
    let re = Regex::new(r"\$\(([^)]+)\)")
        .with_context(|| "Failed to compile variable reference regex")?;

    let mut variables = Vec::new();

    for cap in re.captures_iter(content) {
        if let Some(var_name) = cap.get(1) {
            let name = var_name.as_str();

            // Skip variables that shouldn't be validated
            if should_skip_variable(name) {
                continue;
            }

            let name_string = name.to_string();
            if !variables.contains(&name_string) {
                variables.push(name_string);
            }
        }
    }

    Ok(variables)
}