Rediscover Go settings only when Go work needs them

This commit is contained in:
2026-10-10 14:11:48 +00:00
parent 9333573cd8
commit c8eb64adec
8 changed files with 104 additions and 48 deletions

View File

@@ -53,13 +53,9 @@ func Check(ctx context.Context, request Request) (Candidate, error) {
}
if request.AfterValidate != nil {
callbackErr := request.AfterValidate(ctx, captured, request.ToolOwner)
refreshErr := request.ToolOwner.Refresh(ctx)
if callbackErr != nil {
return captured, fmt.Errorf("after candidate validation: %w", callbackErr)
}
if refreshErr != nil {
return captured, fmt.Errorf("refresh Go settings after candidate callback: %w", refreshErr)
}
}
if err := Recheck(ctx, captured, ""); err != nil {
return captured, err
@@ -143,13 +139,9 @@ func Validate(ctx context.Context, request Request, captured Candidate) error {
}
if request.AdditionalChecks != nil {
callbackErr := request.AdditionalChecks(ctx, captured)
refreshErr := owner.Refresh(ctx)
if callbackErr != nil {
return fmt.Errorf("validate candidate %s: %w", captured.Commit, callbackErr)
}
if refreshErr != nil {
return fmt.Errorf("refresh Go settings after additional checks: %w", refreshErr)
}
}
return nil
}

View File

@@ -304,6 +304,28 @@ func TestCheckRefreshesGoSettingsAfterAdditionalChecks(t *testing.T) {
}
}
func TestCheckNeedsNoGoDiscoveryAfterFinalCallback(t *testing.T) {
repository := newRepository(t, "# Example Product v1.2.3\n\nRelease note.\n")
gitPath, err := exec.LookPath("git")
if err != nil {
t.Fatal(err)
}
gitOnly := t.TempDir()
if err := os.Symlink(gitPath, filepath.Join(gitOnly, "git")); err != nil {
t.Fatal(err)
}
_, err = Check(context.Background(), Request{
RepositoryDir: repository, Product: "Example Product", Version: testVersion,
AfterValidate: func(context.Context, Candidate, *gotool.Owner) error {
t.Setenv("PATH", gitOnly)
return nil
},
})
if err != nil {
t.Fatalf("completed Go work was rediscovered after final callback: %v", err)
}
}
func TestCheckSharesGoDiscoveryWithAfterValidateCallback(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("the controlled Go command fixture uses a POSIX shell")

View File

@@ -320,9 +320,6 @@ func TestOwnerReusesSettingsUntilInheritedEnvironmentChanges(t *testing.T) {
cache := filepath.Join(t.TempDir(), "changed-cache")
t.Setenv("GOCACHE", cache)
if err := owner.Refresh(context.Background()); err != nil {
t.Fatal(err)
}
third, err := owner.Acquire(context.Background())
if err != nil {
t.Fatal(err)
@@ -332,6 +329,24 @@ func TestOwnerReusesSettingsUntilInheritedEnvironmentChanges(t *testing.T) {
}
}
func TestOwnerAcceptsGoConfigurationWithoutReadableSnapshot(t *testing.T) {
clearGoSettings(t)
t.Setenv("GOENV", t.TempDir())
directory := t.TempDir()
if _, err := New(context.Background(), directory); err != nil {
t.Fatalf("underlying Go discovery rejected fixture: %v", err)
}
owner := NewOwner(directory)
first, err := owner.Acquire(context.Background())
if err != nil {
t.Fatalf("owner added a snapshot prerequisite: %v", err)
}
second, err := owner.Acquire(context.Background())
if err != nil || first == second {
t.Fatalf("unavailable snapshot should rediscover on demand: first=%p second=%p error=%v", first, second, err)
}
}
func TestOwnerRefreshesWhenPersistentSettingsChange(t *testing.T) {
clearGoSettings(t)
firstCache := filepath.Join(t.TempDir(), "first-cache")
@@ -355,9 +370,6 @@ func TestOwnerRefreshesWhenPersistentSettingsChange(t *testing.T) {
t.Fatalf("initial GOCACHE = %q, want %q", first.settings["GOCACHE"], firstCache)
}
writeSettings(secondCache)
if err := owner.Refresh(context.Background()); err != nil {
t.Fatal(err)
}
second, err := owner.Acquire(context.Background())
if err != nil {
t.Fatal(err)

View File

@@ -4,7 +4,6 @@ import (
"bytes"
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
@@ -13,12 +12,13 @@ import (
// Owner lazily discovers and reuses one Go tool while its inherited
// environment and persistent settings file remain unchanged.
type Owner struct {
directory string
tool *Tool
env map[string]string
file string
contents []byte
fileFound bool
directory string
tool *Tool
env map[string]string
file string
contents []byte
fileFound bool
snapshotAvailable bool
}
// NewOwner creates an operation-local Go tool owner without running Go.
@@ -29,7 +29,7 @@ func NewOwner(directory string) *Owner {
// Acquire returns the operation's Go tool, rediscovering settings when their
// process or persistent inputs have changed.
func (owner *Owner) Acquire(ctx context.Context) (*Tool, error) {
if owner.tool != nil {
if owner.tool != nil && owner.snapshotAvailable {
changed, err := owner.inputsChanged()
if err == nil && !changed {
return owner.tool, nil
@@ -41,38 +41,24 @@ func (owner *Owner) Acquire(ctx context.Context) (*Tool, error) {
return owner.tool, nil
}
// Refresh checks the owner's inputs after a consumer callback and rediscovers
// settings when the callback changed the environment or persistent file.
func (owner *Owner) Refresh(ctx context.Context) error {
if owner.tool == nil {
return nil
}
changed, err := owner.inputsChanged()
if err == nil && !changed {
return nil
}
return owner.discover(ctx)
}
func (owner *Owner) discover(ctx context.Context) error {
environment := inheritedEnvironment()
tool, err := New(ctx, owner.directory)
if err != nil {
return err
}
settingsFile, err := resolveSettingsFile(owner.directory, tool.settingsFile)
if err != nil {
return err
}
contents, found, err := readSettingsFile(settingsFile)
if err != nil {
return fmt.Errorf("read Go settings file %q: %w", settingsFile, err)
settingsFile, snapshotErr := resolveSettingsFile(owner.directory, tool.settingsFile)
var contents []byte
var found bool
if snapshotErr == nil {
contents, found, snapshotErr = readSettingsFile(settingsFile)
}
owner.tool = tool
owner.env = environment
owner.file = settingsFile
owner.contents = contents
owner.fileFound = found
owner.snapshotAvailable = snapshotErr == nil
return nil
}

View File

@@ -127,13 +127,9 @@ func Publish(ctx context.Context, request Request) (Result, error) {
}
if request.AfterValidate != nil {
callbackErr := request.AfterValidate(ctx, captured, checks.ToolOwner)
refreshErr := checks.ToolOwner.Refresh(ctx)
if callbackErr != nil {
return result, fmt.Errorf("after candidate validation: %w", callbackErr)
}
if refreshErr != nil {
return result, fmt.Errorf("refresh Go settings after candidate callback: %w", refreshErr)
}
}
if err := candidate.Recheck(ctx, captured, ""); err != nil {
return result, fmt.Errorf("post-validation local guard: %w", err)