witforge-cli 0.7.0

Command-line interface for witforge.
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
//! CLI diagnostic rendering and collection.

use std::collections::BTreeSet;
use std::fmt::Write as _;
use std::fs;
use std::io::{self, Write};
use std::path::{Component, Path, PathBuf};

use anyhow::{Context, Result, anyhow};
use clap::ValueEnum;
use serde_json::{Value, json};

use witforge::diagnostic::{Diagnostic, LocatedDiagnostic, Position, Severity, help_uri};

/// Write `text` to stdout and flush, ignoring a closed pipe and returning any other I/O error.
pub(crate) fn write_stdout_str(text: &str) -> Result<bool> {
    let mut stdout = io::stdout().lock();
    match stdout
        .write_all(text.as_bytes())
        .and_then(|()| stdout.flush())
    {
        Ok(()) => Ok(true),
        Err(err) if err.kind() == io::ErrorKind::BrokenPipe => Ok(false),
        Err(err) => Err(anyhow!("failed to write to stdout: {err}")),
    }
}

/// Write `text` to stderr without panicking if stderr is closed or unwritable.
pub(crate) fn write_stderr_str(text: &str) {
    let mut stderr = io::stderr().lock();
    let _ = stderr.write_all(text.as_bytes());
    let _ = stderr.flush();
}

/// Write one line to stderr without panicking if stderr is closed or unwritable.
pub(crate) fn write_stderr_line(line: &str) {
    let mut stderr = io::stderr().lock();
    let _ = writeln!(stderr, "{line}");
    let _ = stderr.flush();
}

/// The diagnostic output format.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub(crate) enum Format {
    /// Human-readable diagnostics on stderr; the default.
    Human,
    /// A versioned JSON diagnostic envelope on stdout.
    Json,
    /// SARIF 2.1.0 on stdout.
    Sarif,
}

pub(crate) struct Reporter {
    format: Format,
    quiet: bool,
    diagnostics: Vec<LocatedDiagnostic>,
    suppressed: bool,
    stdout_closed: bool,
    stdout_error: Option<io::Error>,
}

impl Reporter {
    pub(crate) fn new(format: Format, quiet: bool) -> Self {
        Self {
            format,
            quiet,
            diagnostics: Vec::new(),
            suppressed: false,
            stdout_closed: false,
            stdout_error: None,
        }
    }

    pub(crate) fn human(&self) -> bool {
        self.format == Format::Human
    }

    pub(crate) fn is_quiet(&self) -> bool {
        self.quiet
    }

    pub(crate) fn suppress(&mut self) {
        self.suppressed = true;
    }

    pub(crate) fn is_suppressed(&self) -> bool {
        self.suppressed
    }

    pub(crate) fn write_stdout_line(&mut self, line: &str) {
        if self.stdout_closed || self.stdout_error.is_some() {
            return;
        }
        let mut stdout = io::stdout().lock();
        if let Err(err) = writeln!(stdout, "{line}") {
            if err.kind() == io::ErrorKind::BrokenPipe {
                self.stdout_closed = true;
            } else {
                self.stdout_error = Some(err);
            }
        }
    }

    pub(crate) fn push(&mut self, diagnostic: Diagnostic) {
        self.push_located(LocatedDiagnostic::from_diagnostic(diagnostic));
    }

    pub(crate) fn push_located(&mut self, located: LocatedDiagnostic) {
        if self.human() {
            write_stderr_line(&format!("error: {}", located.diagnostic.message));
        } else {
            self.diagnostics.push(located);
        }
    }

    pub(crate) fn note_located(&mut self, human: &str, located: LocatedDiagnostic) {
        if self.human() {
            if !self.quiet {
                self.write_stdout_line(human);
            }
        } else {
            self.diagnostics.push(located);
        }
    }

    pub(crate) fn finish(&mut self) -> Result<()> {
        if !self.suppressed && !self.human() {
            let rendered = match self.format {
                Format::Human => None,
                Format::Json => Some(diagnostic_json(&self.diagnostics)?),
                Format::Sarif => Some(sarif_json(&self.diagnostics)?),
            };
            if let Some(text) = rendered {
                self.write_stdout_line(&text);
            }
        }
        if !self.stdout_closed && self.stdout_error.is_none() {
            let mut stdout = io::stdout().lock();
            if let Err(err) = stdout.flush() {
                if err.kind() == io::ErrorKind::BrokenPipe {
                    self.stdout_closed = true;
                } else {
                    self.stdout_error = Some(err);
                }
            }
        }
        if let Some(err) = self.stdout_error.take() {
            return Err(anyhow!("failed to write to stdout: {err}"));
        }
        Ok(())
    }
}

/// Normalize a physical filesystem path for JSON `file` fields and workflow annotations:
/// cwd-relative with forward slashes when inside the current working directory, otherwise a
/// forward-slash cleaned absolute path (never containing `..` or Windows `\\?\` prefixes).
pub(crate) fn normalize_file_path(raw: &str) -> String {
    match resolve_cwd_relative(raw) {
        NormalizedPath::Relative(rel) => rel,
        NormalizedPath::Absolute(abs) => abs,
    }
}

/// Normalize a physical filesystem path for SARIF `artifactLocation.uri`:
/// cwd-relative with forward slashes when inside the current working directory, otherwise an
/// absolute percent-encoded `file:///` URI (never `..` and never a logical identifier).
pub(crate) fn sarif_artifact_uri(raw: &str) -> String {
    match resolve_cwd_relative(raw) {
        NormalizedPath::Relative(rel) => rel,
        NormalizedPath::Absolute(abs) => absolute_path_to_file_uri(&abs),
    }
}

enum NormalizedPath {
    Relative(String),
    Absolute(String),
}

fn resolve_cwd_relative(raw: &str) -> NormalizedPath {
    let input = Path::new(raw);
    let cwd = std::env::current_dir().ok();
    let cwd_canon = cwd.as_deref().and_then(|dir| fs::canonicalize(dir).ok());

    // Fast path for a relative path that lexically stays inside cwd without `..`.
    if !input.is_absolute() {
        let cleaned = clean_lexical(input);
        if !cleaned
            .components()
            .any(|component| matches!(component, Component::ParentDir))
        {
            return NormalizedPath::Relative(path_to_forward_slashes(&cleaned));
        }
    }

    let abs_lexical = if input.is_absolute() {
        clean_lexical(input)
    } else if let Some(ref cwd) = cwd {
        clean_lexical(&cwd.join(input))
    } else {
        clean_lexical(input)
    };

    if let Some(ref cwd) = cwd
        && let Ok(rel) = abs_lexical.strip_prefix(cwd)
    {
        return NormalizedPath::Relative(path_to_forward_slashes(rel));
    }

    let abs_canon = canonicalize_best_effort(&abs_lexical);
    if let Some(ref cwd_canon) = cwd_canon
        && let Ok(rel) = abs_canon.strip_prefix(cwd_canon)
    {
        return NormalizedPath::Relative(path_to_forward_slashes(rel));
    }

    NormalizedPath::Absolute(strip_verbatim_and_slashes(&abs_canon))
}

fn clean_lexical(path: &Path) -> PathBuf {
    let mut parts: Vec<Component<'_>> = Vec::new();
    for component in path.components() {
        match component {
            Component::CurDir => {}
            Component::ParentDir => match parts.last() {
                Some(Component::Normal(_)) => {
                    parts.pop();
                }
                Some(Component::RootDir | Component::Prefix(_)) => {}
                _ => parts.push(component),
            },
            _ => parts.push(component),
        }
    }
    if parts.is_empty() {
        PathBuf::from(".")
    } else {
        parts.into_iter().collect()
    }
}

fn canonicalize_best_effort(path: &Path) -> PathBuf {
    if let Ok(canon) = fs::canonicalize(path) {
        return canon;
    }
    let mut tail = Vec::new();
    let mut cursor = path;
    while let Some(name) = cursor.file_name() {
        tail.push(name.to_os_string());
        let Some(parent) = cursor.parent() else {
            break;
        };
        if let Ok(mut canon) = fs::canonicalize(parent) {
            for component in tail.into_iter().rev() {
                canon.push(component);
            }
            return canon;
        }
        cursor = parent;
    }
    path.to_path_buf()
}

fn path_to_forward_slashes(path: &Path) -> String {
    let raw = strip_verbatim_and_slashes(path);
    if raw.is_empty() { ".".to_string() } else { raw }
}

fn strip_verbatim_and_slashes(path: &Path) -> String {
    let text = path.to_string_lossy();
    let stripped = if let Some(unc) = text.strip_prefix(r"\\?\UNC\") {
        format!("//{unc}")
    } else if let Some(rest) = text.strip_prefix(r"\\?\") {
        rest.to_string()
    } else {
        text.into_owned()
    };
    stripped.replace('\\', "/")
}

fn absolute_path_to_file_uri(abs_forward: &str) -> String {
    let mut out = String::from("file://");
    if let Some(unc) = abs_forward.strip_prefix("//") {
        encode_uri_path(&mut out, unc);
    } else if abs_forward.starts_with('/') {
        encode_uri_path(&mut out, abs_forward);
    } else {
        // Windows drive path like `C:/path/to/file`.
        out.push('/');
        encode_uri_path(&mut out, abs_forward);
    }
    out
}

fn encode_uri_path(out: &mut String, path: &str) {
    for &byte in path.as_bytes() {
        if byte.is_ascii_alphanumeric() || matches!(byte, b'/' | b'-' | b'.' | b'_' | b'~' | b':') {
            out.push(char::from(byte));
        } else {
            let _ = write!(out, "%{byte:02X}");
        }
    }
}

fn diagnostic_json(diagnostics: &[LocatedDiagnostic]) -> Result<String> {
    let values: Vec<Value> = diagnostics.iter().map(diagnostic_value).collect();
    serde_json::to_string_pretty(&json!({
        "version": 1,
        "tool": {
            "name": "witforge",
            "version": env!("CARGO_PKG_VERSION"),
        },
        "diagnostics": values,
    }))
    .context("failed to render diagnostics as JSON")
}

fn diagnostic_value(located: &LocatedDiagnostic) -> Value {
    let diagnostic = &located.diagnostic;
    let mut value = json!({
        "code": diagnostic.code,
        "severity": diagnostic.severity.as_str(),
        "message": &diagnostic.message,
    });
    let object = value.as_object_mut().expect("JSON object");
    if let Some(path) = &diagnostic.path {
        object.insert("path".to_string(), Value::String(path.clone()));
    }
    if let Some(file) = &located.file {
        object.insert("file".to_string(), Value::String(normalize_file_path(file)));
    }
    if let Some(logical) = &located.logical {
        object.insert("logical".to_string(), Value::String(logical.clone()));
    }
    if let Some(position) = diagnostic.position {
        insert_position_fields(object, position);
    }
    if !located.related.is_empty() {
        let related: Vec<Value> = located
            .related
            .iter()
            .map(|rel| {
                let mut rel_val = json!({
                    "file": normalize_file_path(&rel.file),
                    "message": &rel.message,
                });
                if let (Some(pos), Some(rel_obj)) = (rel.position, rel_val.as_object_mut()) {
                    insert_position_fields(rel_obj, pos);
                }
                rel_val
            })
            .collect();
        object.insert("related".to_string(), Value::Array(related));
    }
    value
}

fn insert_position_fields(object: &mut serde_json::Map<String, Value>, position: Position) {
    object.insert("line".to_string(), json!(position.line));
    object.insert("column".to_string(), json!(position.column));
    object.insert("end_line".to_string(), json!(position.end_line));
    object.insert("end_column".to_string(), json!(position.end_column));
}

fn sarif_json(diagnostics: &[LocatedDiagnostic]) -> Result<String> {
    let mut rules = BTreeSet::new();
    for located in diagnostics {
        rules.insert(located.diagnostic.code);
    }
    let rules: Vec<Value> = rules
        .into_iter()
        .map(|code| {
            let description = witforge::diagnostic::lookup(code).map_or(code, |info| info.summary);
            json!({
                "id": code,
                "shortDescription": { "text": description },
                "helpUri": help_uri(code),
            })
        })
        .collect();
    let results: Vec<Value> = diagnostics.iter().map(sarif_result).collect();
    serde_json::to_string_pretty(&json!({
        "$schema": "https://json.schemastore.org/sarif-2.1.0.json",
        "version": "2.1.0",
        "runs": [{
            "tool": {
                "driver": {
                    "name": "witforge",
                    "version": env!("CARGO_PKG_VERSION"),
                    "informationUri": "https://github.com/asamarts/witforge",
                    "rules": rules,
                }
            },
            "results": results,
        }],
    }))
    .context("failed to render diagnostics as SARIF")
}

fn sarif_region(position: Position) -> Value {
    json!({
        "startLine": position.line,
        "startColumn": position.column,
        "endLine": position.end_line,
        "endColumn": position.end_column,
    })
}

fn sarif_result(located: &LocatedDiagnostic) -> Value {
    let diagnostic = &located.diagnostic;
    let mut result = json!({
        "ruleId": diagnostic.code,
        "level": diagnostic.severity.as_str(),
        "message": { "text": &diagnostic.message },
    });
    let mut location_obj = serde_json::Map::new();
    if let Some(file) = &located.file {
        let mut physical = json!({
            "artifactLocation": { "uri": sarif_artifact_uri(file) },
        });
        if let (Some(position), Some(object)) = (diagnostic.position, physical.as_object_mut()) {
            object.insert("region".to_string(), sarif_region(position));
        }
        location_obj.insert("physicalLocation".to_string(), physical);
    }
    if let Some(logical) = &located.logical {
        location_obj.insert(
            "logicalLocations".to_string(),
            json!([{ "fullyQualifiedName": logical }]),
        );
    }
    if !location_obj.is_empty() {
        result["locations"] = Value::Array(vec![Value::Object(location_obj)]);
    }
    if !located.related.is_empty() {
        let related: Vec<Value> = located
            .related
            .iter()
            .enumerate()
            .map(|(index, rel)| {
                let mut physical = json!({
                    "artifactLocation": { "uri": sarif_artifact_uri(&rel.file) },
                });
                if let (Some(pos), Some(obj)) = (rel.position, physical.as_object_mut()) {
                    obj.insert("region".to_string(), sarif_region(pos));
                }
                json!({
                    "id": index + 1,
                    "physicalLocation": physical,
                    "message": { "text": &rel.message },
                })
            })
            .collect();
        result["relatedLocations"] = Value::Array(related);
    }
    result
}

/// Build a [`LocatedDiagnostic`] for a note carrying a logical contract path and optional
/// physical source file/position.
pub(crate) fn located_note(
    code: &'static str,
    message: impl Into<String>,
    logical: String,
    physical: Option<&(String, Position)>,
    related: Option<(String, Option<Position>, &'static str)>,
) -> LocatedDiagnostic {
    let mut diagnostic = Diagnostic::new(code, Severity::Note, message).with_path(logical.clone());
    if let Some((_, pos)) = physical {
        diagnostic = diagnostic.with_position(*pos);
    }
    let mut located = LocatedDiagnostic::from_diagnostic(diagnostic).with_logical(logical);
    if let Some((file, _)) = physical {
        located = located.with_file(file.clone());
    }
    if let Some((file, position, rel_msg)) = related {
        located = located.with_related(witforge::diagnostic::RelatedLocation::new(
            file,
            position,
            rel_msg.to_string(),
        ));
    }
    located
}