Skip to content

Commit

Permalink
feat(env mode): allow for tasks to override the global env mode
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-olszewski committed Sep 17, 2024
1 parent 6ebb8ef commit 5b32fc6
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 8 deletions.
5 changes: 1 addition & 4 deletions crates/turborepo-lib/src/run/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use {
};

use crate::{
cli::{DryRunMode, EnvMode},
cli::DryRunMode,
commands::CommandBase,
engine::{Engine, EngineBuilder},
opts::Opts,
Expand Down Expand Up @@ -375,9 +375,6 @@ impl RunBuilder {
root_package_json.clone(),
)
} else if self.allow_no_turbo_json && !self.root_turbo_json_path.exists() {
// We explicitly set env mode to loose as otherwise no env vars will be passed
// to tasks.
self.opts.run_opts.env_mode = EnvMode::Loose;
TurboJsonLoader::workspace_no_turbo_json(
self.repo_root.clone(),
pkg_dep_graph.packages(),
Expand Down
4 changes: 4 additions & 0 deletions crates/turborepo-lib/src/run/summary/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ pub struct TaskSummaryTaskDefinition {
env: Vec<String>,
pass_through_env: Option<Vec<String>>,
interactive: bool,
#[serde(skip_serializing_if = "Option::is_none")]
env_mode: Option<EnvMode>,
}

#[derive(Debug, Serialize, Clone)]
Expand Down Expand Up @@ -279,6 +281,7 @@ impl From<TaskDefinition> for TaskSummaryTaskDefinition {
output_logs,
persistent,
interactive,
env_mode,
} = value;

let mut outputs = inclusions;
Expand Down Expand Up @@ -313,6 +316,7 @@ impl From<TaskDefinition> for TaskSummaryTaskDefinition {
interactive,
env,
pass_through_env,
env_mode,
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion crates/turborepo-lib/src/task_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use turborepo_errors::Spanned;
pub use visitor::{Error as VisitorError, Visitor};

use crate::{
cli::OutputLogsMode,
cli::{EnvMode, OutputLogsMode},
run::task_id::{TaskId, TaskName},
turbo_json::RawTaskDefinition,
};
Expand Down Expand Up @@ -76,6 +76,9 @@ pub struct TaskDefinition {
// Tasks that take stdin input cannot be cached as their outputs may depend on the
// input.
pub interactive: bool,

// Override for global env mode setting
pub env_mode: Option<EnvMode>,
}

impl Default for TaskDefinition {
Expand All @@ -91,6 +94,7 @@ impl Default for TaskDefinition {
output_logs: Default::default(),
persistent: Default::default(),
interactive: Default::default(),
env_mode: Default::default(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/turborepo-lib/src/task_graph/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl<'a> Visitor<'a> {
.task_definition(&info)
.ok_or(Error::MissingDefinition)?;

let task_env_mode = self.global_env_mode;
let task_env_mode = task_definition.env_mode.unwrap_or(self.global_env_mode);
package_task_event.track_env_mode(&task_env_mode.to_string());

let dependency_set = engine.dependencies(&info).ok_or(Error::MissingDefinition)?;
Expand Down
3 changes: 3 additions & 0 deletions crates/turborepo-lib/src/turbo_json/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use turborepo_repository::{

use super::{Pipeline, RawTaskDefinition, TurboJson, CONFIG_FILE};
use crate::{
cli::EnvMode,
config::Error,
run::{task_access::TASK_ACCESS_CONFIG_PATH, task_id::TaskName},
};
Expand Down Expand Up @@ -298,6 +299,7 @@ fn root_turbo_json_from_scripts(scripts: &[String]) -> Result<TurboJson, Error>
task_name,
Spanned::new(RawTaskDefinition {
cache: Some(Spanned::new(false)),
env_mode: Some(EnvMode::Loose),
..Default::default()
}),
);
Expand All @@ -316,6 +318,7 @@ fn workspace_turbo_json_from_scripts(scripts: &[String]) -> Result<TurboJson, Er
task_name,
Spanned::new(RawTaskDefinition {
cache: Some(Spanned::new(false)),
env_mode: Some(EnvMode::Loose),
..Default::default()
}),
);
Expand Down
10 changes: 10 additions & 0 deletions crates/turborepo-lib/src/turbo_json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ pub struct RawTaskDefinition {
output_logs: Option<Spanned<OutputLogsMode>>,
#[serde(skip_serializing_if = "Option::is_none")]
interactive: Option<Spanned<bool>>,
// TODO: Remove this once we have the ability to load task definitions directly
// instead of deriving them from a TurboJson
#[serde(skip)]
env_mode: Option<EnvMode>,
}

macro_rules! set_field {
Expand Down Expand Up @@ -256,6 +260,7 @@ impl RawTaskDefinition {
set_field!(self, other, env);
set_field!(self, other, pass_through_env);
set_field!(self, other, interactive);
set_field!(self, other, env_mode);
}
}

Expand Down Expand Up @@ -404,6 +409,7 @@ impl TryFrom<RawTaskDefinition> for TaskDefinition {
output_logs: *raw_task.output_logs.unwrap_or_default(),
persistent: *raw_task.persistent.unwrap_or_default(),
interactive,
env_mode: raw_task.env_mode,
})
}
}
Expand Down Expand Up @@ -726,6 +732,7 @@ mod tests {
output_logs: Some(Spanned::new(OutputLogsMode::Full).with_range(246..252)),
persistent: Some(Spanned::new(true).with_range(278..282)),
interactive: Some(Spanned::new(true).with_range(309..313)),
env_mode: None,
},
TaskDefinition {
env: vec!["OS".to_string()],
Expand All @@ -741,6 +748,7 @@ mod tests {
topological_dependencies: vec![],
persistent: true,
interactive: true,
env_mode: None,
}
; "full"
)]
Expand All @@ -765,6 +773,7 @@ mod tests {
output_logs: Some(Spanned::new(OutputLogsMode::Full).with_range(279..285)),
persistent: Some(Spanned::new(true).with_range(315..319)),
interactive: None,
env_mode: None,
},
TaskDefinition {
env: vec!["OS".to_string()],
Expand All @@ -780,6 +789,7 @@ mod tests {
topological_dependencies: vec![],
persistent: true,
interactive: false,
env_mode: None,
}
; "full (windows)"
)]
Expand Down
4 changes: 2 additions & 2 deletions turborepo-tests/integration/tests/run/allow-no-root-turbo.t
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Runs test tasks
\xe2\x80\xa2 Packages in scope: another, my-app, util (esc)
\xe2\x80\xa2 Running test in 3 packages (esc)
\xe2\x80\xa2 Remote caching disabled (esc)
my-app:test: cache bypass, force executing ac1233e4c102becc
my-app:test: cache bypass, force executing d80016a1a60c4c0a
my-app:test:
my-app:test: > test
my-app:test: > echo $MY_VAR
Expand All @@ -30,7 +30,7 @@ Ensure caching is disabled
\xe2\x80\xa2 Packages in scope: another, my-app, util (esc)
\xe2\x80\xa2 Running test in 3 packages (esc)
\xe2\x80\xa2 Remote caching disabled (esc)
my-app:test: cache bypass, force executing ac1233e4c102becc
my-app:test: cache bypass, force executing d80016a1a60c4c0a
my-app:test:
my-app:test: > test
my-app:test: > echo $MY_VAR
Expand Down

0 comments on commit 5b32fc6

Please sign in to comment.