Skip to content

Commit

Permalink
Introduce CallerRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Nov 30, 2022
1 parent 7f29ccb commit cf0beb8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
13 changes: 10 additions & 3 deletions go/runfiles/global.go
Expand Up @@ -25,7 +25,7 @@ import (
// the runfiles manifest maps s to an empty name (indicating an empty runfile
// not present in the filesystem), Rlocation returns an error that wraps ErrEmpty.
func Rlocation(path string) (string, error) {
return RlocationFrom(path, CurrentRepository(1))
return RlocationFrom(path, CallerRepository(1))
}

func RlocationFrom(path string, sourceRepo string) (string, error) {
Expand Down Expand Up @@ -54,10 +54,17 @@ var legacyExternalGeneratedFile = regexp.MustCompile(`^bazel-out[/][^/]+/bin/ext
var legacyExternalFile = regexp.MustCompile(`^external/([^/]+)/`)

// CurrentRepository returns the canonical name of the Bazel repository that
// contains the source file of the caller of this function.
func CurrentRepository() string {
return CallerRepository(1)
}

// CallerRepository returns the canonical name of the Bazel repository that
// contains the source file of the caller of this function with skip frames
// skipped. For example, passing 0 will return the name of the repository that
// contains the caller of CurrentRepository.
func CurrentRepository(skip int) string {
// contains the caller of CallerRepository and behaves identically to
// CurrentRepository.
func CallerRepository(skip int) string {
_, file, _, _ := runtime.Caller(skip + 1)
if match := legacyExternalGeneratedFile.FindStringSubmatch(file); match != nil {
return match[1]
Expand Down
2 changes: 1 addition & 1 deletion go/runfiles/runfiles.go
Expand Up @@ -83,7 +83,7 @@ func New(opts ...Option) (*Runfiles, error) {
}

if o.sourceRepo == noSourceRepoSentinel {
o.sourceRepo = SourceRepo(CurrentRepository(1))
o.sourceRepo = SourceRepo(CallerRepository(1))
}

if o.manifest == "" {
Expand Down
4 changes: 2 additions & 2 deletions tests/runfiles/runfiles_bazel_test.go
Expand Up @@ -66,7 +66,7 @@ import (
func PrintRepo() {
_, file, _, _ := runtime.Caller(0)
fmt.Printf("%s: '%s'\n", file, runfiles.CurrentRepository(0))
fmt.Printf("%s: '%s'\n", file, runfiles.CurrentRepository())
}
-- pkg/BUILD.bazel --
-- pkg/BUILD.bazel --
Expand Down Expand Up @@ -110,7 +110,7 @@ import (
func PrintRepo() {
_, file, _, _ := runtime.Caller(0)
fmt.Printf("%s: '%s'\n", file, runfiles.CurrentRepository(0))
fmt.Printf("%s: '%s'\n", file, runfiles.CurrentRepository())
}
-- BUILD.bazel --
load("@io_bazel_rules_go//go:def.bzl", "go_binary")
Expand Down

0 comments on commit cf0beb8

Please sign in to comment.