Skip to content

Commit

Permalink
fix(gotool_extractor): when no global corpus is given, use package's …
Browse files Browse the repository at this point in the history
…corpus for each file (#3290)
  • Loading branch information
schroederc committed Dec 5, 2018
1 parent 9474b21 commit 6bc18f5
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion kythe/docs/schema/schema.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1939,7 +1939,7 @@ public class E {}
//- Pkg.node/kind package
package foo

//- File = vname("", _, _, "kythe/schema/example.go", "").node/kind file
//- File = vname("", _, _, "schema/example.go", "").node/kind file
//- File childof Pkg
--------------------------------------------------------------------------------

Expand Down
22 changes: 18 additions & 4 deletions kythe/go/extractors/golang/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,11 +435,25 @@ func (p *Package) addFiles(cu *apb.CompilationUnit, root, base string, names []s
path = filepath.Join(base, name)
}
trimmed := strings.TrimPrefix(path, root+"/")
vn := &spb.VName{
Corpus: p.ext.Corpus,
Path: trimmed,
}
if vn.Corpus == "" {
// If no global corpus is specified, use the package's corpus for each of
// its files. The package corpus is based on the rules in
// kythe/go/extractors/govname and is usually either the package's
// repository root (e.g. github.com/golang/protobuf) or a custom top-level
// domain (e.g. k8s.io).
vn.Corpus = p.VName.Corpus
components := strings.SplitN(vn.Path, string(filepath.Separator), 2)
vn.Path = strings.TrimPrefix(components[1], vn.Corpus+"/")
if components[0] != "src" {
vn.Root = components[0]
}
}
cu.RequiredInput = append(cu.RequiredInput, &apb.CompilationUnit_FileInput{
VName: &spb.VName{
Corpus: p.ext.Corpus,
Path: trimmed,
},
VName: vn,
Info: &apb.FileInfo{
Path: trimmed,
Digest: path, // provisional, until the file is loaded
Expand Down
7 changes: 4 additions & 3 deletions kythe/go/indexer/cmd/go_example/go_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,16 @@ func main() {
}

ctx := context.Background()
base, err := ioutil.TempDir("", "src")
base, err := ioutil.TempDir("", "go_example")
if err != nil {
log.Fatalf("Error creating temporary directory: %v", err)
}
defer os.RemoveAll(base) // best-effort
dir := filepath.Join(base, *importPath)
dir := filepath.Join(base, "src", *importPath)
if err := vfs.MkdirAll(ctx, dir, 0755); err != nil {
log.Fatalf("Error creating output directory: %v", err)
}
bc.GOPATH = base

for _, path := range flag.Args() {
if err := copyFile(ctx, path, dir); err != nil {
Expand All @@ -92,7 +93,7 @@ func main() {
if err := os.Chdir(base); err != nil {
log.Fatalf("Error changing directory to %q: %v", dir, err)
}
pkg, err := ext.ImportDir(*importPath)
pkg, err := ext.Locate(*importPath)
if err != nil {
log.Fatalf("Error locating package: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion kythe/go/indexer/testdata/basic/filenode.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Package file verifies that a file node is generated by the indexer, and that
// it has the correct relationship to its package.
//
//- File=vname("", _, "", "src/test/basic/filenode.go", "").node/kind file
//- File=vname("", "test", "", "basic/filenode.go", "").node/kind file
//- @file defines/binding Pkg
package file

Expand Down
2 changes: 1 addition & 1 deletion kythe/go/indexer/testdata/basic/packageinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import "fmt"
//- A childof PkgInit=vname("package.<init>@107", "test", _, "pkginit", "go")
//- PkgInit.node/kind function
//-
//- InitDef=vname(_, _, "", "src/test/pkginit/packageinit.go", "go")
//- InitDef=vname(_, "test", "", "pkginit/packageinit.go", "go")
//- defines PkgInit
//- InitDef.node/kind anchor
//- InitDef.loc/start 0
Expand Down
2 changes: 1 addition & 1 deletion kythe/go/indexer/testdata/basic/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ package pkg

//- Pkg=vname("package", "test", _, "pkg", "go").node/kind package
//- Pkg.doc/uri "http://godoc.org/test/pkg"
//- File=vname("", _, _, "src/test/pkg/packages.go", "").node/kind file
//- File=vname("", "test", _, "pkg/packages.go", "").node/kind file
//- File childof Pkg

0 comments on commit 6bc18f5

Please sign in to comment.