daml-lint 0.6.1

Static analysis scanner for Daml smart contracts
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
use daml_lint::detector::{ConfiguredDetector, Detector, Severity};
use daml_lint::detectors;
use serde::Deserialize;
use serde_json::Value;
use std::collections::{BTreeMap, BTreeSet};
use std::error::Error;
use std::path::{Path, PathBuf};

#[derive(Debug)]
pub enum ConfigError {
    MissingCurrentDir {
        source: std::io::Error,
    },
    ReadConfig {
        path: PathBuf,
        source: std::io::Error,
    },
    ParseConfig {
        path: PathBuf,
        source: serde_json::Error,
    },
    PluginResolveFailed {
        plugin: String,
        tried: Vec<PathBuf>,
    },
    PluginManifestMissingSection {
        plugin: String,
        path: PathBuf,
    },
    PluginManifestRead {
        path: PathBuf,
        source: std::io::Error,
    },
    PluginManifestParse {
        path: PathBuf,
        source: serde_json::Error,
    },
    UnknownRuleId {
        rule_id: String,
    },
    MissingPluginRule {
        plugin: String,
        rule: String,
        package_json: PathBuf,
    },
    RuleNameMismatch {
        plugin: String,
        rule: String,
        script: PathBuf,
        name: String,
    },
    RuleLoadFailed {
        plugin: String,
        rule: String,
        path: PathBuf,
        source: Box<crate::detectors::script::ScriptLoadError>,
    },
    RuleSettingMissingSeverity {
        value: String,
    },
    RuleSettingInvalidSeverity {
        value: String,
    },
    RuleSettingInvalidType {
        kind: String,
    },
}

impl std::fmt::Display for ConfigError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::MissingCurrentDir { source } => {
                write!(f, "could not resolve current directory: {source}")
            }
            Self::ReadConfig { path, source } => {
                write!(f, "could not read config {}: {source}", path.display())
            }
            Self::ParseConfig { path, source } => {
                write!(f, "invalid config {}: {source}", path.display())
            }
            Self::PluginResolveFailed { plugin, tried } => {
                let tried = tried
                    .iter()
                    .map(|path| path.display().to_string())
                    .collect::<Vec<_>>()
                    .join(", ");
                write!(f, "could not resolve plugin '{plugin}'. Tried: {tried}")
            }
            Self::PluginManifestMissingSection { plugin, path } => write!(
                f,
                "plugin '{plugin}' package {} is missing damlLint.rules",
                path.display()
            ),
            Self::PluginManifestRead { path, source } => {
                write!(f, "could not read {}: {source}", path.display())
            }
            Self::PluginManifestParse { path, source } => {
                write!(f, "invalid {}: {source}", path.display())
            }
            Self::UnknownRuleId { rule_id } => {
                write!(f, "configures unknown rule '{rule_id}'")
            }
            Self::MissingPluginRule {
                plugin,
                rule,
                package_json,
            } => write!(
                f,
                "plugin '{plugin}' does not declare rule '{rule}' in {}",
                package_json.display()
            ),
            Self::RuleNameMismatch {
                plugin,
                rule,
                script,
                name,
            } => write!(
                f,
                "plugin '{plugin}' declares rule '{rule}' but {} defines NAME '{name}'",
                script.display()
            ),
            Self::RuleLoadFailed {
                plugin,
                rule,
                path,
                source,
            } => write!(
                f,
                "plugin '{plugin}' rule '{rule}' failed to load from {}: {source}",
                path.display()
            ),
            Self::RuleSettingMissingSeverity { value } => {
                write!(
                    f,
                    "rule setting array must include a severity or 'off': {value}"
                )
            }
            Self::RuleSettingInvalidSeverity { value } => {
                write!(
                    f,
                    "rule setting must be a severity, 'off', or an array containing one: {value}",
                )
            }
            Self::RuleSettingInvalidType { kind } => {
                write!(
                    f,
                    "rule setting must be a severity, 'off', or an array (got {kind})"
                )
            }
        }
    }
}

impl Error for ConfigError {}

#[derive(Debug)]
pub struct LintConfig {
    base_dir: PathBuf,
    plugin_paths: Vec<PathBuf>,
    plugins: Vec<String>,
    rules: BTreeMap<String, RuleSetting>,
}

impl LintConfig {
    /// Read and validate `.daml-lint.json`, returning defaults when missing.
    ///
    /// Returns:
    /// - `Ok` with an explicit config loaded from `explicit_path`, or
    ///   defaults when that file is absent.
    /// - `Err` when the config path is unreadable, JSON is invalid, or plugin
    ///   metadata cannot be parsed.
    #[must_use = "propagate config read/parse failures"]
    pub fn load(explicit_path: Option<&Path>) -> Result<Self, ConfigError> {
        let Some(path) = find_config_path(explicit_path)? else {
            return Self::default_for_cwd();
        };

        let source = std::fs::read_to_string(&path).map_err(|source| ConfigError::ReadConfig {
            path: path.clone(),
            source,
        })?;
        let raw: RawConfig =
            serde_json::from_str(&source).map_err(|source| ConfigError::ParseConfig {
                path: path.clone(),
                source,
            })?;
        let base_dir = path
            .parent()
            .unwrap_or_else(|| Path::new("."))
            .to_path_buf();
        let plugin_paths = raw
            .plugin_paths
            .into_iter()
            .map(|path| resolve_config_path(&base_dir, path))
            .collect();

        Ok(Self {
            base_dir,
            plugin_paths,
            plugins: raw.plugins,
            rules: raw.rules,
        })
    }

    /// Load configured plugins into detector objects.
    ///
    /// Returns `Err` if plugin resolution fails, manifest loading fails, or any
    /// enabled plugin script cannot be loaded.
    #[must_use = "load plugin detectors and handle failures before linting"]
    pub fn load_plugin_detectors(&self) -> Result<Vec<Box<dyn Detector>>, ConfigError> {
        let mut detectors: Vec<Box<dyn Detector>> = Vec::new();
        for plugin in &self.plugins {
            let package_dir = self.resolve_plugin_package(plugin)?;
            let manifest = read_plugin_manifest(plugin, &package_dir)?;
            let namespace = plugin_namespace(plugin);

            for (rule_name, rule_id, setting) in self.enabled_rules_for_namespace(&namespace) {
                let Some(rule_path) = manifest.rules.get(rule_name) else {
                    return Err(ConfigError::MissingPluginRule {
                        plugin: plugin.to_string(),
                        rule: rule_name.to_string(),
                        package_json: package_dir.join("package.json"),
                    });
                };
                let script_path = package_dir.join(rule_path);
                let detector =
                    detectors::script::load_script_with_options(&script_path, &setting.options)
                        .map_err(|source| ConfigError::RuleLoadFailed {
                            plugin: plugin.to_string(),
                            rule: rule_name.to_string(),
                            path: script_path.clone(),
                            source: Box::new(source),
                        })?;
                if detector.name() != rule_name {
                    return Err(ConfigError::RuleNameMismatch {
                        plugin: plugin.to_string(),
                        rule: rule_name.to_string(),
                        script: script_path,
                        name: detector.name().to_string(),
                    });
                }
                detectors.push(Box::new(ConfiguredDetector::with_name(
                    detector,
                    rule_id.to_string(),
                )));
            }
        }
        Ok(detectors)
    }

    /// Apply configuration-level severity/enablement overrides to detectors in
    /// declaration order.
    #[must_use]
    pub fn apply_rule_settings(&self, detectors: Vec<Box<dyn Detector>>) -> Vec<Box<dyn Detector>> {
        detectors
            .into_iter()
            .filter_map(|detector| {
                let setting = self.rules.get(detector.name());
                if setting.is_some_and(|setting| !setting.enabled) {
                    return None;
                }
                let severity = setting.and_then(|setting| setting.severity);
                if let Some(severity) = severity {
                    let configured: Box<dyn Detector> =
                        Box::new(ConfiguredDetector::with_severity(detector, severity));
                    Some(configured)
                } else {
                    Some(detector)
                }
            })
            .collect()
    }

    /// Validate every enabled rule-id in config against the concrete detector set.
    ///
    /// Returns `Err` when the config references a rule-id that does not exist in
    /// `detectors`.
    #[must_use = "handle configuration validation errors before scanning"]
    pub fn validate_rule_settings(
        &self,
        detectors: &[Box<dyn Detector>],
    ) -> Result<(), ConfigError> {
        let detector_names: BTreeSet<&str> =
            detectors.iter().map(|detector| detector.name()).collect();
        for (rule_id, setting) in &self.rules {
            if setting.enabled && !detector_names.contains(rule_id.as_str()) {
                return Err(ConfigError::UnknownRuleId {
                    rule_id: rule_id.clone(),
                });
            }
        }
        Ok(())
    }

    fn default_for_cwd() -> Result<Self, ConfigError> {
        Ok(Self {
            base_dir: std::env::current_dir()
                .map_err(|source| ConfigError::MissingCurrentDir { source })?,
            plugin_paths: Vec::new(),
            plugins: Vec::new(),
            rules: BTreeMap::new(),
        })
    }

    fn enabled_rules_for_namespace(
        &self,
        namespace: &str,
    ) -> impl Iterator<Item = (&str, &str, &RuleSetting)> {
        let prefix = format!("{namespace}/");
        self.rules.iter().filter_map(move |(rule_id, setting)| {
            if !setting.enabled {
                return None;
            }
            rule_id
                .strip_prefix(&prefix)
                .map(|rule_name| (rule_name, rule_id.as_str(), setting))
        })
    }

    fn resolve_plugin_package(&self, plugin: &str) -> Result<PathBuf, ConfigError> {
        let candidates = package_candidates(plugin);
        let mut tried = Vec::new();

        for package_name in &candidates {
            for package_dir in self.package_dirs(package_name) {
                tried.push(package_dir.clone());
                if package_dir.join("package.json").is_file() {
                    return Ok(package_dir);
                }
            }
        }

        Err(ConfigError::PluginResolveFailed {
            plugin: plugin.to_string(),
            tried,
        })
    }

    fn package_dirs(&self, package_name: &str) -> Vec<PathBuf> {
        let mut dirs = vec![self.base_dir.join("node_modules").join(package_name)];
        for plugin_path in &self.plugin_paths {
            dirs.push(plugin_path.clone());
            dirs.push(plugin_path.join(package_name));
            dirs.push(plugin_path.join("node_modules").join(package_name));
        }
        dirs
    }
}

#[derive(Debug, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
struct RawConfig {
    #[serde(default)]
    plugin_paths: Vec<PathBuf>,
    #[serde(default)]
    plugins: Vec<String>,
    #[serde(default)]
    rules: BTreeMap<String, RuleSetting>,
}

#[derive(Debug, Clone)]
struct RuleSetting {
    enabled: bool,
    severity: Option<Severity>,
    options: Value,
}

impl RuleSetting {
    fn from_value(value: Value) -> Result<Self, ConfigError> {
        match value {
            Value::Array(items) => Self::from_array(&items),
            level => {
                Self::from_level_value(&level).map(|level| Self::from_level(level, empty_options()))
            }
        }
    }

    fn from_array(items: &[Value]) -> Result<Self, ConfigError> {
        let Some((level_value, option_values)) = items.split_first() else {
            return Err(ConfigError::RuleSettingMissingSeverity {
                value: "[]".to_string(),
            });
        };
        let level = Self::from_level_value(level_value)?;
        let options = match option_values {
            [] => empty_options(),
            [single] => single.clone(),
            many => Value::Array(many.to_vec()),
        };
        Ok(Self::from_level(level, options))
    }

    const fn from_level(level: RuleLevel, options: Value) -> Self {
        match level {
            RuleLevel::Off => Self {
                enabled: false,
                severity: None,
                options,
            },
            RuleLevel::Severity(severity) => Self {
                enabled: true,
                severity: Some(severity),
                options,
            },
        }
    }

    fn from_level_value(value: &Value) -> Result<RuleLevel, ConfigError> {
        match value {
            Value::String(level) => parse_level_string(level),
            Value::Number(level) => match level.as_u64() {
                Some(0) => Ok(RuleLevel::Off),
                Some(1) => Ok(RuleLevel::Severity(Severity::Medium)),
                Some(2) => Ok(RuleLevel::Severity(Severity::High)),
                _ => Err(ConfigError::RuleSettingInvalidSeverity {
                    value: value.to_string(),
                }),
            },
            _ => Err(ConfigError::RuleSettingInvalidType {
                kind: serde_json::to_string(value).unwrap_or_else(|_| "value".to_string()),
            }),
        }
    }
}

impl<'de> Deserialize<'de> for RuleSetting {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = Value::deserialize(deserializer)?;
        Self::from_value(value).map_err(serde::de::Error::custom)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum RuleLevel {
    Off,
    Severity(Severity),
}

#[derive(Debug, Deserialize)]
struct PackageJson {
    #[serde(rename = "damlLint")]
    daml_lint: Option<PluginManifest>,
}

#[derive(Debug, Deserialize)]
struct PluginManifest {
    rules: BTreeMap<String, PathBuf>,
}

fn find_config_path(explicit_path: Option<&Path>) -> Result<Option<PathBuf>, ConfigError> {
    if let Some(path) = explicit_path {
        return Ok(Some(path.to_path_buf()));
    }

    let path = std::env::current_dir()
        .map_err(|source| ConfigError::MissingCurrentDir { source })?
        .join(".daml-lint.json");
    Ok(path.is_file().then_some(path))
}

fn resolve_config_path(base_dir: &Path, path: PathBuf) -> PathBuf {
    if path.is_absolute() {
        path
    } else {
        base_dir.join(path)
    }
}

fn read_plugin_manifest(plugin: &str, package_dir: &Path) -> Result<PluginManifest, ConfigError> {
    let package_json_path = package_dir.join("package.json");
    let source = std::fs::read_to_string(&package_json_path).map_err(|source| {
        ConfigError::PluginManifestRead {
            path: package_json_path.clone(),
            source,
        }
    })?;
    let package_json: PackageJson =
        serde_json::from_str(&source).map_err(|source| ConfigError::PluginManifestParse {
            path: package_json_path.clone(),
            source,
        })?;
    package_json
        .daml_lint
        .ok_or_else(|| ConfigError::PluginManifestMissingSection {
            plugin: plugin.to_string(),
            path: package_json_path,
        })
}

fn package_candidates(plugin: &str) -> Vec<String> {
    if let Some((scope, package)) = plugin.split_once('/') {
        if package.starts_with("daml-lint-plugin-") {
            vec![plugin.to_string()]
        } else {
            vec![
                format!("{scope}/daml-lint-plugin-{package}"),
                plugin.to_string(),
            ]
        }
    } else if plugin.starts_with("daml-lint-plugin-") {
        vec![plugin.to_string()]
    } else {
        vec![format!("daml-lint-plugin-{plugin}"), plugin.to_string()]
    }
}

fn plugin_namespace(plugin: &str) -> String {
    if let Some((scope, package)) = plugin.split_once('/') {
        format!("{scope}/{}", strip_plugin_prefix(package))
    } else {
        strip_plugin_prefix(plugin).to_string()
    }
}

fn strip_plugin_prefix(package: &str) -> &str {
    package.strip_prefix("daml-lint-plugin-").unwrap_or(package)
}

fn parse_level_string(level: &str) -> Result<RuleLevel, ConfigError> {
    match level.to_lowercase().as_str() {
        "off" => Ok(RuleLevel::Off),
        "warn" | "warning" | "medium" => Ok(RuleLevel::Severity(Severity::Medium)),
        "error" | "high" => Ok(RuleLevel::Severity(Severity::High)),
        "critical" => Ok(RuleLevel::Severity(Severity::Critical)),
        "low" => Ok(RuleLevel::Severity(Severity::Low)),
        "info" => Ok(RuleLevel::Severity(Severity::Info)),
        _ => Err(ConfigError::RuleSettingInvalidSeverity {
            value: level.to_string(),
        }),
    }
}

fn empty_options() -> Value {
    Value::Object(serde_json::Map::new())
}