Skip to content

Commit

Permalink
Use forward slashes for TrimmedPath() instead of os.PathSeparator
Browse files Browse the repository at this point in the history
Go stdlib runtime.Caller() currently returns forward slashes on Windows (see golang/go#3335) which causes EntryCaller.TrimmedPath() to return full paths instead of the expected trimmed paths on Windows. This is because EntryCaller.TrimmedPath() uses os.PathSeparator to trim the path which is '\' on Windows. According to the discussion on the Go bug, it seems like os.PathSeparator might be '' in some cases on Unix too so might cause issues on non-Windows platforms too.

This PR replaces the two occurrences of os.PathSeparator with ''/' as that is what runtime.Caller() currently produces on all platforms.

Fixes: uber-go#382
See also: golango/go#18151
  • Loading branch information
moitias committed Mar 23, 2017
1 parent 4257c7c commit 0f55c6d
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions zapcore/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package zapcore

import (
"fmt"
"os"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -102,13 +101,25 @@ func (ec EntryCaller) TrimmedPath() string {
if !ec.Defined {
return "undefined"
}
// nb. To make sure we trim the path correctly on Windows too, we
// counter-intuitively need to use '/' and *not* os.PathSeparator here,
// because the path given originates from Go stdlib, specifically
// runtime.Caller() which (as of Mar/17) returns forward slashes even on
// Windows.
//
// See https://github.com/golang/go/issues/3335
// and https://github.com/golang/go/issues/18151
//
// for discussion on the issue on Go side.
//
// Find the last separator.
idx := strings.LastIndexByte(ec.File, os.PathSeparator)
//
idx := strings.LastIndexByte(ec.File, '/')
if idx == -1 {
return ec.FullPath()
}
// Find the penultimate separator.
idx = strings.LastIndexByte(ec.File[:idx], os.PathSeparator)
idx = strings.LastIndexByte(ec.File[:idx], '/')
if idx == -1 {
return ec.FullPath()
}
Expand Down

0 comments on commit 0f55c6d

Please sign in to comment.