Skip to content

Commit

Permalink
Also materialize canonical names
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Jun 24, 2024
1 parent fd4d591 commit 0b6a5cc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
49 changes: 22 additions & 27 deletions go/runfiles/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (r *Runfiles) Open(name string) (fs.File, error) {
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrInvalid}
}
if name == "." {
return &rootDir{".", r, nil}, nil
return &rootDirFile{".", r, nil}, nil
}
split := strings.SplitN(name, "/", 2)
key := repoMappingKey{r.sourceRepo, split[0]}
Expand All @@ -55,16 +55,16 @@ func (r *Runfiles) Open(name string) (fs.File, error) {
return f, nil
}
// Return a special file for a repo dir that knows its apparent name.
return &repoDirFile{f.(fs.ReadDirFile), split[0]}, nil
return &renamedDirFile{f.(fs.ReadDirFile), split[0]}, nil
}

type rootDir struct {
type rootDirFile struct {
dirFile
rf *Runfiles
entries []fs.DirEntry
}

func (r *rootDir) ReadDir(n int) ([]fs.DirEntry, error) {
func (r *rootDirFile) ReadDir(n int) ([]fs.DirEntry, error) {
if err := r.initEntries(); err != nil {
return nil, err
}
Expand All @@ -79,7 +79,7 @@ func (r *rootDir) ReadDir(n int) ([]fs.DirEntry, error) {
return entries, nil
}

func (r *rootDir) initEntries() error {
func (r *rootDirFile) initEntries() error {
if r.entries != nil {
return nil
}
Expand All @@ -98,64 +98,59 @@ func (r *rootDir) initEntries() error {
if err != nil {
return err
}
rootDirFile := rootFile.(fs.ReadDirFile)
realEntries, err := rootDirFile.ReadDir(-1)
realDirFile := rootFile.(fs.ReadDirFile)
realEntries, err := realDirFile.ReadDir(-1)
if err != nil {
return err
}
for _, e := range realEntries {
if apparent, ok := canonicalToApparentName[e.Name()]; ok && e.IsDir() {
// A repo directory that is visible to the current source repo is materialized
// under its apparent name.
r.entries = append(r.entries, repoDirEntry{e, apparent})
} else if _, ok := allCanonicalNames[e.Name()]; ok && e.IsDir() {
// Skip repo directory that isn't visible to the current source repo.
} else {
// This is either a root symlink or Bzlmod is not enabled. In the latter case,
// allCanonicalNames only contains the name of the main repo. Since without
// Bzlmod there is no strict repo visibility, we materialize all directories.
r.entries = append(r.entries, e)
if apparent, ok := canonicalToApparentName[e.Name()]; ok && e.IsDir() && apparent != e.Name() {
// A repo directory that is visible to the current source repo is additionally
// materialized under its apparent name. We do not use a symlink as
// fs.WalkDir doesn't descend into symlinks.
r.entries = append(r.entries, renamedDirEntry{e, apparent})
}
r.entries = append(r.entries, e)
}
sort.Slice(r.entries, func(i, j int) bool {
return r.entries[i].Name() < r.entries[j].Name()
})
return nil
}

type repoDirFile struct {
type renamedDirFile struct {
fs.ReadDirFile
name string
}

func (r repoDirFile) Stat() (fs.FileInfo, error) {
func (r renamedDirFile) Stat() (fs.FileInfo, error) {
info, err := r.ReadDirFile.Stat()
if err != nil {
return nil, err
}
return repoDirFileInfo{info, r.name}, nil
return renamedDirInfo{info, r.name}, nil
}

type repoDirEntry struct {
type renamedDirEntry struct {
fs.DirEntry
name string
}

func (r repoDirEntry) Name() string { return r.name }
func (r repoDirEntry) Info() (fs.FileInfo, error) {
func (r renamedDirEntry) Name() string { return r.name }
func (r renamedDirEntry) Info() (fs.FileInfo, error) {
info, err := r.DirEntry.Info()
if err != nil {
return nil, err
}
return repoDirFileInfo{info, r.name}, nil
return renamedDirInfo{info, r.name}, nil
}

type repoDirFileInfo struct {
type renamedDirInfo struct {
fs.FileInfo
name string
}

func (r repoDirFileInfo) Name() string { return r.name }
func (r renamedDirInfo) Name() string { return r.name }

type emptyFile string

Expand Down
7 changes: 7 additions & 0 deletions go/tools/bazel/runfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ func Runfile(path string) (string, error) {
// FindBinary may be called from tests invoked with 'bazel test' and
// binaries invoked with 'bazel run'. On Windows,
// only tests invoked with 'bazel test' are supported.
//
// Deprecated: Use runfiles.Rlocation instead. The path argument can be
// obtained by passing `$(rlocationpath //pkg:name)` to the `go_*` target
// via `args`, `env` or `x_defs` (the latter is also available on `go_library`).
// This avoids hardcoding the package and name of the binary in the source code.
func FindBinary(pkg, name string) (string, bool) {
if err := ensureRunfiles(); err != nil {
return "", false
Expand Down Expand Up @@ -165,6 +170,8 @@ func FindBinary(pkg, name string) (string, bool) {
}

// A RunfileEntry describes a runfile.
//
// Deprecated: See comment on ListRunfiles.
type RunfileEntry struct {
// Workspace is the bazel workspace the file came from. For example,
// this would be "io_bazel_rules_go" for a file in rules_go.
Expand Down

0 comments on commit 0b6a5cc

Please sign in to comment.