From b5f02bca52d08ce7de7291b521b96f48d98d45a1 Mon Sep 17 00:00:00 2001 From: Chris Olszewski Date: Wed, 12 Jun 2024 14:37:03 -0700 Subject: [PATCH] chore(shim): make dynamic downloads opt in (#8458) ### Description Until we properly document and advertise the feature we'll have this opt-in. Adds `TURBO_DOWNLOAD_LOCAL_ENABLED` in favor of `TURBO_DOWNLOAD_LOCAL_DISABLED` ### Testing Instructions Manual testing that env vars are respected ``` [0 olszewski@chriss-mbp] /tmp/more-lefthook $ turbo_dev --version -vv 2024-06-12T11:41:28.918-0700 [DEBUG] turborepo_lib::shim: Global turbo version: 2.0.4-canary.2 2024-06-12T11:41:28.919-0700 [DEBUG] turborepo_repository::package_json: loading package.json from /private/tmp/more-lefthook/package.json 2024-06-12T11:41:28.925-0700 [DEBUG] turborepo_repository::package_json: loading package.json from /private/tmp/package.json 2024-06-12T11:41:28.925-0700 [DEBUG] turborepo_repository::package_json: loading package.json from /private/package.json 2024-06-12T11:41:28.925-0700 [DEBUG] turborepo_repository::package_json: loading package.json from /package.json 2024-06-12T11:41:28.925-0700 [DEBUG] turborepo_lib::shim: Repository Root: /private/tmp/more-lefthook 2024-06-12T11:41:28.926-0700 [DEBUG] turborepo_lib::shim::local_turbo_state: No local turbo binary found at: /private/tmp/more-lefthook/node_modules/turbo-darwin-arm64/bin/turbo 2024-06-12T11:41:28.926-0700 [DEBUG] turborepo_lib::shim::local_turbo_state: No local turbo binary found at: /private/tmp/more-lefthook/node_modules/turbo/node_modules/turbo-darwin-arm64/bin/turbo 2024-06-12T11:41:28.926-0700 [DEBUG] turborepo_lib::shim::local_turbo_config: downloading correct local version not enabled 2024-06-12T11:41:28.926-0700 [DEBUG] turborepo_lib::shim: Running command as global turbo WARNING No locally installed `turbo` found. Using version: 2.0.4-canary.2. 2.0.4-canary.2 [0 olszewski@chriss-mbp] /tmp/more-lefthook $ TURBO_DOWNLOAD_LOCAL_ENABLED=1 turbo_dev --version -vv 2024-06-12T11:41:37.467-0700 [DEBUG] turborepo_lib::shim: Global turbo version: 2.0.4-canary.2 2024-06-12T11:41:37.467-0700 [DEBUG] turborepo_repository::package_json: loading package.json from /private/tmp/more-lefthook/package.json 2024-06-12T11:41:37.470-0700 [DEBUG] turborepo_repository::package_json: loading package.json from /private/tmp/package.json 2024-06-12T11:41:37.470-0700 [DEBUG] turborepo_repository::package_json: loading package.json from /private/package.json 2024-06-12T11:41:37.470-0700 [DEBUG] turborepo_repository::package_json: loading package.json from /package.json 2024-06-12T11:41:37.470-0700 [DEBUG] turborepo_lib::shim: Repository Root: /private/tmp/more-lefthook 2024-06-12T11:41:37.470-0700 [DEBUG] turborepo_lib::shim::local_turbo_state: No local turbo binary found at: /private/tmp/more-lefthook/node_modules/turbo-darwin-arm64/bin/turbo 2024-06-12T11:41:37.470-0700 [DEBUG] turborepo_lib::shim::local_turbo_state: No local turbo binary found at: /private/tmp/more-lefthook/node_modules/turbo/node_modules/turbo-darwin-arm64/bin/turbo 2024-06-12T11:41:37.492-0700 [DEBUG] turborepo_lib::shim: Found configuration for turbo version 2.0.3 2024-06-12T11:41:37.492-0700 [DEBUG] turborepo_lib::shim: Running turbo@2.0.3 via npx 2024-06-12T11:41:37.492-0700 [DEBUG] turborepo_lib::shim: supports_skip_infer_and_single_package true 2024-06-12T11:41:38.068-0700 [DEBUG] turborepo_lib::shim: Global turbo version: 2.0.3 2.0.3 [0 olszewski@chriss-mbp] /tmp/more-lefthook $ TURBO_DOWNLOAD_LOCAL_ENABLED=0 turbo_dev --version -vv 2024-06-12T11:41:41.123-0700 [DEBUG] turborepo_lib::shim: Global turbo version: 2.0.4-canary.2 2024-06-12T11:41:41.123-0700 [DEBUG] turborepo_repository::package_json: loading package.json from /private/tmp/more-lefthook/package.json 2024-06-12T11:41:41.125-0700 [DEBUG] turborepo_repository::package_json: loading package.json from /private/tmp/package.json 2024-06-12T11:41:41.125-0700 [DEBUG] turborepo_repository::package_json: loading package.json from /private/package.json 2024-06-12T11:41:41.125-0700 [DEBUG] turborepo_repository::package_json: loading package.json from /package.json 2024-06-12T11:41:41.125-0700 [DEBUG] turborepo_lib::shim: Repository Root: /private/tmp/more-lefthook 2024-06-12T11:41:41.125-0700 [DEBUG] turborepo_lib::shim::local_turbo_state: No local turbo binary found at: /private/tmp/more-lefthook/node_modules/turbo-darwin-arm64/bin/turbo 2024-06-12T11:41:41.125-0700 [DEBUG] turborepo_lib::shim::local_turbo_state: No local turbo binary found at: /private/tmp/more-lefthook/node_modules/turbo/node_modules/turbo-darwin-arm64/bin/turbo 2024-06-12T11:41:41.125-0700 [DEBUG] turborepo_lib::shim::local_turbo_config: downloading correct local version not enabled 2024-06-12T11:41:41.125-0700 [DEBUG] turborepo_lib::shim: Running command as global turbo WARNING No locally installed `turbo` found. Using version: 2.0.4-canary.2. 2.0.4-canary.2 ``` --- .../src/shim/local_turbo_config.rs | 39 ++++++++++++------- turborepo-tests/helpers/setup.sh | 2 +- turborepo-tests/helpers/setup_example_test.sh | 2 +- .../integration/tests/find-turbo/setup.sh | 2 +- .../tests/lockfile-aware-caching/setup.sh | 2 +- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/crates/turborepo-lib/src/shim/local_turbo_config.rs b/crates/turborepo-lib/src/shim/local_turbo_config.rs index 179ca3195bdc1..735559d2a19ac 100644 --- a/crates/turborepo-lib/src/shim/local_turbo_config.rs +++ b/crates/turborepo-lib/src/shim/local_turbo_config.rs @@ -3,7 +3,7 @@ use std::env; use tracing::debug; use turborepo_repository::{inference::RepoState, package_manager::PackageManager}; -const TURBO_DOWNLOAD_LOCAL_DISABLED: &str = "TURBO_DOWNLOAD_LOCAL_DISABLED"; +const TURBO_DOWNLOAD_LOCAL_ENABLED: &str = "TURBO_DOWNLOAD_LOCAL_ENABLED"; /// Struct containing information about the desired local turbo version /// according to lockfiles, package.jsons, and if all else fails turbo.json @@ -12,13 +12,26 @@ pub struct LocalTurboConfig { turbo_version: String, } +fn is_env_var_truthy(env_var: &str) -> Option { + let value = env::var(env_var).ok()?; + match value.as_str() { + "1" | "true" => Some(true), + "0" | "false" => Some(false), + _ => None, + } +} + impl LocalTurboConfig { pub fn infer(repo_state: &RepoState) -> Option { - // Don't attempt a download if user has opted out - if env::var(TURBO_DOWNLOAD_LOCAL_DISABLED) - .map_or(false, |disable| matches!(disable.as_str(), "1" | "true")) - { - debug!("downloading correct local version disabled"); + Self::infer_internal(repo_state, is_env_var_truthy(TURBO_DOWNLOAD_LOCAL_ENABLED)) + } + + // Used for testing when we want to manually set the controlling env var + fn infer_internal(repo_state: &RepoState, is_enabled: Option) -> Option { + // TODO: once we have properly communicated this functionality we should make + // this opt-out. + if !is_enabled.unwrap_or(false) { + debug!("downloading correct local version not enabled"); return None; } let turbo_version = Self::turbo_version_from_lockfile(repo_state)?; @@ -78,7 +91,7 @@ mod test { .unwrap(); assert_eq!( - LocalTurboConfig::infer(&repo), + LocalTurboConfig::infer_internal(&repo, Some(true)), Some(LocalTurboConfig { turbo_version: "2.0.3".into() }) @@ -103,7 +116,7 @@ mod test { .unwrap(); assert_eq!( - LocalTurboConfig::infer(&repo), + LocalTurboConfig::infer_internal(&repo, Some(true)), Some(LocalTurboConfig { turbo_version: "2.0.3".into() }) @@ -128,7 +141,7 @@ mod test { package_manager: Err(Error::MissingPackageManager), }; - assert_eq!(LocalTurboConfig::infer(&repo), None,); + assert_eq!(LocalTurboConfig::infer_internal(&repo, Some(true)), None,); } #[test] @@ -149,7 +162,7 @@ mod test { package_manager: Err(Error::MissingPackageManager), }; - assert_eq!(LocalTurboConfig::infer(&repo), None); + assert_eq!(LocalTurboConfig::infer_internal(&repo, Some(true)), None); } #[test] @@ -166,7 +179,7 @@ mod test { turbo_json .create_with_contents(include_bytes!("../../fixtures/local_config/turbo.v1.json")) .unwrap(); - assert_eq!(LocalTurboConfig::infer(&repo), None); + assert_eq!(LocalTurboConfig::infer_internal(&repo, Some(true)), None); } #[test] @@ -183,7 +196,7 @@ mod test { turbo_json .create_with_contents(include_bytes!("../../fixtures/local_config/turbo.v2.json")) .unwrap(); - assert_eq!(LocalTurboConfig::infer(&repo), None,); + assert_eq!(LocalTurboConfig::infer_internal(&repo, Some(true)), None,); } #[test] @@ -196,6 +209,6 @@ mod test { root_package_json: PackageJson::default(), package_manager: Err(Error::MissingPackageManager), }; - assert_eq!(LocalTurboConfig::infer(&repo), None,); + assert_eq!(LocalTurboConfig::infer_internal(&repo, Some(true)), None,); } } diff --git a/turborepo-tests/helpers/setup.sh b/turborepo-tests/helpers/setup.sh index 9e15edfa48032..c8580cd5ac7cb 100755 --- a/turborepo-tests/helpers/setup.sh +++ b/turborepo-tests/helpers/setup.sh @@ -12,5 +12,5 @@ fi # disable the first-run telemetry message export TURBO_TELEMETRY_MESSAGE_DISABLED=1 export TURBO_GLOBAL_WARNING_DISABLED=1 -export TURBO_DOWNLOAD_LOCAL_DISABLED=1 +export TURBO_DOWNLOAD_LOCAL_ENABLED=0 TURBO=${MONOREPO_ROOT_DIR}/target/debug/turbo${EXT} diff --git a/turborepo-tests/helpers/setup_example_test.sh b/turborepo-tests/helpers/setup_example_test.sh index f4fd970947d9b..f7445c0097a45 100644 --- a/turborepo-tests/helpers/setup_example_test.sh +++ b/turborepo-tests/helpers/setup_example_test.sh @@ -3,7 +3,7 @@ set -eo pipefail export TURBO_TELEMETRY_MESSAGE_DISABLED=1 -export TURBO_DOWNLOAD_LOCAL_DISABLED=1 +export TURBO_DOWNLOAD_LOCAL_ENABLED=0 # Start by figuring out which example we're testing and its package manager example_path=$1 diff --git a/turborepo-tests/integration/tests/find-turbo/setup.sh b/turborepo-tests/integration/tests/find-turbo/setup.sh index bafa637cf0d21..387b9c37e14d4 100644 --- a/turborepo-tests/integration/tests/find-turbo/setup.sh +++ b/turborepo-tests/integration/tests/find-turbo/setup.sh @@ -1,6 +1,6 @@ #!/bin/bash -export TURBO_DOWNLOAD_LOCAL_DISABLED=1 +export TURBO_DOWNLOAD_LOCAL_ENABLED=0 SCRIPT_DIR=$(dirname ${BASH_SOURCE[0]}) TARGET_DIR=$1 FIXTURE_DIR=$2 diff --git a/turborepo-tests/integration/tests/lockfile-aware-caching/setup.sh b/turborepo-tests/integration/tests/lockfile-aware-caching/setup.sh index 3280fe5e03e09..9a2f9ddd05cd8 100755 --- a/turborepo-tests/integration/tests/lockfile-aware-caching/setup.sh +++ b/turborepo-tests/integration/tests/lockfile-aware-caching/setup.sh @@ -1,7 +1,7 @@ #!/bin/bash export TURBO_GLOBAL_WARNING_DISABLED=1 -export TURBO_DOWNLOAD_LOCAL_DISABLED=1 +export TURBO_DOWNLOAD_LOCAL_ENABLED=0 SCRIPT_DIR=$(dirname ${BASH_SOURCE[0]}) TARGET_DIR=$1 FIXTURE_DIR=$2