github.com/evanw/esbuild@v0.21.4/internal/helpers/stack.go (about)

     1  package helpers
     2  
     3  import (
     4  	"runtime/debug"
     5  	"strings"
     6  )
     7  
     8  func PrettyPrintedStack() string {
     9  	lines := strings.Split(strings.TrimSpace(string(debug.Stack())), "\n")
    10  
    11  	// Strip the first "goroutine" line
    12  	if len(lines) > 0 {
    13  		if first := lines[0]; strings.HasPrefix(first, "goroutine ") && strings.HasSuffix(first, ":") {
    14  			lines = lines[1:]
    15  		}
    16  	}
    17  
    18  	sb := strings.Builder{}
    19  
    20  	for _, line := range lines {
    21  		// Indented lines are source locations
    22  		if strings.HasPrefix(line, "\t") {
    23  			line = line[1:]
    24  			line = strings.TrimPrefix(line, "github.com/evanw/esbuild/")
    25  			if offset := strings.LastIndex(line, " +0x"); offset != -1 {
    26  				line = line[:offset]
    27  			}
    28  			sb.WriteString(" (")
    29  			sb.WriteString(line)
    30  			sb.WriteString(")")
    31  			continue
    32  		}
    33  
    34  		// Other lines are function calls
    35  		if sb.Len() > 0 {
    36  			sb.WriteByte('\n')
    37  		}
    38  		if strings.HasSuffix(line, ")") {
    39  			if paren := strings.LastIndexByte(line, '('); paren != -1 {
    40  				line = line[:paren]
    41  			}
    42  		}
    43  		if slash := strings.LastIndexByte(line, '/'); slash != -1 {
    44  			line = line[slash+1:]
    45  		}
    46  		sb.WriteString(line)
    47  	}
    48  
    49  	return sb.String()
    50  }