Skip to content

Commit e5e73c1

Browse files
author
Bryan C. Mills
committed
zip: document isVendoredPackage false-positives
I had thought that the known bug in isVendoredPackage was strictly conservative, but it turns out not to be. Updates golang/go#37397 Updates golang/go#31562 Change-Id: I60f6062c41ec2d116766930f751d7df083535355 Reviewed-on: https://go-review.googlesource.com/c/mod/+/220657 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent b0a8b12 commit e5e73c1

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

zip/vendor_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2020 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package zip
6+
7+
import "testing"
8+
9+
func TestIsVendoredPackage(t *testing.T) {
10+
for _, tc := range []struct {
11+
path string
12+
want bool
13+
falsePositive bool // is this case affected by https://golang.org/issue/37397?
14+
}{
15+
{path: "vendor/foo/foo.go", want: true},
16+
{path: "pkg/vendor/foo/foo.go", want: true},
17+
{path: "longpackagename/vendor/foo/foo.go", want: true},
18+
19+
{path: "vendor/vendor.go", want: false},
20+
21+
// We ideally want these cases to be false, but they are affected by
22+
// https://golang.org/issue/37397, and if we fix them we will invalidate
23+
// existing module checksums. We must leave them as-is-for now.
24+
{path: "pkg/vendor/vendor.go", falsePositive: true},
25+
{path: "longpackagename/vendor/vendor.go", falsePositive: true},
26+
} {
27+
got := isVendoredPackage(tc.path)
28+
want := tc.want
29+
if tc.falsePositive {
30+
want = true
31+
}
32+
if got != want {
33+
t.Errorf("isVendoredPackage(%q) = %t; want %t", tc.path, got, tc.want)
34+
if tc.falsePositive {
35+
t.Logf("(Expected a false-positive due to https://golang.org/issue/37397.)")
36+
}
37+
}
38+
}
39+
}

zip/zip.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,12 @@ func (f dirFile) Path() string { return f.slashPath }
316316
func (f dirFile) Lstat() (os.FileInfo, error) { return f.info, nil }
317317
func (f dirFile) Open() (io.ReadCloser, error) { return os.Open(f.filePath) }
318318

319+
// isVendoredPackage attempts to report whether the given filename is contained
320+
// in a package whose import path contains (but does not end with) the component
321+
// "vendor".
322+
//
323+
// Unfortunately, isVendoredPackage reports false positives for files in any
324+
// non-top-level package whose import path ends in "vendor".
319325
func isVendoredPackage(name string) bool {
320326
var i int
321327
if strings.HasPrefix(name, "vendor/") {
@@ -325,15 +331,8 @@ func isVendoredPackage(name string) bool {
325331
//
326332
// i = j + len("/vendor/")
327333
//
328-
// (See https://golang.org/issue/31562.)
329-
//
330-
// Unfortunately, we can't fix it without invalidating checksums.
331-
// Fortunately, the error appears to be strictly conservative: we'll retain
332-
// vendored packages that we should have pruned, but we won't prune
333-
// non-vendored packages that we should have retained.
334-
//
335-
// Since this defect doesn't seem to break anything, it's not worth fixing
336-
// for now.
334+
// (See https://golang.org/issue/31562 and https://golang.org/issue/37397.)
335+
// Unfortunately, we can't fix it without invalidating module checksums.
337336
i += len("/vendor/")
338337
} else {
339338
return false

0 commit comments

Comments
 (0)