Skip to content

Commit

Permalink
Merge pull request #15789 from github/mbg/go/autobuilder-review-comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mbg authored Mar 4, 2024
2 parents 73fe20f + 9b5bf51 commit e5de4f2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 33 deletions.
12 changes: 5 additions & 7 deletions go/extractor/cli/go-autobuilder/go-autobuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,9 @@ func installDependenciesAndBuild() {
}
}

// Track all projects which could not be extracted successfully
var unsuccessfulProjects = []string{}

// Attempt to extract all workspaces; we will tolerate individual extraction failures here
for i, workspace := range workspaces {
goVersionInfo := workspace.RequiredGoVersion()
Expand Down Expand Up @@ -639,13 +642,8 @@ func installDependenciesAndBuild() {
}

workspaces[i].Extracted = extract(workspace)
}

// Find all projects which could not be extracted successfully
var unsuccessfulProjects = []string{}

for _, workspace := range workspaces {
if !workspace.Extracted {
if !workspaces[i].Extracted {
unsuccessfulProjects = append(unsuccessfulProjects, workspace.BaseDir)
}
}
Expand All @@ -666,7 +664,7 @@ func installDependenciesAndBuild() {
strings.Join(unsuccessfulProjects, ", "))
diagnostics.EmitExtractionFailedForProjects(unsuccessfulProjects)
} else {
log.Println("Success: extraction succeeded for all discovered projects.")
log.Printf("Success: extraction succeeded for all %d discovered project(s).\n", len(workspaces))
}
}

Expand Down
20 changes: 15 additions & 5 deletions go/extractor/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ var filesToRemove []string = []string{}

// Try to initialize a go.mod file for projects that do not already have one.
func InitGoModForLegacyProject(path string) {
log.Printf("Project appears to be a legacy Go project, attempting to initialize go.mod in %s\n", path)
log.Printf("The code in %s seems to be missing a go.mod file. Attempting to initialize one...\n", path)

modInit := toolchain.InitModule(path)

Expand Down Expand Up @@ -217,9 +217,13 @@ func discoverWorkspace(workFilePath string) GoWorkspace {
if err != nil {
// We couldn't read the `go.work` file for some reason; let's try to find `go.mod` files ourselves
log.Printf("Unable to read %s, falling back to finding `go.mod` files manually:\n%s\n", workFilePath, err.Error())

goModFilePaths := findGoModFiles(baseDir)
log.Printf("Discovered the following Go modules in %s:\n%s\n", baseDir, strings.Join(goModFilePaths, "\n"))

return GoWorkspace{
BaseDir: baseDir,
Modules: LoadGoModules(findGoModFiles(baseDir)),
Modules: LoadGoModules(goModFilePaths),
DepMode: GoGetWithModules,
ModMode: getModMode(GoGetWithModules, baseDir),
}
Expand All @@ -230,9 +234,13 @@ func discoverWorkspace(workFilePath string) GoWorkspace {
if err != nil {
// The `go.work` file couldn't be parsed for some reason; let's try to find `go.mod` files ourselves
log.Printf("Unable to parse %s, falling back to finding `go.mod` files manually:\n%s\n", workFilePath, err.Error())

goModFilePaths := findGoModFiles(baseDir)
log.Printf("Discovered the following Go modules in %s:\n%s\n", baseDir, strings.Join(goModFilePaths, "\n"))

return GoWorkspace{
BaseDir: baseDir,
Modules: LoadGoModules(findGoModFiles(baseDir)),
Modules: LoadGoModules(goModFilePaths),
DepMode: GoGetWithModules,
ModMode: getModMode(GoGetWithModules, baseDir),
}
Expand Down Expand Up @@ -471,8 +479,10 @@ func getBuildRoots(emitDiagnostics bool) (goWorkspaces []GoWorkspace, totalModul

// Finds Go workspaces in the current working directory.
func GetWorkspaceInfo(emitDiagnostics bool) []GoWorkspace {
bazelPaths := util.FindAllFilesWithName(".", "BUILD", "vendor")
bazelPaths = append(bazelPaths, util.FindAllFilesWithName(".", "BUILD.bazel", "vendor")...)
bazelPaths := slices.Concat(
util.FindAllFilesWithName(".", "BUILD", "vendor"),
util.FindAllFilesWithName(".", "BUILD.bazel", "vendor"),
)
if len(bazelPaths) > 0 {
// currently not supported
if emitDiagnostics {
Expand Down
22 changes: 1 addition & 21 deletions go/extractor/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,27 +320,7 @@ func FindAllFilesWithName(root string, name string, dirsToSkip ...string) []stri
return paths
}

// Determines whether there are any Go source files in locations which do not have a Go.mod
// file in the same directory or higher up in the file hierarchy, relative to the `root`.
func AnyGoFilesOutsideDirs(root string, dirsToSkip ...string) bool {
found := false
filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() && slices.Contains(dirsToSkip, path) {
return filepath.SkipDir
}
if filepath.Ext(d.Name()) == ".go" {
found = true
return filepath.SkipAll
}
return nil
})
return found
}

// Returns an array of any Go source files in locations which do not have a Go.mod
// Returns an array of any Go source files in locations which do not have a `go.mod`
// file in the same directory or higher up in the file hierarchy, relative to the `root`.
func GoFilesOutsideDirs(root string, dirsToSkip ...string) []string {
result := []string{}
Expand Down

0 comments on commit e5de4f2

Please sign in to comment.