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
/*
* Mitts Build — Rust-first build and package tooling with shared artifact cache
* Copyright (c) 2026 Alexander Panasenko
*
* Contact: alex@prod.codes
* Author: https://prod.codes/about/
* Project: https://github.com/alex09x/mitts
* SPDX-License-Identifier: MIT OR Apache-2.0
*/
use std::path::{Path, PathBuf};
/// Searches standard ~/.cargo/registry/src, ~/.cache/mitts/sources, and ~/.cargo/git/checkouts
/// for unpacked crate source directory matching name, version, and optional lockfile source.
pub fn find_crate_source_dir(name: &str, version: &str, source: Option<&str>) -> Option<PathBuf> {
let is_git = source.map(|s| s.starts_with("git")).unwrap_or(false);
let expected_hash = source.and_then(|s| s.split('#').nth(1));
if let Ok(home) = std::env::var("HOME") {
let home_path = Path::new(&home);
let find_in_git = || -> Option<PathBuf> {
let git_src = home_path.join(".cargo").join("git").join("checkouts");
if !git_src.is_dir() {
return None;
}
if let Ok(repos) = std::fs::read_dir(&git_src) {
for repo in repos.flatten() {
let repo_path = repo.path();
if let Ok(commits) = std::fs::read_dir(&repo_path) {
for commit in commits.flatten() {
let commit_dir_name = commit.file_name();
let commit_str = commit_dir_name.to_string_lossy();
if let Some(exp) = expected_hash {
if !exp.starts_with(commit_str.as_ref())
&& !commit_str.starts_with(exp)
{
continue;
}
}
let commit_path = commit.path();
if commit_path.is_dir() {
let c_toml = commit_path.join("Cargo.toml");
if c_toml.is_file() {
if let Ok(content) = std::fs::read_to_string(&c_toml) {
if content.contains(&format!("name = \"{}\"", name)) {
return Some(commit_path);
}
}
}
// Check subdirectories (e.g. crates/* or nested packages)
if let Ok(subs) = std::fs::read_dir(&commit_path) {
for sub in subs.flatten() {
let sub_path = sub.path();
if sub_path.is_dir() {
let s_name = sub.file_name();
let s_str = s_name.to_string_lossy();
if s_str.starts_with('.') || s_str == "target" {
continue;
}
let sub_toml = sub_path.join("Cargo.toml");
if sub_toml.is_file() {
if let Ok(content) =
std::fs::read_to_string(&sub_toml)
{
if content
.contains(&format!("name = \"{}\"", name))
{
return Some(sub_path);
}
}
}
if s_str == "crates" {
if let Ok(nested) = std::fs::read_dir(&sub_path) {
for n in nested.flatten() {
let n_path = n.path();
if n_path.is_dir() {
let n_toml = n_path.join("Cargo.toml");
if n_toml.is_file() {
if let Ok(content) =
std::fs::read_to_string(&n_toml)
{
if content.contains(&format!(
"name = \"{}\"",
name
)) {
return Some(n_path);
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
None
};
if is_git {
if let Some(path) = find_in_git() {
return Some(path);
}
}
let cargo_src = home_path.join(".cargo").join("registry").join("src");
if cargo_src.is_dir() {
let target_name = format!("{}-{}", name, version);
if let Ok(entries) = std::fs::read_dir(&cargo_src) {
for entry in entries.flatten() {
let candidate = entry.path().join(&target_name);
if candidate.is_dir() && candidate.join("Cargo.toml").is_file() {
return Some(candidate);
}
}
}
}
let mitts_src = home_path
.join(".cache")
.join("mitts")
.join("sources")
.join(format!("{}-{}", name, version));
if mitts_src.is_dir() && mitts_src.join("Cargo.toml").is_file() {
return Some(mitts_src);
}
if !is_git {
if let Some(path) = find_in_git() {
return Some(path);
}
}
}
None
}