Commit 7df95654 authored by Chandan Singh's avatar Chandan Singh Committed by Tristan Van Berkom

_loader/ Report element-path when failing to load elements

It can be confusing, especially to new BuildStream users, that the CLI
expects targets to be specified relative to element-path and not the
current directory. Previously, the CLI would give a generic message
stating that the file could not be found but it was not obvious that it
was looking in the `element-path` directory.

Explicitly print the element-path in the summary. Also, try to check if
the specified element exists in the elements directory and print a hint
to use the element-path relative paths if that's the case.

Fixes #396.

This is is also related to #341. This commit aims to tackle that issue
by trying to educate users about element-path.
parent 55dc3b27
Pipeline #23366600 passed with stages
in 29 minutes and 3 seconds
......@@ -207,7 +207,23 @@ class Loader():
# Load the data and process any conditional statements therein
fullpath = os.path.join(self._basedir, filename)
node = _yaml.load(fullpath, shortname=filename, copy_tree=rewritable)
node = _yaml.load(fullpath, shortname=filename, copy_tree=rewritable)
except LoadError as e:
if e.reason == LoadErrorReason.MISSING_FILE:
# If we can't find the file, try to suggest plausible
# alternatives by stripping the element-path from the given
# filename, and verifying that it exists.
message = "Could not find element '{}' in elements directory '{}'".format(filename, self._basedir)
detail = None
elements_dir = os.path.relpath(self._basedir,
element_relpath = os.path.relpath(filename, elements_dir)
if filename.startswith(elements_dir) and os.path.exists(os.path.join(self._basedir, element_relpath)):
detail = "Did you mean '{}'?".format(element_relpath)
raise LoadError(LoadErrorReason.MISSING_FILE,
message, detail=detail) from e
element = LoadElement(node, filename, self)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment