Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/core/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ type BuildState struct {

// initOnce is used to control loading the subrepo .plzconfig
initOnce *sync.Once
// initErr is the result of loading the subrepo .plzconfig, returned from every call to Initialise.
initErr error
}

type isPreloadingKey struct{}
Expand All @@ -241,26 +243,27 @@ func (state *BuildState) Copy() *BuildState {
*ret = *state

ret.initOnce = new(sync.Once)
ret.initErr = nil
return ret
}

// Initialise will load the .plzconfig from the subrepo. We can only do this once the subrepo is built hence why
// it's not done up front. Once we have done that, we can initialise the parser for the subrepo.
func (state *BuildState) Initialise(subrepo *Subrepo) (err error) {
// The result is remembered, so if loading the config fails every caller gets the error, not just the first one.
func (state *BuildState) Initialise(subrepo *Subrepo) error {
state.initOnce.Do(func() {
// If we are the root repo, or a cross-compilation of that, we don't want to re-load the config files. That's
// handled for us already in plz.go
if state.CurrentSubrepo != "" {
state.RepoConfig = &Configuration{}
if err = readSubrepoConfig(state.RepoConfig, subrepo); err != nil {
return
}
if err = validateSubrepoNameAndPluginConfig(state.Config, state.RepoConfig, subrepo); err != nil {
if err := readSubrepoConfig(state.RepoConfig, subrepo); err != nil {
state.initErr = err
return
}
state.initErr = validateSubrepoNameAndPluginConfig(state.Config, state.RepoConfig, subrepo)
}
})
return
return state.initErr
}

// A stateProgress records various points of progress for a State.
Expand Down
18 changes: 18 additions & 0 deletions src/core/subrepo_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
package core

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/thought-machine/please/src/cli"
)

func TestDir(t *testing.T) {
s := &Subrepo{Name: "repo", Root: "plz-out/gen/repo"}
assert.Equal(t, "plz-out/gen/repo/package", s.Dir("package"))
}

func TestInitialiseReturnsConfigErrorEveryTime(t *testing.T) {
dir := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(dir, ".plzconfig"), []byte(":not valid config\n"), 0644))

state := NewDefaultBuildState().ForSubrepo("subrepo", false)
subrepo := NewSubrepo(state, "subrepo", dir, nil, cli.HostArch(), false)

// Several packages in the subrepo can ask for it to be initialised; all of them should see the error,
// not just the first one.
assert.Error(t, state.Initialise(subrepo))
assert.Error(t, state.Initialise(subrepo))
}
Loading