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
//! File discovery types: discovered files, file IDs, and entry points.
use std::path::{Path, PathBuf};
/// A discovered source file on disk.
///
/// # Examples
///
/// ```
/// use fallow_types::discover::{DiscoveredFile, FileId};
/// use std::path::PathBuf;
///
/// let file = DiscoveredFile {
/// id: FileId(0),
/// path: PathBuf::from("/project/src/index.ts"),
/// size_bytes: 2048,
/// };
/// assert_eq!(file.id, FileId(0));
/// assert_eq!(file.size_bytes, 2048);
/// ```
#[derive(Debug, Clone)]
pub struct DiscoveredFile {
/// Unique file index.
pub id: FileId,
/// Absolute path.
pub path: PathBuf,
/// File size in bytes (for sorting largest-first).
pub size_bytes: u64,
}
/// Compact file identifier.
///
/// A newtype wrapper around `u32` used as a stable index into file arrays.
/// `FileId`s are path-sorted (not insertion order) for stable cross-run identity.
///
/// # Examples
///
/// ```
/// use fallow_types::discover::FileId;
///
/// let id = FileId(42);
/// assert_eq!(id.0, 42);
///
/// // Implements Copy
/// let copy = id;
/// assert_eq!(id, copy);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct FileId(pub u32);
const _: () = assert!(std::mem::size_of::<FileId>() == 4);
#[cfg(all(target_pointer_width = "64", unix))]
const _: () = assert!(std::mem::size_of::<DiscoveredFile>() == 40);
/// Persistable file identity for cache entries that need to survive `FileId`
/// churn across runs.
///
/// `FileId` remains a dense in-memory index. This key is path-derived, root
/// relative where possible, and uses `/` separators so graph-cache metadata can
/// compare file identity without relying on platform path display quirks.
#[derive(
Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
)]
pub struct StableFileKey(String);
impl StableFileKey {
/// Build a stable key from an absolute path and the analysis root.
#[must_use]
pub fn from_root_relative(root: &Path, path: &Path) -> Self {
let relative = path.strip_prefix(root).unwrap_or(path);
Self(normalize_path(relative))
}
/// Build a stable key from an already-root-relative path.
#[must_use]
pub fn from_relative(path: &Path) -> Self {
Self(normalize_path(path))
}
/// Stable string used in persisted cache manifests.
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
fn normalize_path(path: &Path) -> String {
path.to_string_lossy().replace('\\', "/")
}
/// An entry point into the module graph.
#[derive(Debug, Clone)]
pub struct EntryPoint {
/// Absolute path to the entry point file.
pub path: PathBuf,
/// How this entry point was discovered.
pub source: EntryPointSource,
}
impl std::fmt::Display for EntryPointSource {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::PackageJsonMain => f.write_str("package.json main"),
Self::PackageJsonModule => f.write_str("package.json module"),
Self::PackageJsonExports => f.write_str("package.json exports"),
Self::PackageJsonBin => f.write_str("package.json bin"),
Self::PackageJsonTypes => f.write_str("package.json types"),
Self::PackageJsonTypings => f.write_str("package.json typings"),
Self::PackageJsonSource => f.write_str("package.json source"),
Self::PackageJsonBrowser => f.write_str("package.json browser"),
Self::PackageJsonScript { .. } => f.write_str("package.json script"),
Self::Plugin { name } => write!(f, "{name}"),
Self::TestFile => f.write_str("test file"),
Self::DefaultIndex => f.write_str("default index"),
Self::ManualEntry => f.write_str("manual entry"),
Self::InfrastructureConfig => f.write_str("infrastructure config"),
Self::DynamicallyLoaded => f.write_str("dynamically loaded"),
}
}
}
impl EntryPointSource {
/// A script entry point that the package.json script `script` runs.
#[must_use]
pub fn package_json_script(script: impl Into<String>) -> Self {
Self::PackageJsonScript {
script: script.into(),
}
}
/// The package.json script key of a script entry point. `None` for every
/// other source.
#[must_use]
pub fn script(&self) -> Option<&str> {
if let Self::PackageJsonScript { script } = self {
Some(script)
} else {
None
}
}
}
/// Where an entry point was discovered from.
#[derive(Debug, Clone)]
pub enum EntryPointSource {
/// The `main` field in package.json.
PackageJsonMain,
/// The `module` field in package.json.
PackageJsonModule,
/// The `exports` field in package.json.
PackageJsonExports,
/// The `bin` field in package.json.
PackageJsonBin,
/// The `types` field in package.json.
PackageJsonTypes,
/// The `typings` field in package.json.
PackageJsonTypings,
/// The `source` field in package.json.
PackageJsonSource,
/// The `browser` field in package.json.
PackageJsonBrowser,
/// A script command in package.json.
PackageJsonScript {
/// The key of the script in `scripts`, e.g. `build`.
script: String,
},
/// Detected by a framework plugin.
Plugin {
/// Name of the plugin that detected this entry point.
name: String,
},
/// A test file (e.g., `*.test.ts`, `*.spec.ts`).
TestFile,
/// A default index file (e.g., `src/index.ts`).
DefaultIndex,
/// Manually configured in fallow config.
ManualEntry,
/// Discovered from infrastructure config files (Dockerfile, Procfile, fly.toml).
InfrastructureConfig,
/// Declared in `dynamicallyLoaded` config as a runtime-loaded file.
DynamicallyLoaded,
}
#[cfg(test)]
mod stable_file_key_tests {
use super::*;
#[test]
fn stable_file_key_strips_root_prefix() {
let key = StableFileKey::from_root_relative(
Path::new("/project"),
Path::new("/project/src/index.ts"),
);
assert_eq!(key.as_str(), "src/index.ts");
}
#[test]
fn stable_file_key_keeps_path_when_outside_root() {
let key =
StableFileKey::from_root_relative(Path::new("/project"), Path::new("/other/file.ts"));
assert_eq!(key.as_str(), "/other/file.ts");
}
#[test]
fn stable_file_key_normalizes_windows_separators() {
let key = StableFileKey::from_relative(Path::new(r"src\feature\file.ts"));
assert_eq!(key.as_str(), "src/feature/file.ts");
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn entry_point_source_display_all_variants() {
assert_eq!(
EntryPointSource::PackageJsonMain.to_string(),
"package.json main"
);
assert_eq!(
EntryPointSource::PackageJsonModule.to_string(),
"package.json module"
);
assert_eq!(
EntryPointSource::PackageJsonExports.to_string(),
"package.json exports"
);
assert_eq!(
EntryPointSource::PackageJsonBin.to_string(),
"package.json bin"
);
assert_eq!(
EntryPointSource::PackageJsonTypes.to_string(),
"package.json types"
);
assert_eq!(
EntryPointSource::PackageJsonTypings.to_string(),
"package.json typings"
);
assert_eq!(
EntryPointSource::PackageJsonSource.to_string(),
"package.json source"
);
assert_eq!(
EntryPointSource::PackageJsonBrowser.to_string(),
"package.json browser"
);
assert_eq!(
EntryPointSource::PackageJsonScript {
script: "build".to_string()
}
.to_string(),
"package.json script"
);
assert_eq!(
EntryPointSource::Plugin {
name: "vitest".to_string()
}
.to_string(),
"vitest"
);
assert_eq!(EntryPointSource::TestFile.to_string(), "test file");
assert_eq!(EntryPointSource::DefaultIndex.to_string(), "default index");
assert_eq!(EntryPointSource::ManualEntry.to_string(), "manual entry");
assert_eq!(
EntryPointSource::InfrastructureConfig.to_string(),
"infrastructure config"
);
assert_eq!(
EntryPointSource::DynamicallyLoaded.to_string(),
"dynamically loaded"
);
}
}