Skip to content

Commit 51a112f

Browse files
committed
path/filepath: handle c: as first parameter in Join properly
This is CL 11882 brought back to life. Fixes #11551 Change-Id: I29810183957745442d1e9937f56a66fc9c6cc82a Reviewed-on: https://go-review.googlesource.com/17470 Reviewed-by: Russ Cox <[email protected]> Run-TryBot: Alex Brainman <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 05390a0 commit 51a112f

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/path/filepath/path_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,12 @@ var winjointests = []JoinTest{
267267
{[]string{`C:\Windows\`, `System32`}, `C:\Windows\System32`},
268268
{[]string{`C:\Windows\`, ``}, `C:\Windows`},
269269
{[]string{`C:\`, `Windows`}, `C:\Windows`},
270-
{[]string{`C:`, `Windows`}, `C:\Windows`},
270+
{[]string{`C:`, `a`}, `C:a`},
271+
{[]string{`C:`, `a\b`}, `C:a\b`},
272+
{[]string{`C:`, `a`, `b`}, `C:a\b`},
273+
{[]string{`C:.`, `a`}, `C:a`},
274+
{[]string{`C:a`, `b`}, `C:a\b`},
275+
{[]string{`C:a`, `b`, `d`}, `C:a\b\d`},
271276
{[]string{`\\host\share`, `foo`}, `\\host\share\foo`},
272277
{[]string{`\\host\share\foo`}, `\\host\share\foo`},
273278
{[]string{`//host/share`, `foo/bar`}, `\\host\share\foo\bar`},

src/path/filepath/path_windows.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ func join(elem []string) string {
120120

121121
// joinNonEmpty is like join, but it assumes that the first element is non-empty.
122122
func joinNonEmpty(elem []string) string {
123+
if len(elem[0]) == 2 && elem[0][1] == ':' {
124+
// First element is drive leter without terminating slash.
125+
// Keep path relative to current directory on that drive.
126+
return Clean(elem[0] + strings.Join(elem[1:], string(Separator)))
127+
}
123128
// The following logic prevents Join from inadvertently creating a
124129
// UNC path on Windows. Unless the first element is a UNC path, Join
125130
// shouldn't create a UNC path. See golang.org/issue/9167.

0 commit comments

Comments
 (0)