rsconstruct 0.9.135

Rust based fast build system
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
use super::Builder;
use crate::cli::DisplayOptions;
use crate::color;
use crate::executor::{Executor, ExecutorOptions};
use anyhow::{Context, Result, bail};
use std::collections::HashSet;
use std::fmt::Write;
use std::fs;
use std::path::PathBuf;

impl Builder {
    /// Clean build artifacts using the dependency graph.
    /// If `processor_filter` is `Some`, only clean outputs from the selected
    /// processors; the selectors are the ones `build -p` takes (names with or
    /// without the `processor.` prefix, instances, `@type`, `@tool`, `@short`).
    /// When `sweep_empty_dirs` is true (default), directories left empty after
    /// per-product cleanup are removed bottom-up.
    pub fn clean(
        &self,
        ctx: &crate::build_context::BuildContext,
        verbose: bool,
        processor_filter: Option<&[String]>,
        sweep_empty_dirs: bool,
    ) -> Result<()> {
        // Create processors and build graph (fast path: skip dependency scanning)
        let processors = self.create_processors()?;
        let allowed = super::build::resolve_processor_filter(processor_filter, None, &processors)?;
        if let Some(names) = &allowed {
            crate::output::info(&color::bold(&format!(
                "Cleaning outputs for: {}",
                names.join(", ")
            )));
        } else {
            crate::output::info(&color::bold("Cleaning build artifacts..."));
        }
        let mut graph = self.build_graph_for_clean_with_processors(ctx, &processors)?;

        // Filter the graph to only include products from the selected processors
        if let Some(names) = &allowed {
            let filter_set: HashSet<&str> = names.iter().map(std::string::String::as_str).collect();
            graph.retain_products(|p| filter_set.contains(p.processor.as_str()));
        }

        // Collect every directory whose contents may be affected by the clean:
        // the parent of each output file, and the output_dirs themselves
        // (whose parents become candidates once the dir is removed).
        let mut candidate_dirs: HashSet<PathBuf> = HashSet::new();
        for product in graph.products() {
            for output in &product.outputs {
                if let Some(parent) = output.parent()
                    && !parent.as_os_str().is_empty()
                {
                    candidate_dirs.insert(parent.to_path_buf());
                }
            }
            for dir in &product.output_dirs {
                if let Some(parent) = dir.parent()
                    && !parent.as_os_str().is_empty()
                {
                    candidate_dirs.insert(parent.to_path_buf());
                }
            }
        }

        // Use executor to clean (batch_size doesn't matter for clean)
        let policy = crate::executor::IncrementalPolicy;
        let executor = Executor::new(
            &processors,
            ctx,
            &policy,
            ExecutorOptions {
                parallel: 1,
                verbose: false,
                display_opts: DisplayOptions::minimal(),
                batch_size: None,
                explain: false,
                retry: 0,
                force: false,
                keep_going: false,
                timings: false,
                worker_stack_bytes: self.config.build.worker_stack_mib * 1024 * 1024,
            },
        );
        let stats = executor.clean(&graph, verbose)?;

        // Walk every candidate directory bottom-up: try fs::remove_dir (only
        // succeeds if empty), then climb to the parent and try again. Stops at
        // the project root. Try-and-ignore avoids the TOCTOU race where another
        // process populates the dir between an emptiness check and the removal.
        let dirs_removed = if sweep_empty_dirs {
            remove_empty_ancestors(&candidate_dirs, verbose)
        } else {
            0
        };

        // Print summary
        let total_files: usize = stats.values().sum();
        if total_files == 0 && dirs_removed == 0 {
            crate::output::info(&color::dim("Clean summary: nothing to clean"));
        } else {
            crate::output::info(&color::bold("Clean summary:"));
            let sorted_stats: std::collections::BTreeMap<_, _> = stats.iter().collect();
            for (proc, count) in &sorted_stats {
                crate::output::info(&format!("  {proc}: {count} file(s)"));
            }
            if dirs_removed > 0 {
                crate::output::info(&format!("  {dirs_removed} empty dir(s) removed"));
            }
            crate::output::info(&color::green(&format!(
                "Total: {total_files} file(s) removed",
            )));
        }
        Ok(())
    }

    /// `clean products`: remove the selected processors' products in full,
    /// output files and cache entries both, so the next build runs them
    /// again instead of restoring them. `selectors` is the `build -p`
    /// language and must select at least one processor. With `dry_run`,
    /// print what would go and remove nothing.
    ///
    /// The cache entry removed is the one for each product's current inputs
    /// (the key the next build would look up); entries for earlier inputs are
    /// `cache remove-stale`'s business, and the blobs are `cache trim`'s. A
    /// remote cache, when pull is on, may still satisfy the next build.
    pub fn clean_products(
        &self,
        ctx: &crate::build_context::BuildContext,
        verbose: bool,
        selectors: &[String],
        dry_run: bool,
        sweep_empty_dirs: bool,
    ) -> Result<()> {
        let processors = self.create_processors()?;
        let allowed = super::build::resolve_processor_filter(Some(selectors), None, &processors)?
            .unwrap_or_default();
        if allowed.is_empty() {
            bail!(
                "clean products needs -p with at least one processor; `clean outputs` cleans every processor's outputs"
            );
        }
        crate::output::info(&color::bold(&format!(
            "{} products of: {}",
            if dry_run { "Would clean" } else { "Cleaning" },
            allowed.join(", ")
        )));

        // The full graph, dependencies included: the cache key of a product
        // mixes in its input checksums, and generated inputs are only known
        // once dependencies are resolved.
        let mut graph = self.build_graph_with_processors(ctx, &processors)?;
        let filter_set: HashSet<&str> = allowed.iter().map(String::as_str).collect();
        graph.retain_products(|p| filter_set.contains(p.processor.as_str()));

        let mut cache_keys: Vec<(String, String)> = Vec::new();
        for product in graph.products() {
            let input_checksum = crate::checksum::combined_input_checksum(ctx, &product.inputs)?;
            cache_keys.push((
                product.processor.clone(),
                product.descriptor_key(&input_checksum),
            ));
        }

        if dry_run {
            let mut files = 0usize;
            let mut dirs = 0usize;
            for product in graph.products() {
                for output in &product.outputs {
                    if output.exists() {
                        files += 1;
                        if verbose {
                            crate::output::info(&format!("  would remove {}", output.display()));
                        }
                    }
                }
                for dir in &product.output_dirs {
                    if dir.exists() {
                        dirs += 1;
                        if verbose {
                            crate::output::info(&format!("  would remove {}/", dir.display()));
                        }
                    }
                }
            }
            let entries = cache_keys
                .iter()
                .filter(|(_, key)| self.object_store.has_descriptor(key))
                .count();
            crate::output::info(&format!(
                "Would remove {files} output file(s), {dirs} output dir(s) and {entries} cache entr{} for {} product(s); nothing removed (--dry-run)",
                if entries == 1 { "y" } else { "ies" },
                graph.products().len()
            ));
            return Ok(());
        }

        let mut candidate_dirs: HashSet<PathBuf> = HashSet::new();
        for product in graph.products() {
            for output in &product.outputs {
                if let Some(parent) = output.parent()
                    && !parent.as_os_str().is_empty()
                {
                    candidate_dirs.insert(parent.to_path_buf());
                }
            }
            for dir in &product.output_dirs {
                if let Some(parent) = dir.parent()
                    && !parent.as_os_str().is_empty()
                {
                    candidate_dirs.insert(parent.to_path_buf());
                }
            }
        }
        let policy = crate::executor::IncrementalPolicy;
        let executor = Executor::new(
            &processors,
            ctx,
            &policy,
            ExecutorOptions {
                parallel: 1,
                verbose: false,
                display_opts: DisplayOptions::minimal(),
                batch_size: None,
                explain: false,
                retry: 0,
                force: false,
                keep_going: false,
                timings: false,
                worker_stack_bytes: self.config.build.worker_stack_mib * 1024 * 1024,
            },
        );
        let stats = executor.clean(&graph, verbose)?;

        let mut entries: std::collections::BTreeMap<&str, usize> =
            std::collections::BTreeMap::new();
        for (processor, key) in &cache_keys {
            if self.object_store.remove_descriptor(key)? {
                *entries.entry(processor.as_str()).or_default() += 1;
            }
        }

        let dirs_removed = if sweep_empty_dirs {
            remove_empty_ancestors(&candidate_dirs, verbose)
        } else {
            0
        };

        let total_files: usize = stats.values().sum();
        let total_entries: usize = entries.values().sum();
        if total_files == 0 && total_entries == 0 && dirs_removed == 0 {
            crate::output::info(&color::dim("Clean summary: nothing to clean"));
            return Ok(());
        }
        crate::output::info(&color::bold("Clean summary:"));
        for name in &allowed {
            let files = stats.get(name).copied().unwrap_or(0);
            let cached = entries.get(name.as_str()).copied().unwrap_or(0);
            crate::output::info(&format!(
                "  {}: {files} file(s), {cached} cache entr{}",
                crate::tables::short_processor_name(name),
                if cached == 1 { "y" } else { "ies" }
            ));
        }
        if dirs_removed > 0 {
            crate::output::info(&format!("  {dirs_removed} empty dir(s) removed"));
        }
        crate::output::info(&color::green(&format!(
            "Total: {total_files} file(s) and {total_entries} cache entr{} removed; the next build runs these products again",
            if total_entries == 1 { "y" } else { "ies" }
        )));
        Ok(())
    }

    /// Remove all build outputs and cache directories (.rsconstruct/ and out/)
    pub fn distclean(&self) -> Result<()> {
        crate::output::info(&color::bold("Removing build directories..."));

        let rsconstruct_dir = std::path::Path::new(".rsconstruct");
        if rsconstruct_dir.exists() {
            fs::remove_dir_all(rsconstruct_dir)
                .context("Failed to remove .rsconstruct/ directory")?;
            crate::output::info(&format!("Removed {}", rsconstruct_dir.display()));
        }

        // The configured output root, not a hardcoded "out".
        let out_dir = std::path::Path::new(&self.config.build.output_dir);
        if out_dir.exists() {
            fs::remove_dir_all(out_dir)
                .with_context(|| format!("Failed to remove {}/ directory", out_dir.display()))?;
            crate::output::info(&format!("Removed {}", out_dir.display()));
        }

        crate::output::info(&color::green("Distclean completed!"));
        Ok(())
    }

    /// Hard clean using `git clean -qffxd`. Requires a git repository.
    pub fn hardclean(&self, ctx: &crate::build_context::BuildContext) -> Result<()> {
        use std::process::Command;

        if !std::path::Path::new(".git").exists() {
            bail!("Not a git repository. Hardclean requires a git repository.");
        }

        crate::output::info(&color::bold("Running git clean -qffxd..."));

        let mut cmd = Command::new("git");
        cmd.args(["clean", "-qffxd"]);
        let output =
            crate::processor::run_command_capture(ctx, &cmd).context("Failed to run git clean")?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            bail!("git clean failed:\n{stderr}");
        }

        println!("{}", color::green("Hardclean completed!"));
        Ok(())
    }

    /// Remove files not tracked by git and not known as `RSConstruct` build outputs.
    /// Dry-run by default (lists files); use `force` to actually delete.
    pub fn clean_unknown(
        &self,
        ctx: &crate::build_context::BuildContext,
        force: bool,
        verbose: bool,
        respect_gitignore: bool,
    ) -> Result<()> {
        use ignore::WalkBuilder;
        use std::process::Command;

        if !std::path::Path::new(".git").exists() {
            bail!("Not a git repository. clean unknown requires a git repository.");
        }

        // Build graph to discover RSConstruct outputs
        let processors = self.create_processors()?;
        let graph = self.build_graph_for_clean_with_processors(ctx, &processors)?;

        // Collect RSConstruct-known output files
        let mut rsconstruct_outputs: HashSet<PathBuf> = HashSet::new();
        let mut rsconstruct_output_dirs: Vec<PathBuf> = Vec::new();
        for product in graph.products() {
            for output in &product.outputs {
                rsconstruct_outputs.insert(output.clone());
            }
            for dir in &product.output_dirs {
                rsconstruct_output_dirs.push(dir.as_ref().clone());
            }
        }

        // Get git-tracked files
        let mut cmd = Command::new("git");
        cmd.args(["ls-files", "--cached"]);
        let output = crate::processor::run_command_capture(ctx, &cmd)
            .context("Failed to run git ls-files")?;
        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            bail!("git ls-files failed:\n{stderr}");
        }
        let git_tracked: HashSet<PathBuf> = String::from_utf8_lossy(&output.stdout)
            .lines()
            .map(PathBuf::from)
            .collect();

        // Walk the project tree, respecting .gitignore so that intentionally
        // ignored files (IDE configs, virtualenvs, *.pyc, etc.) are not flagged
        // as unknown. RSConstruct outputs in gitignored directories (e.g. out/)
        // are already excluded via the rsconstruct_outputs/rsconstruct_output_dirs
        // checks, so respecting .gitignore doesn't hide anything we care about.
        let mut unknown_files: Vec<PathBuf> = Vec::new();
        let walker = WalkBuilder::new(".")
            .hidden(false)
            .git_ignore(respect_gitignore)
            .git_global(respect_gitignore)
            .git_exclude(respect_gitignore)
            .build();

        for entry in walker {
            let Ok(entry) = entry else { continue };
            // Skip directories — we only care about files
            if !entry.file_type().is_some_and(|ft| ft.is_file()) {
                continue;
            }
            let path = entry
                .path()
                .strip_prefix("./")
                .unwrap_or_else(|_| entry.path());
            let path = PathBuf::from(path);

            // Skip .git/ and .rsconstruct/
            if path.starts_with(".git") || path.starts_with(".rsconstruct") {
                continue;
            }

            // Skip git-tracked files
            if git_tracked.contains(&path) {
                continue;
            }

            // Skip RSConstruct output files
            if rsconstruct_outputs.contains(&path) {
                continue;
            }

            // Skip files inside RSConstruct output directories
            if rsconstruct_output_dirs
                .iter()
                .any(|dir| path.starts_with(dir))
            {
                continue;
            }

            unknown_files.push(path);
        }

        unknown_files.sort();

        if unknown_files.is_empty() {
            println!("{}", color::green("No unknown files found."));
            return Ok(());
        }

        if force {
            println!("{}", color::bold("Removing unknown files..."));
            let mut removed = 0usize;
            for path in &unknown_files {
                if let Err(e) = fs::remove_file(path) {
                    eprintln!("  Failed to remove {}: {}", path.display(), e);
                } else {
                    if verbose {
                        println!("  {}", path.display());
                    }
                    removed += 1;
                }
            }

            // Clean up empty parent directories (bottom-up).
            // Walk the full parent chain of each removed file so that
            // nested empty directories are cleaned up too.
            let mut dirs_to_check: HashSet<PathBuf> = HashSet::new();
            for path in &unknown_files {
                let mut current = path.parent();
                while let Some(dir) = current {
                    if dir == std::path::Path::new("") {
                        break;
                    }
                    dirs_to_check.insert(dir.to_path_buf());
                    current = dir.parent();
                }
            }
            let mut sorted_dirs: Vec<PathBuf> = dirs_to_check.into_iter().collect();
            // Sort by depth descending so we remove leaf dirs first
            sorted_dirs.sort_by_key(|b| std::cmp::Reverse(b.components().count()));
            let mut dirs_removed = 0usize;
            for dir in &sorted_dirs {
                // Try to remove — only succeeds if empty
                if fs::remove_dir(dir).is_ok() {
                    dirs_removed += 1;
                }
            }

            let mut summary = format!("Removed {removed} unknown file(s)");
            if dirs_removed > 0 {
                let _ = write!(summary, ", {dirs_removed} empty dir(s)");
            }
            println!("{}", color::green(&summary));
        } else {
            println!(
                "{}",
                color::bold(&format!("Found {} unknown file(s):", unknown_files.len()))
            );
            for path in &unknown_files {
                println!("  {}", path.display());
            }
            println!();
            println!(
                "{}",
                color::dim("This is a dry run. Run without --dry-run to delete them.")
            );
        }

        Ok(())
    }
}

/// Remove every empty directory rooted at any of the given candidates, walking
/// upward toward the project root. `fs::remove_dir` only succeeds on empty
/// directories, so non-empty ones (and their ancestors) are left intact.
/// Returns the number of directories removed.
fn remove_empty_ancestors(candidates: &HashSet<PathBuf>, verbose: bool) -> usize {
    let mut all: HashSet<PathBuf> = HashSet::new();
    for dir in candidates {
        let mut current: Option<&std::path::Path> = Some(dir.as_path());
        while let Some(p) = current {
            if p.as_os_str().is_empty() || p == std::path::Path::new(".") {
                break;
            }
            all.insert(p.to_path_buf());
            current = p.parent();
        }
    }

    // Deepest first so children are tried before their parents.
    let mut sorted: Vec<PathBuf> = all.into_iter().collect();
    sorted.sort_by_key(|p| std::cmp::Reverse(p.components().count()));

    let mut removed = 0usize;
    for dir in &sorted {
        if fs::remove_dir(dir).is_ok() {
            removed += 1;
            if verbose {
                println!("Removed empty directory: {}", dir.display());
            }
        }
    }
    removed
}