bevy_brp_mcp 0.22.2

MCP server for Bevy Remote Protocol (BRP) integration
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
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::time::SystemTime;

use super::constants::BUILD_SCRIPT_FILE;
use super::constants::CARGO_CONFIG_DIR;
use super::constants::CARGO_CONFIG_FILE;
use super::constants::CARGO_CONFIG_TOML_FILE;
use super::constants::CARGO_LOCK_FILE;
use super::constants::DEP_INFO_EXTENSION;
use super::constants::RUST_TOOLCHAIN_FILE;
use super::constants::RUST_TOOLCHAIN_TOML_FILE;
use crate::app_tools::constants::CARGO_MANIFEST_FILE;
use crate::app_tools::targets::BevyTarget;
use crate::error::Error;
use crate::error::Result;

#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) enum FreshnessCheckResult {
    Fresh,
    Stale(String),
    Unknown(String),
}

pub(super) fn check_target_freshness(target: &BevyTarget, profile: &str) -> FreshnessCheckResult {
    if !target.is_app() {
        return FreshnessCheckResult::Unknown(
            "lock-free freshness checks are only supported for app binaries".to_string(),
        );
    }

    try_check_target_freshness(target, profile)
        .unwrap_or_else(|error| FreshnessCheckResult::Unknown(format!("{error}")))
}

fn try_check_target_freshness(target: &BevyTarget, profile: &str) -> Result<FreshnessCheckResult> {
    let binary_path = target.get_binary_path(profile);
    if !binary_path.exists() {
        return Ok(FreshnessCheckResult::Stale(format!(
            "binary does not exist: {}",
            binary_path.display()
        )));
    }

    let binary_mtime = file_modified_time(&binary_path)?;
    let dep_info_path = dep_info_path(target, profile);
    if !dep_info_path.exists() {
        return Ok(FreshnessCheckResult::Unknown(format!(
            "dep-info file does not exist: {}",
            dep_info_path.display()
        )));
    }

    let dep_info_contents = fs::read_to_string(&dep_info_path).map_err(|error| {
        Error::FileOperation(format!(
            "Failed to read dep-info file {}: {error}",
            dep_info_path.display()
        ))
    })?;
    let dep_info_dir = dep_info_path
        .parent()
        .ok_or_else(|| Error::FileOrPathNotFound("Dep-info file has no parent directory".into()))?;
    let dependencies = parse_dep_info_dependencies(&dep_info_contents, dep_info_dir);

    if dependencies.is_empty() {
        return Ok(FreshnessCheckResult::Unknown(format!(
            "dep-info file had no dependencies: {}",
            dep_info_path.display()
        )));
    }

    for dependency in dependencies {
        let Some(staleness_reason) = compare_input_to_binary(&dependency, binary_mtime)? else {
            continue;
        };
        return Ok(FreshnessCheckResult::Stale(staleness_reason));
    }

    for input in extra_fingerprint_inputs(target) {
        let Some(staleness_reason) = compare_optional_input_to_binary(&input, binary_mtime)? else {
            continue;
        };
        return Ok(FreshnessCheckResult::Stale(staleness_reason));
    }

    Ok(FreshnessCheckResult::Fresh)
}

fn dep_info_path(target: &BevyTarget, profile: &str) -> PathBuf {
    target
        .get_binary_path(profile)
        .with_extension(DEP_INFO_EXTENSION)
}

fn extra_fingerprint_inputs(target: &BevyTarget) -> Vec<PathBuf> {
    let mut inputs = vec![target.manifest.clone()];

    let workspace_manifest = target.workspace_root.join(CARGO_MANIFEST_FILE);
    if workspace_manifest != target.manifest {
        inputs.push(workspace_manifest);
    }

    inputs.push(target.workspace_root.join(CARGO_LOCK_FILE));
    inputs.extend(find_cargo_config_files(
        &target.manifest,
        &target.workspace_root,
    ));

    if let Some(package_dir) = target.manifest.parent() {
        inputs.push(package_dir.join(BUILD_SCRIPT_FILE));
    }

    inputs.push(target.workspace_root.join(RUST_TOOLCHAIN_TOML_FILE));
    inputs.push(target.workspace_root.join(RUST_TOOLCHAIN_FILE));

    inputs
}

fn find_cargo_config_files(manifest_path: &Path, workspace_root: &Path) -> Vec<PathBuf> {
    let mut configs = Vec::new();

    let Some(mut current_dir) = manifest_path.parent() else {
        return configs;
    };

    loop {
        configs.push(
            current_dir
                .join(CARGO_CONFIG_DIR)
                .join(CARGO_CONFIG_TOML_FILE),
        );
        configs.push(current_dir.join(CARGO_CONFIG_DIR).join(CARGO_CONFIG_FILE));

        if current_dir == workspace_root {
            break;
        }

        let Some(parent) = current_dir.parent() else {
            break;
        };
        current_dir = parent;
    }

    configs
}

fn compare_input_to_binary(input_path: &Path, binary_mtime: SystemTime) -> Result<Option<String>> {
    compare_path_to_binary(
        input_path,
        binary_mtime,
        MissingInputPolicy::TreatAsStale,
        "dependency listed in dep-info is missing",
        "dependency is newer than binary",
    )
}

fn compare_optional_input_to_binary(
    input_path: &Path,
    binary_mtime: SystemTime,
) -> Result<Option<String>> {
    compare_path_to_binary(
        input_path,
        binary_mtime,
        MissingInputPolicy::Ignore,
        "",
        "build input is newer than binary",
    )
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum MissingInputPolicy {
    Ignore,
    TreatAsStale,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum BackslashState {
    ReadingToken,
    Escaped,
}

impl BackslashState {
    const fn is_escaped(self) -> bool { matches!(self, Self::Escaped) }
}

fn compare_path_to_binary(
    input_path: &Path,
    binary_mtime: SystemTime,
    missing_input_policy: MissingInputPolicy,
    missing_reason: &str,
    stale_reason: &str,
) -> Result<Option<String>> {
    if !input_path.exists() {
        return Ok((missing_input_policy == MissingInputPolicy::TreatAsStale)
            .then(|| format!("{missing_reason}: {}", input_path.display())));
    }

    let input_mtime = file_modified_time(input_path)?;
    Ok((input_mtime > binary_mtime).then(|| format!("{stale_reason}: {}", input_path.display())))
}

fn file_modified_time(path: &Path) -> Result<SystemTime> {
    fs::metadata(path)
        .map_err(|error| {
            Error::FileOperation(format!(
                "Failed to read metadata for {}: {error}",
                path.display()
            ))
        })?
        .modified()
        .map_err(|error| {
            Error::FileOperation(format!(
                "Failed to read modification time for {}: {error}",
                path.display()
            ))
            .into()
        })
}

fn parse_dep_info_dependencies(contents: &str, base_dir: &Path) -> Vec<PathBuf> {
    let Some((_, dependency_text)) = contents.split_once(':') else {
        return Vec::new();
    };

    let mut dependencies = Vec::new();
    let mut current = String::new();
    let mut backslash_state = BackslashState::ReadingToken;

    for ch in dependency_text.chars() {
        if backslash_state.is_escaped() {
            match ch {
                '\n' | '\r' => {},
                _ => current.push(ch),
            }
            backslash_state = BackslashState::ReadingToken;
            continue;
        }

        match ch {
            '\\' => backslash_state = BackslashState::Escaped,
            c if c.is_whitespace() => {
                push_dependency(&mut dependencies, &mut current, base_dir);
            },
            _ => current.push(ch),
        }
    }

    push_dependency(&mut dependencies, &mut current, base_dir);
    dependencies
}

fn push_dependency(dependencies: &mut Vec<PathBuf>, current: &mut String, base_dir: &Path) {
    if current.is_empty() {
        return;
    }

    let raw_path = std::mem::take(current);
    let path = PathBuf::from(&raw_path);
    if path.is_absolute() {
        dependencies.push(path);
    } else {
        dependencies.push(base_dir.join(path));
    }
}

#[cfg(test)]
#[allow(
    clippy::expect_used,
    reason = "tests should panic on unexpected values"
)]
mod tests {
    use std::fs;
    use std::path::Path;
    use std::path::PathBuf;
    use std::thread;
    use std::time::Duration;

    use tempfile::tempdir;

    use super::FreshnessCheckResult;
    use super::check_target_freshness;
    use super::parse_dep_info_dependencies;
    use crate::app_tools::targets::BevyTarget;
    use crate::app_tools::targets::TargetType;

    const FILE_TIMESTAMP_ADVANCE_MS: u64 = 20;

    fn test_target(workspace_root: &Path, manifest_path: &Path, name: &str) -> BevyTarget {
        BevyTarget {
            name:           name.to_string(),
            target_type:    TargetType::App,
            package_name:   "pkg".to_string(),
            workspace_root: workspace_root.to_path_buf(),
            manifest:       manifest_path.to_path_buf(),
            relative:       PathBuf::new(),
            source:         PathBuf::new(),
        }
    }

    #[test]
    fn parses_dep_info_with_escaped_spaces_and_line_continuations() {
        let base_dir = Path::new("/tmp");
        let dependencies = parse_dep_info_dependencies(
            "target/debug/demo: /tmp/one.rs /tmp/two\\ with\\ spaces.rs \\\n             /tmp/three.rs",
            base_dir,
        );

        assert_eq!(
            dependencies,
            vec![
                PathBuf::from("/tmp/one.rs"),
                PathBuf::from("/tmp/two with spaces.rs"),
                PathBuf::from("/tmp/three.rs"),
            ]
        );
    }

    #[test]
    fn returns_fresh_when_binary_is_newer_than_inputs() {
        let temp_dir = tempdir().expect("temp dir");
        let workspace_root = temp_dir.path();
        let manifest_path = workspace_root.join("Cargo.toml");
        let src_path = workspace_root.join("src/main.rs");
        let binary_path = workspace_root.join("target/debug/demo");
        let dep_info_path = workspace_root.join("target/debug/demo.d");

        fs::create_dir_all(src_path.parent().expect("src parent")).expect("create src dir");
        fs::create_dir_all(binary_path.parent().expect("binary parent"))
            .expect("create target dir");
        fs::write(
            &manifest_path,
            "[package]\nname = \"demo\"\nversion = \"0.1.0\"\n",
        )
        .expect("write manifest");
        fs::write(workspace_root.join("Cargo.lock"), "# lock\n").expect("write lock");
        fs::write(&src_path, "fn main() {}\n").expect("write source");

        thread::sleep(Duration::from_millis(FILE_TIMESTAMP_ADVANCE_MS));
        fs::write(&binary_path, "binary").expect("write binary");
        fs::write(
            &dep_info_path,
            format!("{}: {}\n", binary_path.display(), src_path.display()),
        )
        .expect("write dep info");

        let target = test_target(workspace_root, &manifest_path, "demo");
        assert_eq!(
            check_target_freshness(&target, "debug"),
            FreshnessCheckResult::Fresh
        );
    }

    #[test]
    fn returns_stale_when_dependency_is_newer_than_binary() {
        let temp_dir = tempdir().expect("temp dir");
        let workspace_root = temp_dir.path();
        let manifest_path = workspace_root.join("Cargo.toml");
        let src_path = workspace_root.join("src/main.rs");
        let binary_path = workspace_root.join("target/debug/demo");
        let dep_info_path = workspace_root.join("target/debug/demo.d");

        fs::create_dir_all(src_path.parent().expect("src parent")).expect("create src dir");
        fs::create_dir_all(binary_path.parent().expect("binary parent"))
            .expect("create target dir");
        fs::write(
            &manifest_path,
            "[package]\nname = \"demo\"\nversion = \"0.1.0\"\n",
        )
        .expect("write manifest");
        fs::write(workspace_root.join("Cargo.lock"), "# lock\n").expect("write lock");
        fs::write(&binary_path, "binary").expect("write binary");

        thread::sleep(Duration::from_millis(FILE_TIMESTAMP_ADVANCE_MS));
        fs::write(&src_path, "fn main() {}\n").expect("write source");
        fs::write(
            &dep_info_path,
            format!("{}: {}\n", binary_path.display(), src_path.display()),
        )
        .expect("write dep info");

        let target = test_target(workspace_root, &manifest_path, "demo");
        assert!(matches!(
            check_target_freshness(&target, "debug"),
            FreshnessCheckResult::Stale(reason)
                if reason.contains("dependency is newer than binary")
        ));
    }

    #[test]
    fn returns_unknown_when_dep_info_is_missing() {
        let temp_dir = tempdir().expect("temp dir");
        let workspace_root = temp_dir.path();
        let manifest_path = workspace_root.join("Cargo.toml");
        let binary_path = workspace_root.join("target/debug/demo");

        fs::create_dir_all(binary_path.parent().expect("binary parent"))
            .expect("create target dir");
        fs::write(
            &manifest_path,
            "[package]\nname = \"demo\"\nversion = \"0.1.0\"\n",
        )
        .expect("write manifest");
        fs::write(&binary_path, "binary").expect("write binary");

        let target = test_target(workspace_root, &manifest_path, "demo");
        assert!(matches!(
            check_target_freshness(&target, "debug"),
            FreshnessCheckResult::Unknown(reason)
                if reason.contains("dep-info file does not exist")
        ));
    }
}