Skip to content

Commit

Permalink
feat: allow for specifying path of root turbo.json (#9087)
Browse files Browse the repository at this point in the history
### 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]>
  • Loading branch information
chris-olszewski and NicholasLYang authored Sep 3, 2024
1 parent e3468ce commit ac57dc7
Show file tree
Hide file tree
Showing 19 changed files with 1,460 additions and 1,171 deletions.
4 changes: 4 additions & 0 deletions crates/turborepo-lib/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ pub struct Args {
/// should be used.
#[clap(long, global = true)]
pub dangerously_disable_package_manager_check: bool,
/// Use the `turbo.json` located at the provided path instead of one at the
/// root of the repository.
#[clap(long, global = true)]
pub root_turbo_json: Option<Utf8PathBuf>,
#[clap(flatten, next_help_heading = "Run Arguments")]
// DO NOT MAKE THIS VISIBLE
// This is explicitly set to None in `run`
Expand Down
12 changes: 4 additions & 8 deletions crates/turborepo-lib/src/commands/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ mod test {

use anyhow::Result;
use tempfile::{NamedTempFile, TempDir};
use turbopath::{AbsoluteSystemPathBuf, AnchoredSystemPath};
use turbopath::AbsoluteSystemPathBuf;
use turborepo_ui::ColorConfig;
use turborepo_vercel_api_mock::start_test_server;

Expand Down Expand Up @@ -609,7 +609,7 @@ mod test {
let port = port_scanner::request_open_port().unwrap();
let handle = tokio::spawn(start_test_server(port));
let mut base = CommandBase {
global_config_path: Some(
override_global_config_path: Some(
AbsoluteSystemPathBuf::try_from(user_config_file.path().to_path_buf()).unwrap(),
),
repo_root: repo_root.clone(),
Expand Down Expand Up @@ -675,7 +675,7 @@ mod test {
let port = port_scanner::request_open_port().unwrap();
let handle = tokio::spawn(start_test_server(port));
let mut base = CommandBase {
global_config_path: Some(
override_global_config_path: Some(
AbsoluteSystemPathBuf::try_from(user_config_file.path().to_path_buf()).unwrap(),
),
repo_root: repo_root.clone(),
Expand Down Expand Up @@ -712,11 +712,7 @@ mod test {

// verify space id is added to turbo.json
let turbo_json_contents = fs::read_to_string(&turbo_json_file).unwrap();
let turbo_json = RawTurboJson::parse(
&turbo_json_contents,
AnchoredSystemPath::new("turbo.json").unwrap(),
)
.unwrap();
let turbo_json = RawTurboJson::parse(&turbo_json_contents, "turbo.json").unwrap();
assert_eq!(
turbo_json.experimental_spaces.unwrap().id.unwrap(),
turborepo_vercel_api_mock::EXPECTED_SPACE_ID.into()
Expand Down
20 changes: 12 additions & 8 deletions crates/turborepo-lib/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ pub(crate) mod unlink;
pub struct CommandBase {
pub repo_root: AbsoluteSystemPathBuf,
pub color_config: ColorConfig,
#[cfg(test)]
pub global_config_path: Option<AbsoluteSystemPathBuf>,
pub override_global_config_path: Option<AbsoluteSystemPathBuf>,
config: OnceCell<ConfigurationOptions>,
args: Args,
version: &'static str,
Expand All @@ -50,16 +49,14 @@ impl CommandBase {
repo_root,
color_config,
args,
#[cfg(test)]
global_config_path: None,
override_global_config_path: None,
config: OnceCell::new(),
version,
}
}

#[cfg(test)]
pub fn with_global_config_path(mut self, path: AbsoluteSystemPathBuf) -> Self {
self.global_config_path = Some(path);
pub fn with_override_global_config_path(mut self, path: AbsoluteSystemPathBuf) -> Self {
self.override_global_config_path = Some(path);
self
}

Expand Down Expand Up @@ -119,6 +116,13 @@ impl CommandBase {
.and_then(|args| args.cache_dir.clone())
}),
)
.with_root_turbo_json_path(
self.args
.root_turbo_json
.clone()
.map(AbsoluteSystemPathBuf::from_cwd)
.transpose()?,
)
.build()
}

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

Expand Down
2 changes: 1 addition & 1 deletion crates/turborepo-lib/src/commands/prune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ impl<'a> Prune<'a> {
Err(e) => return Err(e.into()),
};

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

let pruned_turbo_json = turbo_json.prune_tasks(workspaces);
new_turbo_path.create_with_contents(serde_json::to_string_pretty(&pruned_turbo_json)?)?;
Expand Down
Loading

0 comments on commit ac57dc7

Please sign in to comment.