Skip to content

Commit

Permalink
Handle relative import paths. (#102)
Browse files Browse the repository at this point in the history
* Handle relative import paths.

* Rename relative-imports test data.
  • Loading branch information
melindalu authored and pmbethe09 committed Sep 16, 2016
1 parent cca9e30 commit 363e97c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
14 changes: 14 additions & 0 deletions go/tools/gazelle/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ func TestGenerator(t *testing.T) {
},
},
},
"lib/relativeimporter": {
{
Call: &bzl.CallExpr{
X: &bzl.LiteralExpr{Token: "go_library"},
},
},
},
"bin": {
{
Call: &bzl.CallExpr{
Expand Down Expand Up @@ -108,6 +115,13 @@ func TestGenerator(t *testing.T) {
stub.fixtures["lib/internal/deep"][1].Call,
},
},
{
Path: "lib/relativeimporter/BUILD",
Stmt: []bzl.Expr{
loadExpr("go_library"),
stub.fixtures["lib/relativeimporter"][0].Call,
},
},
{
Path: "bin/BUILD",
Stmt: []bzl.Expr{
Expand Down
7 changes: 6 additions & 1 deletion go/tools/gazelle/rules/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func NewGenerator(goPrefix string) Generator {
return &generator{
goPrefix: goPrefix,
r: resolverFunc(func(importpath, dir string) (label, error) {
if importpath != goPrefix && !strings.HasPrefix(importpath, goPrefix+"/") && !strings.HasPrefix(importpath, "./") {
if importpath != goPrefix && !strings.HasPrefix(importpath, goPrefix+"/") && !isRelative(importpath) {
return e.resolve(importpath, dir)
}
return r.resolve(importpath, dir)
Expand Down Expand Up @@ -202,3 +202,8 @@ func isStandard(importpath string) bool {
seg := strings.SplitN(importpath, "/", 2)[0]
return !strings.Contains(seg, ".")
}

// isRelative determines if an importpath is relative.
func isRelative(importpath string) bool {
return strings.HasPrefix(importpath, "./") || strings.HasPrefix(importpath, "..")
}
11 changes: 11 additions & 0 deletions go/tools/gazelle/rules/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ func TestGenerator(t *testing.T) {
)
`,
},
{
dir: "lib/relativeimporter",
want: `
go_library(
name = "go_default_library",
srcs = ["importer.go"],
visibility = ["//visibility:public"],
deps = ["//lib/internal/deep:go_default_library"],
)
`,
},
{
dir: "bin",
want: `
Expand Down
4 changes: 2 additions & 2 deletions go/tools/gazelle/rules/resolve_structured.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ type structuredResolver struct {
// resolve takes a Go importpath within the same respository as r.goPrefix
// and resolves it into a label in Bazel.
func (r structuredResolver) resolve(importpath, dir string) (label, error) {
if strings.HasPrefix(importpath, "./") {
importpath = path.Join(r.goPrefix, dir, importpath[2:])
if isRelative(importpath) {
importpath = path.Clean(path.Join(r.goPrefix, dir, importpath))
}

if importpath == r.goPrefix {
Expand Down
24 changes: 24 additions & 0 deletions go/tools/gazelle/testdata/repo/lib/relativeimporter/importer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* Copyright 2016 The Bazel Authors. All rights reserved.
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.
*/

package relativeimporter

import (
"log"

"../internal/deep"
)

func think() {
t := &deep.Thought{}
log.Println(t.Compute())
}

0 comments on commit 363e97c

Please sign in to comment.