Prepare recovery tools for the host target

This commit is contained in:
2026-11-04 00:50:59 +00:00
parent 10028c4abb
commit d8ec9389e0
3 changed files with 53 additions and 12 deletions

View File

@@ -43,10 +43,16 @@ func TemporaryExecutable(path string) bool {
return err == nil
}
// RecoveryTool returns module-selected Go arguments for a later invocation.
// An empty result means this module does not select ReleaseKit. API-only modules
// RecoveryInvocation describes a module-selected Go tool invocation.
type RecoveryInvocation struct {
Directory string
Args []string
}
// RecoveryTool returns a module-selected Go invocation for later use.
// A nil result means this module does not select ReleaseKit. API-only modules
// receive a private module root that outlives launcher-owned bootstraps.
func RecoveryTool(ctx context.Context, moduleDir, recordDir string) ([]string, error) {
func RecoveryTool(ctx context.Context, moduleDir, recordDir string) (*RecoveryInvocation, error) {
path := filepath.Join(moduleDir, "go.mod")
data, err := os.ReadFile(path)
if err != nil {
@@ -58,7 +64,7 @@ func RecoveryTool(ctx context.Context, moduleDir, recordDir string) ([]string, e
}
for _, tool := range module.Tool {
if tool.Path == releaseKitCommand {
return []string{"-C", moduleDir, "tool", "-modfile=", releaseKitCommand}, nil
return &RecoveryInvocation{Directory: moduleDir, Args: []string{"tool", "-modfile=", releaseKitCommand}}, nil
}
}
// Ask Go about the selected module graph, including transitive dependencies.
@@ -140,15 +146,31 @@ func RecoveryTool(ctx context.Context, moduleDir, recordDir string) ([]string, e
return nil, err
}
// Prepare the private graph and sums before the later tool invocation.
environment := map[string]string{"GOWORK": "off", "GOTOOLCHAIN": "local"}
hostJSON, err := discover(ctx, directory, environment, "go", "env", "-json", "GOHOSTOS", "GOHOSTARCH")
if err != nil {
return nil, fmt.Errorf("prepare recovery tool: %w", err)
}
var host struct {
GOHOSTOS string
GOHOSTARCH string
}
if err := json.Unmarshal(hostJSON, &host); err != nil {
return nil, fmt.Errorf("prepare recovery tool: decode host target: %w", err)
}
if host.GOHOSTOS == "" || host.GOHOSTARCH == "" {
return nil, fmt.Errorf("prepare recovery tool: missing host target")
}
environment["GOOS"], environment["GOARCH"] = host.GOHOSTOS, host.GOHOSTARCH
captured := &combinedWriter{mode: captureStatus}
err = process.Run(ctx, process.Invocation{
Args: []string{"go", "list", "-mod=mod", "-modfile=", "-deps", releaseKitCommand},
Dir: directory, Env: map[string]string{"GOWORK": "off", "GOTOOLCHAIN": "local"},
Dir: directory, Env: environment,
Stdout: captured, Stderr: captured,
})
if err != nil {
return nil, fmt.Errorf("prepare recovery tool: %w: %s", err, diagnosticText(captured.take(), captured.omitted()))
}
complete = true
return []string{"-C", directory, "tool", "-modfile=", releaseKitCommand}, nil
return &RecoveryInvocation{Directory: directory, Args: []string{"tool", "-modfile=", releaseKitCommand}}, nil
}