mise 2026.9.0

Dev tools, env vars, and tasks in one CLI
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
use std::collections::{BTreeMap, BTreeSet};
use std::path::{Component, Path, PathBuf};

use eyre::{Context, Result, bail};

use super::{
    ProjectId, WorkspaceDiscoveryContext, WorkspaceProject, WorkspaceProvenance, WorkspaceProvider,
};

const GO_MOD: &str = "go.mod";
const GO_WORK: &str = "go.work";

/// Discovers Go modules listed by a workspace file.
#[derive(Debug, Default)]
pub(crate) struct GoWorkspaceProvider;

impl WorkspaceProvider for GoWorkspaceProvider {
    fn id(&self) -> &str {
        "go"
    }

    fn discover(&self, workspace_root: &Path) -> Result<Vec<WorkspaceProject>> {
        let context = WorkspaceDiscoveryContext::new();
        self.discover_with_context(workspace_root, &context)
    }

    fn discover_with_context(
        &self,
        workspace_root: &Path,
        context: &WorkspaceDiscoveryContext,
    ) -> Result<Vec<WorkspaceProject>> {
        let workfile_path = workspace_root.join(GO_WORK);
        if !context.is_file(&workfile_path) {
            return Ok(Vec::new());
        }
        let canonical_root = context.canonicalize(workspace_root).wrap_err_with(|| {
            format!(
                "failed to resolve Go workspace root {}",
                workspace_root.display()
            )
        })?;
        let lexical_root = lexical_absolute(workspace_root)?;
        let module_directories = read_workfile(context, &workfile_path)?;
        let mut modules = BTreeMap::new();

        for directory in module_directories {
            let candidate = if directory.is_absolute() {
                directory
            } else {
                workspace_root.join(directory)
            };
            let lexical_candidate = lexical_absolute(&candidate)?;
            if !lexical_candidate.starts_with(&lexical_root)
                && !lexical_candidate.starts_with(&canonical_root)
            {
                continue;
            }
            let candidate = lexical_candidate;
            if !context.exists(&candidate) {
                if missing_path_resolves_outside(&candidate, &lexical_root, &canonical_root)? {
                    continue;
                }
                bail!(
                    "Go workspace module {} is missing {GO_MOD}",
                    candidate.display()
                );
            }
            let canonical_module = context.canonicalize(&candidate).wrap_err_with(|| {
                format!(
                    "failed to resolve Go workspace module {}",
                    candidate.display()
                )
            })?;
            let Ok(relative) = canonical_module.strip_prefix(&canonical_root) else {
                continue;
            };
            if !context.is_file(&canonical_module.join(GO_MOD)) {
                bail!(
                    "Go workspace module {} is missing {GO_MOD}",
                    candidate.display()
                );
            }
            let relative = normalize_relative(relative);
            let module_path = read_module_path(context, &canonical_module.join(GO_MOD))?;
            let id = ProjectId::new(self.id(), &module_path)?;
            let source = relative.join(GO_MOD);
            let provenance = WorkspaceProvenance {
                provider: Some(self.id().to_string()),
                source: Some(source),
            };
            let mut project = WorkspaceProject::new(id, relative);
            project.provenance = provenance;
            project
                .metadata
                .insert("workspace_source".to_string(), GO_MOD.to_string());
            modules.insert(canonical_module, project);
        }

        Ok(modules.into_values().collect())
    }
}

fn read_workfile(context: &WorkspaceDiscoveryContext, path: &Path) -> Result<Vec<PathBuf>> {
    let contents = context
        .read_to_string(path)
        .wrap_err_with(|| format!("failed to read Go workspace metadata {}", path.display()))?;
    parse_workfile(&contents)
        .wrap_err_with(|| format!("failed to parse Go workspace metadata {}", path.display()))
}

fn parse_workfile(contents: &str) -> Result<Vec<PathBuf>> {
    let mut directories = Vec::new();
    let mut in_use_block = false;

    for (index, raw_line) in contents.lines().enumerate() {
        let line_number = index + 1;
        let line = strip_comment(raw_line).trim();
        if line.is_empty() {
            continue;
        }
        if in_use_block {
            if line == ")" {
                in_use_block = false;
                continue;
            }
            directories.push(PathBuf::from(parse_argument(line).wrap_err_with(|| {
                format!("invalid use directive on line {line_number}")
            })?));
            continue;
        }

        let Some(arguments) = directive_arguments(line, "use") else {
            continue;
        };
        if arguments == "(" {
            in_use_block = true;
        } else {
            directories.push(PathBuf::from(parse_argument(arguments).wrap_err_with(
                || format!("invalid use directive on line {line_number}"),
            )?));
        }
    }

    if in_use_block {
        bail!("unterminated use block");
    }
    Ok(directories)
}

fn read_module_path(context: &WorkspaceDiscoveryContext, path: &Path) -> Result<String> {
    let contents = context
        .read_to_string(path)
        .wrap_err_with(|| format!("failed to read Go module metadata {}", path.display()))?;
    let mut module_path = None;

    for (index, raw_line) in contents.lines().enumerate() {
        let line = strip_comment(raw_line).trim();
        let Some(arguments) = directive_arguments(line, "module") else {
            continue;
        };
        let parsed = parse_argument(arguments)
            .wrap_err_with(|| format!("invalid module directive on line {}", index + 1))?;
        if module_path.replace(parsed).is_some() {
            bail!("Go module metadata contains multiple module directives");
        }
    }

    module_path.ok_or_else(|| eyre::eyre!("Go module metadata is missing a module directive"))
}

fn directive_arguments<'a>(line: &'a str, directive: &str) -> Option<&'a str> {
    let rest = line.strip_prefix(directive)?;
    if !rest.starts_with(char::is_whitespace) {
        return None;
    }
    Some(rest.trim())
}

fn parse_argument(value: &str) -> Result<String> {
    if value.is_empty() {
        bail!("directive requires one argument");
    }
    if let Some(value) = value.strip_prefix('"') {
        let Some(value) = value.strip_suffix('"') else {
            bail!("unterminated interpreted Go string");
        };
        return unescape_go_string(value);
    }
    if let Some(value) = value.strip_prefix('`') {
        let Some(value) = value.strip_suffix('`') else {
            bail!("unterminated raw Go string");
        };
        if value.contains('`') {
            bail!("unexpected raw Go string delimiter");
        }
        return Ok(value.to_string());
    }
    if value.split_whitespace().count() != 1 {
        bail!("directive requires exactly one argument");
    }
    Ok(value.to_string())
}

fn unescape_go_string(value: &str) -> Result<String> {
    let mut characters = value.chars();
    let mut unescaped = String::new();
    while let Some(character) = characters.next() {
        if character == '"' {
            bail!("unexpected interpreted Go string delimiter");
        }
        if character != '\\' {
            unescaped.push(character);
            continue;
        }
        let escape = characters
            .next()
            .ok_or_else(|| eyre::eyre!("unterminated Go string escape"))?;
        let escaped = match escape {
            'a' => '\u{0007}',
            'b' => '\u{0008}',
            'f' => '\u{000c}',
            'n' => '\n',
            'r' => '\r',
            't' => '\t',
            'v' => '\u{000b}',
            '\\' => '\\',
            '\'' => '\'',
            '"' => '"',
            'x' => escape_character(&mut characters, 2, 16)?,
            'u' => escape_character(&mut characters, 4, 16)?,
            'U' => escape_character(&mut characters, 8, 16)?,
            digit @ '0'..='7' => {
                let mut digits = digit.to_string();
                for _ in 0..2 {
                    digits.push(
                        characters
                            .next()
                            .ok_or_else(|| eyre::eyre!("incomplete octal Go string escape"))?,
                    );
                }
                decoded_character(&digits, 8)?
            }
            _ => bail!("unsupported Go string escape \\{escape}"),
        };
        unescaped.push(escaped);
    }
    Ok(unescaped)
}

fn escape_character(
    characters: &mut std::str::Chars<'_>,
    length: usize,
    radix: u32,
) -> Result<char> {
    let digits = characters.take(length).collect::<String>();
    if digits.chars().count() != length {
        bail!("incomplete Go string escape");
    }
    decoded_character(&digits, radix)
}

fn decoded_character(digits: &str, radix: u32) -> Result<char> {
    let value = u32::from_str_radix(digits, radix)
        .wrap_err_with(|| format!("invalid Go string escape {digits:?}"))?;
    char::from_u32(value).ok_or_else(|| eyre::eyre!("invalid Go character escape {digits:?}"))
}

fn strip_comment(line: &str) -> &str {
    let mut quote = None;
    let mut escaped = false;
    for (index, character) in line.char_indices() {
        match quote {
            Some('"') if escaped => escaped = false,
            Some('"') if character == '\\' => escaped = true,
            Some(delimiter) if character == delimiter => quote = None,
            Some(_) => {}
            None if matches!(character, '"' | '`') => quote = Some(character),
            None if character == '/' && line[index..].starts_with("//") => {
                return &line[..index];
            }
            None => {}
        }
    }
    line
}

fn normalize_relative(path: &Path) -> PathBuf {
    if path.as_os_str().is_empty() {
        PathBuf::from(".")
    } else {
        path.to_path_buf()
    }
}

fn lexical_absolute(path: &Path) -> Result<PathBuf> {
    let absolute = if path.is_absolute() {
        path.to_path_buf()
    } else {
        std::env::current_dir()
            .wrap_err("failed to resolve the current directory")?
            .join(path)
    };
    let mut normalized = PathBuf::new();
    for component in absolute.components() {
        match component {
            Component::CurDir => {}
            Component::ParentDir => {
                if normalized.parent().is_some() {
                    normalized.pop();
                }
            }
            component => normalized.push(component.as_os_str()),
        }
    }
    Ok(normalized)
}

fn missing_path_resolves_outside(
    candidate: &Path,
    lexical_root: &Path,
    canonical_root: &Path,
) -> Result<bool> {
    let mut candidate = candidate.to_path_buf();
    let mut followed = BTreeSet::new();
    loop {
        if !candidate.starts_with(lexical_root) && !candidate.starts_with(canonical_root) {
            return Ok(true);
        }
        let mut next = None;
        for ancestor in candidate.ancestors() {
            if !ancestor.starts_with(lexical_root) && !ancestor.starts_with(canonical_root) {
                break;
            }
            if let Ok(target) = std::fs::read_link(ancestor) {
                if !followed.insert(ancestor.to_path_buf()) {
                    return Ok(false);
                }
                let target = if target.is_absolute() {
                    target
                } else {
                    ancestor.parent().unwrap_or(lexical_root).join(target)
                };
                let suffix = candidate.strip_prefix(ancestor)?;
                next = Some(lexical_absolute(&target.join(suffix))?);
                break;
            }
            if ancestor == lexical_root || ancestor == canonical_root {
                break;
            }
        }
        match next {
            Some(next) => candidate = next,
            None => return Ok(false),
        }
    }
}

#[cfg(test)]
mod tests {
    use std::fs;

    use tempfile::tempdir;

    use super::*;

    fn write(path: &Path, contents: &str) {
        fs::create_dir_all(path.parent().unwrap()).unwrap();
        fs::write(path, contents).unwrap();
    }

    fn module(path: &Path, module_path: &str) {
        write(
            &path.join(GO_MOD),
            &format!("module {module_path}\n\ngo 1.25\n"),
        );
    }

    #[test]
    fn discovers_single_and_block_use_directives() {
        let temp = tempdir().unwrap();
        write(
            &temp.path().join(GO_WORK),
            "go 1.25\nuse ./cmd/api // app\nuse (\n ./lib\n \"./tools/code\\x20gen\"\n)\n",
        );
        module(&temp.path().join("cmd/api"), "example.com/api");
        module(&temp.path().join("lib"), "example.com/lib");
        module(&temp.path().join("tools/code gen"), "example.com/codegen");

        let projects = GoWorkspaceProvider.discover(temp.path()).unwrap();
        let summary = projects
            .iter()
            .map(|project| (project.id.as_str(), project.root.as_path()))
            .collect::<Vec<_>>();

        assert_eq!(
            summary,
            vec![
                ("go:example.com/api", Path::new("cmd/api")),
                ("go:example.com/lib", Path::new("lib")),
                ("go:example.com/codegen", Path::new("tools/code gen")),
            ]
        );
        assert!(
            projects
                .iter()
                .all(|project| project.dependencies.is_empty())
        );
    }

    #[test]
    fn supports_root_modules_and_quoted_module_paths() {
        let temp = tempdir().unwrap();
        write(&temp.path().join(GO_WORK), "go 1.25\nuse .\n");
        write(
            &temp.path().join(GO_MOD),
            "module \"example.com/root\" // root module\n",
        );

        let projects = GoWorkspaceProvider.discover(temp.path()).unwrap();

        assert_eq!(projects[0].id.as_str(), "go:example.com/root");
        assert_eq!(projects[0].root, Path::new("."));
        assert_eq!(projects[0].provenance.source, Some("./go.mod".into()));
    }

    #[test]
    fn interpreted_strings_require_three_digit_octal_escapes() {
        assert_eq!(unescape_go_string(r"code\040gen").unwrap(), "code gen");
        assert!(unescape_go_string(r"code\0gen").is_err());
        assert!(unescape_go_string(r"code\04gen").is_err());
    }

    #[test]
    fn cleans_use_paths_before_filesystem_access() {
        let temp = tempdir().unwrap();
        write(
            &temp.path().join(GO_WORK),
            "go 1.25\nuse ./missing/../app\n",
        );
        module(&temp.path().join("app"), "example.com/app");

        let projects = GoWorkspaceProvider.discover(temp.path()).unwrap();

        assert_eq!(projects.len(), 1);
        assert_eq!(projects[0].id.as_str(), "go:example.com/app");
    }

    #[test]
    fn skips_modules_outside_the_monorepo_root() {
        let temp = tempdir().unwrap();
        let workspace = temp.path().join("workspace");
        let external = temp.path().join("external");
        write(
            &workspace.join(GO_WORK),
            "go 1.25\nuse ./app\nuse ../external\nuse ../not-a-module\nuse ../missing\n",
        );
        module(&workspace.join("app"), "example.com/app");
        module(&external, "example.com/external");
        fs::create_dir_all(temp.path().join("not-a-module")).unwrap();

        let projects = GoWorkspaceProvider.discover(&workspace).unwrap();

        assert_eq!(projects.len(), 1);
        assert_eq!(projects[0].id.as_str(), "go:example.com/app");
    }

    #[test]
    fn reports_invalid_workspace_modules() {
        let temp = tempdir().unwrap();
        write(&temp.path().join(GO_WORK), "go 1.25\nuse ./missing\n");

        assert!(
            GoWorkspaceProvider
                .discover(temp.path())
                .unwrap_err()
                .to_string()
                .contains("missing go.mod")
        );

        write(&temp.path().join(GO_WORK), "go 1.25\nuse (\n");
        let error = GoWorkspaceProvider.discover(temp.path()).unwrap_err();
        assert!(format!("{error:#}").contains("unterminated use block"));
    }

    #[cfg(unix)]
    #[test]
    fn skips_missing_paths_through_external_symlinks() {
        let temp = tempdir().unwrap();
        let workspace = temp.path().join("workspace");
        let external = temp.path().join("external");
        fs::create_dir_all(&external).unwrap();
        write(
            &workspace.join(GO_WORK),
            "go 1.25\nuse ./app\nuse ./dangling-link\nuse ./external-link/missing\nuse ./chained-link/missing\n",
        );
        module(&workspace.join("app"), "example.com/app");
        std::os::unix::fs::symlink(external.join("missing"), workspace.join("dangling-link"))
            .unwrap();
        std::os::unix::fs::symlink(&external, workspace.join("external-link")).unwrap();
        std::os::unix::fs::symlink("external-link", workspace.join("chained-link")).unwrap();

        let projects = GoWorkspaceProvider.discover(&workspace).unwrap();

        assert_eq!(projects.len(), 1);
        assert_eq!(projects[0].id.as_str(), "go:example.com/app");
    }

    #[test]
    fn ignores_roots_without_a_workfile() {
        let temp = tempdir().unwrap();
        module(temp.path(), "example.com/standalone");

        assert!(
            GoWorkspaceProvider
                .discover(temp.path())
                .unwrap()
                .is_empty()
        );
    }
}