aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2020-11-18 12:51:49 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2020-11-19 13:07:57 +0300
commitf91468192ad7066a5e6fa63ddc4e4231e632a457 (patch)
tree5e399478261a27e8b2298b0f56b08a0e5b7dc3ab
parent15507d112650477ad64a5c9b19b4e515124affec (diff)
Remove target files on ad hoc rule update buildscript error
-rw-r--r--libbuild2/adhoc-rule-buildscript.cxx19
-rw-r--r--tests/recipe/buildscript/testscript73
2 files changed, 78 insertions, 14 deletions
diff --git a/libbuild2/adhoc-rule-buildscript.cxx b/libbuild2/adhoc-rule-buildscript.cxx
index a09d2c7..9470d3c 100644
--- a/libbuild2/adhoc-rule-buildscript.cxx
+++ b/libbuild2/adhoc-rule-buildscript.cxx
@@ -10,7 +10,7 @@
#include <libbuild2/target.hxx>
#include <libbuild2/context.hxx>
#include <libbuild2/algorithm.hxx>
-#include <libbuild2/filesystem.hxx> // path_perms()
+#include <libbuild2/filesystem.hxx> // path_perms(), auto_rmfile
#include <libbuild2/diagnostics.hxx>
#include <libbuild2/parser.hxx> // attributes
@@ -563,6 +563,20 @@ namespace build2
if (!ctx.dry_run || verb >= 2)
{
+ // On failure remove the target files that may potentially exist but
+ // be invalid.
+ //
+ small_vector<auto_rmfile, 8> rms;
+
+ if (!ctx.dry_run)
+ {
+ for (const target* m (&t); m != nullptr; m = m->adhoc_member)
+ {
+ if (auto* f = m->is_a<file> ())
+ rms.emplace_back (f->path ());
+ }
+ }
+
build::script::default_runner r;
p.execute (*rs, *bs, e, script, r);
@@ -588,6 +602,9 @@ namespace build2
}
#endif
dd.check_mtime (tp);
+
+ for (auto& rm: rms)
+ rm.cancel ();
}
}
}
diff --git a/tests/recipe/buildscript/testscript b/tests/recipe/buildscript/testscript
index 787bafd..6b3f6c7 100644
--- a/tests/recipe/buildscript/testscript
+++ b/tests/recipe/buildscript/testscript
@@ -17,24 +17,71 @@
: update
:
{
- echo 'bar' >=bar;
+ : success
+ :
+ {
+ echo 'bar' >=bar;
- cat <<EOI >=buildfile;
- foo: bar
- {{
- cp $path($<) $path($>)
- }}
- EOI
+ cat <<EOI >=buildfile;
+ foo: bar
+ {{
+ cp $path($<) $path($>)
+ }}
+ EOI
- $* 2>'cp file{foo}';
+ $* 2>'cp file{foo}';
- cat <<<foo >'bar';
+ cat <<<foo >'bar';
- # While at it, make sure there is no rebuild.
- #
- $* 2>/'info: dir{./} is up to date';
+ # While at it, make sure there is no rebuild.
+ #
+ $* 2>/'info: dir{./} is up to date';
- $* clean 2>-
+ $* clean 2>-
+ }
+
+ : error
+ :
+ : Test that the target file is removed on error and is created on subsequent
+ : successful update.
+ :
+ {
+ echo 'bar' >=bar;
+
+ cat <<EOI >=buildfile;
+ foo: bar
+ {{
+ diag concat $<
+
+ t = $path($>)
+ p = $path($<)
+
+ cp $p $t
+
+ cat $(p).baz >>$t
+ }}
+ EOI
+
+ $* 2>>~%EOE% != 0;
+ concat file{bar.}
+ %cat: unable to print '.+bar.baz': .+%
+ buildfile:10:3: error: cat exited with code 1
+ %.+
+ EOE
+
+ test -f foo != 0;
+
+ echo 'baz' >=bar.baz;
+
+ $* 2>'concat file{bar.}';
+
+ cat <<<foo >>EOO;
+ bar
+ baz
+ EOO
+
+ $* clean 2>-
+ }
}
: clean