Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions e2e/tasks/test_task_source_freshness_with_cwd
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,25 @@ pushd b
assert "mise run -q cwd_task" "running b 2"
assert_empty "mise run -q cwd_task"
popd

# A static task dir may use the conventional ./ prefix. Relative sources and
# outputs should still be matched against the same effective directory.
cat <<EOF >>mise.toml

[tasks.relative_dir]
run = "cp input.txt output.txt && cat output.txt"
dir = "./relative"
sources = ["input.txt"]
outputs = ["output.txt"]
EOF

mkdir relative
echo "relative 1" >relative/input.txt

assert "mise run -q relative_dir" "relative 1"
assert_empty "mise run -q relative_dir"

echo "relative 2" >relative/input.txt

assert "mise run -q relative_dir" "relative 2"
assert_empty "mise run -q relative_dir"
35 changes: 33 additions & 2 deletions src/task/task_source_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::collections::BTreeMap;
use std::fs::{self, File};
use std::hash::{DefaultHasher, Hash, Hasher};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::path::{Component, Path, PathBuf};
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use walkdir::WalkDir;
Expand Down Expand Up @@ -365,10 +365,21 @@ fn resolve_task_path(root: &Path, path: impl AsRef<Path>) -> PathBuf {
}
}

fn normalize_task_cwd(path: PathBuf) -> PathBuf {
let mut normalized: PathBuf = path
.components()
.filter(|component| !matches!(component, Component::CurDir))
.collect();
if normalized.as_os_str().is_empty() && !path.as_os_str().is_empty() {
normalized.push(".");
}
normalized
}

/// Get the working directory for a task
pub async fn task_cwd(task: &Task, config: &Arc<Config>) -> Result<PathBuf> {
if let Some(d) = task.dir(config).await? {
Ok(d)
Ok(normalize_task_cwd(d))
} else {
Ok(config
.project_root
Expand Down Expand Up @@ -1667,6 +1678,26 @@ mod tests {
assert!(!is_source(&matcher, Path::new("/workspace/src/other.go")));
}

#[test]
fn relative_sources_match_when_task_dir_starts_with_dot() -> Result<()> {
let temp = tempfile::tempdir()?;
let workspace = temp.path();
let task_cwd = normalize_task_cwd(workspace.join("./sub"));
let source = workspace.join("sub/input.txt");
fs::create_dir_all(source.parent().unwrap())?;
fs::write(&source, "source")?;

let sources = vec!["input.txt".to_string()];
let matcher = build_source_matcher(workspace, &task_cwd, &sources);
let metadatas = get_file_metadatas(&task_cwd, &sources, &matcher)?;

assert_eq!(
metadatas.into_iter().map(|(path, _)| path).collect_vec(),
[source]
);
Ok(())
}

#[test]
fn content_hash_cache_reuses_unchanged_files() {
let tmp = tempfile::tempdir().unwrap();
Expand Down
Loading