Skip to content

Commit

Permalink
Use spaceID from turbo.json if available (#4687)
Browse files Browse the repository at this point in the history
The flag will takes precedence over config
  • Loading branch information
mehulkar committed Apr 25, 2023
1 parent 0235c88 commit 73ee47c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
57 changes: 37 additions & 20 deletions cli/internal/fs/turbo_json.go
Expand Up @@ -22,6 +22,12 @@ const (
topologicalPipelineDelimiter = "^"
)

// SpaceConfig is used to marshal and unmarshal the
// `experimentalSpaceId` field in a turbo.json
type SpaceConfig struct {
ID string `json:"id"`
}

type rawTurboJSON struct {
// Global root filesystem dependencies
GlobalDependencies []string `json:"globalDependencies,omitempty"`
Expand All @@ -39,9 +45,12 @@ type rawTurboJSON struct {

// Extends can be the name of another workspace
Extends []string `json:"extends,omitempty"`

// Configuration for the space
Space *SpaceConfig `json:"experimentalSpaces,omitempty"`
}

// pristineTurboJSON is used when marshaling a TurboJSON object into a turbo.json string
// pristineTurboJSON is used when marshaling a TurboJSON object into a json string
// Notably, it includes a PristinePipeline instead of the regular Pipeline. (i.e. TaskDefinition
// instead of BookkeepingTaskDefinition.)
type pristineTurboJSON struct {
Expand All @@ -51,6 +60,7 @@ type pristineTurboJSON struct {
Pipeline PristinePipeline `json:"pipeline"`
RemoteCacheOptions RemoteCacheOptions `json:"remoteCache,omitempty"`
Extends []string `json:"extends,omitempty"`
Space *SpaceConfig `json:"experimentalSpaces,omitempty"`
}

// TurboJSON represents a turbo.json configuration file
Expand All @@ -60,9 +70,8 @@ type TurboJSON struct {
GlobalPassthroughEnv []string
Pipeline Pipeline
RemoteCacheOptions RemoteCacheOptions

// A list of Workspace names
Extends []string
Extends []string // A list of Workspace names
SpaceID string
}

// RemoteCacheOptions is a struct for deserializing .remoteCache of configFile
Expand Down Expand Up @@ -611,7 +620,7 @@ func (c TaskDefinition) MarshalJSON() ([]byte, error) {
}

// UnmarshalJSON deserializes the contents of turbo.json into a TurboJSON struct
func (c *TurboJSON) UnmarshalJSON(data []byte) error {
func (tj *TurboJSON) UnmarshalJSON(data []byte) error {
raw := &rawTurboJSON{}
if err := json.Unmarshal(data, &raw); err != nil {
return err
Expand Down Expand Up @@ -642,21 +651,25 @@ func (c *TurboJSON) UnmarshalJSON(data []byte) error {
}

// turn the set into an array and assign to the TurboJSON struct fields.
c.GlobalEnv = envVarDependencies.UnsafeListOfStrings()
sort.Strings(c.GlobalEnv)
tj.GlobalEnv = envVarDependencies.UnsafeListOfStrings()
sort.Strings(tj.GlobalEnv)

if raw.GlobalPassthroughEnv != nil {
c.GlobalPassthroughEnv = envVarPassthroughs.UnsafeListOfStrings()
sort.Strings(c.GlobalPassthroughEnv)
tj.GlobalPassthroughEnv = envVarPassthroughs.UnsafeListOfStrings()
sort.Strings(tj.GlobalPassthroughEnv)
}

c.GlobalDeps = globalFileDependencies.UnsafeListOfStrings()
sort.Strings(c.GlobalDeps)
tj.GlobalDeps = globalFileDependencies.UnsafeListOfStrings()
sort.Strings(tj.GlobalDeps)

// copy these over, we don't need any changes here.
c.Pipeline = raw.Pipeline
c.RemoteCacheOptions = raw.RemoteCacheOptions
c.Extends = raw.Extends
tj.Pipeline = raw.Pipeline
tj.RemoteCacheOptions = raw.RemoteCacheOptions
tj.Extends = raw.Extends
// Directly to SpaceID, we don't need to keep the struct
if raw.Space != nil {
tj.SpaceID = raw.Space.ID
}

return nil
}
Expand All @@ -666,13 +679,17 @@ func (c *TurboJSON) UnmarshalJSON(data []byte) error {
// This is used by `turbo prune` to generate a pruned turbo.json
// and also by --summarize & --dry=json to serialize the known config
// into something we can print to screen
func (c *TurboJSON) MarshalJSON() ([]byte, error) {
func (tj *TurboJSON) MarshalJSON() ([]byte, error) {
raw := pristineTurboJSON{}
raw.GlobalDependencies = c.GlobalDeps
raw.GlobalEnv = c.GlobalEnv
raw.GlobalPassthroughEnv = c.GlobalPassthroughEnv
raw.Pipeline = c.Pipeline.Pristine()
raw.RemoteCacheOptions = c.RemoteCacheOptions
raw.GlobalDependencies = tj.GlobalDeps
raw.GlobalEnv = tj.GlobalEnv
raw.GlobalPassthroughEnv = tj.GlobalPassthroughEnv
raw.Pipeline = tj.Pipeline.Pristine()
raw.RemoteCacheOptions = tj.RemoteCacheOptions

if tj.SpaceID != "" {
raw.Space = &SpaceConfig{ID: tj.SpaceID}
}

return json.Marshal(&raw)
}
Expand Down
6 changes: 6 additions & 0 deletions cli/internal/run/run.go
Expand Up @@ -215,6 +215,12 @@ func (r *run) run(ctx gocontext.Context, targets []string) error {
// TODO: these values come from a config file, hopefully viper can help us merge these
r.opts.cacheOpts.RemoteCacheOpts = turboJSON.RemoteCacheOptions

// If a spaceID wasn't passed as a flag, read it from the turbo.json config.
// If that is not set either, we'll still end up with a blank string.
if r.opts.runOpts.ExperimentalSpaceID == "" {
r.opts.runOpts.ExperimentalSpaceID = turboJSON.SpaceID
}

pipeline := turboJSON.Pipeline
g.Pipeline = pipeline
scmInstance, err := scm.FromInRepo(r.base.RepoRoot)
Expand Down

0 comments on commit 73ee47c

Please sign in to comment.