Skip to content

Commit f084829

Browse files
committed
Go: Only fail autobuilder if all projects cannot be extracted
1 parent 20836c7 commit f084829

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

go/extractor/cli/go-autobuilder/go-autobuilder.go

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ func installDependencies(workspace project.GoWorkspace) {
484484
}
485485

486486
// Run the extractor.
487-
func extract(workspace project.GoWorkspace) {
487+
func extract(workspace project.GoWorkspace) bool {
488488
extractor, err := util.GetExtractorPath()
489489
if err != nil {
490490
log.Fatalf("Could not determine path of extractor: %v.\n", err)
@@ -519,8 +519,11 @@ func extract(workspace project.GoWorkspace) {
519519
cmd.Stderr = os.Stderr
520520
err = cmd.Run()
521521
if err != nil {
522-
log.Fatalf("Extraction failed: %s\n", err.Error())
522+
log.Printf("Extraction failed for %s: %s\n", workspace.BaseDir, err.Error())
523+
return false
523524
}
525+
526+
return true
524527
}
525528

526529
// Build the project and run the extractor.
@@ -542,7 +545,8 @@ func installDependenciesAndBuild() {
542545
// Remove temporary extractor files (e.g. auto-generated go.mod files) when we are done
543546
defer project.RemoveTemporaryExtractorFiles()
544547

545-
for _, workspace := range workspaces {
548+
// Attempt to extract all workspaces; we will tolerate individual extraction failures here
549+
for i, workspace := range workspaces {
546550
goVersionInfo := workspace.RequiredGoVersion()
547551

548552
// This diagnostic is not required if the system Go version is 1.21 or greater, since the
@@ -606,7 +610,35 @@ func installDependenciesAndBuild() {
606610
}
607611
}
608612

609-
extract(workspace)
613+
workspaces[i].Extracted = extract(workspace)
614+
}
615+
616+
// Find all projects which could not be extracted successfully
617+
var unsuccessfulProjects = []string{}
618+
619+
for _, workspace := range workspaces {
620+
if !workspace.Extracted {
621+
unsuccessfulProjects = append(unsuccessfulProjects, workspace.BaseDir)
622+
}
623+
}
624+
625+
// If all projects could not be extracted successfully, we fail the overall extraction.
626+
if len(unsuccessfulProjects) == len(workspaces) {
627+
log.Fatalln("Extraction failed for all discovered Go projects.")
628+
}
629+
630+
// If there is at least one project that could not be extracted successfully,
631+
// emit a diagnostic that reports which projects we could not extract successfully.
632+
// We only consider this a warning, since there may be test projects etc. which
633+
// do not matter if they cannot be extracted successfully.
634+
if len(unsuccessfulProjects) > 0 {
635+
log.Printf(
636+
"Warning: extraction failed for %d project(s): %s\n",
637+
len(unsuccessfulProjects),
638+
strings.Join(unsuccessfulProjects, ", "))
639+
diagnostics.EmitExtractionFailedForProjects(unsuccessfulProjects)
640+
} else {
641+
log.Println("Success: extraction succeeded for all discovered projects.")
610642
}
611643
}
612644

go/extractor/diagnostics/diagnostics.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,3 +493,18 @@ func EmitNewerSystemGoRequired(requiredVersion string) {
493493
noLocation,
494494
)
495495
}
496+
497+
func EmitExtractionFailedForProjects(path []string) {
498+
emitDiagnostic(
499+
"go/autobuilder/extraction-failed-for-project",
500+
fmt.Sprintf("Unable to extract %d Go projects", len(path)),
501+
fmt.Sprintf(
502+
"The following %d Go project%s could not be extracted successfully:\n\n`%s`\n",
503+
len(path),
504+
plural(len(path), "", "s"),
505+
strings.Join(path, "`, `")),
506+
severityWarning,
507+
fullVisibility,
508+
noLocation,
509+
)
510+
}

go/extractor/project/project.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type GoWorkspace struct {
4444
Modules []*GoModule // A list of `go.mod` files
4545
DepMode DependencyInstallerMode // A value indicating how to install dependencies for this workspace
4646
ModMode ModMode // A value indicating which module mode to use for this workspace
47+
Extracted bool // A value indicating whether this workspace was extracted successfully
4748
}
4849

4950
// Represents a nullable version string.

0 commit comments

Comments
 (0)