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
//! `docker-compose*.yml` / `compose.yaml` declaration sensor.
//!
//! Walks each service's `environment:` (literal map or list form) and
//! `env_file:` (one or many .env paths). For env_file references we record a
//! `EnvFrom` source so the orchestrator can chain into the dotenv parser.
//!
//! 𝚅𝚒𝚋𝚎𝚌𝚛𝚊𝚏𝚝𝚎𝚍. with AI Agents ⓒ 2025-2026 Loctree Team
use std::path::Path;
use serde_yaml::Value;
use super::io_helpers::{hash_value, mtime_info, relativize};
use super::types::{EnvSource, EnvSourceKind, ValuePresence};
/// Parse a docker-compose YAML file. Returns:
/// - inline declarations (`environment:`)
/// - referenced env_file paths (resolved relative to the compose file dir)
pub fn parse_compose_file(
path: &Path,
root: &Path,
base_rank: u8,
) -> (Vec<(String, EnvSource)>, Vec<std::path::PathBuf>) {
let raw = match std::fs::read_to_string(path) {
Ok(r) => r,
Err(_) => return (Vec::new(), Vec::new()),
};
let yaml: Value = match serde_yaml::from_str(&raw) {
Ok(v) => v,
Err(_) => return (Vec::new(), Vec::new()),
};
let rel = relativize(path, root);
let (mtime, age) = mtime_info(path);
let mtime_str = mtime.unwrap_or_default();
let mut inline = Vec::new();
let mut env_file_refs = Vec::new();
let dir = path.parent().unwrap_or(root);
let services = yaml.get("services").and_then(Value::as_mapping);
if let Some(services) = services {
for (_svc_name, svc_value) in services {
let Some(svc) = svc_value.as_mapping() else {
continue;
};
// environment: literal
if let Some(env) = svc.get(Value::String("environment".into())) {
collect_environment(
env,
&rel,
&mtime_str,
age,
base_rank,
EnvSourceKind::DockerCompose,
&mut inline,
);
}
// env_file: single string or list
if let Some(env_file) = svc.get(Value::String("env_file".into())) {
match env_file {
Value::String(s) => {
let resolved = dir.join(s);
env_file_refs.push(resolved);
// Record the REFERENCE itself as a declaration source
// for transparency (without an env name we can only
// attach it later, so we add a sentinel only when
// chained — here we just expose the path).
push_env_file_reference(s, &rel, &mtime_str, age, base_rank, &mut inline);
}
Value::Sequence(seq) => {
for entry in seq {
if let Some(s) = entry.as_str() {
let resolved = dir.join(s);
env_file_refs.push(resolved);
push_env_file_reference(
s,
&rel,
&mtime_str,
age,
base_rank,
&mut inline,
);
}
}
}
_ => {}
}
}
}
}
(inline, env_file_refs)
}
fn collect_environment(
value: &Value,
rel_path: &str,
mtime: &str,
age: Option<u32>,
base_rank: u8,
kind: EnvSourceKind,
out: &mut Vec<(String, EnvSource)>,
) {
match value {
Value::Mapping(m) => {
for (k, v) in m {
let Some(key) = k.as_str() else {
continue;
};
let presence = scalar_to_presence(v);
out.push((
key.to_string(),
EnvSource {
kind,
path: rel_path.to_string(),
line: None,
mtime: mtime.to_string(),
mtime_age_days: age,
git_age_days: None,
value_present: presence,
precedence_rank: base_rank,
},
));
}
}
Value::Sequence(seq) => {
for entry in seq {
if let Some(s) = entry.as_str() {
if let Some((name, val)) = s.split_once('=') {
let presence = if val.is_empty() {
ValuePresence::Empty
} else {
ValuePresence::Plain {
value_hash: hash_value(val),
}
};
out.push((
name.trim().to_string(),
EnvSource {
kind,
path: rel_path.to_string(),
line: None,
mtime: mtime.to_string(),
mtime_age_days: age,
git_age_days: None,
value_present: presence,
precedence_rank: base_rank,
},
));
} else {
// `KEY` only — value comes from host env at runtime.
out.push((
s.to_string(),
EnvSource {
kind,
path: rel_path.to_string(),
line: None,
mtime: mtime.to_string(),
mtime_age_days: age,
git_age_days: None,
value_present: ValuePresence::EnvFrom {
reference: "host".into(),
},
precedence_rank: base_rank,
},
));
}
}
}
}
_ => {}
}
}
fn scalar_to_presence(v: &Value) -> ValuePresence {
match v {
Value::Null => ValuePresence::Empty,
Value::String(s) if s.is_empty() => ValuePresence::Empty,
Value::String(s) => ValuePresence::Plain {
value_hash: hash_value(s),
},
Value::Bool(b) => ValuePresence::Plain {
value_hash: hash_value(&b.to_string()),
},
Value::Number(n) => ValuePresence::Plain {
value_hash: hash_value(&n.to_string()),
},
_ => ValuePresence::EnvFrom {
reference: "complex".into(),
},
}
}
fn push_env_file_reference(
file_ref: &str,
rel_path: &str,
mtime: &str,
age: Option<u32>,
base_rank: u8,
out: &mut Vec<(String, EnvSource)>,
) {
// We cannot know which keys this env_file references without reading it.
// We push a synthetic `__env_file__` declaration with a reference value
// so the orchestrator can re-issue dotenv parsing at the resolved path.
// (The synthetic name is filtered out before the report is emitted.)
out.push((
"__env_file__".to_string(),
EnvSource {
kind: EnvSourceKind::DockerComposeEnvFile,
path: rel_path.to_string(),
line: None,
mtime: mtime.to_string(),
mtime_age_days: age,
git_age_days: None,
value_present: ValuePresence::EnvFrom {
reference: file_ref.to_string(),
},
precedence_rank: base_rank,
},
));
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::TempDir;
#[test]
fn parses_environment_mapping() {
let tmp = TempDir::new().unwrap();
let compose = tmp.path().join("docker-compose.yml");
fs::write(
&compose,
"
services:
api:
image: foo
environment:
DATABASE_URL: postgres://localhost/x
LOG_LEVEL: debug
",
)
.unwrap();
let (decls, refs) = parse_compose_file(&compose, tmp.path(), 50);
assert!(refs.is_empty());
let names: Vec<&str> = decls.iter().map(|(n, _)| n.as_str()).collect();
assert!(names.contains(&"DATABASE_URL"));
assert!(names.contains(&"LOG_LEVEL"));
}
#[test]
fn parses_environment_list_form() {
let tmp = TempDir::new().unwrap();
let compose = tmp.path().join("docker-compose.yml");
fs::write(
&compose,
"
services:
worker:
image: x
environment:
- REDIS_URL=redis://r:6379
- HOST_INHERITED
",
)
.unwrap();
let (decls, _refs) = parse_compose_file(&compose, tmp.path(), 50);
let redis = decls.iter().find(|(n, _)| n == "REDIS_URL").unwrap();
assert!(matches!(redis.1.value_present, ValuePresence::Plain { .. }));
let host = decls.iter().find(|(n, _)| n == "HOST_INHERITED").unwrap();
assert!(matches!(
host.1.value_present,
ValuePresence::EnvFrom { .. }
));
}
#[test]
fn extracts_env_file_references() {
let tmp = TempDir::new().unwrap();
let compose = tmp.path().join("docker-compose.yml");
fs::write(
&compose,
"
services:
api:
image: foo
env_file:
- ./.env
- ./.env.production
",
)
.unwrap();
let (decls, refs) = parse_compose_file(&compose, tmp.path(), 50);
assert_eq!(refs.len(), 2);
// Synthetic markers for the env_file references appear in decls.
let synth: Vec<&str> = decls.iter().map(|(n, _)| n.as_str()).collect();
assert!(synth.contains(&"__env_file__"));
}
}