Skip to main content

leo_lang/cli/commands/
clean.rs

1// Copyright (C) 2019-2026 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17use anyhow::anyhow;
18
19use super::*;
20
21/// Clean outputs folder command
22#[derive(Parser, Debug)]
23pub struct LeoClean {}
24
25impl Command for LeoClean {
26    type Input = ();
27    type Output = ();
28
29    fn log_span(&self) -> Span {
30        tracing::span!(tracing::Level::INFO, "Leo")
31    }
32
33    fn prelude(&self, _: Context) -> Result<Self::Input> {
34        Ok(())
35    }
36
37    fn apply(self, context: Context, _: Self::Input) -> Result<Self::Output> {
38        match context.resolve_targets()? {
39            Some((root, targets)) => {
40                // Workspace mode: build/ lives at the workspace root and is
41                // shared across all members, so remove it once. Then loop
42                // members to clear pre-shared-layout `<member>/build/` and
43                // pre-flat-layout `<member>/outputs/` leftovers.
44                remove_dir_if_present(&root.join(leo_package::BUILD_DIRECTORY), "build directory");
45                for target in &targets {
46                    let member_name = target.file_name().and_then(|n| n.to_str()).unwrap_or("?");
47                    if targets.len() > 1 {
48                        println!("\n--- workspace member '{member_name}' ---");
49                    }
50                    clean_member_legacy(target);
51                }
52                Ok(())
53            }
54            None => clean_package(context),
55        }
56    }
57}
58
59fn clean_package(context: Context) -> Result<()> {
60    let path = context.dir()?;
61
62    let manifest_path = path.join(leo_package::MANIFEST_FILENAME);
63
64    if !manifest_path.exists() {
65        return Err(anyhow!(
66            "{} doesn't exist - this doesn't appear to be a Leo package.",
67            leo_package::MANIFEST_FILENAME
68        )
69        .into());
70    }
71
72    // Best-effort legacy cleanup: pre-flat-layout builds created a top-level
73    // `outputs/` directory. This entry only fires when wiping an upgraded
74    // checkout that still has the stale directory on disk.
75    remove_dir_if_present(&path.join("outputs"), "legacy outputs directory");
76
77    // Removes the build/ directory.
78    remove_dir_if_present(&path.join(leo_package::BUILD_DIRECTORY), "build directory");
79
80    Ok(())
81}
82
83/// Per-member cleanup in workspace mode. The shared
84/// `<workspace_root>/build/` is removed once by the caller. Best-effort:
85/// `resolve_targets` already validated each member, so this only sweeps
86/// migration leftovers and never fails the whole `leo clean`.
87fn clean_member_legacy(member_dir: &std::path::Path) {
88    // Migration aid: a per-member `build/` from a pre-shared-layout checkout.
89    remove_dir_if_present(&member_dir.join(leo_package::BUILD_DIRECTORY), "legacy per-member build directory");
90    // Migration aid: a per-member `outputs/` from a pre-flat-layout checkout.
91    remove_dir_if_present(&member_dir.join("outputs"), "legacy outputs directory");
92}
93
94/// Best-effort remove `path` if it exists; log on success.
95fn remove_dir_if_present(path: &std::path::Path, label: &str) {
96    if std::fs::remove_dir_all(path).is_ok() {
97        tracing::info!("🧹 Cleaned the {label} {}", path.display().to_string().dimmed());
98    }
99}