From 1ff6e8bf030eb18d76b98380732498545aac2877 Mon Sep 17 00:00:00 2001 From: Mehul Kar Date: Mon, 24 Apr 2023 14:21:26 -0700 Subject: [PATCH 1/2] Use spaceID from turbo.json if available --- cli/internal/fs/turbo_json.go | 49 +++++++++++++++++++++-------------- cli/internal/run/run.go | 6 +++++ 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/cli/internal/fs/turbo_json.go b/cli/internal/fs/turbo_json.go index 71ef29d391da8..fa70eb640f3d1 100644 --- a/cli/internal/fs/turbo_json.go +++ b/cli/internal/fs/turbo_json.go @@ -22,6 +22,10 @@ const ( topologicalPipelineDelimiter = "^" ) +type SpaceConfig struct { + ID string `json:"id"` +} + type rawTurboJSON struct { // Global root filesystem dependencies GlobalDependencies []string `json:"globalDependencies,omitempty"` @@ -39,9 +43,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 { @@ -51,6 +58,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 @@ -60,9 +68,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 @@ -611,7 +618,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 @@ -642,21 +649,22 @@ 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 + tj.SpaceID = raw.Space.ID return nil } @@ -666,13 +674,14 @@ 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 + raw.Space = SpaceConfig{ID: tj.SpaceID} return json.Marshal(&raw) } diff --git a/cli/internal/run/run.go b/cli/internal/run/run.go index 2ac114121187d..fb5dda5cb5b61 100644 --- a/cli/internal/run/run.go +++ b/cli/internal/run/run.go @@ -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) From 0f560325cb0b1d0aa64ee6c3e6636232895824e5 Mon Sep 17 00:00:00 2001 From: Mehul Kar Date: Mon, 24 Apr 2023 15:44:18 -0700 Subject: [PATCH 2/2] omit --- cli/internal/fs/turbo_json.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/cli/internal/fs/turbo_json.go b/cli/internal/fs/turbo_json.go index fa70eb640f3d1..9887061cf3c9b 100644 --- a/cli/internal/fs/turbo_json.go +++ b/cli/internal/fs/turbo_json.go @@ -22,6 +22,8 @@ const ( topologicalPipelineDelimiter = "^" ) +// SpaceConfig is used to marshal and unmarshal the +// `experimentalSpaceId` field in a turbo.json type SpaceConfig struct { ID string `json:"id"` } @@ -45,7 +47,7 @@ type rawTurboJSON struct { Extends []string `json:"extends,omitempty"` // Configuration for the space - Space SpaceConfig `json:"experimentalSpaces,omitempty"` + Space *SpaceConfig `json:"experimentalSpaces,omitempty"` } // pristineTurboJSON is used when marshaling a TurboJSON object into a json string @@ -58,7 +60,7 @@ type pristineTurboJSON struct { Pipeline PristinePipeline `json:"pipeline"` RemoteCacheOptions RemoteCacheOptions `json:"remoteCache,omitempty"` Extends []string `json:"extends,omitempty"` - Space SpaceConfig `json:"experimentalSpaces,omitempty"` + Space *SpaceConfig `json:"experimentalSpaces,omitempty"` } // TurboJSON represents a turbo.json configuration file @@ -664,7 +666,10 @@ func (tj *TurboJSON) UnmarshalJSON(data []byte) error { tj.Pipeline = raw.Pipeline tj.RemoteCacheOptions = raw.RemoteCacheOptions tj.Extends = raw.Extends - tj.SpaceID = raw.Space.ID + // Directly to SpaceID, we don't need to keep the struct + if raw.Space != nil { + tj.SpaceID = raw.Space.ID + } return nil } @@ -681,7 +686,10 @@ func (tj *TurboJSON) MarshalJSON() ([]byte, error) { raw.GlobalPassthroughEnv = tj.GlobalPassthroughEnv raw.Pipeline = tj.Pipeline.Pristine() raw.RemoteCacheOptions = tj.RemoteCacheOptions - raw.Space = SpaceConfig{ID: tj.SpaceID} + + if tj.SpaceID != "" { + raw.Space = &SpaceConfig{ID: tj.SpaceID} + } return json.Marshal(&raw) }