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
/*
* 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 super::types::EnvMacroDiscovery;
use super::unescape::unescape_rust_string;
use std::path::Path;
/// Skips whitespace, line comments (//...), and block comments (/*...*/) including nested block comments.
fn skip_trivia(source: &str, mut pos: usize) -> usize {
let len = source.len();
while pos < len {
if source[pos..].starts_with("//") {
pos += 2;
while pos < len {
if let Some(ch) = source[pos..].chars().next() {
pos += ch.len_utf8();
if ch == '\n' {
break;
}
} else {
break;
}
}
} else if source[pos..].starts_with("/*") {
pos += 2;
let mut depth = 1;
while pos < len && depth > 0 {
if source[pos..].starts_with("/*") {
depth += 1;
pos += 2;
} else if source[pos..].starts_with("*/") {
depth -= 1;
pos += 2;
} else if let Some(ch) = source[pos..].chars().next() {
pos += ch.len_utf8();
} else {
break;
}
}
} else if let Some(ch) = source[pos..].chars().next() {
if ch.is_whitespace() {
pos += ch.len_utf8();
} else {
break;
}
} else {
break;
}
}
pos
}
/// Scans Rust source code for compile-time env!("VAR") and option_env!("VAR") invocations.
/// Advances strictly by UTF-8 character boundaries to prevent panicking on non-ASCII code.
pub fn scan_env_macros(source: &str) -> EnvMacroDiscovery {
let len = source.len();
let mut i = 0;
let mut vars = Vec::new();
while i < len {
// 1. Skip line comments
if source[i..].starts_with("//") {
i += 2;
while i < len {
if let Some(ch) = source[i..].chars().next() {
i += ch.len_utf8();
if ch == '\n' {
break;
}
} else {
break;
}
}
continue;
}
// 2. Skip block comments (handling nested comments)
if source[i..].starts_with("/*") {
i += 2;
let mut depth = 1;
while i < len && depth > 0 {
if source[i..].starts_with("/*") {
depth += 1;
i += 2;
} else if source[i..].starts_with("*/") {
depth -= 1;
i += 2;
} else if let Some(ch) = source[i..].chars().next() {
i += ch.len_utf8();
} else {
break;
}
}
continue;
}
// 3. Skip raw strings: r#"..."# or r"..."
let raw_prefix = if source[i..].starts_with("r#") || source[i..].starts_with("r\"") {
Some(1)
} else if source[i..].starts_with("br#")
|| source[i..].starts_with("br\"")
|| source[i..].starts_with("cr#")
|| source[i..].starts_with("cr\"")
{
Some(2)
} else {
None
};
if let Some(pref_len) = raw_prefix {
let rest = &source[i + pref_len..];
let mut hashes = 0;
while rest[hashes..].starts_with('#') {
hashes += 1;
}
if rest[hashes..].starts_with('"') {
let content_start = i + pref_len + hashes + 1;
let closing = format!("\"{}", "#".repeat(hashes));
if let Some(pos) = source[content_start..].find(&closing) {
i = content_start + pos + closing.len();
continue;
} else {
break;
}
}
}
// 4. Skip normal string literals: "..." (or b"...", c"...")
let str_prefix = if source[i..].starts_with('"') {
Some(1)
} else if source[i..].starts_with("b\"") || source[i..].starts_with("c\"") {
Some(2)
} else {
None
};
if let Some(pref_len) = str_prefix {
i += pref_len;
let mut escaped = false;
while i < len {
if let Some(ch) = source[i..].chars().next() {
i += ch.len_utf8();
if escaped {
escaped = false;
} else if ch == '\\' {
escaped = true;
} else if ch == '"' {
break;
}
} else {
break;
}
}
continue;
}
// 5. Check for compile-time env! or option_env! invocation
let (ident_len, is_macro_ident) = if source[i..].starts_with("option_env") {
let next_ch = source[i + 10..].chars().next();
let is_ident_boundary = next_ch
.map(|c| !c.is_alphanumeric() && c != '_')
.unwrap_or(true);
(10, is_ident_boundary)
} else if source[i..].starts_with("env") {
let next_ch = source[i + 3..].chars().next();
let is_ident_boundary = next_ch
.map(|c| !c.is_alphanumeric() && c != '_')
.unwrap_or(true);
(3, is_ident_boundary)
} else {
(0, false)
};
if is_macro_ident {
if i > 0 {
if let Some(prev) = source[..i].chars().next_back() {
if prev.is_alphanumeric() || prev == '_' {
i += ident_len;
continue;
}
}
}
let after_ident = i + ident_len;
let bang_pos = skip_trivia(source, after_ident);
if bang_pos < len
&& source[bang_pos..].starts_with('!')
&& !source[bang_pos..].starts_with("!=")
{
let delim_pos = skip_trivia(source, bang_pos + 1);
if delim_pos < len {
let delim = source[delim_pos..].chars().next();
if delim == Some('(') || delim == Some('[') || delim == Some('{') {
let mut j = delim_pos + 1;
j = skip_trivia(source, j);
if source[j..].starts_with('"') {
j += 1;
let str_start = j;
let mut escaped = false;
let mut str_end = None;
while j < len {
if let Some(ch) = source[j..].chars().next() {
j += ch.len_utf8();
if escaped {
escaped = false;
} else if ch == '\\' {
escaped = true;
} else if ch == '"' {
str_end = Some(j - 1);
break;
}
} else {
break;
}
}
if let Some(end) = str_end {
let raw_token = &source[str_start..end];
if let Some(unescaped) = unescape_rust_string(raw_token) {
vars.push(unescaped);
i = j;
continue;
} else {
return EnvMacroDiscovery::UntrackedDynamic;
}
} else {
return EnvMacroDiscovery::UntrackedDynamic;
}
} else if source[j..].starts_with("r#") || source[j..].starts_with("r\"") {
let pref_len = 1;
let rest = &source[j + pref_len..];
let mut hashes = 0;
while rest[hashes..].starts_with('#') {
hashes += 1;
}
if rest[hashes..].starts_with('"') {
let content_start = j + pref_len + hashes + 1;
let closing = format!("\"{}", "#".repeat(hashes));
if let Some(pos) = source[content_start..].find(&closing) {
let raw_token = &source[content_start..content_start + pos];
vars.push(raw_token.to_string());
i = content_start + pos + closing.len();
continue;
} else {
return EnvMacroDiscovery::UntrackedDynamic;
}
} else {
return EnvMacroDiscovery::UntrackedDynamic;
}
} else if source[j..].starts_with('#') {
// `env!(#var)` is `quote!` interpolation: a proc macro emitting
// the call for another crate, not a read of this crate's env.
i = j;
continue;
} else {
return EnvMacroDiscovery::UntrackedDynamic;
}
}
}
}
}
if let Some(ch) = source[i..].chars().next() {
i += ch.len_utf8();
} else {
break;
}
}
vars.sort();
vars.dedup();
EnvMacroDiscovery::Tracked(vars)
}
/// Recursively scans a package directory for compile-time env!/option_env! usages across
/// all .rs files, reusing the per-file memo in `env_macro_cache` for unchanged files.
pub fn discover_package_env_macros(package_dir: &Path) -> (EnvMacroDiscovery, Vec<String>) {
super::env_macro_cache::discover(package_dir)
}
#[cfg(test)]
#[path = "env_macro_tests.rs"]
mod tests;