From 8574c38f81c85561cb3d2d6e3c2255feded550ce Mon Sep 17 00:00:00 2001 From: Christoph Blecker Date: Thu, 25 Oct 2018 11:27:53 -0700 Subject: [PATCH] Update srcimporter.go to golang/go@5f0a9ba Also move to third_party/ as it's copied code. --- test/typecheck/BUILD | 7 +- test/typecheck/main.go | 2 +- third_party/BUILD | 1 + .../go-srcimporter}/BUILD | 4 +- third_party/go-srcimporter/LICENSE | 27 +++++++ third_party/go-srcimporter/OWNERS | 6 ++ third_party/go-srcimporter/README.kubernetes | 4 + .../go-srcimporter}/srcimporter.go | 77 +++++-------------- 8 files changed, 63 insertions(+), 65 deletions(-) rename {test/typecheck/srcimporter => third_party/go-srcimporter}/BUILD (83%) create mode 100644 third_party/go-srcimporter/LICENSE create mode 100644 third_party/go-srcimporter/OWNERS create mode 100644 third_party/go-srcimporter/README.kubernetes rename {test/typecheck/srcimporter => third_party/go-srcimporter}/srcimporter.go (72%) diff --git a/test/typecheck/BUILD b/test/typecheck/BUILD index acf8b7885ba..a7b35fd050d 100644 --- a/test/typecheck/BUILD +++ b/test/typecheck/BUILD @@ -17,7 +17,7 @@ go_library( srcs = ["main.go"], importpath = "k8s.io/kubernetes/test/typecheck", deps = [ - "//test/typecheck/srcimporter:go_default_library", + "//third_party/go-srcimporter:go_default_library", "//vendor/golang.org/x/crypto/ssh/terminal:go_default_library", ], ) @@ -31,10 +31,7 @@ filegroup( filegroup( name = "all-srcs", - srcs = [ - ":package-srcs", - "//test/typecheck/srcimporter:all-srcs", - ], + srcs = [":package-srcs"], tags = ["automanaged"], ) diff --git a/test/typecheck/main.go b/test/typecheck/main.go index 80e5b90f3f0..b6c55506af8 100644 --- a/test/typecheck/main.go +++ b/test/typecheck/main.go @@ -37,7 +37,7 @@ import ( "golang.org/x/crypto/ssh/terminal" - "k8s.io/kubernetes/test/typecheck/srcimporter" + srcimporter "k8s.io/kubernetes/third_party/go-srcimporter" ) var ( diff --git a/third_party/BUILD b/third_party/BUILD index cd376959766..ce2dc0c079b 100644 --- a/third_party/BUILD +++ b/third_party/BUILD @@ -26,6 +26,7 @@ filegroup( "//third_party/forked/golang/reflect:all-srcs", "//third_party/forked/golang/template:all-srcs", "//third_party/forked/gonum/graph:all-srcs", + "//third_party/go-srcimporter:all-srcs", ], tags = ["automanaged"], ) diff --git a/test/typecheck/srcimporter/BUILD b/third_party/go-srcimporter/BUILD similarity index 83% rename from test/typecheck/srcimporter/BUILD rename to third_party/go-srcimporter/BUILD index fbc8f7b4b89..9ac17a8f3c5 100644 --- a/test/typecheck/srcimporter/BUILD +++ b/third_party/go-srcimporter/BUILD @@ -1,9 +1,11 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") +licenses(["notice"]) + go_library( name = "go_default_library", srcs = ["srcimporter.go"], - importpath = "k8s.io/kubernetes/test/typecheck/srcimporter", + importpath = "k8s.io/kubernetes/third_party/go-srcimporter", visibility = ["//visibility:public"], ) diff --git a/third_party/go-srcimporter/LICENSE b/third_party/go-srcimporter/LICENSE new file mode 100644 index 00000000000..6a66aea5eaf --- /dev/null +++ b/third_party/go-srcimporter/LICENSE @@ -0,0 +1,27 @@ +Copyright (c) 2009 The Go Authors. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google Inc. nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/third_party/go-srcimporter/OWNERS b/third_party/go-srcimporter/OWNERS new file mode 100644 index 00000000000..e40fb6bb555 --- /dev/null +++ b/third_party/go-srcimporter/OWNERS @@ -0,0 +1,6 @@ +reviewers: + - cblecker + - rmmh +approvers: + - cblecker + - rmmh diff --git a/third_party/go-srcimporter/README.kubernetes b/third_party/go-srcimporter/README.kubernetes new file mode 100644 index 00000000000..81f0a53023f --- /dev/null +++ b/third_party/go-srcimporter/README.kubernetes @@ -0,0 +1,4 @@ +Files copied from https://github.com/golang/go/blob/5f0a9ba1342674b3209c13035b5aa39a96dbd80c/src/go/internal/srcimporter/srcimporter.go at commit 5f0a9ba1342674b3209c13035b5aa39a96dbd80c. + +Used in k8s.io/kubernetes/test/typecheck. Need to copy here, as it's marked as a go/internal library. +Only modification is to remove the go/internal/srcimporter import alias. diff --git a/test/typecheck/srcimporter/srcimporter.go b/third_party/go-srcimporter/srcimporter.go similarity index 72% rename from test/typecheck/srcimporter/srcimporter.go rename to third_party/go-srcimporter/srcimporter.go index 88693eec5ab..3743e078b93 100644 --- a/test/typecheck/srcimporter/srcimporter.go +++ b/third_party/go-srcimporter/srcimporter.go @@ -1,21 +1,3 @@ -/* -Copyright 2018 The Kubernetes Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -// Forked from go's go/internal/srcimporter - // Copyright 2017 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. @@ -31,6 +13,8 @@ import ( "go/parser" "go/token" "go/types" + "io" + "os" "path/filepath" "sync" ) @@ -43,7 +27,7 @@ type Importer struct { packages map[string]*types.Package } -// New returns a new Importer for the given context, file set, and map +// NewImporter returns a new Importer for the given context, file set, and map // of packages. The context is used to resolve import paths to package paths, // and identifying the files belonging to the package. If the context provides // non-nil file system functions, they are used instead of the regular package @@ -62,9 +46,9 @@ func New(ctxt *build.Context, fset *token.FileSet, packages map[string]*types.Pa // for a package that is in the process of being imported. var importing types.Package -// Import (path) is a shortcut for ImportFrom(path, "", 0). +// Import(path) is a shortcut for ImportFrom(path, ".", 0). func (p *Importer) Import(path string) (*types.Package, error) { - return p.ImportFrom(path, "", 0) + return p.ImportFrom(path, ".", 0) // use "." rather than "" (see issue #24441) } // ImportFrom imports the package with the given import path resolved from the given srcDir, @@ -78,23 +62,10 @@ func (p *Importer) ImportFrom(path, srcDir string, mode types.ImportMode) (*type panic("non-zero import mode") } - // determine package path (do vendor resolution) - var bp *build.Package - var err error - switch { - default: - if abs, err := p.absPath(srcDir); err == nil { // see issue #14282 - srcDir = abs - } - bp, err = p.ctxt.Import(path, srcDir, build.FindOnly) - - case build.IsLocalImport(path): - // "./x" -> "srcDir/x" - bp, err = p.ctxt.ImportDir(filepath.Join(srcDir, path), build.FindOnly) - - case p.isAbsPath(path): - return nil, fmt.Errorf("invalid absolute import path %q", path) + if abs, err := p.absPath(srcDir); err == nil { // see issue #14282 + srcDir = abs } + bp, err := p.ctxt.Import(path, srcDir, 0) if err != nil { return nil, err // err may be *build.NoGoError - return as is } @@ -131,11 +102,6 @@ func (p *Importer) ImportFrom(path, srcDir string, mode types.ImportMode) (*type } }() - // collect package files - bp, err = p.ctxt.ImportDir(bp.Dir, 0) - if err != nil { - return nil, err // err may be *build.NoGoError - return as is - } var filenames []string filenames = append(filenames, bp.GoFiles...) filenames = append(filenames, bp.CgoFiles...) @@ -180,7 +146,11 @@ func (p *Importer) ImportFrom(path, srcDir string, mode types.ImportMode) (*type } func (p *Importer) parseFiles(dir string, filenames []string) ([]*ast.File, error) { - open := p.ctxt.OpenFile // possibly nil + // use build.Context's OpenFile if there is one + open := p.ctxt.OpenFile + if open == nil { + open = func(name string) (io.ReadCloser, error) { return os.Open(name) } + } files := make([]*ast.File, len(filenames)) errors := make([]error, len(filenames)) @@ -190,22 +160,13 @@ func (p *Importer) parseFiles(dir string, filenames []string) ([]*ast.File, erro for i, filename := range filenames { go func(i int, filepath string) { defer wg.Done() - if open != nil { - src, err := open(filepath) - if err != nil { - errors[i] = fmt.Errorf("opening package file %s failed (%v)", filepath, err) - return - } - files[i], errors[i] = parser.ParseFile(p.fset, filepath, src, 0) - src.Close() // ignore Close error - parsing may have succeeded which is all we need - } else { - // Special-case when ctxt doesn't provide a custom OpenFile and use the - // parser's file reading mechanism directly. This appears to be quite a - // bit faster than opening the file and providing an io.ReaderCloser in - // both cases. - // TODO(gri) investigate performance difference (issue #19281) - files[i], errors[i] = parser.ParseFile(p.fset, filepath, nil, 0) + src, err := open(filepath) + if err != nil { + errors[i] = err // open provides operation and filename in error + return } + files[i], errors[i] = parser.ParseFile(p.fset, filepath, src, 0) + src.Close() // ignore Close error - parsing may have succeeded which is all we need }(i, p.joinPath(dir, filename)) } wg.Wait()