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.
- package/Cargo.lock +572 -0
- package/Cargo.toml +41 -0
- package/LICENSE +21 -0
- package/README.md +617 -0
- package/assets/dart-decimate-banner.png +0 -0
- package/npm/bin/dart-decimate-mcp.js +48 -0
- package/npm/bin/dart-decimate.js +48 -0
- package/npm/scripts/postinstall.js +46 -0
- package/npm/scripts/test-npx-local.js +54 -0
- package/npm/scripts/test-npx-mcp-local.js +56 -0
- package/package.json +45 -0
- package/src/baseline.rs +643 -0
- package/src/bin/dart-decimate-mcp.rs +11 -0
- package/src/boundaries/tests.rs +60 -0
- package/src/boundaries.rs +317 -0
- package/src/changed_scope.rs +117 -0
- package/src/ci_template.rs +399 -0
- package/src/cli/analyze.rs +462 -0
- package/src/cli/analyzer_options.rs +82 -0
- package/src/cli/audit_run.rs +327 -0
- package/src/cli/boundary_args.rs +44 -0
- package/src/cli/ci_template_run.rs +196 -0
- package/src/cli/command_name.rs +20 -0
- package/src/cli/common_args.rs +167 -0
- package/src/cli/config_run.rs +78 -0
- package/src/cli/coverage_run.rs +393 -0
- package/src/cli/decision_surface_run.rs +99 -0
- package/src/cli/default_command.rs +69 -0
- package/src/cli/dupes_args.rs +150 -0
- package/src/cli/entry_points.rs +121 -0
- package/src/cli/error.rs +149 -0
- package/src/cli/error_output.rs +68 -0
- package/src/cli/explain_run.rs +72 -0
- package/src/cli/fix_run.rs +162 -0
- package/src/cli/flags_args.rs +23 -0
- package/src/cli/health_args.rs +200 -0
- package/src/cli/hooks_run.rs +191 -0
- package/src/cli/impact_run.rs +95 -0
- package/src/cli/init_run.rs +49 -0
- package/src/cli/inspect_args.rs +20 -0
- package/src/cli/inspect_run.rs +66 -0
- package/src/cli/issue_filter_args.rs +240 -0
- package/src/cli/list_run.rs +311 -0
- package/src/cli/mode_args.rs +72 -0
- package/src/cli/output_format.rs +62 -0
- package/src/cli/regression_args.rs +156 -0
- package/src/cli/request_args.rs +175 -0
- package/src/cli/schema_commands.rs +72 -0
- package/src/cli/scope_args.rs +174 -0
- package/src/cli/security_args.rs +175 -0
- package/src/cli/security_gate_run.rs +66 -0
- package/src/cli/security_summary_run.rs +43 -0
- package/src/cli/summary_args.rs +12 -0
- package/src/cli/tests.rs +516 -0
- package/src/cli/trace_args.rs +67 -0
- package/src/cli/trace_run.rs +131 -0
- package/src/cli/trace_tests.rs +235 -0
- package/src/cli/unsupported_run.rs +116 -0
- package/src/cli/watch_run.rs +153 -0
- package/src/cli.rs +603 -0
- package/src/config/boundary_config.rs +235 -0
- package/src/config/cache.rs +25 -0
- package/src/config/dependencies.rs +114 -0
- package/src/config/health.rs +237 -0
- package/src/config/jsonc.rs +66 -0
- package/src/config/policy.rs +91 -0
- package/src/config/rule_aliases.rs +487 -0
- package/src/config/rules.rs +339 -0
- package/src/config.rs +635 -0
- package/src/coverage.rs +469 -0
- package/src/dart_parser.rs +673 -0
- package/src/decision_surface.rs +405 -0
- package/src/dependencies/analyze.rs +218 -0
- package/src/dependencies/codegen.rs +118 -0
- package/src/dependencies/discovery.rs +60 -0
- package/src/dependencies/overrides.rs +76 -0
- package/src/dependencies/private_src_imports.rs +32 -0
- package/src/dependencies/pubspec_document.rs +86 -0
- package/src/dependencies/pubspec_entry.rs +96 -0
- package/src/dependencies/tests.rs +544 -0
- package/src/dependencies/usage.rs +33 -0
- package/src/dependencies.rs +553 -0
- package/src/dependency_scripts.rs +91 -0
- package/src/dupes/lex.rs +236 -0
- package/src/dupes/tests.rs +119 -0
- package/src/dupes.rs +625 -0
- package/src/explain.rs +692 -0
- package/src/extract/directives.rs +252 -0
- package/src/extract/error_tests.rs +25 -0
- package/src/extract/members.rs +210 -0
- package/src/extract/primary_constructors.rs +421 -0
- package/src/extract/references.rs +151 -0
- package/src/extract/routes.rs +691 -0
- package/src/extract/signatures.rs +266 -0
- package/src/extract/string_tests.rs +18 -0
- package/src/extract/strings.rs +107 -0
- package/src/extract/tests.rs +697 -0
- package/src/extract.rs +680 -0
- package/src/feature_flags/tests.rs +76 -0
- package/src/feature_flags.rs +453 -0
- package/src/finding_identity.rs +60 -0
- package/src/fix.rs +551 -0
- package/src/generated.rs +53 -0
- package/src/graph/augment_tests.rs +212 -0
- package/src/graph/conditionals.rs +74 -0
- package/src/graph/package_config_tests.rs +100 -0
- package/src/graph/parts.rs +248 -0
- package/src/graph/pubspec_tests.rs +119 -0
- package/src/graph/tests.rs +837 -0
- package/src/graph/uri_tests.rs +102 -0
- package/src/graph.rs +644 -0
- package/src/health/coverage.rs +309 -0
- package/src/health/ownership.rs +175 -0
- package/src/health/runtime_coverage.rs +616 -0
- package/src/health/runtime_intelligence.rs +511 -0
- package/src/health/scores.rs +296 -0
- package/src/health/tests.rs +240 -0
- package/src/health/threshold_types.rs +163 -0
- package/src/health/thresholds.rs +229 -0
- package/src/health/types.rs +636 -0
- package/src/health.rs +597 -0
- package/src/hooks/agent_settings.rs +175 -0
- package/src/hooks/templates.rs +51 -0
- package/src/hooks.rs +569 -0
- package/src/impact.rs +287 -0
- package/src/init.rs +237 -0
- package/src/inspect.rs +142 -0
- package/src/intelligence/tests.rs +232 -0
- package/src/intelligence.rs +305 -0
- package/src/lib.rs +198 -0
- package/src/main.rs +5 -0
- package/src/manifest/mcp.rs +565 -0
- package/src/manifest.rs +552 -0
- package/src/mcp/cli_args/value_args.rs +175 -0
- package/src/mcp/cli_args.rs +658 -0
- package/src/mcp/cli_args_tests.rs +522 -0
- package/src/mcp/code_execute.rs +671 -0
- package/src/mcp/tools.rs +660 -0
- package/src/mcp.rs +325 -0
- package/src/output/audit_risk.rs +135 -0
- package/src/output/dependency_findings.rs +327 -0
- package/src/output/duplication_findings.rs +114 -0
- package/src/output/feature_flag_findings.rs +122 -0
- package/src/output/format.rs +31 -0
- package/src/output/graph_findings.rs +461 -0
- package/src/output/health_findings.rs +612 -0
- package/src/output/human.rs +27 -0
- package/src/output/next_steps.rs +92 -0
- package/src/output/route_findings.rs +76 -0
- package/src/output/runtime_coverage.rs +515 -0
- package/src/output/scope.rs +183 -0
- package/src/output/security_findings.rs +390 -0
- package/src/output/security_sarif.rs +150 -0
- package/src/output/suppressions.rs +323 -0
- package/src/output/symbol_findings.rs +257 -0
- package/src/output/tests.rs +114 -0
- package/src/output/types.rs +604 -0
- package/src/output/widget_findings.rs +213 -0
- package/src/output.rs +682 -0
- package/src/package_map.rs +495 -0
- package/src/policy/packs.rs +232 -0
- package/src/policy/tests.rs +134 -0
- package/src/policy.rs +640 -0
- package/src/project_list.rs +376 -0
- package/src/report_schema/inventory.rs +418 -0
- package/src/report_schema.rs +488 -0
- package/src/routes.rs +354 -0
- package/src/scan/tests.rs +250 -0
- package/src/scan.rs +275 -0
- package/src/security/detect.rs +639 -0
- package/src/security/tests.rs +178 -0
- package/src/security.rs +373 -0
- package/src/security_gate.rs +555 -0
- package/src/symbols/path_filters.rs +27 -0
- package/src/symbols/private_type_leaks.rs +125 -0
- package/src/symbols/riverpod.rs +113 -0
- package/src/symbols/tests.rs +692 -0
- package/src/symbols.rs +693 -0
- package/src/trace/tests.rs +225 -0
- package/src/trace.rs +690 -0
- package/src/unsupported.rs +47 -0
- package/src/widgets/lifecycle.rs +484 -0
- package/src/widgets/params.rs +236 -0
- package/src/widgets/tests.rs +430 -0
- package/src/widgets/top_level.rs +110 -0
- package/src/widgets/unrendered.rs +374 -0
- package/src/widgets.rs +590 -0
- package/src/workspace_scope.rs +358 -0
package/src/graph.rs
ADDED
|
@@ -0,0 +1,644 @@
|
|
|
1
|
+
use std::collections::{BTreeMap, BTreeSet};
|
|
2
|
+
use std::path::{Component, Path, PathBuf};
|
|
3
|
+
|
|
4
|
+
use petgraph::graph::{DiGraph, NodeIndex};
|
|
5
|
+
use petgraph::visit::EdgeRef;
|
|
6
|
+
use serde::{Deserialize, Serialize};
|
|
7
|
+
use thiserror::Error;
|
|
8
|
+
|
|
9
|
+
use crate::package_map::PackageMap;
|
|
10
|
+
use crate::{DartCombinator, DartFile, DartUriCondition, Location};
|
|
11
|
+
|
|
12
|
+
mod conditionals;
|
|
13
|
+
mod parts;
|
|
14
|
+
|
|
15
|
+
use conditionals::{selected_exports, selected_imports};
|
|
16
|
+
pub use parts::{InvalidPartReason, InvalidPartRelationship};
|
|
17
|
+
use parts::{add_orphan_part_relationships, add_part_dependency};
|
|
18
|
+
|
|
19
|
+
/// A directed dependency graph where nodes are Dart files.
|
|
20
|
+
pub type DependencyGraph = DiGraph<ModuleNode, DependencyEdge>;
|
|
21
|
+
|
|
22
|
+
/// A Dart file node in the module graph.
|
|
23
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
24
|
+
pub struct ModuleNode {
|
|
25
|
+
/// Normalized filesystem path for the Dart file.
|
|
26
|
+
pub path: PathBuf,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/// A directed Dart library relationship edge in the module graph.
|
|
30
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
31
|
+
pub struct DependencyEdge {
|
|
32
|
+
/// Raw import/export/part/augment URI from the source file.
|
|
33
|
+
pub specifier: String,
|
|
34
|
+
/// Whether the dependency came from an import, export, part, or augment directive.
|
|
35
|
+
pub kind: DependencyKind,
|
|
36
|
+
/// Location of the directive in the source file.
|
|
37
|
+
pub location: Location,
|
|
38
|
+
/// Import/export visibility metadata carried by this edge.
|
|
39
|
+
pub visibility: DependencyVisibility,
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/// Dart import/export visibility metadata attached to a dependency edge.
|
|
43
|
+
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
|
44
|
+
pub struct DependencyVisibility {
|
|
45
|
+
/// Optional import prefix after `as`.
|
|
46
|
+
pub prefix: Option<String>,
|
|
47
|
+
/// Whether the import uses `deferred as`.
|
|
48
|
+
pub deferred: bool,
|
|
49
|
+
/// Conditional URI guard that selected this edge, if any.
|
|
50
|
+
#[serde(skip_serializing_if = "Option::is_none")]
|
|
51
|
+
pub condition: Option<DartUriCondition>,
|
|
52
|
+
/// `show` and `hide` combinators applied to the import or export.
|
|
53
|
+
pub combinators: Vec<DartCombinator>,
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/// Options controlling dependency graph construction.
|
|
57
|
+
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
|
58
|
+
pub struct GraphOptions {
|
|
59
|
+
/// Conditional URI environment values. Empty means include every branch.
|
|
60
|
+
pub conditional_environment: BTreeMap<String, String>,
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/// Import/export/part/augment dependency category.
|
|
64
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
|
65
|
+
#[serde(rename_all = "kebab-case")]
|
|
66
|
+
pub enum DependencyKind {
|
|
67
|
+
/// An `import` directive.
|
|
68
|
+
Import,
|
|
69
|
+
/// An `export` directive.
|
|
70
|
+
Export,
|
|
71
|
+
/// A `part` directive.
|
|
72
|
+
Part,
|
|
73
|
+
/// A `library augment` directive.
|
|
74
|
+
Augment,
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/// A resolved graph edge with source and target paths.
|
|
78
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
79
|
+
pub struct ResolvedDependency {
|
|
80
|
+
/// File containing the directive.
|
|
81
|
+
pub from_path: PathBuf,
|
|
82
|
+
/// File resolved from the directive.
|
|
83
|
+
pub to_path: PathBuf,
|
|
84
|
+
/// Raw import/export/part/augment URI from the source file.
|
|
85
|
+
pub specifier: String,
|
|
86
|
+
/// Whether the dependency came from an import, export, part, or augment directive.
|
|
87
|
+
pub kind: DependencyKind,
|
|
88
|
+
/// Location of the directive in the source file.
|
|
89
|
+
pub location: Location,
|
|
90
|
+
/// Import/export visibility metadata carried by this edge.
|
|
91
|
+
pub visibility: DependencyVisibility,
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/// A local dependency URI that did not match a parsed Dart file.
|
|
95
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
96
|
+
pub struct UnresolvedDependency {
|
|
97
|
+
/// File containing the directive.
|
|
98
|
+
pub from_path: PathBuf,
|
|
99
|
+
/// Raw import/export/part/augment URI from the source file.
|
|
100
|
+
pub specifier: String,
|
|
101
|
+
/// Whether the dependency came from an import, export, part, or augment directive.
|
|
102
|
+
pub kind: DependencyKind,
|
|
103
|
+
/// Location of the directive in the source file.
|
|
104
|
+
pub location: Location,
|
|
105
|
+
/// Local filesystem path Dart Decimate tried to resolve.
|
|
106
|
+
pub attempted_path: PathBuf,
|
|
107
|
+
/// Import/export visibility metadata carried by this unresolved edge.
|
|
108
|
+
pub visibility: DependencyVisibility,
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/// A Phase 2 module graph plus path lookup indexes.
|
|
112
|
+
#[derive(Debug, Clone)]
|
|
113
|
+
pub struct ModuleGraph {
|
|
114
|
+
root: PathBuf,
|
|
115
|
+
graph: DependencyGraph,
|
|
116
|
+
nodes_by_path: BTreeMap<PathBuf, NodeIndex>,
|
|
117
|
+
unresolved: Vec<UnresolvedDependency>,
|
|
118
|
+
invalid_part_relationships: Vec<InvalidPartRelationship>,
|
|
119
|
+
packages: PackageMap,
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
impl ModuleGraph {
|
|
123
|
+
/// Project root used to resolve package and relative imports.
|
|
124
|
+
#[must_use]
|
|
125
|
+
pub fn root(&self) -> &Path {
|
|
126
|
+
&self.root
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/// Underlying petgraph directed graph.
|
|
130
|
+
#[must_use]
|
|
131
|
+
pub fn graph(&self) -> &DependencyGraph {
|
|
132
|
+
&self.graph
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/// Number of Dart file nodes.
|
|
136
|
+
#[must_use]
|
|
137
|
+
pub fn node_count(&self) -> usize {
|
|
138
|
+
self.graph.node_count()
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/// Number of resolved dependency edges.
|
|
142
|
+
#[must_use]
|
|
143
|
+
pub fn edge_count(&self) -> usize {
|
|
144
|
+
self.graph.edge_count()
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/// Local dependencies that resolved to paths absent from the parsed input.
|
|
148
|
+
#[must_use]
|
|
149
|
+
pub fn unresolved(&self) -> &[UnresolvedDependency] {
|
|
150
|
+
&self.unresolved
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/// Resolved `part` edges whose target file does not declare a matching `part of`.
|
|
154
|
+
#[must_use]
|
|
155
|
+
pub fn invalid_part_relationships(&self) -> &[InvalidPartRelationship] {
|
|
156
|
+
&self.invalid_part_relationships
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/// Local package names discovered from Pub package resolution metadata.
|
|
160
|
+
#[must_use]
|
|
161
|
+
pub fn package_names(&self) -> Vec<&str> {
|
|
162
|
+
self.packages.names()
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/// Lookup a node by normalized path.
|
|
166
|
+
#[must_use]
|
|
167
|
+
pub fn node_index(&self, path: impl AsRef<Path>) -> Option<NodeIndex> {
|
|
168
|
+
let path = normalize_against(&self.root, path.as_ref());
|
|
169
|
+
self.nodes_by_path.get(&path).copied()
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/// Return all resolved dependencies in graph edge order.
|
|
173
|
+
#[must_use]
|
|
174
|
+
pub fn dependencies(&self) -> Vec<ResolvedDependency> {
|
|
175
|
+
self.graph
|
|
176
|
+
.edge_references()
|
|
177
|
+
.map(|edge| {
|
|
178
|
+
let from = &self.graph[edge.source()].path;
|
|
179
|
+
let to = &self.graph[edge.target()].path;
|
|
180
|
+
let weight = edge.weight();
|
|
181
|
+
ResolvedDependency {
|
|
182
|
+
from_path: from.clone(),
|
|
183
|
+
to_path: to.clone(),
|
|
184
|
+
specifier: weight.specifier.clone(),
|
|
185
|
+
kind: weight.kind,
|
|
186
|
+
location: weight.location,
|
|
187
|
+
visibility: weight.visibility.clone(),
|
|
188
|
+
}
|
|
189
|
+
})
|
|
190
|
+
.collect()
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/// Errors produced while building a module graph.
|
|
195
|
+
#[derive(Debug, Error)]
|
|
196
|
+
pub enum GraphError {
|
|
197
|
+
/// A directory could not be read during pubspec discovery.
|
|
198
|
+
#[error("failed to read directory {path}: {source}")]
|
|
199
|
+
ReadDir {
|
|
200
|
+
/// Directory path.
|
|
201
|
+
path: PathBuf,
|
|
202
|
+
/// Underlying IO error.
|
|
203
|
+
source: std::io::Error,
|
|
204
|
+
},
|
|
205
|
+
/// A directory entry could not be read during pubspec discovery.
|
|
206
|
+
#[error("failed to read directory entry under {path}: {source}")]
|
|
207
|
+
ReadDirEntry {
|
|
208
|
+
/// Directory being scanned.
|
|
209
|
+
path: PathBuf,
|
|
210
|
+
/// Underlying IO error.
|
|
211
|
+
source: std::io::Error,
|
|
212
|
+
},
|
|
213
|
+
/// A file type could not be read during pubspec discovery.
|
|
214
|
+
#[error("failed to read file type for {path}: {source}")]
|
|
215
|
+
FileType {
|
|
216
|
+
/// Entry path.
|
|
217
|
+
path: PathBuf,
|
|
218
|
+
/// Underlying IO error.
|
|
219
|
+
source: std::io::Error,
|
|
220
|
+
},
|
|
221
|
+
/// A discovered `pubspec.yaml` could not be read.
|
|
222
|
+
#[error("failed to read pubspec {path}: {source}")]
|
|
223
|
+
ReadPubspec {
|
|
224
|
+
/// Pubspec path.
|
|
225
|
+
path: PathBuf,
|
|
226
|
+
/// Underlying IO error.
|
|
227
|
+
source: std::io::Error,
|
|
228
|
+
},
|
|
229
|
+
/// A discovered `pubspec.yaml` could not be parsed.
|
|
230
|
+
#[error("failed to parse pubspec {path}: {source}")]
|
|
231
|
+
ParsePubspec {
|
|
232
|
+
/// Pubspec path.
|
|
233
|
+
path: PathBuf,
|
|
234
|
+
/// Underlying YAML parse error.
|
|
235
|
+
source: serde_yaml_ng::Error,
|
|
236
|
+
},
|
|
237
|
+
/// A discovered `.dart_tool/package_config.json` could not be read.
|
|
238
|
+
#[error("failed to read package config {path}: {source}")]
|
|
239
|
+
ReadPackageConfig {
|
|
240
|
+
/// Package config path.
|
|
241
|
+
path: PathBuf,
|
|
242
|
+
/// Underlying IO error.
|
|
243
|
+
source: std::io::Error,
|
|
244
|
+
},
|
|
245
|
+
/// A discovered `.dart_tool/package_config.json` could not be parsed.
|
|
246
|
+
#[error("failed to parse package config {path}: {source}")]
|
|
247
|
+
ParsePackageConfig {
|
|
248
|
+
/// Package config path.
|
|
249
|
+
path: PathBuf,
|
|
250
|
+
/// Underlying JSON parse error.
|
|
251
|
+
source: serde_json::Error,
|
|
252
|
+
},
|
|
253
|
+
/// A pub workspace glob pattern was invalid.
|
|
254
|
+
#[error("invalid pub workspace glob pattern {pattern}: {source}")]
|
|
255
|
+
WorkspacePattern {
|
|
256
|
+
/// Invalid pattern.
|
|
257
|
+
pattern: String,
|
|
258
|
+
/// Underlying glob pattern error.
|
|
259
|
+
source: glob::PatternError,
|
|
260
|
+
},
|
|
261
|
+
/// A pub workspace glob expansion failed.
|
|
262
|
+
#[error("failed to expand pub workspace glob {pattern}: {source}")]
|
|
263
|
+
WorkspaceGlob {
|
|
264
|
+
/// Pattern being expanded.
|
|
265
|
+
pattern: String,
|
|
266
|
+
/// Underlying glob error.
|
|
267
|
+
source: glob::GlobError,
|
|
268
|
+
},
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/// Build a directed module graph from extracted Dart file facts.
|
|
272
|
+
///
|
|
273
|
+
/// Local relative imports and exports are resolved from the source file's
|
|
274
|
+
/// directory. `package:` imports resolve through local package names discovered
|
|
275
|
+
/// from `pubspec.yaml` files, including pub workspaces and path dependencies.
|
|
276
|
+
///
|
|
277
|
+
/// # Errors
|
|
278
|
+
///
|
|
279
|
+
/// Returns [`GraphError`] if a discovered `pubspec.yaml` cannot be read or
|
|
280
|
+
/// parsed, or if a workspace glob entry is invalid.
|
|
281
|
+
pub fn build_module_graph(
|
|
282
|
+
root: impl AsRef<Path>,
|
|
283
|
+
files: &[DartFile],
|
|
284
|
+
) -> Result<ModuleGraph, GraphError> {
|
|
285
|
+
build_module_graph_with_options(root, files, &GraphOptions::default())
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/// Build a directed module graph using explicit graph construction options.
|
|
289
|
+
///
|
|
290
|
+
/// # Errors
|
|
291
|
+
///
|
|
292
|
+
/// Returns [`GraphError`] under the same conditions as [`build_module_graph`].
|
|
293
|
+
pub fn build_module_graph_with_options(
|
|
294
|
+
root: impl AsRef<Path>,
|
|
295
|
+
files: &[DartFile],
|
|
296
|
+
options: &GraphOptions,
|
|
297
|
+
) -> Result<ModuleGraph, GraphError> {
|
|
298
|
+
let root = normalize_path(root.as_ref());
|
|
299
|
+
let packages = PackageMap::discover(&root)?;
|
|
300
|
+
let mut graph = DependencyGraph::new();
|
|
301
|
+
let mut nodes_by_path = BTreeMap::new();
|
|
302
|
+
|
|
303
|
+
for file in files {
|
|
304
|
+
let path = normalize_against(&root, &file.path);
|
|
305
|
+
let node = graph.add_node(ModuleNode { path: path.clone() });
|
|
306
|
+
nodes_by_path.insert(path, node);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
let known_paths = nodes_by_path.keys().cloned().collect::<BTreeSet<_>>();
|
|
310
|
+
let files_by_path = files
|
|
311
|
+
.iter()
|
|
312
|
+
.map(|file| (normalize_against(&root, &file.path), file))
|
|
313
|
+
.collect::<BTreeMap<_, _>>();
|
|
314
|
+
let mut unresolved = Vec::new();
|
|
315
|
+
let mut invalid_part_relationships = Vec::new();
|
|
316
|
+
let mut referenced_part_paths = BTreeSet::new();
|
|
317
|
+
let mut part_owners = BTreeMap::new();
|
|
318
|
+
|
|
319
|
+
for file in files {
|
|
320
|
+
add_file_dependencies(
|
|
321
|
+
&root,
|
|
322
|
+
&packages,
|
|
323
|
+
&known_paths,
|
|
324
|
+
&nodes_by_path,
|
|
325
|
+
&files_by_path,
|
|
326
|
+
&mut graph,
|
|
327
|
+
&mut unresolved,
|
|
328
|
+
&mut invalid_part_relationships,
|
|
329
|
+
&mut referenced_part_paths,
|
|
330
|
+
&mut part_owners,
|
|
331
|
+
options,
|
|
332
|
+
file,
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
add_orphan_part_relationships(
|
|
337
|
+
&root,
|
|
338
|
+
&packages,
|
|
339
|
+
&files_by_path,
|
|
340
|
+
&referenced_part_paths,
|
|
341
|
+
&mut invalid_part_relationships,
|
|
342
|
+
);
|
|
343
|
+
invalid_part_relationships.sort_by(|left, right| {
|
|
344
|
+
(
|
|
345
|
+
&left.part_path,
|
|
346
|
+
&left.library_path,
|
|
347
|
+
left.location.line,
|
|
348
|
+
&left.specifier,
|
|
349
|
+
)
|
|
350
|
+
.cmp(&(
|
|
351
|
+
&right.part_path,
|
|
352
|
+
&right.library_path,
|
|
353
|
+
right.location.line,
|
|
354
|
+
&right.specifier,
|
|
355
|
+
))
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
Ok(ModuleGraph {
|
|
359
|
+
root,
|
|
360
|
+
graph,
|
|
361
|
+
nodes_by_path,
|
|
362
|
+
unresolved,
|
|
363
|
+
invalid_part_relationships,
|
|
364
|
+
packages,
|
|
365
|
+
})
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
#[expect(
|
|
369
|
+
clippy::too_many_arguments,
|
|
370
|
+
reason = "keeps per-file edge construction in one owner without hiding graph state"
|
|
371
|
+
)]
|
|
372
|
+
fn add_file_dependencies(
|
|
373
|
+
root: &Path,
|
|
374
|
+
packages: &PackageMap,
|
|
375
|
+
known_paths: &BTreeSet<PathBuf>,
|
|
376
|
+
nodes_by_path: &BTreeMap<PathBuf, NodeIndex>,
|
|
377
|
+
files_by_path: &BTreeMap<PathBuf, &DartFile>,
|
|
378
|
+
graph: &mut DependencyGraph,
|
|
379
|
+
unresolved: &mut Vec<UnresolvedDependency>,
|
|
380
|
+
invalid_part_relationships: &mut Vec<InvalidPartRelationship>,
|
|
381
|
+
referenced_part_paths: &mut BTreeSet<PathBuf>,
|
|
382
|
+
part_owners: &mut BTreeMap<PathBuf, PathBuf>,
|
|
383
|
+
options: &GraphOptions,
|
|
384
|
+
file: &DartFile,
|
|
385
|
+
) {
|
|
386
|
+
let from_path = normalize_against(root, &file.path);
|
|
387
|
+
let Some(from_index) = nodes_by_path.get(&from_path).copied() else {
|
|
388
|
+
return;
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
if let Some(library) = &file.library
|
|
392
|
+
&& let Some(augment_uri) = &library.augment_uri
|
|
393
|
+
{
|
|
394
|
+
add_dependency(
|
|
395
|
+
root,
|
|
396
|
+
packages,
|
|
397
|
+
known_paths,
|
|
398
|
+
nodes_by_path,
|
|
399
|
+
graph,
|
|
400
|
+
unresolved,
|
|
401
|
+
from_index,
|
|
402
|
+
&from_path,
|
|
403
|
+
augment_uri,
|
|
404
|
+
DependencyKind::Augment,
|
|
405
|
+
library.location,
|
|
406
|
+
DependencyVisibility::default(),
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
let imports = selected_imports(&file.imports, &options.conditional_environment);
|
|
411
|
+
for import in imports {
|
|
412
|
+
add_dependency(
|
|
413
|
+
root,
|
|
414
|
+
packages,
|
|
415
|
+
known_paths,
|
|
416
|
+
nodes_by_path,
|
|
417
|
+
graph,
|
|
418
|
+
unresolved,
|
|
419
|
+
from_index,
|
|
420
|
+
&from_path,
|
|
421
|
+
&import.uri,
|
|
422
|
+
DependencyKind::Import,
|
|
423
|
+
import.location,
|
|
424
|
+
DependencyVisibility {
|
|
425
|
+
prefix: import.prefix.clone(),
|
|
426
|
+
deferred: import.deferred,
|
|
427
|
+
condition: import.condition.clone(),
|
|
428
|
+
combinators: import.combinators.clone(),
|
|
429
|
+
},
|
|
430
|
+
);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
let exports = selected_exports(&file.exports, &options.conditional_environment);
|
|
434
|
+
for export in exports {
|
|
435
|
+
add_dependency(
|
|
436
|
+
root,
|
|
437
|
+
packages,
|
|
438
|
+
known_paths,
|
|
439
|
+
nodes_by_path,
|
|
440
|
+
graph,
|
|
441
|
+
unresolved,
|
|
442
|
+
from_index,
|
|
443
|
+
&from_path,
|
|
444
|
+
&export.uri,
|
|
445
|
+
DependencyKind::Export,
|
|
446
|
+
export.location,
|
|
447
|
+
DependencyVisibility {
|
|
448
|
+
condition: export.condition.clone(),
|
|
449
|
+
combinators: export.combinators.clone(),
|
|
450
|
+
..DependencyVisibility::default()
|
|
451
|
+
},
|
|
452
|
+
);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
for part in &file.parts {
|
|
456
|
+
add_part_dependency(
|
|
457
|
+
root,
|
|
458
|
+
packages,
|
|
459
|
+
known_paths,
|
|
460
|
+
nodes_by_path,
|
|
461
|
+
files_by_path,
|
|
462
|
+
graph,
|
|
463
|
+
unresolved,
|
|
464
|
+
invalid_part_relationships,
|
|
465
|
+
referenced_part_paths,
|
|
466
|
+
part_owners,
|
|
467
|
+
from_index,
|
|
468
|
+
&from_path,
|
|
469
|
+
file,
|
|
470
|
+
&part.uri,
|
|
471
|
+
part.location,
|
|
472
|
+
);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
#[expect(
|
|
477
|
+
clippy::too_many_arguments,
|
|
478
|
+
reason = "keeps edge construction single-owner without creating a shallow context wrapper"
|
|
479
|
+
)]
|
|
480
|
+
fn add_dependency(
|
|
481
|
+
root: &Path,
|
|
482
|
+
packages: &PackageMap,
|
|
483
|
+
known_paths: &BTreeSet<PathBuf>,
|
|
484
|
+
nodes_by_path: &BTreeMap<PathBuf, NodeIndex>,
|
|
485
|
+
graph: &mut DependencyGraph,
|
|
486
|
+
unresolved: &mut Vec<UnresolvedDependency>,
|
|
487
|
+
from_index: NodeIndex,
|
|
488
|
+
from_path: &Path,
|
|
489
|
+
specifier: &str,
|
|
490
|
+
kind: DependencyKind,
|
|
491
|
+
location: Location,
|
|
492
|
+
visibility: DependencyVisibility,
|
|
493
|
+
) {
|
|
494
|
+
let Some(target) = resolve_local_uri(root, packages, from_path, specifier) else {
|
|
495
|
+
return;
|
|
496
|
+
};
|
|
497
|
+
let target_path = target.path;
|
|
498
|
+
|
|
499
|
+
if !known_paths.contains(&target_path) {
|
|
500
|
+
if !target.local {
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
unresolved.push(UnresolvedDependency {
|
|
505
|
+
from_path: from_path.to_path_buf(),
|
|
506
|
+
specifier: specifier.to_owned(),
|
|
507
|
+
kind,
|
|
508
|
+
location,
|
|
509
|
+
attempted_path: target_path,
|
|
510
|
+
visibility,
|
|
511
|
+
});
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
let Some(target_index) = nodes_by_path.get(&target_path).copied() else {
|
|
516
|
+
return;
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
graph.add_edge(
|
|
520
|
+
from_index,
|
|
521
|
+
target_index,
|
|
522
|
+
DependencyEdge {
|
|
523
|
+
specifier: specifier.to_owned(),
|
|
524
|
+
kind,
|
|
525
|
+
location,
|
|
526
|
+
visibility,
|
|
527
|
+
},
|
|
528
|
+
);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
pub(super) struct ResolvedTarget {
|
|
532
|
+
pub(super) path: PathBuf,
|
|
533
|
+
pub(super) local: bool,
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
pub(super) fn resolve_local_uri(
|
|
537
|
+
root: &Path,
|
|
538
|
+
packages: &PackageMap,
|
|
539
|
+
from_path: &Path,
|
|
540
|
+
specifier: &str,
|
|
541
|
+
) -> Option<ResolvedTarget> {
|
|
542
|
+
if specifier.starts_with("dart:") {
|
|
543
|
+
return None;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
if let Some(rest) = specifier.strip_prefix("package:") {
|
|
547
|
+
let (package, path) = rest.split_once('/')?;
|
|
548
|
+
let path = percent_decode_uri_path(path);
|
|
549
|
+
return packages
|
|
550
|
+
.resolve(package, &path)
|
|
551
|
+
.map(|resolution| ResolvedTarget {
|
|
552
|
+
path: resolution.path,
|
|
553
|
+
local: resolution.local,
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
if specifier.contains(':') {
|
|
558
|
+
return None;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
let specifier = percent_decode_uri_path(specifier);
|
|
562
|
+
let base = from_path.parent().unwrap_or(root);
|
|
563
|
+
Some(ResolvedTarget {
|
|
564
|
+
path: normalize_path(&base.join(&specifier)),
|
|
565
|
+
local: true,
|
|
566
|
+
})
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
fn percent_decode_uri_path(value: &str) -> String {
|
|
570
|
+
let bytes = value.as_bytes();
|
|
571
|
+
let mut decoded = Vec::with_capacity(bytes.len());
|
|
572
|
+
let mut index = 0;
|
|
573
|
+
|
|
574
|
+
while index < bytes.len() {
|
|
575
|
+
if bytes[index] == b'%'
|
|
576
|
+
&& let (Some(high), Some(low)) = (bytes.get(index + 1), bytes.get(index + 2))
|
|
577
|
+
&& let Some(decoded_byte) = decode_hex_pair(*high, *low)
|
|
578
|
+
{
|
|
579
|
+
decoded.push(decoded_byte);
|
|
580
|
+
index += 3;
|
|
581
|
+
continue;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
decoded.push(bytes[index]);
|
|
585
|
+
index += 1;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
String::from_utf8(decoded).unwrap_or_else(|_| value.to_owned())
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
fn decode_hex_pair(high: u8, low: u8) -> Option<u8> {
|
|
592
|
+
Some(hex_value(high)? * 16 + hex_value(low)?)
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
const fn hex_value(value: u8) -> Option<u8> {
|
|
596
|
+
match value {
|
|
597
|
+
b'0'..=b'9' => Some(value - b'0'),
|
|
598
|
+
b'a'..=b'f' => Some(value - b'a' + 10),
|
|
599
|
+
b'A'..=b'F' => Some(value - b'A' + 10),
|
|
600
|
+
_ => None,
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
pub(crate) fn normalize_against(root: &Path, path: &Path) -> PathBuf {
|
|
605
|
+
if path.is_absolute() || path.starts_with(root) {
|
|
606
|
+
normalize_path(path)
|
|
607
|
+
} else {
|
|
608
|
+
normalize_path(&root.join(path))
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
pub(crate) fn normalize_path(path: &Path) -> PathBuf {
|
|
613
|
+
let mut normalized = PathBuf::new();
|
|
614
|
+
|
|
615
|
+
for component in path.components() {
|
|
616
|
+
match component {
|
|
617
|
+
Component::CurDir => {}
|
|
618
|
+
Component::ParentDir => {
|
|
619
|
+
if !normalized.pop() {
|
|
620
|
+
normalized.push(component.as_os_str());
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
Component::Prefix(prefix) => normalized.push(prefix.as_os_str()),
|
|
624
|
+
Component::RootDir | Component::Normal(_) => normalized.push(component.as_os_str()),
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
if normalized.as_os_str().is_empty() {
|
|
629
|
+
return PathBuf::from(".");
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
normalized
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
#[cfg(test)]
|
|
636
|
+
mod augment_tests;
|
|
637
|
+
#[cfg(test)]
|
|
638
|
+
mod package_config_tests;
|
|
639
|
+
#[cfg(test)]
|
|
640
|
+
mod pubspec_tests;
|
|
641
|
+
#[cfg(test)]
|
|
642
|
+
mod tests;
|
|
643
|
+
#[cfg(test)]
|
|
644
|
+
mod uri_tests;
|