Rediscover Go settings only when Go work needs them
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user