Skip to content
This repository has been archived by the owner on Sep 21, 2021. It is now read-only.

Commit

Permalink
Merge pull request #7 from stripe/carl-vendor
Browse files Browse the repository at this point in the history
Work around GO15VENDOREXPERIMENT for Go 1.5
  • Loading branch information
zenazn committed Mar 4, 2016
2 parents ea09240 + a7c2f1d commit 452e37e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
7 changes: 7 additions & 0 deletions package15.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build !go1.6

package main

import "os"

var useVendor = os.Getenv("GO15VENDOREXPERIMENT") == "1"
7 changes: 7 additions & 0 deletions package16.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build go1.6

package main

import "os"

var useVendor = os.Getenv("GO15VENDOREXPERIMENT") == "0" || os.Getenv("GO15VENDOREXPERIMENT") == ""
50 changes: 49 additions & 1 deletion safesql.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ package main
import (
"flag"
"fmt"
"go/build"
"go/types"
"os"
"path/filepath"
"strings"

"golang.org/x/tools/go/callgraph"
"golang.org/x/tools/go/loader"
Expand All @@ -32,7 +35,9 @@ func main() {
os.Exit(2)
}

c := loader.Config{}
c := loader.Config{
FindPackage: FindPackage,
}
c.Import("database/sql")
for _, pkg := range pkgs {
c.Import(pkg)
Expand Down Expand Up @@ -198,3 +203,46 @@ func FindNonConstCalls(cg *callgraph.Graph, qms []*QueryMethod) []ssa.CallInstru

return bad
}

// Deal with GO15VENDOREXPERIMENT
func FindPackage(ctxt *build.Context, path, dir string, mode build.ImportMode) (*build.Package, error) {
if !useVendor {
return ctxt.Import(path, dir, mode)
}

// First, walk up the filesystem from dir looking for vendor directories
var vendorDir string
for tmp := dir; vendorDir == "" && tmp != "/"; tmp = filepath.Dir(tmp) {
dname := filepath.Join(tmp, "vendor", filepath.FromSlash(path))
fd, err := os.Open(dname)
if err != nil {
continue
}
// Directories are only valid if they contain at least one file
// with suffix ".go" (this also ensures that the file descriptor
// we have is in fact a directory)
names, err := fd.Readdirnames(-1)
if err != nil {
continue
}
for _, name := range names {
if strings.HasSuffix(name, ".go") {
vendorDir = filepath.ToSlash(dname)
break
}
}
}

if vendorDir != "" {
pkg, err := ctxt.ImportDir(vendorDir, mode)
if err != nil {
return nil, err
}
// Go tries to derive a valid import path for the package, but
// it's wrong (it includes "/vendor/"). Overwrite it here.
pkg.ImportPath = path
return pkg, nil
}

return ctxt.Import(path, dir, mode)
}

0 comments on commit 452e37e

Please sign in to comment.