Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the subrepo FS when parsing a BUILD file #2961

Merged
merged 3 commits into from
Nov 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions src/parse/parse_step.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package parse

import (
"fmt"
"io"
iofs "io/fs"
"path/filepath"
"strings"

Expand Down Expand Up @@ -138,13 +140,28 @@ func parseSubrepoPackage(state *core.BuildState, pkg, subrepo string, dependent
return state.CheckArchSubrepo(dependent.Subrepo), nil
}

func openFile(fs iofs.FS, subrepoName, name string) (io.ReadSeekCloser, error) {
file, err := fs.Open(name)
if err != nil {
return nil, fmt.Errorf("failed to open build file: %v", err)
}

reader, ok := file.(io.ReadSeekCloser)
if !ok {
return nil, fmt.Errorf("opened file is not seekable: ///%v/%v", subrepoName, name)
}
return reader, nil
}

// parsePackage parses a BUILD file and adds the package to the build graph
func parsePackage(state *core.BuildState, label, dependent core.BuildLabel, subrepo *core.Subrepo, mode core.ParseMode) (*core.Package, error) {
packageName := label.PackageName
pkg := core.NewPackage(packageName)
pkg.Subrepo = subrepo
var fileSystem iofs.FS = fs.HostFS
if subrepo != nil {
pkg.SubrepoName = subrepo.Name
fileSystem = subrepo.FS
}
// Only load the internal package for the host repo's state
if state.ParentState == nil && packageName == InternalPackageName {
Expand All @@ -156,10 +173,15 @@ func parsePackage(state *core.BuildState, label, dependent core.BuildLabel, subr
return nil, fmt.Errorf("failed to parse internal package: %w", err)
}
} else {
filename, dir := buildFileName(state, label.PackageName, subrepo)
filename, dir := buildFileName(state, subrepo, fileSystem, label.PackageName)
if filename != "" {
file, err := openFile(fileSystem, pkg.SubrepoName, filename)
if err != nil {
return nil, err
}
defer file.Close()
pkg.Filename = filename
if err := state.Parser.ParseFile(pkg, &label, &dependent, mode, pkg.Filename); err != nil {
if err := state.Parser.ParseReader(pkg, file, &label, &dependent, mode); err != nil {
return nil, err
}
} else {
Expand All @@ -186,10 +208,9 @@ func parsePackage(state *core.BuildState, label, dependent core.BuildLabel, subr

// buildFileName returns the name of the BUILD file for a package, or the empty string if one
// doesn't exist. It also returns the directory that it looked in.
func buildFileName(state *core.BuildState, pkgName string, subrepo *core.Subrepo) (string, string) {
func buildFileName(state *core.BuildState, subrepo *core.Subrepo, fs iofs.FS, pkgName string) (string, string) {
config := state.Config
if subrepo != nil {
pkgName = subrepo.Dir(pkgName)
config = subrepo.State.Config
}
// Bazel defines targets in its "external" package from its WORKSPACE file.
Expand All @@ -199,7 +220,8 @@ func buildFileName(state *core.BuildState, pkgName string, subrepo *core.Subrepo
return "WORKSPACE", ""
}
for _, buildFileName := range config.Parse.BuildFileName {
if filename := filepath.Join(pkgName, buildFileName); fs.FileExists(filename) {
filename := filepath.Join(pkgName, buildFileName)
if info, err := iofs.Stat(fs, filename); err == nil && !info.IsDir() {
return filename, pkgName
}
}
Expand Down
Loading