From 9862c97d386bf9d886efa0c192b09be309299dde Mon Sep 17 00:00:00 2001 From: "Adam J. Stewart" Date: Sat, 2 Jun 2018 22:53:18 -0500 Subject: Fix package error message line numbers (#8271) Line numbers were reported as zero-indexed, but we need to adjust. --- lib/spack/spack/build_environment.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'lib') 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 get_package_context(traceback, context=3): 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) -- cgit v1.2.3-60-g2f50