Skip to content

Commit

Permalink
chore(shim): make dynamic downloads opt in (#8458)
Browse files Browse the repository at this point in the history
### 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 [email protected] 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
```
  • Loading branch information
chris-olszewski committed Jun 12, 2024
1 parent c98b46a commit b5f02bc
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
39 changes: 26 additions & 13 deletions crates/turborepo-lib/src/shim/local_turbo_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -12,13 +12,26 @@ pub struct LocalTurboConfig {
turbo_version: String,
}

fn is_env_var_truthy(env_var: &str) -> Option<bool> {
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<Self> {
// 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<bool>) -> Option<Self> {
// 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)?;
Expand Down Expand Up @@ -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()
})
Expand All @@ -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()
})
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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,);
}
}
2 changes: 1 addition & 1 deletion turborepo-tests/helpers/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}
2 changes: 1 addition & 1 deletion turborepo-tests/helpers/setup_example_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion turborepo-tests/integration/tests/find-turbo/setup.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit b5f02bc

Please sign in to comment.