cargo-propagate-features 0.0.5

Cargo subcommand to automatically propagate workspace crate features to dependencies
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
//! Cargo subcommand to automatically propagate workspace crate features to
//! dependencies.
//!
//! For any crate A with feature X depending on crate B that also has feature X,
//! this tool ensures that crate A's feature X includes "B/X" in its feature
//! dependencies.

use std::collections::{
    HashMap,
    HashSet,
};
use std::path::PathBuf;

use anyhow::{
    Context,
    Result,
};
use cargo_plugin_utils::ProgressLogger;
use clap::Parser;
use toml_edit::{
    DocumentMut,
    Item,
};

#[derive(Parser, Debug)]
#[command(
    name = "cargo-propagate-features",
    about = "Automatically propagate workspace crate features to dependencies",
    bin_name = "cargo"
)]
struct Cli {
    #[command(subcommand)]
    command: Command,
}

#[derive(Parser, Debug)]
enum Command {
    #[command(name = "propagate-features")]
    PropagateFeatures(PropagateArgs),
}

#[derive(Parser, Debug)]
struct PropagateArgs {
    /// Show what would be changed without modifying files
    #[arg(long)]
    dry_run: bool,

    /// Comma-separated list of features to propagate
    #[arg(long, default_value = "backend,cli,desktop,web")]
    features: String,

    /// Path to Cargo.toml manifest (idiomatic cargo flag)
    ///
    /// Note: When using `cargo run`, place this flag BEFORE the `--`:
    /// `cargo run --manifest-path <path> -- propagate-features`
    #[arg(long)]
    manifest_path: Option<PathBuf>,

    /// Suppress output when there are no changes
    #[arg(long)]
    quiet: bool,
}

#[derive(Debug, Clone)]
struct CrateInfo {
    name: String,
    path: PathBuf,
    features: HashSet<String>,
    dependencies: HashSet<String>,
}

fn main() -> Result<()> {
    let cli = Cli::parse();

    match cli.command {
        Command::PropagateFeatures(args) => propagate_features(args),
    }
}

fn propagate_features(args: PropagateArgs) -> Result<()> {
    let target_features: HashSet<String> = args
        .features
        .split(',')
        .map(|s| s.trim().to_string())
        .collect();

    let mut logger = ProgressLogger::new(args.quiet);

    // Use cargo_metadata to get all workspace packages - no need to manually
    // discover! Process all packages in the workspace (supports both single-package
    // projects and workspace projects with packages in crates/ or elsewhere).
    let packages = cargo_plugin_utils::get_workspace_packages(args.manifest_path.as_deref())
        .context("Failed to get cargo metadata. Make sure you're in a Cargo project or use --manifest-path.")?;

    let mut crates: Vec<CrateInfo> = packages
        .iter()
        .filter_map(|pkg| {
            let manifest_dir = pkg.manifest_path.as_std_path().parent()?;

            // Extract features from cargo_metadata (already parsed!)
            let features: HashSet<String> = pkg.features.keys().cloned().collect();

            // Extract workspace dependencies (any dependency that exists in the workspace)
            // We'll filter to workspace packages later when we have the full set
            let dependencies: HashSet<String> = pkg
                .dependencies
                .iter()
                .map(|dep| dep.name.as_str().to_string())
                .collect();

            Some(CrateInfo {
                name: pkg.name.as_str().to_string(),
                path: manifest_dir.to_path_buf(),
                features,
                dependencies,
            })
        })
        .collect();

    // Build a set of workspace package names for efficient lookup
    let workspace_package_names: HashSet<String> = packages
        .iter()
        .map(|p| p.name.as_str().to_string())
        .collect();

    // Filter dependencies to only include those actually in the workspace
    for crate_info in &mut crates {
        crate_info
            .dependencies
            .retain(|dep_name| workspace_package_names.contains(dep_name));
    }

    // Build a map of crate name -> features
    let crate_features: HashMap<String, HashSet<String>> = crates
        .iter()
        .map(|c| (c.name.clone(), c.features.clone()))
        .collect();

    let mut total_changes = 0;

    // First pass: Remove hardcoded runtime features from dependencies
    logger.set_progress(crates.len() as u64);
    logger.set_message("Checking");

    for crate_info in &crates {
        logger.inc();

        let cargo_toml_path = crate_info.path.join("Cargo.toml");
        let content = std::fs::read_to_string(&cargo_toml_path)
            .context(format!("Failed to read {:?}", cargo_toml_path))?;

        let mut doc = content
            .parse::<DocumentMut>()
            .context(format!("Failed to parse {:?}", cargo_toml_path))?;

        let mut changed = false;

        // Check regular dependencies
        changed |= remove_hardcoded_features(
            &mut doc,
            "dependencies",
            &target_features,
            &crate_info.name,
            &workspace_package_names,
            &mut logger,
        )?;

        // Check dev-dependencies
        changed |= remove_hardcoded_features(
            &mut doc,
            "dev-dependencies",
            &target_features,
            &crate_info.name,
            &workspace_package_names,
            &mut logger,
        )?;

        // Check target-specific dependencies
        // Note: This section has deep nesting due to TOML structure traversal
        #[allow(clippy::excessive_nesting)]
        if let Some(target_table) = doc.get_mut("target").and_then(|t| t.as_table_mut()) {
            for (target_name, target_config) in target_table.iter_mut() {
                let section_name = format!("target.{}.dependencies", target_name);
                if let Some(deps) = target_config
                    .get_mut("dependencies")
                    .and_then(|d| d.as_table_mut())
                {
                    for (dep_name, dep_value) in deps.iter_mut() {
                        let Some(dep_table) = dep_value.as_inline_table_mut() else {
                            continue;
                        };

                        // Only process workspace dependencies that are actually in the workspace
                        let dep_name_str = dep_name.to_string();
                        if !dep_table.contains_key("workspace")
                            || !workspace_package_names.contains(&dep_name_str)
                            || !dep_table.contains_key("features")
                        {
                            continue;
                        }

                        let Some(features_array) =
                            dep_table.get_mut("features").and_then(|f| f.as_array_mut())
                        else {
                            continue;
                        };

                        let original_len = features_array.len();
                        features_array.retain(|v| {
                            v.as_str()
                                .map(|s| !target_features.contains(s))
                                .unwrap_or(true)
                        });

                        if features_array.len() < original_len {
                            logger.println(&format!(
                                "   ⚠️  {}: Removed hardcoded runtime features from {} in {}",
                                crate_info.name, dep_name, section_name
                            ));
                            changed = true;
                            total_changes += 1;

                            // If features array is now empty, remove it
                            if features_array.is_empty() {
                                dep_table.remove("features");
                            }
                        }
                    }
                }
            }
        }

        if changed {
            if args.dry_run {
                logger.println(&format!("   [DRY RUN] Would update {:?}", cargo_toml_path));
            } else {
                std::fs::write(&cargo_toml_path, doc.to_string())
                    .context(format!("Failed to write {:?}", cargo_toml_path))?;
                logger.println(&format!("   💾 Updated {:?}", cargo_toml_path));
            }
        }
    }
    logger.finish();

    // Second pass: Propagate features
    logger.set_progress(crates.len() as u64);
    logger.set_message("Propagating");

    for crate_info in &crates {
        logger.inc();

        // Check if this crate has any of our target features
        let has_target_features: Vec<_> = target_features
            .iter()
            .filter(|f| crate_info.features.contains(*f))
            .collect();

        if has_target_features.is_empty() {
            continue;
        }

        let cargo_toml_path = crate_info.path.join("Cargo.toml");
        let content = std::fs::read_to_string(&cargo_toml_path)
            .context(format!("Failed to read {:?}", cargo_toml_path))?;

        let mut doc = content
            .parse::<DocumentMut>()
            .context(format!("Failed to parse {:?}", cargo_toml_path))?;

        let mut changed = false;

        // Note: This section has deep nesting due to TOML structure traversal
        #[allow(clippy::excessive_nesting)]
        for feature_name in &has_target_features {
            // Find dependencies that have this feature
            let deps_with_feature: Vec<_> = crate_info
                .dependencies
                .iter()
                .filter(|dep| {
                    crate_features
                        .get(*dep)
                        .map(|f| f.contains(*feature_name))
                        .unwrap_or(false)
                })
                .collect();

            if deps_with_feature.is_empty() {
                continue;
            }

            // Update the feature in the TOML
            if let Some(feature_item) = doc
                .get_mut("features")
                .and_then(|f| f.as_table_like_mut())
                .and_then(|table| table.get_mut(feature_name.as_str()))
            {
                let feature_array = match feature_item {
                    Item::Value(toml_edit::Value::Array(arr)) => arr,
                    _ => continue,
                };

                // Get existing feature dependencies
                let mut existing: HashSet<String> = feature_array
                    .iter()
                    .filter_map(|v| v.as_str().map(|s| s.to_string()))
                    .collect();

                // Add missing feature dependencies
                for dep in &deps_with_feature {
                    let dep_feature = format!("{}/{}", dep, feature_name);
                    if !existing.contains(&dep_feature) {
                        logger.status(
                            "Adding",
                            &format!("{}/{} to {}", dep, feature_name, feature_name),
                        );
                        feature_array.push(dep_feature.clone());
                        existing.insert(dep_feature);
                        changed = true;
                        total_changes += 1;
                    }
                }
            }
        }

        if changed {
            if args.dry_run {
                logger.println(&format!("   [DRY RUN] Would update {:?}", cargo_toml_path));
            } else {
                std::fs::write(&cargo_toml_path, doc.to_string())
                    .context(format!("Failed to write {:?}", cargo_toml_path))?;
                logger.println(&format!("   💾 Updated {:?}", cargo_toml_path));
            }
        }
    }
    logger.finish();

    // In quiet mode, show nothing. Otherwise show summary.
    if !args.quiet {
        if total_changes > 0 {
            logger.println("✨ Complete!");
            if args.dry_run {
                logger.println(&format!("   Would make {} changes", total_changes));
                logger.println("   Run without --dry-run to apply changes");
            } else {
                logger.println(&format!("   Made {} changes", total_changes));
            }
        } else {
            logger.println("✨ No changes needed");
        }
    }

    Ok(())
}

fn remove_hardcoded_features(
    doc: &mut DocumentMut,
    section: &str,
    target_features: &HashSet<String>,
    crate_name: &str,
    workspace_package_names: &HashSet<String>,
    logger: &mut ProgressLogger,
) -> Result<bool> {
    let mut changed = false;

    let Some(deps) = doc.get_mut(section).and_then(|d| d.as_table_mut()) else {
        return Ok(false);
    };

    for (dep_name, dep_value) in deps.iter_mut() {
        let Some(dep_table) = dep_value.as_inline_table_mut() else {
            continue;
        };

        // Only process workspace dependencies that are actually in the workspace
        let dep_name_str = dep_name.to_string();
        if !dep_table.contains_key("workspace")
            || !workspace_package_names.contains(&dep_name_str)
            || !dep_table.contains_key("features")
        {
            continue;
        }

        let Some(features_array) = dep_table.get_mut("features").and_then(|f| f.as_array_mut())
        else {
            continue;
        };

        let original_len = features_array.len();
        features_array.retain(|v| {
            v.as_str()
                .map(|s| !target_features.contains(s))
                .unwrap_or(true)
        });

        if features_array.len() < original_len {
            logger.println(&format!(
                "   ⚠️  {}: Removed hardcoded runtime features from {} in [{}]",
                crate_name, dep_name, section
            ));
            changed = true;

            // If features array is now empty, remove it
            if features_array.is_empty() {
                dep_table.remove("features");
            }
        }
    }

    Ok(changed)
}