dart-decimate 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/Cargo.lock +572 -0
  2. package/Cargo.toml +41 -0
  3. package/LICENSE +21 -0
  4. package/README.md +617 -0
  5. package/assets/dart-decimate-banner.png +0 -0
  6. package/npm/bin/dart-decimate-mcp.js +48 -0
  7. package/npm/bin/dart-decimate.js +48 -0
  8. package/npm/scripts/postinstall.js +46 -0
  9. package/npm/scripts/test-npx-local.js +54 -0
  10. package/npm/scripts/test-npx-mcp-local.js +56 -0
  11. package/package.json +45 -0
  12. package/src/baseline.rs +643 -0
  13. package/src/bin/dart-decimate-mcp.rs +11 -0
  14. package/src/boundaries/tests.rs +60 -0
  15. package/src/boundaries.rs +317 -0
  16. package/src/changed_scope.rs +117 -0
  17. package/src/ci_template.rs +399 -0
  18. package/src/cli/analyze.rs +462 -0
  19. package/src/cli/analyzer_options.rs +82 -0
  20. package/src/cli/audit_run.rs +327 -0
  21. package/src/cli/boundary_args.rs +44 -0
  22. package/src/cli/ci_template_run.rs +196 -0
  23. package/src/cli/command_name.rs +20 -0
  24. package/src/cli/common_args.rs +167 -0
  25. package/src/cli/config_run.rs +78 -0
  26. package/src/cli/coverage_run.rs +393 -0
  27. package/src/cli/decision_surface_run.rs +99 -0
  28. package/src/cli/default_command.rs +69 -0
  29. package/src/cli/dupes_args.rs +150 -0
  30. package/src/cli/entry_points.rs +121 -0
  31. package/src/cli/error.rs +149 -0
  32. package/src/cli/error_output.rs +68 -0
  33. package/src/cli/explain_run.rs +72 -0
  34. package/src/cli/fix_run.rs +162 -0
  35. package/src/cli/flags_args.rs +23 -0
  36. package/src/cli/health_args.rs +200 -0
  37. package/src/cli/hooks_run.rs +191 -0
  38. package/src/cli/impact_run.rs +95 -0
  39. package/src/cli/init_run.rs +49 -0
  40. package/src/cli/inspect_args.rs +20 -0
  41. package/src/cli/inspect_run.rs +66 -0
  42. package/src/cli/issue_filter_args.rs +240 -0
  43. package/src/cli/list_run.rs +311 -0
  44. package/src/cli/mode_args.rs +72 -0
  45. package/src/cli/output_format.rs +62 -0
  46. package/src/cli/regression_args.rs +156 -0
  47. package/src/cli/request_args.rs +175 -0
  48. package/src/cli/schema_commands.rs +72 -0
  49. package/src/cli/scope_args.rs +174 -0
  50. package/src/cli/security_args.rs +175 -0
  51. package/src/cli/security_gate_run.rs +66 -0
  52. package/src/cli/security_summary_run.rs +43 -0
  53. package/src/cli/summary_args.rs +12 -0
  54. package/src/cli/tests.rs +516 -0
  55. package/src/cli/trace_args.rs +67 -0
  56. package/src/cli/trace_run.rs +131 -0
  57. package/src/cli/trace_tests.rs +235 -0
  58. package/src/cli/unsupported_run.rs +116 -0
  59. package/src/cli/watch_run.rs +153 -0
  60. package/src/cli.rs +603 -0
  61. package/src/config/boundary_config.rs +235 -0
  62. package/src/config/cache.rs +25 -0
  63. package/src/config/dependencies.rs +114 -0
  64. package/src/config/health.rs +237 -0
  65. package/src/config/jsonc.rs +66 -0
  66. package/src/config/policy.rs +91 -0
  67. package/src/config/rule_aliases.rs +487 -0
  68. package/src/config/rules.rs +339 -0
  69. package/src/config.rs +635 -0
  70. package/src/coverage.rs +469 -0
  71. package/src/dart_parser.rs +673 -0
  72. package/src/decision_surface.rs +405 -0
  73. package/src/dependencies/analyze.rs +218 -0
  74. package/src/dependencies/codegen.rs +118 -0
  75. package/src/dependencies/discovery.rs +60 -0
  76. package/src/dependencies/overrides.rs +76 -0
  77. package/src/dependencies/private_src_imports.rs +32 -0
  78. package/src/dependencies/pubspec_document.rs +86 -0
  79. package/src/dependencies/pubspec_entry.rs +96 -0
  80. package/src/dependencies/tests.rs +544 -0
  81. package/src/dependencies/usage.rs +33 -0
  82. package/src/dependencies.rs +553 -0
  83. package/src/dependency_scripts.rs +91 -0
  84. package/src/dupes/lex.rs +236 -0
  85. package/src/dupes/tests.rs +119 -0
  86. package/src/dupes.rs +625 -0
  87. package/src/explain.rs +692 -0
  88. package/src/extract/directives.rs +252 -0
  89. package/src/extract/error_tests.rs +25 -0
  90. package/src/extract/members.rs +210 -0
  91. package/src/extract/primary_constructors.rs +421 -0
  92. package/src/extract/references.rs +151 -0
  93. package/src/extract/routes.rs +691 -0
  94. package/src/extract/signatures.rs +266 -0
  95. package/src/extract/string_tests.rs +18 -0
  96. package/src/extract/strings.rs +107 -0
  97. package/src/extract/tests.rs +697 -0
  98. package/src/extract.rs +680 -0
  99. package/src/feature_flags/tests.rs +76 -0
  100. package/src/feature_flags.rs +453 -0
  101. package/src/finding_identity.rs +60 -0
  102. package/src/fix.rs +551 -0
  103. package/src/generated.rs +53 -0
  104. package/src/graph/augment_tests.rs +212 -0
  105. package/src/graph/conditionals.rs +74 -0
  106. package/src/graph/package_config_tests.rs +100 -0
  107. package/src/graph/parts.rs +248 -0
  108. package/src/graph/pubspec_tests.rs +119 -0
  109. package/src/graph/tests.rs +837 -0
  110. package/src/graph/uri_tests.rs +102 -0
  111. package/src/graph.rs +644 -0
  112. package/src/health/coverage.rs +309 -0
  113. package/src/health/ownership.rs +175 -0
  114. package/src/health/runtime_coverage.rs +616 -0
  115. package/src/health/runtime_intelligence.rs +511 -0
  116. package/src/health/scores.rs +296 -0
  117. package/src/health/tests.rs +240 -0
  118. package/src/health/threshold_types.rs +163 -0
  119. package/src/health/thresholds.rs +229 -0
  120. package/src/health/types.rs +636 -0
  121. package/src/health.rs +597 -0
  122. package/src/hooks/agent_settings.rs +175 -0
  123. package/src/hooks/templates.rs +51 -0
  124. package/src/hooks.rs +569 -0
  125. package/src/impact.rs +287 -0
  126. package/src/init.rs +237 -0
  127. package/src/inspect.rs +142 -0
  128. package/src/intelligence/tests.rs +232 -0
  129. package/src/intelligence.rs +305 -0
  130. package/src/lib.rs +198 -0
  131. package/src/main.rs +5 -0
  132. package/src/manifest/mcp.rs +565 -0
  133. package/src/manifest.rs +552 -0
  134. package/src/mcp/cli_args/value_args.rs +175 -0
  135. package/src/mcp/cli_args.rs +658 -0
  136. package/src/mcp/cli_args_tests.rs +522 -0
  137. package/src/mcp/code_execute.rs +671 -0
  138. package/src/mcp/tools.rs +660 -0
  139. package/src/mcp.rs +325 -0
  140. package/src/output/audit_risk.rs +135 -0
  141. package/src/output/dependency_findings.rs +327 -0
  142. package/src/output/duplication_findings.rs +114 -0
  143. package/src/output/feature_flag_findings.rs +122 -0
  144. package/src/output/format.rs +31 -0
  145. package/src/output/graph_findings.rs +461 -0
  146. package/src/output/health_findings.rs +612 -0
  147. package/src/output/human.rs +27 -0
  148. package/src/output/next_steps.rs +92 -0
  149. package/src/output/route_findings.rs +76 -0
  150. package/src/output/runtime_coverage.rs +515 -0
  151. package/src/output/scope.rs +183 -0
  152. package/src/output/security_findings.rs +390 -0
  153. package/src/output/security_sarif.rs +150 -0
  154. package/src/output/suppressions.rs +323 -0
  155. package/src/output/symbol_findings.rs +257 -0
  156. package/src/output/tests.rs +114 -0
  157. package/src/output/types.rs +604 -0
  158. package/src/output/widget_findings.rs +213 -0
  159. package/src/output.rs +682 -0
  160. package/src/package_map.rs +495 -0
  161. package/src/policy/packs.rs +232 -0
  162. package/src/policy/tests.rs +134 -0
  163. package/src/policy.rs +640 -0
  164. package/src/project_list.rs +376 -0
  165. package/src/report_schema/inventory.rs +418 -0
  166. package/src/report_schema.rs +488 -0
  167. package/src/routes.rs +354 -0
  168. package/src/scan/tests.rs +250 -0
  169. package/src/scan.rs +275 -0
  170. package/src/security/detect.rs +639 -0
  171. package/src/security/tests.rs +178 -0
  172. package/src/security.rs +373 -0
  173. package/src/security_gate.rs +555 -0
  174. package/src/symbols/path_filters.rs +27 -0
  175. package/src/symbols/private_type_leaks.rs +125 -0
  176. package/src/symbols/riverpod.rs +113 -0
  177. package/src/symbols/tests.rs +692 -0
  178. package/src/symbols.rs +693 -0
  179. package/src/trace/tests.rs +225 -0
  180. package/src/trace.rs +690 -0
  181. package/src/unsupported.rs +47 -0
  182. package/src/widgets/lifecycle.rs +484 -0
  183. package/src/widgets/params.rs +236 -0
  184. package/src/widgets/tests.rs +430 -0
  185. package/src/widgets/top_level.rs +110 -0
  186. package/src/widgets/unrendered.rs +374 -0
  187. package/src/widgets.rs +590 -0
  188. package/src/workspace_scope.rs +358 -0
@@ -0,0 +1,175 @@
1
+ use std::path::PathBuf;
2
+
3
+ use clap::ArgMatches;
4
+
5
+ use crate::output::ReportCommand;
6
+ use crate::{BoundaryCallRule, BoundaryRule, DartDecimateConfig};
7
+
8
+ use super::{CliError, TraceSymbolSpec};
9
+
10
+ pub(super) struct TraceRequestArgs {
11
+ pub(super) file: Option<PathBuf>,
12
+ pub(super) symbol: Option<TraceSymbolSpec>,
13
+ pub(super) dependency: Option<String>,
14
+ pub(super) clone: Option<String>,
15
+ }
16
+
17
+ pub(super) fn boundary_rules(
18
+ command: ReportCommand,
19
+ subcommand: &ArgMatches,
20
+ ) -> Result<Vec<BoundaryRule>, CliError> {
21
+ if !matches!(command, ReportCommand::Check | ReportCommand::Audit) {
22
+ return Ok(Vec::new());
23
+ }
24
+
25
+ subcommand
26
+ .get_many::<String>("boundary")
27
+ .into_iter()
28
+ .flatten()
29
+ .map(|value| parse_boundary_rule(value))
30
+ .collect()
31
+ }
32
+
33
+ pub(super) fn boundary_call_rules(
34
+ command: ReportCommand,
35
+ subcommand: &ArgMatches,
36
+ config: &DartDecimateConfig,
37
+ ) -> Result<Vec<BoundaryCallRule>, CliError> {
38
+ let mut rules = config.boundary_calls.clone();
39
+ if !matches!(command, ReportCommand::Check | ReportCommand::Audit) {
40
+ return Ok(rules);
41
+ }
42
+
43
+ rules.extend(cli_boundary_call_rules(subcommand)?);
44
+ Ok(rules)
45
+ }
46
+
47
+ pub(super) fn policy_pack_paths(
48
+ command: ReportCommand,
49
+ subcommand: &ArgMatches,
50
+ config: &DartDecimateConfig,
51
+ ) -> Vec<PathBuf> {
52
+ let mut paths = config.policy_packs.clone();
53
+ if matches!(command, ReportCommand::Check | ReportCommand::Audit)
54
+ && let Some(values) = subcommand.get_many::<PathBuf>("policy-pack")
55
+ {
56
+ paths.extend(values.cloned());
57
+ }
58
+ paths
59
+ }
60
+
61
+ fn cli_boundary_call_rules(subcommand: &ArgMatches) -> Result<Vec<BoundaryCallRule>, CliError> {
62
+ subcommand
63
+ .get_many::<String>("boundary-call")
64
+ .into_iter()
65
+ .flatten()
66
+ .map(|value| parse_boundary_call_rule(value))
67
+ .collect()
68
+ }
69
+
70
+ pub(super) fn trace_request_args(
71
+ command: ReportCommand,
72
+ subcommand: &ArgMatches,
73
+ ) -> Result<TraceRequestArgs, CliError> {
74
+ let inspect_symbol =
75
+ command == ReportCommand::Inspect && subcommand.get_one::<String>("symbol").is_some();
76
+ let trace_file = if command == ReportCommand::TraceFile
77
+ || (command == ReportCommand::Inspect && !inspect_symbol)
78
+ {
79
+ subcommand.get_one::<PathBuf>("file").cloned()
80
+ } else {
81
+ None
82
+ };
83
+ let trace_symbol = if matches!(command, ReportCommand::TraceSymbol | ReportCommand::Inspect)
84
+ && (command == ReportCommand::TraceSymbol || inspect_symbol)
85
+ {
86
+ let file = subcommand
87
+ .try_get_one::<PathBuf>("file")
88
+ .ok()
89
+ .flatten()
90
+ .cloned();
91
+ subcommand
92
+ .get_one::<String>("symbol")
93
+ .map(|value| parse_trace_symbol(file, value))
94
+ .transpose()?
95
+ } else {
96
+ None
97
+ };
98
+ let trace_dependency = if command == ReportCommand::TraceDependency {
99
+ subcommand.get_one::<String>("dependency").cloned()
100
+ } else {
101
+ None
102
+ };
103
+ let trace_clone = if command == ReportCommand::TraceClone {
104
+ subcommand.get_one::<String>("fingerprint").cloned()
105
+ } else {
106
+ None
107
+ };
108
+ if command == ReportCommand::Inspect && trace_file.is_none() && trace_symbol.is_none() {
109
+ return Err(CliError::MissingInspectTarget);
110
+ }
111
+
112
+ Ok(TraceRequestArgs {
113
+ file: trace_file,
114
+ symbol: trace_symbol,
115
+ dependency: trace_dependency,
116
+ clone: trace_clone,
117
+ })
118
+ }
119
+
120
+ fn parse_trace_symbol(file: Option<PathBuf>, value: &str) -> Result<TraceSymbolSpec, CliError> {
121
+ if let Some(file) = file {
122
+ if value.is_empty() || value.contains(':') {
123
+ return Err(CliError::TraceSymbol {
124
+ value: value.to_owned(),
125
+ });
126
+ }
127
+ return Ok(TraceSymbolSpec {
128
+ file,
129
+ symbol: value.to_owned(),
130
+ });
131
+ }
132
+
133
+ let Some((file, symbol)) = value.rsplit_once(':') else {
134
+ return Err(CliError::TraceSymbol {
135
+ value: value.to_owned(),
136
+ });
137
+ };
138
+ if file.is_empty() || symbol.is_empty() {
139
+ return Err(CliError::TraceSymbol {
140
+ value: value.to_owned(),
141
+ });
142
+ }
143
+ Ok(TraceSymbolSpec {
144
+ file: PathBuf::from(file),
145
+ symbol: symbol.to_owned(),
146
+ })
147
+ }
148
+
149
+ fn parse_boundary_rule(value: &str) -> Result<BoundaryRule, CliError> {
150
+ let Some((from, disallow)) = value.split_once(':') else {
151
+ return Err(CliError::BoundaryRule {
152
+ value: value.to_owned(),
153
+ });
154
+ };
155
+ if from.is_empty() || disallow.is_empty() {
156
+ return Err(CliError::BoundaryRule {
157
+ value: value.to_owned(),
158
+ });
159
+ }
160
+ Ok(BoundaryRule::new(from, disallow))
161
+ }
162
+
163
+ fn parse_boundary_call_rule(value: &str) -> Result<BoundaryCallRule, CliError> {
164
+ let Some((from, pattern)) = value.split_once(':') else {
165
+ return Err(CliError::BoundaryCallRule {
166
+ value: value.to_owned(),
167
+ });
168
+ };
169
+ if from.is_empty() || pattern.is_empty() {
170
+ return Err(CliError::BoundaryCallRule {
171
+ value: value.to_owned(),
172
+ });
173
+ }
174
+ Ok(BoundaryCallRule::new(from, vec![pattern.to_owned()]))
175
+ }
@@ -0,0 +1,72 @@
1
+ use std::io::Write;
2
+
3
+ use clap::{Arg, Command};
4
+
5
+ use crate::config::config_schema;
6
+ use crate::manifest::dart_decimate_schema;
7
+ use crate::policy::rule_pack_schema;
8
+ use crate::report_schema::report_schema;
9
+
10
+ use super::CliError;
11
+
12
+ pub(super) fn config_schema_command() -> Command {
13
+ schema_command(
14
+ "config-schema",
15
+ "Print the Dart Decimate configuration JSON schema",
16
+ )
17
+ }
18
+
19
+ pub(super) fn report_schema_command() -> Command {
20
+ schema_command(
21
+ "report-schema",
22
+ "Print the Dart Decimate report JSON schema",
23
+ )
24
+ }
25
+
26
+ pub(super) fn rule_pack_schema_command() -> Command {
27
+ schema_command(
28
+ "rule-pack-schema",
29
+ "Print the Dart Decimate policy rule-pack JSON schema",
30
+ )
31
+ }
32
+
33
+ pub(super) fn manifest_command() -> Command {
34
+ schema_command("schema", "Print the Dart Decimate CLI and issue manifest")
35
+ }
36
+
37
+ pub(super) fn run_config_schema<W: Write>(mut writer: W) -> Result<i32, CliError> {
38
+ serde_json::to_writer_pretty(&mut writer, &config_schema())?;
39
+ writeln!(writer)?;
40
+ Ok(0)
41
+ }
42
+
43
+ pub(super) fn run_report_schema<W: Write>(mut writer: W) -> Result<i32, CliError> {
44
+ serde_json::to_writer_pretty(&mut writer, &report_schema())?;
45
+ writeln!(writer)?;
46
+ Ok(0)
47
+ }
48
+
49
+ pub(super) fn run_rule_pack_schema<W: Write>(mut writer: W) -> Result<i32, CliError> {
50
+ serde_json::to_writer_pretty(&mut writer, &rule_pack_schema())?;
51
+ writeln!(writer)?;
52
+ Ok(0)
53
+ }
54
+
55
+ pub(super) fn run_manifest<W: Write>(mut writer: W) -> Result<i32, CliError> {
56
+ serde_json::to_writer_pretty(&mut writer, &dart_decimate_schema())?;
57
+ writeln!(writer)?;
58
+ Ok(0)
59
+ }
60
+
61
+ fn schema_command(name: &'static str, about: &'static str) -> Command {
62
+ Command::new(name).about(about).arg(schema_format_arg())
63
+ }
64
+
65
+ fn schema_format_arg() -> Arg {
66
+ Arg::new("format")
67
+ .long("format")
68
+ .value_name("FORMAT")
69
+ .help("Output format")
70
+ .default_value("human")
71
+ .value_parser(["human", "json"])
72
+ }
@@ -0,0 +1,174 @@
1
+ use std::collections::BTreeSet;
2
+ use std::path::PathBuf;
3
+
4
+ use clap::{Arg, ArgAction, ArgMatches, Command, value_parser};
5
+
6
+ use crate::changed_scope::{changed_file_scope, changed_files};
7
+ use crate::graph::normalize_against;
8
+ use crate::output::{AnalysisResults, ReportCommand};
9
+ use crate::{
10
+ ScannedProject, changed_workspace_file_scope, local_pub_packages, workspace_file_scope,
11
+ };
12
+
13
+ use super::{CliError, CommandRequest};
14
+
15
+ pub(super) fn report_command(command: Command) -> Command {
16
+ command
17
+ .arg(file_arg())
18
+ .arg(workspace_arg())
19
+ .arg(changed_workspaces_arg())
20
+ .arg(changed_since_arg())
21
+ }
22
+
23
+ pub(super) fn file_arg() -> Arg {
24
+ Arg::new("file")
25
+ .long("file")
26
+ .value_name("PATH")
27
+ .help("Scope findings to a specific file")
28
+ .num_args(1)
29
+ .action(ArgAction::Append)
30
+ .value_parser(value_parser!(PathBuf))
31
+ }
32
+
33
+ pub(super) fn workspace_arg() -> Arg {
34
+ Arg::new("workspace")
35
+ .long("workspace")
36
+ .value_name("PATTERN")
37
+ .help("Scope findings to matching local pub package names or roots")
38
+ .num_args(1)
39
+ .action(ArgAction::Append)
40
+ .conflicts_with("changed-workspaces")
41
+ }
42
+
43
+ pub(super) fn changed_workspaces_arg() -> Arg {
44
+ Arg::new("changed-workspaces")
45
+ .long("changed-workspaces")
46
+ .value_name("REF")
47
+ .help("Scope findings to local pub packages changed since a Git ref")
48
+ .conflicts_with("workspace")
49
+ }
50
+
51
+ pub(super) fn changed_since_arg() -> Arg {
52
+ Arg::new("changed-since")
53
+ .long("changed-since")
54
+ .value_name("REF")
55
+ .help("Scope findings to files changed since a Git ref")
56
+ }
57
+
58
+ pub(super) fn workspace_patterns(command: ReportCommand, subcommand: &ArgMatches) -> Vec<String> {
59
+ if !supports_report_scope(command) {
60
+ return Vec::new();
61
+ }
62
+
63
+ subcommand
64
+ .get_many::<String>("workspace")
65
+ .map(|values| values.cloned().collect())
66
+ .unwrap_or_default()
67
+ }
68
+
69
+ pub(super) fn file_paths(command: ReportCommand, subcommand: &ArgMatches) -> Vec<PathBuf> {
70
+ if !supports_report_scope(command) {
71
+ return Vec::new();
72
+ }
73
+
74
+ subcommand
75
+ .get_many::<PathBuf>("file")
76
+ .map(|values| values.cloned().collect())
77
+ .unwrap_or_default()
78
+ }
79
+
80
+ pub(super) fn changed_workspaces(
81
+ command: ReportCommand,
82
+ subcommand: &ArgMatches,
83
+ ) -> Option<String> {
84
+ if supports_report_scope(command) {
85
+ subcommand.get_one::<String>("changed-workspaces").cloned()
86
+ } else {
87
+ None
88
+ }
89
+ }
90
+
91
+ pub(super) fn changed_since(command: ReportCommand, subcommand: &ArgMatches) -> Option<String> {
92
+ if supports_report_scope(command) {
93
+ subcommand.get_one::<String>("changed-since").cloned()
94
+ } else {
95
+ None
96
+ }
97
+ }
98
+
99
+ pub(super) fn apply_report_scope(
100
+ project: &ScannedProject,
101
+ results: &mut AnalysisResults,
102
+ request: &CommandRequest,
103
+ ) -> Result<(), CliError> {
104
+ if request.file_paths.is_empty()
105
+ && request.workspace_patterns.is_empty()
106
+ && request.changed_workspaces.is_none()
107
+ && request.changed_since.is_none()
108
+ {
109
+ return Ok(());
110
+ }
111
+
112
+ if !request.file_paths.is_empty() {
113
+ let file_scope = request
114
+ .file_paths
115
+ .iter()
116
+ .map(|path| normalize_against(&project.root, path))
117
+ .collect::<Vec<_>>();
118
+ results.file_scope = Some(intersect_scope(results.file_scope.take(), file_scope));
119
+ }
120
+
121
+ if let Some(base) = &request.changed_since {
122
+ let file_scope = changed_file_scope_for_command(request.command, project, base)?;
123
+ results.file_scope = Some(intersect_scope(results.file_scope.take(), file_scope));
124
+ }
125
+
126
+ if !request.workspace_patterns.is_empty() {
127
+ let packages = local_pub_packages(&project.root)?;
128
+ let workspace_scope =
129
+ workspace_file_scope(project, &packages, &request.workspace_patterns)?;
130
+ results.file_scope = Some(intersect_scope(results.file_scope.take(), workspace_scope));
131
+ }
132
+
133
+ if let Some(base) = &request.changed_workspaces {
134
+ let packages = local_pub_packages(&project.root)?;
135
+ let changed = changed_files(&project.root, base)?;
136
+ let workspace_scope = changed_workspace_file_scope(project, &packages, &changed);
137
+ results.file_scope = Some(intersect_scope(results.file_scope.take(), workspace_scope));
138
+ }
139
+
140
+ Ok(())
141
+ }
142
+
143
+ fn changed_file_scope_for_command(
144
+ command: ReportCommand,
145
+ project: &ScannedProject,
146
+ base: &str,
147
+ ) -> Result<Vec<PathBuf>, CliError> {
148
+ if command == ReportCommand::Audit {
149
+ return Ok(changed_file_scope(project, base)?);
150
+ }
151
+ Ok(changed_files(&project.root, base)?)
152
+ }
153
+
154
+ fn intersect_scope(existing: Option<Vec<PathBuf>>, next: Vec<PathBuf>) -> Vec<PathBuf> {
155
+ let Some(existing) = existing else {
156
+ return next;
157
+ };
158
+ let next = next.into_iter().collect::<BTreeSet<_>>();
159
+ existing
160
+ .into_iter()
161
+ .filter(|path| next.contains(path))
162
+ .collect()
163
+ }
164
+
165
+ fn supports_report_scope(command: ReportCommand) -> bool {
166
+ !matches!(
167
+ command,
168
+ ReportCommand::TraceFile
169
+ | ReportCommand::TraceSymbol
170
+ | ReportCommand::TraceDependency
171
+ | ReportCommand::TraceClone
172
+ | ReportCommand::Inspect
173
+ )
174
+ }
@@ -0,0 +1,175 @@
1
+ use std::path::PathBuf;
2
+
3
+ use clap::{Arg, ArgAction, ArgMatches, Command, parser::ValueSource, value_parser};
4
+
5
+ use crate::ReportCommand;
6
+ use crate::SecurityOptions;
7
+ use crate::security_gate::{SecurityDiffSource, SecurityGateMode};
8
+
9
+ #[derive(Debug, Clone, Default, PartialEq, Eq)]
10
+ pub(super) struct SecurityCliOptions {
11
+ pub(super) sarif_file: Option<PathBuf>,
12
+ pub(super) gate: Option<SecurityGateMode>,
13
+ pub(super) diff: Option<SecurityDiffSource>,
14
+ pub(super) changed_since: Option<String>,
15
+ pub(super) ci: bool,
16
+ pub(super) fail_on_issues: bool,
17
+ pub(super) summary: bool,
18
+ }
19
+
20
+ #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
21
+ pub(super) enum SecurityIssueMode {
22
+ #[default]
23
+ VerdictOnly,
24
+ FailOnIssues,
25
+ }
26
+
27
+ impl SecurityIssueMode {
28
+ pub(super) const fn fails_on_issues(self) -> bool {
29
+ matches!(self, Self::FailOnIssues)
30
+ }
31
+ }
32
+
33
+ #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
34
+ pub(super) enum SecuritySummaryMode {
35
+ #[default]
36
+ Full,
37
+ CountsOnly,
38
+ }
39
+
40
+ impl SecuritySummaryMode {
41
+ pub(super) const fn is_counts_only(self) -> bool {
42
+ matches!(self, Self::CountsOnly)
43
+ }
44
+ }
45
+
46
+ impl SecurityCliOptions {
47
+ pub(super) const fn issue_mode(&self) -> SecurityIssueMode {
48
+ if self.fail_on_issues || self.ci {
49
+ SecurityIssueMode::FailOnIssues
50
+ } else {
51
+ SecurityIssueMode::VerdictOnly
52
+ }
53
+ }
54
+
55
+ pub(super) const fn summary_mode(&self) -> SecuritySummaryMode {
56
+ if self.summary {
57
+ SecuritySummaryMode::CountsOnly
58
+ } else {
59
+ SecuritySummaryMode::Full
60
+ }
61
+ }
62
+ }
63
+
64
+ pub(super) fn security_command(command: Command) -> Command {
65
+ command
66
+ .arg(
67
+ Arg::new("top")
68
+ .long("top")
69
+ .value_name("N")
70
+ .help("Show only the N most frequent security candidate groups")
71
+ .value_parser(value_parser!(usize)),
72
+ )
73
+ .arg(
74
+ Arg::new("surface")
75
+ .long("surface")
76
+ .help("Include attack-surface inventory entries")
77
+ .action(ArgAction::SetTrue),
78
+ )
79
+ .arg(
80
+ Arg::new("sarif-file")
81
+ .long("sarif-file")
82
+ .value_name("PATH")
83
+ .help("Write security SARIF output to a file")
84
+ .value_parser(value_parser!(PathBuf)),
85
+ )
86
+ .arg(
87
+ Arg::new("ci")
88
+ .long("ci")
89
+ .help("Emit SARIF and fail when security candidates are found")
90
+ .action(ArgAction::SetTrue),
91
+ )
92
+ .arg(
93
+ Arg::new("fail-on-issues")
94
+ .long("fail-on-issues")
95
+ .help("Exit with code 1 when security candidates are found")
96
+ .action(ArgAction::SetTrue),
97
+ )
98
+ .arg(
99
+ Arg::new("gate")
100
+ .long("gate")
101
+ .value_name("MODE")
102
+ .help("Gate security output to changed candidates")
103
+ .value_parser(["new", "newly-reachable"]),
104
+ )
105
+ .arg(
106
+ Arg::new("diff-file")
107
+ .long("diff-file")
108
+ .value_name("PATCH")
109
+ .help("Unified diff for line-level security scoping; use - for stdin")
110
+ .conflicts_with("diff-stdin")
111
+ .value_parser(value_parser!(PathBuf)),
112
+ )
113
+ .arg(
114
+ Arg::new("diff-stdin")
115
+ .long("diff-stdin")
116
+ .help("Read a unified diff from stdin for --gate new")
117
+ .conflicts_with("diff-file")
118
+ .action(ArgAction::SetTrue),
119
+ )
120
+ }
121
+
122
+ pub(super) fn security_options_with_defaults(
123
+ matches: &ArgMatches,
124
+ mut options: SecurityOptions,
125
+ ) -> SecurityOptions {
126
+ if matches.value_source("top") == Some(ValueSource::CommandLine) {
127
+ options.top = matches.get_one::<usize>("top").copied();
128
+ }
129
+ if matches.value_source("surface") == Some(ValueSource::CommandLine) {
130
+ options.surface = matches.get_flag("surface");
131
+ }
132
+ options
133
+ }
134
+
135
+ pub(super) fn security_cli_options(
136
+ command: ReportCommand,
137
+ matches: &ArgMatches,
138
+ ) -> SecurityCliOptions {
139
+ if command != ReportCommand::Security {
140
+ return SecurityCliOptions::default();
141
+ }
142
+ SecurityCliOptions {
143
+ sarif_file: matches.get_one::<PathBuf>("sarif-file").cloned(),
144
+ gate: security_gate_mode(matches),
145
+ diff: security_diff(matches),
146
+ changed_since: matches.get_one::<String>("changed-since").cloned(),
147
+ ci: matches.get_flag("ci"),
148
+ fail_on_issues: matches.get_flag("fail-on-issues"),
149
+ summary: matches.get_flag("summary"),
150
+ }
151
+ }
152
+
153
+ fn security_diff(matches: &ArgMatches) -> Option<SecurityDiffSource> {
154
+ if let Some(path) = matches.get_one::<PathBuf>("diff-file") {
155
+ if path == std::path::Path::new("-") {
156
+ return Some(SecurityDiffSource::Stdin);
157
+ }
158
+ return Some(SecurityDiffSource::File(path.clone()));
159
+ }
160
+ if matches.get_flag("diff-stdin") {
161
+ return Some(SecurityDiffSource::Stdin);
162
+ }
163
+ None
164
+ }
165
+
166
+ fn security_gate_mode(matches: &ArgMatches) -> Option<SecurityGateMode> {
167
+ matches
168
+ .get_one::<String>("gate")
169
+ .map(String::as_str)
170
+ .map(|value| match value {
171
+ "new" => SecurityGateMode::New,
172
+ "newly-reachable" => SecurityGateMode::NewlyReachable,
173
+ _ => unreachable!("clap rejects unsupported security gate modes"),
174
+ })
175
+ }
@@ -0,0 +1,66 @@
1
+ use std::io::{self, Read};
2
+ use std::path::Path;
3
+
4
+ use crate::ScannedProject;
5
+ use crate::security_gate::{self, SecurityDiffSource, SecurityGateError, SecurityGateMode};
6
+
7
+ use super::entry_points::entry_points_for_check;
8
+ use super::{CliError, CommandRequest, resolve_report_path};
9
+
10
+ pub(super) fn apply_security_gate(
11
+ project: &ScannedProject,
12
+ request: &CommandRequest,
13
+ report: &mut crate::JsonReport,
14
+ ) -> Result<(), CliError> {
15
+ let root = &project.root;
16
+ if matches!(
17
+ request.security_gate,
18
+ Some(SecurityGateMode::New | SecurityGateMode::NewlyReachable)
19
+ ) && request.security_diff.is_none()
20
+ && request.security_changed_since.is_none()
21
+ {
22
+ return Err(SecurityGateError::MissingDiffFile.into());
23
+ }
24
+ if request.security_gate.is_none() && request.security_diff.is_none() {
25
+ return Ok(());
26
+ }
27
+ if let Some(scope) = changed_line_scope(root, request)? {
28
+ if request.security_gate == Some(SecurityGateMode::NewlyReachable) {
29
+ let entry_points =
30
+ entry_points_for_check(project, &request.entry_points, request.entry_point_mode());
31
+ security_gate::apply_changed_reachability_gate(
32
+ root,
33
+ &project.graph,
34
+ report,
35
+ entry_points,
36
+ &scope,
37
+ );
38
+ } else {
39
+ security_gate::apply_changed_line_gate(report, &scope);
40
+ }
41
+ }
42
+ Ok(())
43
+ }
44
+
45
+ fn changed_line_scope(
46
+ root: &Path,
47
+ request: &CommandRequest,
48
+ ) -> Result<Option<security_gate::ChangedLineScope>, CliError> {
49
+ let Some(source) = request.security_diff.as_ref() else {
50
+ return match request.security_changed_since.as_deref() {
51
+ Some(base) => Ok(Some(security_gate::changed_lines_from_git(root, base)?)),
52
+ None => Ok(None),
53
+ };
54
+ };
55
+ match source {
56
+ SecurityDiffSource::File(path) => Ok(Some(security_gate::changed_lines_from_diff_file(
57
+ root,
58
+ &resolve_report_path(root, path),
59
+ )?)),
60
+ SecurityDiffSource::Stdin => {
61
+ let mut source = String::new();
62
+ io::stdin().read_to_string(&mut source)?;
63
+ Ok(Some(security_gate::changed_lines_from_diff(root, &source)))
64
+ }
65
+ }
66
+ }
@@ -0,0 +1,43 @@
1
+ use crate::output::{AuditRiskLevel, JsonReport, ReportCommand, Verdict};
2
+
3
+ use super::{CommandRequest, ReportOutputFormat, audit_run::AuditGate};
4
+
5
+ pub(super) fn apply_security_summary(request: &CommandRequest, report: &mut JsonReport) {
6
+ if !request.security_summary_mode.is_counts_only()
7
+ || request.command != ReportCommand::Security
8
+ || request.format == ReportOutputFormat::Sarif
9
+ {
10
+ return;
11
+ }
12
+
13
+ report.findings.clear();
14
+ report.clone_groups.clear();
15
+ report.complexity.clear();
16
+ report.file_scores.clear();
17
+ report.hotspots.clear();
18
+ report.refactoring_targets.clear();
19
+ report.threshold_overrides.clear();
20
+ report.feature_flags.clear();
21
+ report.security_candidates.clear();
22
+ report.attack_surface.clear();
23
+ report.runtime_coverage = None;
24
+ report.next_steps.clear();
25
+ }
26
+
27
+ pub(super) fn exit_code(request: &CommandRequest, report: &JsonReport, regressed: bool) -> i32 {
28
+ if request.security_gate.is_some() {
29
+ if report.verdict == Verdict::Pass {
30
+ 0
31
+ } else {
32
+ 8
33
+ }
34
+ } else if request.security_issue_mode.fails_on_issues() {
35
+ i32::from(report.summary.findings > 0)
36
+ } else if request.command == ReportCommand::Audit && request.audit_gate == AuditGate::NewOnly {
37
+ i32::from(report.summary.risk_level == Some(AuditRiskLevel::Fail))
38
+ } else if request.fail_on_regression {
39
+ i32::from(regressed)
40
+ } else {
41
+ i32::from(report.verdict != Verdict::Pass)
42
+ }
43
+ }