Skip to content

Commit

Permalink
[fud2] Make Empty Ops an Error (#2257)
Browse files Browse the repository at this point in the history
Empty ops currently can be created in the new DSL. When exported, this
creates broken ninja code. This is an error of the user, not creating
the desired target, but this class of error can never create the desired
target. Therefore a helpful error was inserted when this happens.
  • Loading branch information
jku20 authored Aug 12, 2024
1 parent 1349ded commit dccf92b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
12 changes: 11 additions & 1 deletion fud2/fud-core/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,17 @@ impl<'a> Run<'a> {
}
}) {
let stdout_files =
std::fs::File::open(self.plan.workdir.join(filename))?;
std::fs::File::open(self.plan.workdir.join(filename))
// The output file we're emitting to stdout should exist. If it doesn't,
// some op didn't produce the output file it claimed to.
.map_err(|e| if let std::io::ErrorKind::NotFound = e.kind() {
std::io::Error::new(
e.kind(),
format!("{}\nHint: Check ops actually generate all of their targets.", e)
)
} else {
e
})?;
std::io::copy(
&mut std::io::BufReader::new(stdout_files),
&mut std::io::stdout(),
Expand Down
11 changes: 11 additions & 0 deletions fud2/fud-core/src/script/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub(super) enum RhaiSystemErrorKind {
ExpectedString(String),
ExpectedShell,
ExpectedShellDeps,
EmptyOp,
}

impl RhaiSystemError {
Expand Down Expand Up @@ -90,6 +91,13 @@ impl RhaiSystemError {
}
}

pub(super) fn empty_op() -> Self {
Self {
kind: RhaiSystemErrorKind::EmptyOp,
position: rhai::Position::NONE,
}
}

pub(super) fn with_pos(mut self, p: rhai::Position) -> Self {
self.position = p;
self
Expand Down Expand Up @@ -129,6 +137,9 @@ impl Display for RhaiSystemError {
RhaiSystemErrorKind::ExpectedShellDeps => {
write!(f, "Expected `shell_deps`, got `shell`. Ops may contain only one of `shell` or `shell_deps` calls, not calls to both")
}
RhaiSystemErrorKind::EmptyOp => {
write!(f, "Error: Op must contain at least one call to `shell` or `shell_deps`.")
}
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion fud2/fud-core/src/script/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,16 @@ impl ScriptContext {
c
}
Some(ShellCommands::Cmds(c)) => c.clone(),
None => vec![],

None => {
// If cmds is empty, then the op doesn't create any of it's targets and
// should be considered erroneous.
return Err(RhaiSystemError::empty_op()
.with_pos(pos)
.into());
}
};

let op_name = name.clone();
let config_vars = config_vars.clone();
let op_emitter = crate::run::RulesOp {
Expand Down

0 comments on commit dccf92b

Please sign in to comment.