mise 2026.8.13

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
use std::collections::{HashMap, HashSet};
use std::path::Path;
use std::process::Stdio;

use async_trait::async_trait;
use eyre::bail;

use super::{InstallOpts, PackageRequest, PackageState, PackageStatus, SystemPackageManager};
use crate::result::Result;
use crate::system::sudo;

/// Arch-family (Arch, Manjaro, EndeavourOS) via pacman
pub(crate) struct PacmanManager {}

impl PacmanManager {
    pub(crate) fn new() -> Self {
        Self {}
    }

    /// fresh container case: no sync databases, any install would fail
    fn dbs_missing(&self) -> bool {
        let sync = Path::new("/var/lib/pacman/sync");
        !crate::file::ls(sync).unwrap_or_default().iter().any(|p| {
            p.extension()
                .map(|e| e.to_string_lossy() == "db")
                .unwrap_or(false)
        })
    }

    fn refresh(&self, opts: &InstallOpts) -> Result<()> {
        let args = vec!["-Sy".to_string()];
        if opts.dry_run {
            miseprintln!("{}", sudo::argv("pacman", &args).join(" "));
            return Ok(());
        }
        sudo::run("pacman", &args, &[])
    }
}

fn parse_pacman_query(output: &str, requests: &[PackageRequest]) -> Vec<PackageStatus> {
    let mut installed: HashMap<&str, &str> = HashMap::new();
    for line in output.lines() {
        if let Some((name, version)) = line.split_once(' ') {
            installed.insert(name, version);
        }
    }
    requests
        .iter()
        .map(|req| {
            let state = match installed.get(req.name.as_str()) {
                Some(version) => package_state(req, version),
                None => PackageState::Missing,
            };
            PackageStatus {
                request: req.clone(),
                state,
            }
        })
        .collect()
}

fn package_state(req: &PackageRequest, version: &str) -> PackageState {
    // a pin matches the full version-pkgrel or just the version part (any
    // pkgrel)
    match &req.version {
        Some(requested)
            if version != requested && !version.starts_with(&format!("{requested}-")) =>
        {
            PackageState::VersionMismatch {
                installed: version.to_string(),
            }
        }
        _ => PackageState::Installed {
            version: version.to_string(),
        },
    }
}

fn parse_pacman_package(output: &str) -> Option<(&str, &str)> {
    output.lines().find_map(|line| line.split_once(' '))
}

fn parse_pacman_deptest(output: &str) -> HashSet<&str> {
    output
        .lines()
        .map(str::trim)
        .filter(|line| !line.is_empty())
        .collect()
}

fn deptest_requirement(req: &PackageRequest) -> String {
    match &req.version {
        Some(version) => format!("{}={version}", req.name),
        None => req.name.clone(),
    }
}

fn apply_provider_query<'a>(
    status: &mut PackageStatus,
    output: &'a str,
    constraint_satisfied: bool,
) -> Result<&'a str> {
    let Some((provider, version)) = parse_pacman_package(output) else {
        bail!(
            "pacman -Q returned no package for satisfied requirement '{}'",
            status.request.name
        );
    };
    // The provider's package version is display metadata; pacman -T evaluates
    // the requested version against the version declared in Provides.
    status.state = if constraint_satisfied {
        PackageState::Installed {
            version: version.to_string(),
        }
    } else {
        PackageState::VersionMismatch {
            installed: version.to_string(),
        }
    };
    Ok(provider)
}

async fn pacman_query(names: &[String]) -> Result<String> {
    if names.is_empty() {
        return Ok(String::new());
    }
    let mut args = vec!["-Q", "--"];
    args.extend(names.iter().map(String::as_str));
    debug!("$ pacman {}", args.join(" "));
    let output = tokio::process::Command::new("pacman")
        .args(&args)
        // pacman localizes its messages via gettext, so the "was not found"
        // check below only works against the untranslated output.
        .env("LC_ALL", "C")
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .await?;
    // pacman -Q exits 1 when any package is missing ("error: package 'x'
    // was not found" on stderr); installed ones still print to stdout.
    // Anything else on stderr (corrupt db, lock file) is a real error.
    let stderr = String::from_utf8_lossy(&output.stderr);
    let only_missing = !stderr.is_empty()
        && stderr
            .lines()
            .all(|line| line.trim().is_empty() || line.contains("was not found"));
    if !output.status.success() && (output.status.code() != Some(1) || !only_missing) {
        bail!("pacman -Q failed: {}", stderr.trim());
    }
    Ok(String::from_utf8_lossy(&output.stdout).into_owned())
}

async fn pacman_deptest(names: &[String]) -> Result<String> {
    if names.is_empty() {
        return Ok(String::new());
    }
    let mut args = vec!["-T", "--"];
    args.extend(names.iter().map(String::as_str));
    debug!("$ pacman {}", args.join(" "));
    let output = tokio::process::Command::new("pacman")
        .args(&args)
        .env("LC_ALL", "C")
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .await?;
    // deptest returns 127 when at least one requirement is unsatisfied and
    // prints those requirements to stdout. Both 0 and 127 are normal.
    if !matches!(output.status.code(), Some(0 | 127)) {
        let stderr = String::from_utf8_lossy(&output.stderr);
        bail!("pacman -T failed: {}", stderr.trim());
    }
    Ok(String::from_utf8_lossy(&output.stdout).into_owned())
}

#[async_trait(?Send)]
impl SystemPackageManager for PacmanManager {
    fn name(&self) -> &str {
        "pacman"
    }

    fn is_available(&self) -> bool {
        cfg!(target_os = "linux") && crate::file::which("pacman").is_some()
    }

    fn unavailable_reason(&self) -> String {
        if cfg!(target_os = "linux") {
            "pacman not found".to_string()
        } else {
            "only available on linux".to_string()
        }
    }

    async fn installed(&self, pkgs: &[PackageRequest]) -> Result<Vec<PackageStatus>> {
        if pkgs.is_empty() {
            return Ok(vec![]);
        }
        let names = pkgs.iter().map(|pkg| pkg.name.clone()).collect::<Vec<_>>();
        let stdout = pacman_query(&names).await?;
        let mut statuses = parse_pacman_query(&stdout, pkgs);
        let apparent_missing = statuses
            .iter()
            .filter(|status| matches!(status.state, PackageState::Missing))
            .map(|status| deptest_requirement(&status.request))
            .collect::<Vec<_>>();
        if apparent_missing.is_empty() {
            return Ok(statuses);
        }

        // A bare query may answer a virtual package target under the installed
        // provider's real name. Deptest tells us which apparent misses are
        // genuinely unsatisfied; query the provider-satisfied names one at a
        // time so their returned versions can be associated positionally.
        let deptest = pacman_deptest(&apparent_missing).await?;
        let unsatisfied = parse_pacman_deptest(&deptest);
        // A failed version constraint can still have a provider for the bare
        // name. Distinguish that mismatch from a genuinely missing package.
        let versioned_unsatisfied = statuses
            .iter()
            .filter(|status| {
                status.request.version.is_some()
                    && unsatisfied.contains(deptest_requirement(&status.request).as_str())
            })
            .map(|status| status.request.name.clone())
            .collect::<Vec<_>>();
        let bare_deptest = pacman_deptest(&versioned_unsatisfied).await?;
        let bare_missing = parse_pacman_deptest(&bare_deptest);
        for status in statuses
            .iter_mut()
            .filter(|status| matches!(status.state, PackageState::Missing))
        {
            let constraint_satisfied =
                !unsatisfied.contains(deptest_requirement(&status.request).as_str());
            if !constraint_satisfied
                && (status.request.version.is_none()
                    || bare_missing.contains(status.request.name.as_str()))
            {
                continue;
            }
            let output = pacman_query(std::slice::from_ref(&status.request.name)).await?;
            let provider = apply_provider_query(status, &output, constraint_satisfied)?;
            debug!(
                "pacman: {} is satisfied by installed provider {provider}",
                status.request.name
            );
        }
        Ok(statuses)
    }

    fn supports_version_pins(&self) -> bool {
        false
    }

    async fn install(&self, pkgs: &[PackageRequest], opts: &InstallOpts) -> Result<()> {
        // Arch repos only carry the latest version — pacman has no syntax to
        // install an older one, so a pin can be checked (status) but not
        // satisfied here; the CLI filters pinned requests out before calling
        if let Some(p) = pkgs.iter().find(|p| p.version.is_some()) {
            bail!(
                "pacman cannot install a pinned version ('{p}'): Arch repositories only \
                 provide the latest version"
            );
        }
        if opts.update || self.dbs_missing() {
            self.refresh(opts)?;
        }
        let mut args = vec![
            "-S".to_string(),
            "--noconfirm".to_string(),
            "--needed".to_string(),
            // `--` keeps package operands from being parsed as pacman options
            "--".to_string(),
        ];
        args.extend(pkgs.iter().map(|p| p.name.clone()));
        if opts.dry_run {
            miseprintln!("{}", sudo::argv("pacman", &args).join(" "));
            return Ok(());
        }
        sudo::run("pacman", &args, &[])
    }

    async fn upgrade(&self, pkgs: &[PackageRequest], opts: &InstallOpts) -> Result<()> {
        let names = pkgs.iter().map(|pkg| pkg.name.clone()).collect::<Vec<_>>();
        let stdout = pacman_query(&names).await?;
        let installed_names = stdout
            .lines()
            .filter_map(|line| line.split_once(' ').map(|(name, _)| name))
            .collect::<HashSet<_>>();
        let pkgs = pkgs
            .iter()
            .filter(|pkg| installed_names.contains(pkg.name.as_str()))
            .collect::<Vec<_>>();
        let skipped = names.len() - pkgs.len();
        if skipped > 0 {
            warn!(
                "pacman: {skipped} package(s) satisfied by an installed provider; skipping targeted upgrade"
            );
        }
        if pkgs.is_empty() {
            return Ok(());
        }
        // refresh sync DBs, then -S --needed upgrades exactly the named
        // packages that are outdated. Note: Arch officially supports only
        // full-system upgrades (-Syu); upgrading individual packages is a
        // partial upgrade — documented as a caveat in the pacman docs page.
        self.refresh(opts)?;
        let mut args = vec![
            "-S".to_string(),
            "--noconfirm".to_string(),
            "--needed".to_string(),
            "--".to_string(),
        ];
        args.extend(pkgs.iter().map(|p| p.name.clone()));
        if opts.dry_run {
            miseprintln!("{}", sudo::argv("pacman", &args).join(" "));
            return Ok(());
        }
        sudo::run("pacman", &args, &[])
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn req(name: &str, version: Option<&str>) -> PackageRequest {
        PackageRequest {
            name: name.to_string(),
            version: version.map(str::to_string),
            tap_url: None,
        }
    }

    #[test]
    fn test_parse_pacman_query() {
        let requests = vec![
            req("bc", None),
            req("nonexistent", None),
            req("zsh", Some("5.9")),
            req("tmux", Some("3.3")),
        ];
        let output = "bc 1.08.2-1\nzsh 5.9-5\ntmux 3.4-2\n";
        let statuses = parse_pacman_query(output, &requests);
        assert_eq!(
            statuses[0].state,
            PackageState::Installed {
                version: "1.08.2-1".to_string()
            }
        );
        assert_eq!(statuses[1].state, PackageState::Missing);
        // a version-only pin matches any pkgrel
        assert_eq!(
            statuses[2].state,
            PackageState::Installed {
                version: "5.9-5".to_string()
            }
        );
        // a different installed version must not satisfy a pin
        assert_eq!(
            statuses[3].state,
            PackageState::VersionMismatch {
                installed: "3.4-2".to_string()
            }
        );
    }

    #[test]
    fn test_apply_provider_query() {
        let mut status = PackageStatus {
            request: req("mariadb-clients", Some("12.3.2")),
            state: PackageState::Missing,
        };

        // pacman -T validated the version declared by Provides even though the
        // provider package has a different version of its own.
        let provider =
            apply_provider_query(&mut status, "percona-server-clients 9.7.1_1-1\n", true).unwrap();

        assert_eq!(provider, "percona-server-clients");
        assert_eq!(
            status.state,
            PackageState::Installed {
                version: "9.7.1_1-1".to_string()
            }
        );
    }

    #[test]
    fn test_apply_provider_query_version_mismatch() {
        let mut status = PackageStatus {
            request: req("virtual-package", Some("2.0")),
            state: PackageState::Missing,
        };

        apply_provider_query(&mut status, "provider-package 2.0-1\n", false).unwrap();

        assert_eq!(
            status.state,
            PackageState::VersionMismatch {
                installed: "2.0-1".to_string()
            }
        );
    }

    #[test]
    fn test_deptest_requirement_includes_version() {
        assert_eq!(
            deptest_requirement(&req("virtual-package", Some("2.0"))),
            "virtual-package=2.0"
        );
        assert_eq!(
            deptest_requirement(&req("virtual-package", None)),
            "virtual-package"
        );
    }

    #[test]
    fn test_parse_pacman_deptest() {
        let missing = parse_pacman_deptest("missing-one\nmissing-two\n");
        assert_eq!(missing, HashSet::from(["missing-one", "missing-two"]));
    }
}