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
//! MAKE018: Parallel-unsafe targets (race conditions)
//!
//! **Rule**: Detect targets that modify shared state without synchronization
//!
//! **Why this matters**:
//! When Make runs with -j (parallel jobs), targets can run concurrently.
//! Targets that write to the same file or directory can have race conditions.
//! This can cause corrupted builds or intermittent failures.
//!
//! **Auto-fix**: Suggest .NOTPARALLEL or order-only prerequisites
//!
//! ## Examples
//!
//! ❌ **BAD** (parallel-unsafe - multiple targets write to same directory):
//! ```makefile
//! install-bin:
//! \tcp app /usr/bin/app
//!
//! install-lib:
//! \tcp lib.so /usr/bin/lib.so
//! ```
//!
//! ✅ **GOOD** (with .NOTPARALLEL):
//! ```makefile
//! .NOTPARALLEL:
//!
//! install-bin:
//! \tcp app /usr/bin/app
//!
//! install-lib:
//! \tcp lib.so /usr/bin/lib.so
//! ```
//!
//! ✅ **GOOD** (with explicit dependencies):
//! ```makefile
//! install-bin:
//! \tcp app /usr/bin/app
//!
//! install-lib: install-bin
//! \tcp lib.so /usr/bin/lib.so
//! ```
use crate::linter::{Diagnostic, Fix, LintResult, Severity, Span};
use std::collections::{HashMap, HashSet};
/// Patterns that indicate shared state modification
const SHARED_STATE_PATTERNS: &[&str] = &[
"/usr/bin",
"/usr/local/bin",
"/usr/lib",
"/usr/local/lib",
"/etc",
"/var",
"/tmp",
];
/// Check for parallel-unsafe targets
pub fn check(source: &str) -> LintResult {
let mut result = LintResult::new();
// Empty Makefile - no parallel safety issues
if source.trim().is_empty() {
return result;
}
// If .NOTPARALLEL is present, no parallel safety issues
if has_notparallel(source) {
return result;
}
// Collect all targets and their shared state writes
let targets = collect_targets_with_shared_state(source);
// Find targets that write to overlapping shared state
let conflicts = find_parallel_conflicts(&targets);
// Create diagnostic if conflicts found
if !conflicts.is_empty() {
let span = Span::new(1, 1, 1, 1);
let fix_replacement = format!(".NOTPARALLEL:\n\n{}", source);
let diag = Diagnostic::new(
"MAKE018",
Severity::Warning,
format!(
"Multiple targets write to shared state - parallel-unsafe (conflicts: {})",
conflicts.join(", ")
),
span,
)
.with_fix(Fix::new(&fix_replacement));
result.add(diag);
}
result
}
/// Check if Makefile has .NOTPARALLEL
fn has_notparallel(source: &str) -> bool {
for line in source.lines() {
let trimmed = line.trim();
if trimmed == ".NOTPARALLEL:" || trimmed == ".NOTPARALLEL" {
return true;
}
}
false
}
/// Target with its shared state writes
struct TargetState {
name: String,
shared_paths: Vec<String>,
}
/// Collect targets that write to shared state
fn collect_targets_with_shared_state(source: &str) -> Vec<TargetState> {
let mut targets = Vec::new();
let lines: Vec<&str> = source.lines().collect();
let mut i = 0;
while i < lines.len() {
let line = lines[i];
// Check if this is a target line
if line.contains(':')
&& !line.starts_with(char::is_whitespace)
&& !line.trim_start().starts_with('#')
{
if let Some(colon_pos) = line.find(':') {
let target_name = line[..colon_pos].trim().to_string();
// Skip special targets
if target_name.starts_with('.') {
i += 1;
continue;
}
// Collect shared state writes in this target's recipes
let mut shared_paths = Vec::new();
let mut j = i + 1;
while j < lines.len() {
let recipe_line = lines[j];
// Recipe lines start with tab
if !recipe_line.starts_with('\t') {
break;
}
// Check for shared state patterns
for pattern in SHARED_STATE_PATTERNS {
if recipe_line.contains(pattern) {
shared_paths.push(pattern.to_string());
}
}
j += 1;
}
// Only add target if it writes to shared state
if !shared_paths.is_empty() {
targets.push(TargetState {
name: target_name,
shared_paths,
});
}
i = j;
continue;
}
}
i += 1;
}
targets
}
/// Find targets with overlapping shared state writes
fn find_parallel_conflicts(targets: &[TargetState]) -> Vec<String> {
let mut conflicts = HashSet::new();
// Build map of shared path -> targets that write to it
let mut path_to_targets: HashMap<String, Vec<String>> = HashMap::new();
for target in targets {
for path in &target.shared_paths {
path_to_targets
.entry(path.clone())
.or_default()
.push(target.name.clone());
}
}
// Find paths written by multiple targets
for (path, writing_targets) in path_to_targets {
if writing_targets.len() > 1 {
conflicts.insert(path);
}
}
conflicts.into_iter().collect()
}
#[cfg(test)]
mod tests {
use super::*;
// RED PHASE: Write failing tests first
#[test]
fn test_MAKE018_detects_parallel_unsafe_targets() {
let makefile =
"install-bin:\n\tcp app /usr/bin/app\n\ninstall-lib:\n\tcp lib.so /usr/bin/lib.so";
let result = check(makefile);
// Both targets write to /usr/bin - parallel unsafe
assert!(!result.diagnostics.is_empty());
let diag = &result.diagnostics[0];
assert_eq!(diag.code, "MAKE018");
assert_eq!(diag.severity, Severity::Warning);
assert!(diag.message.contains("parallel"));
}
#[test]
fn test_MAKE018_no_warning_with_notparallel() {
let makefile = ".NOTPARALLEL:\n\ninstall-bin:\n\tcp app /usr/bin/app\n\ninstall-lib:\n\tcp lib.so /usr/bin/lib.so";
let result = check(makefile);
// .NOTPARALLEL prevents parallel execution - safe
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_MAKE018_provides_fix() {
let makefile =
"install-bin:\n\tcp app /usr/bin/app\n\ninstall-lib:\n\tcp lib.so /usr/bin/lib.so";
let result = check(makefile);
assert!(!result.diagnostics.is_empty());
assert!(result.diagnostics[0].fix.is_some());
let fix = result.diagnostics[0].fix.as_ref().unwrap();
assert!(fix.replacement.contains(".NOTPARALLEL:"));
}
#[test]
fn test_MAKE018_detects_shared_directory_writes() {
let makefile = "setup-etc:\n\tcp config.conf /etc/app/config.conf\n\nsetup-var:\n\tcp data.db /var/app/data.db\n\nsetup-etc2:\n\tcp default.conf /etc/app/default.conf";
let result = check(makefile);
// setup-etc and setup-etc2 both write to /etc - parallel unsafe
assert!(!result.diagnostics.is_empty());
}
#[test]
fn test_MAKE018_no_warning_for_different_directories() {
let makefile = "install-bin:\n\tcp app /usr/bin/app\n\ninstall-config:\n\tcp config.yaml /home/user/.config/app.yaml";
let result = check(makefile);
// Different directories - safe to run in parallel
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_MAKE018_no_warning_for_single_target() {
let makefile = "install:\n\tcp app /usr/bin/app\n\tcp lib.so /usr/lib/lib.so";
let result = check(makefile);
// Single target - no parallel risk
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_MAKE018_detects_tmp_writes() {
let makefile = "build-a:\n\techo building > /tmp/build.log\n\nbuild-b:\n\techo building > /tmp/build.log";
let result = check(makefile);
// Both write to same temp file - parallel unsafe
assert!(!result.diagnostics.is_empty());
}
#[test]
fn test_MAKE018_empty_makefile() {
let makefile = "";
let result = check(makefile);
assert_eq!(result.diagnostics.len(), 0);
}
}