Skip to content

Commit ac57dc7

Browse files
feat: allow for specifying path of root turbo.json (#9087)
### Description Allow for setting the root `turbo.json` path via `--root-turbo-json=path/to/my.json` or `TURBO_ROOT_TURBO_JSON=path/to/my.json`. This option is not compatible with watch mode. Reviewing this PR should be done commit-wise, there's a lot of prefactoring before the final commit that implements the actual feature. Major changes as part of prefactor: - We parse `turbo.json` based on absolute paths instead of repo root relative ones. This allows us to read a `turbo.json` from wherever a user wants - Moved the reading of a trace generated `turbo.json` outside of the primary `turbo.json` reading logic and into `TaskAccess` - Changed ordering of how `config` layering happens instead of ordering them from least to most significant and always choosing a present value, we now go from most to least significant and only choose a value if we do not already have a value for it. This is necessary as we now have a config option (`TURBO_ROOT_TURBO_JSON`) that alters a less significant config source (`turbo.json`). - Move config sources out of `config.rs` - We are now lazy with evaluating our config sources. This allows us to change the path of the `turbo.json` we read after partial evaluation of config sources ### Testing Instructions Existing unit tests for the refactors. Manual testing of the traced config logic. Integration test for new feature. --------- Co-authored-by: Nicholas Yang <[email protected]>
1 parent e3468ce commit ac57dc7

File tree

19 files changed

+1460
-1171
lines changed

19 files changed

+1460
-1171
lines changed

crates/turborepo-lib/src/cli/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ pub struct Args {
222222
/// should be used.
223223
#[clap(long, global = true)]
224224
pub dangerously_disable_package_manager_check: bool,
225+
/// Use the `turbo.json` located at the provided path instead of one at the
226+
/// root of the repository.
227+
#[clap(long, global = true)]
228+
pub root_turbo_json: Option<Utf8PathBuf>,
225229
#[clap(flatten, next_help_heading = "Run Arguments")]
226230
// DO NOT MAKE THIS VISIBLE
227231
// This is explicitly set to None in `run`

crates/turborepo-lib/src/commands/link.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ mod test {
569569

570570
use anyhow::Result;
571571
use tempfile::{NamedTempFile, TempDir};
572-
use turbopath::{AbsoluteSystemPathBuf, AnchoredSystemPath};
572+
use turbopath::AbsoluteSystemPathBuf;
573573
use turborepo_ui::ColorConfig;
574574
use turborepo_vercel_api_mock::start_test_server;
575575

@@ -609,7 +609,7 @@ mod test {
609609
let port = port_scanner::request_open_port().unwrap();
610610
let handle = tokio::spawn(start_test_server(port));
611611
let mut base = CommandBase {
612-
global_config_path: Some(
612+
override_global_config_path: Some(
613613
AbsoluteSystemPathBuf::try_from(user_config_file.path().to_path_buf()).unwrap(),
614614
),
615615
repo_root: repo_root.clone(),
@@ -675,7 +675,7 @@ mod test {
675675
let port = port_scanner::request_open_port().unwrap();
676676
let handle = tokio::spawn(start_test_server(port));
677677
let mut base = CommandBase {
678-
global_config_path: Some(
678+
override_global_config_path: Some(
679679
AbsoluteSystemPathBuf::try_from(user_config_file.path().to_path_buf()).unwrap(),
680680
),
681681
repo_root: repo_root.clone(),
@@ -712,11 +712,7 @@ mod test {
712712

713713
// verify space id is added to turbo.json
714714
let turbo_json_contents = fs::read_to_string(&turbo_json_file).unwrap();
715-
let turbo_json = RawTurboJson::parse(
716-
&turbo_json_contents,
717-
AnchoredSystemPath::new("turbo.json").unwrap(),
718-
)
719-
.unwrap();
715+
let turbo_json = RawTurboJson::parse(&turbo_json_contents, "turbo.json").unwrap();
720716
assert_eq!(
721717
turbo_json.experimental_spaces.unwrap().id.unwrap(),
722718
turborepo_vercel_api_mock::EXPECTED_SPACE_ID.into()

crates/turborepo-lib/src/commands/mod.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ pub(crate) mod unlink;
3232
pub struct CommandBase {
3333
pub repo_root: AbsoluteSystemPathBuf,
3434
pub color_config: ColorConfig,
35-
#[cfg(test)]
36-
pub global_config_path: Option<AbsoluteSystemPathBuf>,
35+
pub override_global_config_path: Option<AbsoluteSystemPathBuf>,
3736
config: OnceCell<ConfigurationOptions>,
3837
args: Args,
3938
version: &'static str,
@@ -50,16 +49,14 @@ impl CommandBase {
5049
repo_root,
5150
color_config,
5251
args,
53-
#[cfg(test)]
54-
global_config_path: None,
52+
override_global_config_path: None,
5553
config: OnceCell::new(),
5654
version,
5755
}
5856
}
5957

60-
#[cfg(test)]
61-
pub fn with_global_config_path(mut self, path: AbsoluteSystemPathBuf) -> Self {
62-
self.global_config_path = Some(path);
58+
pub fn with_override_global_config_path(mut self, path: AbsoluteSystemPathBuf) -> Self {
59+
self.override_global_config_path = Some(path);
6360
self
6461
}
6562

@@ -119,6 +116,13 @@ impl CommandBase {
119116
.and_then(|args| args.cache_dir.clone())
120117
}),
121118
)
119+
.with_root_turbo_json_path(
120+
self.args
121+
.root_turbo_json
122+
.clone()
123+
.map(AbsoluteSystemPathBuf::from_cwd)
124+
.transpose()?,
125+
)
122126
.build()
123127
}
124128

@@ -129,7 +133,7 @@ impl CommandBase {
129133
// Getting all of the paths.
130134
fn global_config_path(&self) -> Result<AbsoluteSystemPathBuf, ConfigError> {
131135
#[cfg(test)]
132-
if let Some(global_config_path) = self.global_config_path.clone() {
136+
if let Some(global_config_path) = self.override_global_config_path.clone() {
133137
return Ok(global_config_path);
134138
}
135139

crates/turborepo-lib/src/commands/prune.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ impl<'a> Prune<'a> {
449449
Err(e) => return Err(e.into()),
450450
};
451451

452-
let turbo_json = RawTurboJson::parse(&turbo_json_contents, anchored_turbo_path)?;
452+
let turbo_json = RawTurboJson::parse(&turbo_json_contents, anchored_turbo_path.as_str())?;
453453

454454
let pruned_turbo_json = turbo_json.prune_tasks(workspaces);
455455
new_turbo_path.create_with_contents(serde_json::to_string_pretty(&pruned_turbo_json)?)?;

0 commit comments

Comments
 (0)