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
use eyre::bail;
/// Context for loading tasks with optional filtering hints
#[derive(Debug, Clone, Default, Hash, Eq, PartialEq)]
pub struct TaskLoadContext {
/// Specific paths to load tasks from
/// e.g., ["foo/bar", "baz/qux"] from patterns "//foo/bar:task" and "//baz/qux:task"
pub path_hints: Vec<String>,
/// If true, load all tasks from the entire monorepo (for `mise tasks ls --all`)
/// If false (default), only load tasks from current directory hierarchy
pub load_all: bool,
}
impl TaskLoadContext {
/// Create a new context that loads all tasks
pub fn all() -> Self {
Self {
path_hints: vec![],
load_all: true,
}
}
/// Create a context from a task pattern like "//foo/bar:task" or "//foo/bar/..."
pub fn from_pattern(pattern: &str) -> Self {
if let Some(hint) = Self::extract_path_hint(pattern) {
Self {
path_hints: vec![hint],
load_all: false,
}
} else {
Self {
path_hints: vec![],
load_all: true,
}
}
}
/// Create a context from multiple patterns, merging their path hints
pub fn from_patterns<'a>(patterns: impl Iterator<Item = &'a str>) -> Self {
use std::collections::HashSet;
let mut path_hints_set = HashSet::new();
let mut load_all = false;
for pattern in patterns {
if let Some(hint) = Self::extract_path_hint(pattern) {
path_hints_set.insert(hint);
} else {
// If any pattern has no hint, we need to load all
load_all = true;
}
}
Self {
path_hints: path_hints_set.into_iter().collect(),
load_all,
}
}
/// Extract path hint from a monorepo pattern
/// Returns None if the pattern doesn't provide useful filtering information
fn extract_path_hint(pattern: &str) -> Option<String> {
const MONOREPO_PREFIX: &str = "//";
const TASK_SEPARATOR: &str = ":";
const ELLIPSIS: &str = "...";
if !pattern.starts_with(MONOREPO_PREFIX) {
return None;
}
// Remove the // prefix
let without_prefix = pattern.strip_prefix(MONOREPO_PREFIX)?;
// Split on : to separate path from task name
let parts: Vec<&str> = without_prefix.splitn(2, TASK_SEPARATOR).collect();
let path_part = parts.first()?;
// If it's just "//..." or "//" we need everything
if path_part.is_empty() || *path_part == ELLIPSIS {
return None;
}
// Remove trailing ellipsis if present (e.g., "foo/bar/...")
let path_part = path_part.strip_suffix('/').unwrap_or(path_part);
let path_part = path_part.strip_suffix(ELLIPSIS).unwrap_or(path_part);
let path_part = path_part.strip_suffix('/').unwrap_or(path_part);
// If the path still contains "..." anywhere, it's a wildcard pattern
// that could match many paths, so we can't use it as a specific hint
// e.g., ".../graph" or "foo/.../bar" should load all subdirectories
if path_part.contains(ELLIPSIS) {
return None;
}
// If we have a non-empty path hint, return it
if !path_part.is_empty() {
Some(path_part.to_string())
} else {
None
}
}
/// Check if a subdirectory should be loaded based on the context
pub fn should_load_subdir(&self, subdir: &str, _monorepo_root: &str) -> bool {
use std::path::Path;
// If load_all is true, load everything
if self.load_all {
return true;
}
// If no path hints, don't load anything (unless load_all is true)
if self.path_hints.is_empty() {
return false;
}
// Use Path APIs for more robust path comparison
let subdir_path = Path::new(subdir);
// Check if subdir matches or is a parent/child of any hint
for hint in &self.path_hints {
let hint_path = Path::new(hint);
// Check if subdir matches or is a parent/child of this hint
// e.g., hint "foo/bar" should match:
// - "foo/bar" (exact match)
// - "foo/bar/baz" (child - subdir starts with hint)
// - "foo" (parent - hint starts with subdir, might contain the target)
if subdir_path == hint_path
|| subdir_path.starts_with(hint_path)
|| hint_path.starts_with(subdir_path)
{
return true;
}
}
false
}
}
/// Expands :task syntax to //path:task based on current directory relative to monorepo root
///
/// This function handles the special `:task` syntax that refers to tasks in the current
/// config_root within a monorepo. It converts `:build` to either `//:build` (if at monorepo root)
/// or `//project:build` (if in a subdirectory).
///
/// # Arguments
/// * `task` - The task pattern to expand (e.g., ":build")
/// * `config` - The configuration containing monorepo information
///
/// # Returns
/// * `Ok(String)` - The expanded task pattern (e.g., "//project:build")
/// * `Err` - If monorepo is not configured or current directory is outside monorepo root
pub fn expand_colon_task_syntax(
task: &str,
config: &crate::config::Config,
) -> eyre::Result<String> {
// Skip expansion for absolute monorepo paths or explicit global tasks
if task.starts_with("//") || task.starts_with("::") {
return Ok(task.to_string());
}
// Check if this is a colon pattern or a bare name
let is_colon_pattern = task.starts_with(':');
// Reject patterns that look like monorepo paths with wrong syntax (have / and : but don't start with // or :)
if !is_colon_pattern && task.contains('/') && task.contains(':') {
bail!(
"relative path syntax '{}' is not supported, use '//{}' or ':task' for current directory",
task,
task
);
}
// Get the monorepo root (the config file with experimental_monorepo_root = true)
let monorepo_root = config
.config_files
.values()
.find(|cf| cf.experimental_monorepo_root() == Some(true))
.and_then(|cf| cf.project_root());
// If not in monorepo context, only expand if it's a colon pattern (error), otherwise return as-is
if monorepo_root.is_none() {
if is_colon_pattern {
bail!("Cannot use :task syntax without a monorepo root");
}
return Ok(task.to_string());
}
// We're in a monorepo context
let monorepo_root = monorepo_root.unwrap();
// Determine the current directory relative to monorepo root
if let Some(cwd) = &*crate::dirs::CWD {
if let Ok(rel_path) = cwd.strip_prefix(monorepo_root) {
// For bare task names, only expand if we're actually in the monorepo
// For colon patterns, always expand (and error if outside monorepo)
// Convert relative path to monorepo path format
let path_str = rel_path
.to_string_lossy()
.replace(std::path::MAIN_SEPARATOR, "/");
if path_str.is_empty() {
// We're at the root
if is_colon_pattern {
// :task -> //:task (task already has colon)
Ok(format!("//{}", task))
} else {
// bare task -> //:task (add colon)
Ok(format!("//:{}", task))
}
} else {
// We're in a subdirectory
if is_colon_pattern {
// :task -> //path:task
Ok(format!("//{}{}", path_str, task))
} else {
// bare name -> //path:task
Ok(format!("//{}:{}", path_str, task))
}
}
} else {
if is_colon_pattern {
bail!("Cannot use :task syntax outside of monorepo root directory");
}
// Bare name outside monorepo - return as-is for global matching
Ok(task.to_string())
}
} else {
if is_colon_pattern {
bail!("Cannot use :task syntax without a current working directory");
}
Ok(task.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extract_path_hint() {
assert_eq!(
TaskLoadContext::extract_path_hint("//foo/bar:task"),
Some("foo/bar".to_string())
);
assert_eq!(
TaskLoadContext::extract_path_hint("//foo/bar/...:task"),
Some("foo/bar".to_string())
);
assert_eq!(
TaskLoadContext::extract_path_hint("//foo:task"),
Some("foo".to_string())
);
assert_eq!(TaskLoadContext::extract_path_hint("//:task"), None);
assert_eq!(TaskLoadContext::extract_path_hint("//...:task"), None);
assert_eq!(TaskLoadContext::extract_path_hint("foo:task"), None);
// Test patterns with ... in different positions (wildcard patterns)
// ... at the START of path
assert_eq!(
TaskLoadContext::extract_path_hint("//.../api:task"),
None,
"Pattern with ... at start should load all subdirs"
);
assert_eq!(
TaskLoadContext::extract_path_hint("//.../services/api:task"),
None,
"Pattern with ... at start and more path should load all subdirs"
);
// ... in the MIDDLE of path
assert_eq!(
TaskLoadContext::extract_path_hint("//projects/.../api:task"),
None,
"Pattern with ... in middle should load all subdirs"
);
assert_eq!(
TaskLoadContext::extract_path_hint("//libs/.../utils:task"),
None,
"Pattern with ... in middle should load all subdirs"
);
// Multiple ... in path
assert_eq!(
TaskLoadContext::extract_path_hint("//projects/.../api/...:task"),
None,
"Pattern with multiple ... should load all subdirs"
);
assert_eq!(
TaskLoadContext::extract_path_hint("//.../foo/.../bar:task"),
None,
"Pattern with ... at start and middle should load all subdirs"
);
}
#[test]
fn test_should_load_subdir() {
let ctx = TaskLoadContext::from_pattern("//foo/bar:task");
// Should load exact match
assert!(ctx.should_load_subdir("foo/bar", "/root"));
// Should load children
assert!(ctx.should_load_subdir("foo/bar/baz", "/root"));
// Should load parent (might contain target)
assert!(ctx.should_load_subdir("foo", "/root"));
// Should not load unrelated paths
assert!(!ctx.should_load_subdir("baz/qux", "/root"));
}
#[test]
fn test_should_load_subdir_multiple_hints() {
let ctx =
TaskLoadContext::from_patterns(["//foo/bar:task", "//baz/qux:task"].iter().copied());
// Should load exact matches for both hints
assert!(ctx.should_load_subdir("foo/bar", "/root"));
assert!(ctx.should_load_subdir("baz/qux", "/root"));
// Should load children of both hints
assert!(ctx.should_load_subdir("foo/bar/child", "/root"));
assert!(ctx.should_load_subdir("baz/qux/child", "/root"));
// Should load parents of both hints
assert!(ctx.should_load_subdir("foo", "/root"));
assert!(ctx.should_load_subdir("baz", "/root"));
// Should not load unrelated paths
assert!(!ctx.should_load_subdir("other/path", "/root"));
}
#[test]
fn test_load_all_context() {
let ctx = TaskLoadContext::all();
assert!(ctx.load_all);
assert!(ctx.should_load_subdir("any/path", "/root"));
}
#[test]
fn test_from_pattern_root_level_sets_load_all() {
let ctx = TaskLoadContext::from_pattern("//:prl");
assert!(ctx.load_all);
assert!(ctx.path_hints.is_empty());
let ctx = TaskLoadContext::from_pattern("prl");
assert!(ctx.load_all);
assert!(ctx.path_hints.is_empty());
}
#[test]
fn test_from_pattern_with_path_does_not_set_load_all() {
let ctx = TaskLoadContext::from_pattern("//projects/backend:task");
assert!(!ctx.load_all);
assert_eq!(ctx.path_hints, vec!["projects/backend"]);
}
#[test]
fn test_expand_colon_task_syntax() {
// Note: This is a basic structure test. Full integration testing is done in e2e tests
// because it requires a real config with monorepo root setup and CWD manipulation.
// Test that non-colon patterns are returned as-is
// We can't easily test the full expansion here without setting up a real config
// and manipulating CWD, so we just verify the function signature and basic behavior
let task = "regular-task";
// For non-colon tasks, this should work even with empty config
// The actual expansion logic is tested via e2e tests
assert!(!task.starts_with(':'));
}
}