Skip to content

Commit 63fe984

Browse files
committed
WIP
1 parent 8daeb2b commit 63fe984

File tree

1 file changed

+47
-6
lines changed
  • crates/turborepo-lib/src/turbo_json

1 file changed

+47
-6
lines changed

crates/turborepo-lib/src/turbo_json/mod.rs

+47-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use clap::ValueEnum;
1111
use miette::{NamedSource, SourceSpan};
1212
use serde::{Deserialize, Serialize};
1313
use struct_iterable::Iterable;
14-
use turbopath::AbsoluteSystemPath;
14+
use turbopath::{AbsoluteSystemPath, AnchoredSystemPath, AnchoredSystemPathBuf};
1515
use turborepo_errors::Spanned;
1616
use turborepo_repository::package_graph::ROOT_PKG_NAME;
1717
use turborepo_unescape::UnescapedString;
@@ -33,6 +33,8 @@ pub use loader::TurboJsonLoader;
3333

3434
use crate::{boundaries::BoundariesConfig, config::UnnecessaryPackageTaskSyntaxError};
3535

36+
const TURBO_ROOT: &str = "$TURBO_ROOT$";
37+
3638
#[derive(Serialize, Deserialize, Debug, Default, PartialEq, Clone, Deserializable)]
3739
#[serde(rename_all = "camelCase")]
3840
pub struct SpacesJson {
@@ -537,10 +539,11 @@ impl RawTurboJson {
537539
}
538540
}
539541

540-
impl TryFrom<RawTurboJson> for TurboJson {
541-
type Error = Error;
542-
543-
fn try_from(raw_turbo: RawTurboJson) -> Result<Self, Error> {
542+
impl TurboJson {
543+
fn from_raw(
544+
raw_turbo: RawTurboJson,
545+
path_to_repo_root: &AnchoredSystemPath,
546+
) -> Result<Self, Error> {
544547
if let Some(pipeline) = raw_turbo.pipeline {
545548
let (span, text) = pipeline.span_and_text("turbo.json");
546549
return Err(Error::PipelineField { span, text });
@@ -572,6 +575,16 @@ impl TryFrom<RawTurboJson> for TurboJson {
572575
}
573576
}
574577

578+
if let Some(tasks_from_turbo) = raw_turbo.tasks {
579+
for (task_name, task_def) in tasks_from_turbo {
580+
task_def
581+
.inputs
582+
.iter()
583+
.map(|glob| replace_turbo_root_token(&mut task_def, path_to_repo_root))
584+
.collect()
585+
}
586+
}
587+
575588
Ok(TurboJson {
576589
text: raw_turbo.span.text,
577590
path: raw_turbo.span.path,
@@ -629,7 +642,12 @@ impl TurboJson {
629642
path: &AbsoluteSystemPath,
630643
) -> Result<TurboJson, Error> {
631644
let raw_turbo_json = RawTurboJson::read(repo_root, path)?;
632-
raw_turbo_json.try_into()
645+
// pass in repo_root
646+
// This needs the relative path to root
647+
let pkg_path = path.parent().expect("turbo.json is not root");
648+
// relative pkg_path -> repo_root
649+
let path_to_root = AnchoredSystemPathBuf::relative_path_between(pkg_path, repo_root);
650+
TurboJson::from_raw(raw_turbo_json, &path_to_root)
633651
}
634652

635653
pub fn task(&self, task_id: &TaskId, task_name: &TaskName) -> Option<RawTaskDefinition> {
@@ -765,6 +783,29 @@ fn gather_env_vars(
765783
Ok(())
766784
}
767785

786+
fn replace_turbo_root_token(
787+
task_definition: &mut TaskDefinition,
788+
path_to_repo_root: &AnchoredSystemPath,
789+
) {
790+
for input in task_definition.inputs.iter_mut() {
791+
let _ = input.replacen(TURBO_ROOT, path_to_repo_root.as_str(), 1);
792+
}
793+
794+
for output in task_definition.outputs.validated_exclusions().iter_mut() {
795+
let _ = output.iter().map(|glob| {
796+
glob.as_str()
797+
.replacen(TURBO_ROOT, path_to_repo_root.as_str(), 1)
798+
});
799+
}
800+
801+
for output in task_definition.outputs.validated_inclusions().iter_mut() {
802+
let _ = output.iter().map(|glob| {
803+
glob.as_str()
804+
.replacen(TURBO_ROOT, path_to_repo_root.as_str(), 1)
805+
});
806+
}
807+
}
808+
768809
#[cfg(test)]
769810
mod tests {
770811
use anyhow::Result;

0 commit comments

Comments
 (0)