wdl-analysis 0.20.0

Analysis of Workflow Description Language (WDL) documents.
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
//! Validation of supported syntax for WDL versions.

use wdl_ast::AstNode;
use wdl_ast::AstToken;
use wdl_ast::Diagnostic;
use wdl_ast::Span;
use wdl_ast::SupportedVersion;
use wdl_ast::v1;
use wdl_ast::v1::Exponentiation;
use wdl_ast::v1::Expr;
use wdl_ast::v1::HintsKeyword;
use wdl_ast::v1::InputKeyword;
use wdl_ast::v1::MetaKeyword;
use wdl_ast::v1::ParameterMetaKeyword;
use wdl_ast::v1::RequirementsKeyword;
use wdl_ast::version::V1;

use crate::Config;
use crate::Diagnostics;
use crate::VisitReason;
use crate::Visitor;
use crate::document::Document;

/// Creates an "exponentiation requirement" diagnostic.
fn exponentiation_requirement(span: Span) -> Diagnostic {
    Diagnostic::error("use of the exponentiation operator requires WDL version 1.2")
        .with_highlight(span)
}

/// Creates a "requirements section requirement" diagnostic.
fn requirements_section(span: Span) -> Diagnostic {
    Diagnostic::error("use of the `requirements` section requires WDL version 1.2")
        .with_highlight(span)
}

/// Creates a "hints section requirement" diagnostic.
fn hints_section(span: Span) -> Diagnostic {
    Diagnostic::error("use of the `hints` section requires WDL version 1.2").with_highlight(span)
}

/// Creates a "multi-line string requirement" diagnostic.
fn multiline_string_requirement(span: Span) -> Diagnostic {
    Diagnostic::error("use of multi-line strings requires WDL version 1.2").with_highlight(span)
}

/// Creates a "directory type" requirement diagnostic.
fn directory_type_requirement(span: Span) -> Diagnostic {
    Diagnostic::error("use of the `Directory` type requires WDL version 1.2").with_highlight(span)
}

/// Creates an "implicit binding" requirement diagnostic.
fn implicit_binding_requirement(span: Span) -> Diagnostic {
    Diagnostic::error("use of implicit input bindings requires WDL version 1.1 or later")
        .with_highlight(span)
}

/// Creates an "input keyword" requirement diagnostic.
fn input_keyword_requirement(span: Span) -> Diagnostic {
    Diagnostic::error("omitting the `input` keyword in a call statement requires WDL version 1.2")
        .with_label("missing an `input` keyword before this input", span)
        .with_fix("add an `input` keyword followed by a colon before any call inputs")
}

/// Creates a "struct literal requirement" diagnostic.
fn struct_literal_requirement(span: Span) -> Diagnostic {
    Diagnostic::error("use of a struct literal requires WDL version 1.1 or later")
        .with_highlight(span)
}

/// Creates a "struct metadata requirement" diagnostic.
fn struct_metadata_requirement(kind: &str, span: Span) -> Diagnostic {
    Diagnostic::error(format!(
        "use of a `{kind}` section in a struct definition requires WDL version 1.2"
    ))
    .with_highlight(span)
}

/// Creates an "env var" requirement diagnostic.
fn env_var_requirement(span: Span) -> Diagnostic {
    Diagnostic::error("use of environment variable declarations requires WDL version 1.2")
        .with_highlight(span)
}

/// Creates a deprecation warning for a deprecated version feature flag.
fn deprecated_version_feature_flag(flag_name: &str, span: Span) -> Diagnostic {
    Diagnostic::warning(format!(
        "the `{flag_name}` feature flag is deprecated; please remove this feature flag from your \
         configuration file"
    ))
    .with_highlight(span)
}

/// Creates an "unsupported version" diagnostic.
fn unsupported_version(version: SupportedVersion, span: Span) -> Diagnostic {
    Diagnostic::error(format!("unsupported version {version}")).with_highlight(span)
}

/// Creates an "unstable WDL 1.4" diagnostic.
fn unstable_wdl_1_4(span: Span) -> Diagnostic {
    Diagnostic::error(
        "WDL version 1.4 is unstable; set `feature_flags.wdl_1_4 = true` in your configuration to \
         opt in",
    )
    .with_highlight(span)
}

/// Creates a "symbolic module path requires WDL 1.4" diagnostic.
fn symbolic_path_requires_wdl_1_4(span: Span) -> Diagnostic {
    Diagnostic::error("use of a symbolic module path requires WDL version 1.4").with_highlight(span)
}

/// Creates a "`from` selection requires WDL 1.4" diagnostic.
fn from_selection_requires_wdl_1_4(span: Span) -> Diagnostic {
    Diagnostic::error("use of a `from` selection clause requires WDL version 1.4")
        .with_highlight(span)
}

/// Tracks the state of a deprecated version feature flag.
#[derive(Clone, Copy, Debug, Default)]
struct DeprecatedVersionFeatureFlag {
    /// Whether the user explicitly disabled the feature flag.
    explicitly_disabled: bool,
    /// Whether the deprecation warning has been emitted.
    warning_emitted: bool,
}

/// An AST visitor that ensures the syntax present in the document matches the
/// document's declared version.
#[derive(Debug, Default)]
pub struct VersionVisitor {
    /// The state of the deprecated `wdl_1_3` feature flag.
    wdl_1_3_ff: DeprecatedVersionFeatureFlag,
    /// Whether the `wdl_1_4` feature flag is enabled.
    wdl_1_4_enabled: bool,
    /// Stores the supported version of the WDL document we're visiting.
    version: Option<SupportedVersion>,
}

impl Visitor for VersionVisitor {
    fn register(&mut self, config: &Config) {
        self.wdl_1_3_ff.explicitly_disabled = !config.feature_flags().wdl_1_3();
        self.wdl_1_4_enabled = config.feature_flags().wdl_1_4();
    }

    fn reset(&mut self) {
        let wdl_1_3_ff = self.wdl_1_3_ff;
        let wdl_1_4_enabled = self.wdl_1_4_enabled;
        *self = Default::default();
        self.wdl_1_3_ff = wdl_1_3_ff;
        self.wdl_1_4_enabled = wdl_1_4_enabled;
    }

    fn document(
        &mut self,
        _: &mut Diagnostics,
        reason: VisitReason,
        _: &Document,
        version: SupportedVersion,
    ) {
        if reason == VisitReason::Exit {
            return;
        }

        self.version = Some(version);
    }

    fn version_statement(
        &mut self,
        diagnostics: &mut Diagnostics,
        reason: VisitReason,
        stmt: &wdl_ast::VersionStatement,
    ) {
        if reason == VisitReason::Exit {
            return;
        }

        // Emit a deprecation warning if the user explicitly disabled WDL 1.3 and we
        // encounter a WDL 1.3 document.
        if let Some(version) = self.version {
            match version {
                SupportedVersion::V1(V1::Three)
                    if self.wdl_1_3_ff.explicitly_disabled && !self.wdl_1_3_ff.warning_emitted =>
                {
                    diagnostics.add(deprecated_version_feature_flag(
                        "wdl_1_3",
                        stmt.version().span(),
                    ));
                    self.wdl_1_3_ff.warning_emitted = true;
                }
                SupportedVersion::V1(V1::Four) if !self.wdl_1_4_enabled => {
                    diagnostics.add(unstable_wdl_1_4(stmt.version().span()));
                }
                // TODO ACF 2025-10-21: This is an unfortunate consequence of using
                // `#[non_exhaustive]` on the version enums. We should consider removing that
                // attribute in the future to get static assurance that downstream consumers of
                // versions comprehensively handle the possible cases.
                SupportedVersion::V1(V1::Zero | V1::One | V1::Two | V1::Three | V1::Four) => {}
                other => diagnostics.add(unsupported_version(other, stmt.version().span())),
            }
        }
    }

    fn import_statement(
        &mut self,
        diagnostics: &mut Diagnostics,
        reason: VisitReason,
        stmt: &v1::ImportStatement,
    ) {
        if reason == VisitReason::Exit {
            return;
        }

        let Some(version) = self.version else {
            return;
        };
        if version >= SupportedVersion::V1(V1::Four) {
            return;
        }

        // Forms 2 and 3 already require `from`, which subsumes the
        // symbolic-path-only case.
        if let Some(from) = stmt.from_keyword() {
            diagnostics.add(from_selection_requires_wdl_1_4(from.span()));
        } else if let v1::ImportSource::ModulePath(path) = stmt.source() {
            diagnostics.add(symbolic_path_requires_wdl_1_4(path.span()));
        }
    }

    fn requirements_section(
        &mut self,
        diagnostics: &mut Diagnostics,
        reason: VisitReason,
        section: &v1::RequirementsSection,
    ) {
        if reason == VisitReason::Exit {
            return;
        }

        if let Some(version) = self.version
            && version < SupportedVersion::V1(V1::Two)
        {
            diagnostics.add(requirements_section(
                section
                    .token::<RequirementsKeyword<_>>()
                    .expect("should have keyword")
                    .span(),
            ));
        }
    }

    fn task_hints_section(
        &mut self,
        diagnostics: &mut Diagnostics,
        reason: VisitReason,
        section: &v1::TaskHintsSection,
    ) {
        if reason == VisitReason::Exit {
            return;
        }

        if let Some(version) = self.version
            && version < SupportedVersion::V1(V1::Two)
        {
            diagnostics.add(hints_section(
                section
                    .token::<HintsKeyword<_>>()
                    .expect("should have keyword")
                    .span(),
            ));
        }
    }

    fn workflow_hints_section(
        &mut self,
        diagnostics: &mut Diagnostics,
        reason: VisitReason,
        section: &v1::WorkflowHintsSection,
    ) {
        if reason == VisitReason::Exit {
            return;
        }

        if let Some(version) = self.version
            && version < SupportedVersion::V1(V1::Two)
        {
            diagnostics.add(hints_section(
                section
                    .token::<HintsKeyword<_>>()
                    .expect("should have keyword")
                    .span(),
            ));
        }
    }

    fn expr(&mut self, diagnostics: &mut Diagnostics, reason: VisitReason, expr: &v1::Expr) {
        if reason == VisitReason::Exit {
            return;
        }

        if let Some(version) = self.version {
            match expr {
                Expr::Exponentiation(e) if version < SupportedVersion::V1(V1::Two) => {
                    diagnostics.add(exponentiation_requirement(
                        e.token::<Exponentiation<_>>()
                            .expect("should have operator")
                            .span(),
                    ));
                }
                v1::Expr::Literal(v1::LiteralExpr::String(s))
                    if version < SupportedVersion::V1(V1::Two)
                        && s.kind() == v1::LiteralStringKind::Multiline =>
                {
                    diagnostics.add(multiline_string_requirement(s.span()));
                }
                _ => {}
            }
        }
    }

    fn bound_decl(
        &mut self,
        diagnostics: &mut Diagnostics,
        reason: VisitReason,
        decl: &v1::BoundDecl,
    ) {
        if reason == VisitReason::Exit {
            return;
        }

        if let Some(version) = self.version {
            if let Some(env) = decl.env()
                && version < SupportedVersion::V1(V1::Two)
            {
                diagnostics.add(env_var_requirement(env.span()));
            }

            if let v1::Type::Primitive(ty) = decl.ty()
                && version < SupportedVersion::V1(V1::Two)
                && ty.kind() == v1::PrimitiveTypeKind::Directory
            {
                diagnostics.add(directory_type_requirement(ty.span()));
            }

            if version < SupportedVersion::V1(V1::One)
                && let expr @ v1::Expr::Literal(v1::LiteralExpr::Struct(_)) = decl.expr()
            {
                diagnostics.add(struct_literal_requirement(expr.span()));
            }
        }
    }

    fn unbound_decl(
        &mut self,
        diagnostics: &mut Diagnostics,
        reason: VisitReason,
        decl: &v1::UnboundDecl,
    ) {
        if reason == VisitReason::Exit {
            return;
        }

        if let Some(version) = self.version {
            if let Some(env) = decl.env()
                && version < SupportedVersion::V1(V1::Two)
            {
                diagnostics.add(env_var_requirement(env.span()));
            }

            if let v1::Type::Primitive(ty) = decl.ty()
                && version < SupportedVersion::V1(V1::Two)
                && ty.kind() == v1::PrimitiveTypeKind::Directory
            {
                diagnostics.add(directory_type_requirement(ty.span()));
            }
        }
    }

    fn call_statement(
        &mut self,
        diagnostics: &mut Diagnostics,
        reason: VisitReason,
        stmt: &v1::CallStatement,
    ) {
        if reason == VisitReason::Exit {
            return;
        }

        let Some(version) = self.version else {
            return;
        };

        if version < SupportedVersion::V1(V1::Two) {
            // Ensure there is a input keyword child token if there are inputs
            if let Some(input) = stmt.inputs().next()
                && stmt.token::<InputKeyword<_>>().is_none()
            {
                diagnostics.add(input_keyword_requirement(input.span()));
            }
        }

        if version < SupportedVersion::V1(V1::One) {
            for input in stmt.inputs() {
                if input.is_implicit_bind() {
                    diagnostics.add(implicit_binding_requirement(input.span()));
                }
            }
        }
    }

    fn struct_definition(
        &mut self,
        diagnostics: &mut Diagnostics,
        reason: VisitReason,
        def: &v1::StructDefinition,
    ) {
        if reason == VisitReason::Exit {
            return;
        }

        if let Some(version) = self.version
            && version < SupportedVersion::V1(V1::Two)
        {
            if let Some(section) = def.metadata().next() {
                diagnostics.add(struct_metadata_requirement(
                    "meta",
                    section
                        .token::<MetaKeyword<_>>()
                        .expect("should have keyword")
                        .span(),
                ));
            }

            if let Some(section) = def.parameter_metadata().next() {
                diagnostics.add(struct_metadata_requirement(
                    "parameter_meta",
                    section
                        .token::<ParameterMetaKeyword<_>>()
                        .expect("should have keyword")
                        .span(),
                ));
            }
        }
    }
}