Skip to content

Commit

Permalink
fix(cache): no longer let remoteCache.enable override force (#9684)
Browse files Browse the repository at this point in the history
### Description

Fixes #9681

We were setting remote cache items to whatever the value of
`remoteCache.enable` was. This incorrectly re-enabled parts of the cache
that had previously been disabled. The fix is to only care if users
explicitly disable remote caching, this works since if
`remoteCache.enable` isn't specified it is assumed to be `true`.

### Testing Instructions

Added failing unit test in first commit, it passes after the second
commit.
  • Loading branch information
chris-olszewski authored Jan 13, 2025
1 parent 22bfeca commit 4d76d7b
Showing 1 changed file with 51 additions and 6 deletions.
57 changes: 51 additions & 6 deletions crates/turborepo-lib/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ impl<'a> TryFrom<OptsInputs<'a>> for CacheOpts {
return Err(Error::OverlappingCacheOptions);
}

// defaults to fully enabled cache
let mut cache = cache.unwrap_or_default();

if inputs.config.remote_only() {
Expand All @@ -482,11 +483,10 @@ impl<'a> TryFrom<OptsInputs<'a>> for CacheOpts {
if !is_linked {
cache.remote.read = false;
cache.remote.write = false;
} else if let Some(enabled) = inputs.config.enabled {
// We're linked, but if the user has explicitly enabled or disabled, use that
// value
cache.remote.read = enabled;
cache.remote.write = enabled;
} else if let Some(false) = inputs.config.enabled {
// We're linked, but if the user has explicitly disabled remote cache
cache.remote.read = false;
cache.remote.write = false;
};

if inputs.config.remote_cache_read_only() {
Expand Down Expand Up @@ -529,9 +529,10 @@ impl ScopeOpts {

#[cfg(test)]
mod test {
use tempfile::TempDir;
use test_case::test_case;
use turbopath::AbsoluteSystemPathBuf;
use turborepo_cache::CacheOpts;
use turborepo_cache::{CacheActions, CacheConfig, CacheOpts};
use turborepo_ui::ColorConfig;

use super::{APIClientOpts, RepoOpts, RunOpts};
Expand Down Expand Up @@ -773,4 +774,48 @@ mod test {

Ok(())
}

#[test]
fn test_cache_config_force_remote_enable() -> Result<(), anyhow::Error> {
let tmpdir = TempDir::new()?;
let repo_root = AbsoluteSystemPathBuf::try_from(tmpdir.path())?;

repo_root
.join_component("turbo.json")
.create_with_contents(serde_json::to_string_pretty(&serde_json::json!({
"remoteCache": { "enabled": true }
}))?)?;

let mut args = Args::default();
args.command = Some(Command::Run {
execution_args: Box::default(),
run_args: Box::new(RunArgs {
force: Some(Some(true)),
..Default::default()
}),
});

// set token and team to simulate a logged in/linked user
args.token = Some("token".to_string());
args.team = Some("team".to_string());

let base = CommandBase::new(args, repo_root, "1.0.0", ColorConfig::new(false))?;
let actual = base.opts().cache_opts.cache;

assert_eq!(
actual,
CacheConfig {
remote: CacheActions {
read: false,
write: true
},
local: CacheActions {
read: false,
write: true
}
}
);

Ok(())
}
}

0 comments on commit 4d76d7b

Please sign in to comment.