Skip to content

Commit

Permalink
add envMode to turbo.json (#8757)
Browse files Browse the repository at this point in the history
### Description

### Testing Instructions

1. (temporarily) update `@turbo/gen`'s `check-types` command to `
"check-types": "env | sort | tee /dev/tty | wc -l"`
1. execute `turbo run check-types --filter @turbo/gen` and observe the
number
1. add `  "envMode": "loose",` to the root `turbo.json`
1. run the command above again, observer the number is larger,
indicating more environment variables have been included.
  • Loading branch information
dimitropoulos authored Aug 1, 2024
1 parent c8e7130 commit b9899c0
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 10 deletions.
6 changes: 4 additions & 2 deletions crates/turborepo-lib/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use clap::{
};
use clap_complete::{generate, Shell};
pub use error::Error;
use serde::Serialize;
use serde::{Deserialize, Serialize};
use tracing::{debug, error, log::warn};
use turbopath::AbsoluteSystemPathBuf;
use turborepo_api_client::AnonAPIClient;
Expand Down Expand Up @@ -136,7 +136,9 @@ impl Display for DryRunMode {
}
}

#[derive(Copy, Clone, Debug, Default, PartialEq, Serialize, ValueEnum)]
#[derive(
Copy, Clone, Debug, Default, PartialEq, Serialize, ValueEnum, Deserialize, Eq, Deserializable,
)]
#[serde(rename_all = "lowercase")]
pub enum EnvMode {
Loose,
Expand Down
7 changes: 6 additions & 1 deletion crates/turborepo-lib/src/commands/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use turborepo_repository::{
package_graph::PackageGraph, package_json::PackageJson, package_manager::PackageManager,
};

use crate::{cli, commands::CommandBase};
use crate::{
cli::{self, EnvMode},
commands::CommandBase,
};

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
Expand All @@ -21,6 +24,7 @@ struct ConfigOutput<'a> {
ui: bool,
package_manager: PackageManager,
daemon: Option<bool>,
env_mode: EnvMode,
}

pub async fn run(base: CommandBase) -> Result<(), cli::Error> {
Expand Down Expand Up @@ -49,6 +53,7 @@ pub async fn run(base: CommandBase) -> Result<(), cli::Error> {
ui: config.ui(),
package_manager: *package_manager,
daemon: config.daemon,
env_mode: config.env_mode(),
})?
);
Ok(())
Expand Down
16 changes: 16 additions & 0 deletions crates/turborepo-lib/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use turborepo_dirs::config_dir;
use turborepo_ui::UI;

use crate::{
cli::Command,
config::{ConfigurationOptions, Error as ConfigError, TurborepoConfigBuilder},
Args,
};
Expand Down Expand Up @@ -86,6 +87,21 @@ impl CommandBase {
.then_some(true),
)
.with_daemon(self.args.run_args.as_ref().and_then(|args| args.daemon()))
.with_env_mode(
self.args
.command
.as_ref()
.and_then(|c| match c {
Command::Run { execution_args, .. } => execution_args.env_mode,
_ => None,
})
.or_else(|| {
self.args
.execution_args
.as_ref()
.and_then(|args| args.env_mode)
}),
)
.build()
}

Expand Down
38 changes: 34 additions & 4 deletions crates/turborepo-lib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use turborepo_dirs::{config_dir, vercel_config_dir};
use turborepo_errors::TURBO_SITE;

pub use crate::turbo_json::RawTurboJson;
use crate::{commands::CommandBase, turbo_json};
use crate::{cli::EnvMode, commands::CommandBase, turbo_json};

#[derive(Debug, Error, Diagnostic)]
#[error("Environment variables should not be prefixed with \"{env_pipeline_delimiter}\"")]
Expand Down Expand Up @@ -211,6 +211,8 @@ pub struct ConfigurationOptions {
#[serde(rename = "dangerouslyDisablePackageManagerCheck")]
pub(crate) allow_no_package_manager: Option<bool>,
pub(crate) daemon: Option<bool>,
#[serde(rename = "envMode")]
pub(crate) env_mode: Option<EnvMode>,
}

#[derive(Default)]
Expand Down Expand Up @@ -285,6 +287,10 @@ impl ConfigurationOptions {
pub fn daemon(&self) -> Option<bool> {
self.daemon
}

pub fn env_mode(&self) -> EnvMode {
self.env_mode.unwrap_or_default()
}
}

// Maps Some("") to None to emulate how Go handles empty strings
Expand Down Expand Up @@ -320,6 +326,7 @@ impl ResolvedConfigurationOptions for RawTurboJson {
opts.ui = self.ui.map(|ui| ui.use_tui());
opts.allow_no_package_manager = self.allow_no_package_manager;
opts.daemon = self.daemon.map(|daemon| *daemon.as_inner());
opts.env_mode = self.env_mode;
Ok(opts)
}
}
Expand Down Expand Up @@ -357,6 +364,7 @@ fn get_env_var_config(
"allow_no_package_manager",
);
turbo_mapping.insert(OsString::from("turbo_daemon"), "daemon");
turbo_mapping.insert(OsString::from("turbo_env_mode"), "env_mode");
turbo_mapping.insert(OsString::from("turbo_preflight"), "preflight");

// We do not enable new config sources:
Expand Down Expand Up @@ -455,6 +463,15 @@ fn get_env_var_config(
_ => None,
});

let env_mode = output_map
.get("env_mode")
.map(|s| s.as_str())
.and_then(|s| match s {
"strict" => Some(EnvMode::Strict),
"loose" => Some(EnvMode::Loose),
_ => None,
});

// We currently don't pick up a Spaces ID via env var, we likely won't
// continue using the Spaces name, we can add an env var when we have the
// name we want to stick with.
Expand All @@ -479,6 +496,7 @@ fn get_env_var_config(
timeout,
upload_timeout,
spaces_id,
env_mode,
};

Ok(output)
Expand Down Expand Up @@ -539,6 +557,7 @@ fn get_override_env_var_config(
upload_timeout: None,
spaces_id: None,
allow_no_package_manager: None,
env_mode: None,
};

Ok(output)
Expand Down Expand Up @@ -680,6 +699,7 @@ impl TurborepoConfigBuilder {
Option<bool>
);
create_builder!(with_daemon, daemon, Option<bool>);
create_builder!(with_env_mode, env_mode, Option<EnvMode>);

pub fn build(&self) -> Result<ConfigurationOptions, Error> {
// Priority, from least significant to most significant:
Expand Down Expand Up @@ -765,6 +785,9 @@ impl TurborepoConfigBuilder {
if let Some(daemon) = current_source_config.daemon {
acc.daemon = Some(daemon);
}
if let Some(env_mode) = current_source_config.env_mode {
acc.env_mode = Some(env_mode);
}

acc
})
Expand All @@ -780,9 +803,12 @@ mod test {
use tempfile::TempDir;
use turbopath::AbsoluteSystemPathBuf;

use crate::config::{
get_env_var_config, get_override_env_var_config, ConfigurationOptions,
TurborepoConfigBuilder, DEFAULT_API_URL, DEFAULT_LOGIN_URL, DEFAULT_TIMEOUT,
use crate::{
cli::EnvMode,
config::{
get_env_var_config, get_override_env_var_config, ConfigurationOptions,
TurborepoConfigBuilder, DEFAULT_API_URL, DEFAULT_LOGIN_URL, DEFAULT_TIMEOUT,
},
};

#[test]
Expand Down Expand Up @@ -828,6 +854,7 @@ mod test {
);
env.insert("turbo_daemon".into(), "true".into());
env.insert("turbo_preflight".into(), "true".into());
env.insert("turbo_env_mode".into(), "strict".into());

let config = get_env_var_config(&env).unwrap();
assert!(config.preflight());
Expand All @@ -840,6 +867,7 @@ mod test {
assert_eq!(Some(true), config.ui);
assert_eq!(Some(true), config.allow_no_package_manager);
assert_eq!(Some(true), config.daemon);
assert_eq!(Some(EnvMode::Strict), config.env_mode);
}

#[test]
Expand All @@ -852,6 +880,7 @@ mod test {
env.insert("turbo_token".into(), "".into());
env.insert("turbo_ui".into(), "".into());
env.insert("turbo_daemon".into(), "".into());
env.insert("turbo_env_mode".into(), "".into());
env.insert("turbo_preflight".into(), "".into());

let config = get_env_var_config(&env).unwrap();
Expand All @@ -862,6 +891,7 @@ mod test {
assert_eq!(config.token(), None);
assert_eq!(config.ui, None);
assert_eq!(config.daemon, None);
assert_eq!(config.env_mode, None);
assert!(!config.preflight());
}

Expand Down
2 changes: 1 addition & 1 deletion crates/turborepo-lib/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ impl<'a> TryFrom<OptsInputs<'a>> for RunOpts {
single_package: inputs.execution_args.single_package,
graph,
dry_run: inputs.run_args.dry_run,
env_mode: inputs.execution_args.env_mode.unwrap_or_default(),
env_mode: inputs.config.env_mode(),
is_github_actions,
})
}
Expand Down
4 changes: 3 additions & 1 deletion crates/turborepo-lib/src/turbo_json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use turborepo_repository::{package_graph::ROOT_PKG_NAME, package_json::PackageJs
use turborepo_unescape::UnescapedString;

use crate::{
cli::OutputLogsMode,
cli::{EnvMode, OutputLogsMode},
config::{ConfigurationOptions, Error, InvalidEnvPrefixError},
run::{
task_access::{TaskAccessTraceFile, TASK_ACCESS_CONFIG_PATH},
Expand Down Expand Up @@ -133,6 +133,8 @@ pub struct RawTurboJson {
pub allow_no_package_manager: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub daemon: Option<Spanned<bool>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub env_mode: Option<EnvMode>,

#[deserializable(rename = "//")]
#[serde(skip)]
Expand Down
15 changes: 14 additions & 1 deletion turborepo-tests/integration/tests/config.t
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Run test run
"spacesId": null,
"ui": false,
"packageManager": "npm",
"daemon": null
"daemon": null,
"envMode": "strict"
}

Run test run with api overloaded
Expand Down Expand Up @@ -76,3 +77,15 @@ Add flag: `--daemon`
Add flag: `--no-daemon`
$ ${TURBO} --no-daemon config | jq .daemon
false

Confirm that the envMode is `strict` by default
$ ${TURBO} config | jq .envMode
"strict"

Add env var: `TURBO_ENV_MODE=loose`
$ TURBO_ENV_MODE=loose ${TURBO} config | jq .envMode
"loose"

Add flag: `--env-mode=loose`
$ ${TURBO} --env-mode=loose config | jq .envMode
"loose"

0 comments on commit b9899c0

Please sign in to comment.