summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAngelos Evripiotis <jevripiotis@bloomberg.net>2018-10-23 09:31:16 +0100
committerJürg Billeter <j@bitron.ch>2019-02-11 09:24:48 +0000
commit02e48209ec7af316afc8390dcb14d1135115878b (patch)
treec7d18edbc45b76dcdc86c7a8c86c57bf40c3eb96
parent1ed63e542797f5097a1c9a1952fd50e435e1783a (diff)
downloadbuildstream-02e48209ec7af316afc8390dcb14d1135115878b.tar.gz
_includes: better error on missing include
Previously, a missing include would result in an error like this: Could not find file at not-a-file.include Note that the file containing the include was not mentioned. Now we get an error like this instead: element.bst [line 7 column 5]: Include block references a file that could not be found: 'not-a-file.include'.
-rw-r--r--buildstream/_includes.py13
-rw-r--r--tests/format/include.py20
2 files changed, 31 insertions, 2 deletions
diff --git a/buildstream/_includes.py b/buildstream/_includes.py
index d27f901a0..cb7faa4de 100644
--- a/buildstream/_includes.py
+++ b/buildstream/_includes.py
@@ -50,8 +50,17 @@ class Includes:
for include in reversed(includes):
if only_local and ':' in include:
continue
- include_node, file_path, sub_loader = self._include_file(include,
- current_loader)
+ try:
+ include_node, file_path, sub_loader = self._include_file(include,
+ current_loader)
+ except LoadError as e:
+ if e.reason == LoadErrorReason.MISSING_FILE:
+ message = "{}: Include block references a file that could not be found: '{}'.".format(
+ include_provenance, include)
+ raise LoadError(LoadErrorReason.MISSING_FILE, message) from e
+ else:
+ raise
+
if file_path in included:
raise LoadError(LoadErrorReason.RECURSIVE_INCLUDE,
"{}: trying to recursively include {}". format(include_provenance,
diff --git a/tests/format/include.py b/tests/format/include.py
index 7344919ba..536f9faaf 100644
--- a/tests/format/include.py
+++ b/tests/format/include.py
@@ -1,4 +1,5 @@
import os
+import textwrap
import pytest
from buildstream import _yaml
from buildstream._exceptions import ErrorDomain, LoadErrorReason
@@ -27,6 +28,25 @@ def test_include_project_file(cli, datafiles):
assert loaded['included'] == 'True'
+def test_include_missing_file(cli, tmpdir):
+ tmpdir.join('project.conf').write('{"name": "test"}')
+ element = tmpdir.join('include_missing_file.bst')
+
+ # Normally we would use dicts and _yaml.dump to write such things, but here
+ # we want to be sure of a stable line and column number.
+ element.write(textwrap.dedent("""
+ kind: manual
+
+ "(@)":
+ - nosuch.yaml
+ """).strip())
+
+ result = cli.run(project=str(tmpdir), args=['show', str(element.basename)])
+ result.assert_main_error(ErrorDomain.LOAD, LoadErrorReason.MISSING_FILE)
+ # Make sure the root cause provenance is in the output.
+ assert 'line 4 column 2' in result.stderr
+
+
@pytest.mark.datafiles(DATA_DIR)
def test_include_junction_file(cli, tmpdir, datafiles):
project = os.path.join(str(datafiles), 'junction')