From 0f55c6d23dabcd6f2fde953615e8e568dbcb4cb8 Mon Sep 17 00:00:00 2001 From: Matias Lahti Date: Thu, 23 Mar 2017 14:36:07 +0200 Subject: [PATCH] Use forward slashes for TrimmedPath() instead of os.PathSeparator 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: #382 See also: golango/go#18151 --- zapcore/entry.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/zapcore/entry.go b/zapcore/entry.go index b09bbbea6..a0fe7370e 100644 --- a/zapcore/entry.go +++ b/zapcore/entry.go @@ -22,7 +22,6 @@ package zapcore import ( "fmt" - "os" "strings" "sync" "time" @@ -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() }