Skip to content

Commit

Permalink
feat(mfe): add env var indicating mfe proxy (#9395)
Browse files Browse the repository at this point in the history
### Description

MFE development tasks may want to know if there's a corresponding proxy
that is being run. Just checking if the proxy is running is error prone
since they're both persistent tasks and there's no consistent way to
prevent the check from running before the proxy server is listening on
the port.

We avoid needing to perform this check by setting an environment
variable for each development task if there's an associated proxy task
in the configuration. It is then up to the underlying task to alter it's
behavior depending on the presence of this variable.

### Testing Instructions

In a project with a MFE, run a development task and verify that
`TURBO_TASK_HAS_MFE_PROXY` is set to `true`
  • Loading branch information
chris-olszewski authored Nov 6, 2024
1 parent 9775737 commit 180f547
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions crates/turborepo-lib/src/micro_frontends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,10 @@ impl MicroFrontendsConfigs {
pub fn get(&self, package_name: &str) -> Option<&HashSet<TaskId<'static>>> {
self.configs.get(package_name)
}

pub fn task_has_mfe_proxy(&self, task_id: &TaskId) -> bool {
self.configs
.values()
.any(|dev_tasks| dev_tasks.contains(task_id))
}
}
12 changes: 12 additions & 0 deletions crates/turborepo-lib/src/task_graph/visitor/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,23 @@ pub struct PackageGraphCommandProvider<'a> {
package_graph: &'a PackageGraph,
package_manager_binary: Result<PathBuf, which::Error>,
task_args: TaskArgs<'a>,
mfe_configs: Option<&'a MicroFrontendsConfigs>,
}

impl<'a> PackageGraphCommandProvider<'a> {
pub fn new(
repo_root: &'a AbsoluteSystemPath,
package_graph: &'a PackageGraph,
task_args: TaskArgs<'a>,
mfe_configs: Option<&'a MicroFrontendsConfigs>,
) -> Self {
let package_manager_binary = which::which(package_graph.package_manager().command());
Self {
repo_root,
package_graph,
package_manager_binary,
task_args,
mfe_configs,
}
}

Expand Down Expand Up @@ -126,6 +129,15 @@ impl<'a> CommandProvider for PackageGraphCommandProvider<'a> {
cmd.env_clear();
cmd.envs(environment.iter());

// If the task has an associated proxy, then we indicate this to the underlying
// task via an env var
if self
.mfe_configs
.map_or(false, |mfe_configs| mfe_configs.task_has_mfe_proxy(task_id))
{
cmd.env("TURBO_TASK_HAS_MFE_PROXY", "true");
}

// We always open stdin and the visitor will close it depending on task
// configuration
cmd.open_stdin();
Expand Down
1 change: 1 addition & 0 deletions crates/turborepo-lib/src/task_graph/visitor/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ impl<'a> ExecContextFactory<'a> {
visitor.repo_root,
&visitor.package_graph,
visitor.run_opts.task_args(),
visitor.micro_frontends_configs,
);
let mut command_factory = CommandFactory::new();
if let Some(micro_frontends_configs) = visitor.micro_frontends_configs {
Expand Down

0 comments on commit 180f547

Please sign in to comment.