From f91799e6a85cad354a0b1dcbb95d274ea0883388 Mon Sep 17 00:00:00 2001
From: Martin Blanchard <martin.blanchard@codethink.co.uk>
Date: Mon, 30 Jul 2018 13:36:15 +0100
Subject: [PATCH] element.py: Raise an exception on unbuilt element checkout

Trying to checkout an element that has no cached artifacts should be and
handled failure. See BuildStream/buildstream#447.
---
 buildstream/element.py          |  6 ++++++
 tests/frontend/buildcheckout.py | 10 ++++++++++
 2 files changed, 16 insertions(+)

diff --git a/buildstream/element.py b/buildstream/element.py
index 4260d32a53..af65534fcd 100644
--- a/buildstream/element.py
+++ b/buildstream/element.py
@@ -614,6 +614,12 @@ class Element(Plugin):
               dep.stage_artifact(sandbox)
         """
 
+        if not self._cached():
+            detail = "No artifacts have been cached yet for that element\n" + \
+                     "Try building the element first with `bst build`\n"
+            raise ElementError("No artifacts to stage",
+                               detail=detail, reason="uncached-checkout-attempt")
+
         if update_mtimes is None:
             update_mtimes = []
 
diff --git a/tests/frontend/buildcheckout.py b/tests/frontend/buildcheckout.py
index 1ec8f491f6..f27005ada8 100644
--- a/tests/frontend/buildcheckout.py
+++ b/tests/frontend/buildcheckout.py
@@ -96,6 +96,16 @@ def test_build_checkout_deps(datafiles, cli, deps):
         assert not os.path.exists(filename)
 
 
+@pytest.mark.datafiles(DATA_DIR)
+def test_build_checkout_unbuilt(datafiles, cli):
+    project = os.path.join(datafiles.dirname, datafiles.basename)
+    checkout = os.path.join(cli.directory, 'checkout')
+
+    # Check that checking out an unbuilt element fails nicely
+    result = cli.run(project=project, args=['checkout', 'target.bst', checkout])
+    result.assert_main_error(ErrorDomain.STREAM, "uncached-checkout-attempt")
+
+
 @pytest.mark.datafiles(DATA_DIR)
 def test_build_checkout_tarball(datafiles, cli):
     project = os.path.join(datafiles.dirname, datafiles.basename)
-- 
GitLab