diff --git a/lib/spack/spack/build_environment.py b/lib/spack/spack/build_environment.py index c2d91902a4..59af563c92 100644 --- a/lib/spack/spack/build_environment.py +++ b/lib/spack/spack/build_environment.py @@ -775,23 +775,31 @@ def make_stack(tb, stack=None): if isinstance(obj, spack.package.PackageBase): break - # we found obj, the Package implementation we care about. - # point out the location in the install method where we failed. - lines = [] - lines.append("%s:%d, in %s:" % ( - inspect.getfile(frame.f_code), frame.f_lineno, frame.f_code.co_name - )) + # We found obj, the Package implementation we care about. + # Point out the location in the install method where we failed. + lines = [ + '{0}:{1:d}, in {2}:'.format( + inspect.getfile(frame.f_code), + frame.f_lineno - 1, # subtract 1 because f_lineno is 0-indexed + frame.f_code.co_name + ) + ] # Build a message showing context in the install method. sourcelines, start = inspect.getsourcelines(frame) - fl = frame.f_lineno - start - start_ctx = max(0, fl - context) - sourcelines = sourcelines[start_ctx:fl + context + 1] + # Calculate lineno of the error relative to the start of the function. + # Subtract 1 because f_lineno is 0-indexed. + fun_lineno = frame.f_lineno - start - 1 + start_ctx = max(0, fun_lineno - context) + sourcelines = sourcelines[start_ctx:fun_lineno + context + 1] + for i, line in enumerate(sourcelines): - is_error = start_ctx + i == fl - mark = ">> " if is_error else " " - marked = " %s%-6d%s" % (mark, start_ctx + i, line.rstrip()) + is_error = start_ctx + i == fun_lineno + mark = '>> ' if is_error else ' ' + # Add start to get lineno relative to start of file, not function. + marked = ' {0}{1:-6d}{2}'.format( + mark, start + start_ctx + i, line.rstrip()) if is_error: marked = colorize('@R{%s}' % marked) lines.append(marked)