Skip to content

Commit

Permalink
fix: task source bug fixes (jdx#3522)
Browse files Browse the repository at this point in the history
Fixes jdx#2526
Fixes jdx#3499
  • Loading branch information
jdx authored and miguelmig committed Dec 21, 2024
1 parent af7fb91 commit 8fc8fb4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 14 deletions.
10 changes: 10 additions & 0 deletions e2e/assert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ assert_matches() {
fi
}

assert_empty() {
local actual
actual="$(quiet_assert_succeed "$1")"
if [[ -z "$actual" ]]; then
ok "[$1] output is empty"
else
fail "[$1] expected empty output but got '$actual'"
fi
}

require_cmd() {
if ! type -p "$1" >/dev/null; then
title="E2E test $TEST_NAME aborted" err "'$1' is required but was not found in PATH"
Expand Down
35 changes: 35 additions & 0 deletions e2e/tasks/test_task_run_sources
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash

cat <<EOF >mise.toml
[tasks.test]
run = 'cat input > test.txt && echo done'
sources = ['input']
outputs = ['test.txt']
EOF
echo "hello world" >input

mkdir subdir && cd subdir || exit 1
assert "mise -q test" "done"
assert_not_contains "mise -q test" "done"
touch ../input
assert "mise -q test" "done"
touch ../test.txt
assert_not_contains "mise -q test" "done"
touch ../mise.toml
assert "mise -q test" "done"

cat <<EOF >mise.toml
[tasks.hi]
sources = ["o*"]
outputs = ["newer"]
run = "echo hi"
EOF

touch older
sleep 0.001
touch newer
mkdir subdir && cd subdir || exit 1
assert_empty "mise -q hi"
assert_empty "mise -q hi"
touch ../older
assert "mise -q hi" "hi"
22 changes: 12 additions & 10 deletions src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,9 @@ impl Run {
return Ok(false);
}
let run = || -> Result<bool> {
let sources = self.get_last_modified(&self.cwd(task)?, &task.sources)?;
let mut sources = task.sources.clone();
sources.push(task.config_source.to_string_lossy().to_string());
let sources = self.get_last_modified(&self.cwd(task)?, &sources)?;
let outputs = self.get_last_modified(&self.cwd(task)?, &task.outputs)?;
trace!("sources: {sources:?}, outputs: {outputs:?}");
match (sources, outputs) {
Expand All @@ -583,7 +585,7 @@ impl Run {
}
};
Ok(run().unwrap_or_else(|err| {
warn!("sources_are_fresh: {err}");
warn!("sources_are_fresh: {err:?}");
false
}))
}
Expand Down Expand Up @@ -677,17 +679,13 @@ fn is_glob_pattern(path: &str) -> bool {
path.chars().any(|c| glob_chars.contains(&c))
}

fn last_modified_path(
root: impl AsRef<std::ffi::OsStr>,
paths: &[&String],
) -> Result<Option<SystemTime>> {
fn last_modified_path(root: &Path, paths: &[&String]) -> Result<Option<SystemTime>> {
let files = paths.iter().map(|p| {
let base = Path::new(p);

if base.is_relative() {
base.to_path_buf()
} else {
Path::new(&root).join(base)
} else {
base.to_path_buf()
}
});

Expand Down Expand Up @@ -727,7 +725,11 @@ fn last_modified_file(files: impl IntoIterator<Item = PathBuf>) -> Result<Option
Ok(files
.into_iter()
.unique()
.map(|p| p.metadata().map_err(|err| eyre!(err)))
.filter(|p| p.exists())
.map(|p| {
p.metadata()
.map_err(|err| eyre!("{}: {}", display_path(p), err))
})
.collect::<Result<Vec<_>>>()?
.into_iter()
.map(|m| m.modified().map_err(|err| eyre!(err)))
Expand Down
17 changes: 13 additions & 4 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ impl log::Log for Logger {
if let Some(log_file) = &self.log_file {
let mut log_file = log_file.lock().unwrap();
let out = self.render(record, self.file_level);
let _ = writeln!(log_file, "{}", console::strip_ansi_codes(&out));
if !out.is_empty() {
let _ = writeln!(log_file, "{}", console::strip_ansi_codes(&out));
}
}
}
if record.level() <= self.term_level {
ui::multi_progress_report::MultiProgressReport::suspend_if_active(|| {
eprintln!("{}", self.render(record, self.term_level));
let out = self.render(record, self.term_level);
if !out.is_empty() {
eprintln!("{}", self.render(record, self.term_level));
}
});
}
}
Expand Down Expand Up @@ -72,15 +77,19 @@ impl Logger {
match level {
LevelFilter::Off => "".to_string(),
LevelFilter::Trace => {
let level = record.level();
let file = record.file().unwrap_or("<unknown>");
if level == LevelFilter::Trace && file.contains("/expr-lang-") {
return "".to_string();
};
let meta = ui::style::edim(format!(
"{thread_id:>2} [{file}:{line}]",
thread_id = thread_id(),
file = record.file().unwrap_or("<unknown>"),
line = record.line().unwrap_or(0),
));
format!(
"{level} {meta} {args}",
level = self.styled_level(record.level()),
level = self.styled_level(level),
args = record.args()
)
}
Expand Down

0 comments on commit 8fc8fb4

Please sign in to comment.