-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathmod.rs
549 lines (495 loc) · 19.1 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
mod command;
mod error;
mod exec;
mod output;
use std::{
borrow::Cow,
collections::HashSet,
io::Write,
sync::{Arc, Mutex, OnceLock},
};
use console::{Style, StyledObject};
use convert_case::{Case, Casing};
use error::{TaskError, TaskWarning};
use exec::ExecContextFactory;
use futures::{stream::FuturesUnordered, StreamExt};
use itertools::Itertools;
use miette::{Diagnostic, NamedSource, SourceSpan};
use output::{StdWriter, TaskOutput};
use regex::Regex;
use tokio::sync::mpsc;
use tracing::{debug, error, warn, Span};
use turbopath::{AbsoluteSystemPath, AnchoredSystemPath};
use turborepo_ci::{Vendor, VendorBehavior};
use turborepo_env::{platform::PlatformEnv, EnvironmentVariableMap};
use turborepo_errors::TURBO_SITE;
use turborepo_repository::package_graph::{PackageGraph, PackageName, ROOT_PKG_NAME};
use turborepo_telemetry::events::{
generic::GenericEventBuilder, task::PackageTaskEventBuilder, EventBuilder, TrackedErrors,
};
use turborepo_ui::{
sender::UISender, ColorConfig, ColorSelector, OutputClient, OutputSink, PrefixedUI,
};
use crate::{
cli::EnvMode,
engine::{Engine, ExecutionOptions},
microfrontends::MicrofrontendsConfigs,
opts::RunOpts,
process::ProcessManager,
run::{
global_hash::GlobalHashableInputs,
summary::{self, GlobalHashSummary, RunTracker},
task_access::TaskAccess,
task_id::TaskId,
RunCache,
},
task_hash::{self, PackageInputsHashes, TaskHashTrackerState, TaskHasher},
};
// This holds the whole world
pub struct Visitor<'a> {
color_cache: ColorSelector,
dry: bool,
global_env: EnvironmentVariableMap,
global_env_mode: EnvMode,
manager: ProcessManager,
run_opts: &'a RunOpts,
package_graph: Arc<PackageGraph>,
repo_root: &'a AbsoluteSystemPath,
run_cache: Arc<RunCache>,
run_tracker: RunTracker,
task_access: &'a TaskAccess,
sink: OutputSink<StdWriter>,
task_hasher: TaskHasher<'a>,
color_config: ColorConfig,
is_watch: bool,
ui_sender: Option<UISender>,
warnings: Arc<Mutex<Vec<TaskWarning>>>,
micro_frontends_configs: Option<&'a MicrofrontendsConfigs>,
}
#[derive(Debug, thiserror::Error, Diagnostic)]
pub enum Error {
#[error("cannot find package {package_name} for task {task_id}")]
MissingPackage {
package_name: PackageName,
task_id: TaskId<'static>,
},
#[error(
"Your `package.json` script looks like it invokes a Root Task ({task_name}), creating a \
loop of `turbo` invocations. You likely have misconfigured your scripts and tasks or \
your package manager's Workspace structure."
)]
#[diagnostic(
code(recursive_turbo_invocations),
url(
"{}/messages/{}",
TURBO_SITE, self.code().unwrap().to_string().to_case(Case::Kebab)
)
)]
RecursiveTurbo {
task_name: String,
command: String,
#[label("This script calls `turbo`, which calls the script, which calls `turbo`...")]
span: Option<SourceSpan>,
#[source_code]
text: NamedSource,
},
#[error("Could not find definition for task")]
MissingDefinition,
#[error("error while executing engine: {0}")]
Engine(#[from] crate::engine::ExecuteError),
#[error(transparent)]
TaskHash(#[from] task_hash::Error),
#[error(transparent)]
RunSummary(#[from] summary::Error),
#[error("internal errors encountered: {0}")]
InternalErrors(String),
#[error("unable to find package manager binary: {0}")]
Which(#[from] which::Error),
#[error(
"'{package}' is configured with a {mfe_config_filename}, but doesn't have \
'@vercel/microfrontends' listed as a dependency"
)]
MissingMFEDependency {
package: String,
mfe_config_filename: String,
},
}
impl<'a> Visitor<'a> {
// Disabling this lint until we stop adding state to the visitor.
// Once we have the full picture we will go about grouping these pieces of data
// together
#[allow(clippy::too_many_arguments)]
pub async fn new(
package_graph: Arc<PackageGraph>,
run_cache: Arc<RunCache>,
run_tracker: RunTracker,
task_access: &'a TaskAccess,
run_opts: &'a RunOpts,
package_inputs_hashes: PackageInputsHashes,
env_at_execution_start: &'a EnvironmentVariableMap,
global_hash: &'a str,
color_config: ColorConfig,
manager: ProcessManager,
repo_root: &'a AbsoluteSystemPath,
global_env: EnvironmentVariableMap,
ui_sender: Option<UISender>,
is_watch: bool,
micro_frontends_configs: Option<&'a MicrofrontendsConfigs>,
) -> Self {
let task_hasher = TaskHasher::new(
package_inputs_hashes,
run_opts,
env_at_execution_start,
global_hash,
);
let sink = Self::sink(run_opts);
let color_cache = ColorSelector::default();
// Set up correct size for underlying pty
if let Some(app) = ui_sender.as_ref() {
if let Some(pane_size) = app.pane_size().await {
manager.set_pty_size(pane_size.rows, pane_size.cols);
}
}
Self {
color_cache,
dry: false,
global_env_mode: run_opts.env_mode,
manager,
run_opts,
package_graph,
repo_root,
run_cache,
run_tracker,
task_access,
sink,
task_hasher,
color_config,
global_env,
ui_sender,
is_watch,
warnings: Default::default(),
micro_frontends_configs,
}
}
#[tracing::instrument(skip_all)]
pub async fn visit(
&self,
engine: Arc<Engine>,
telemetry: &GenericEventBuilder,
) -> Result<Vec<TaskError>, Error> {
for task in engine.tasks().sorted() {
self.color_cache.color_for_key(&task.to_string());
}
let concurrency = self.run_opts.concurrency as usize;
let (node_sender, mut node_stream) = mpsc::channel(concurrency);
let engine_handle = {
let engine = engine.clone();
tokio::spawn(engine.execute(ExecutionOptions::new(false, concurrency), node_sender))
};
let mut tasks = FuturesUnordered::new();
let errors = Arc::new(Mutex::new(Vec::new()));
let span = Span::current();
let factory = ExecContextFactory::new(self, errors.clone(), self.manager.clone(), &engine)?;
while let Some(message) = node_stream.recv().await {
let span = tracing::debug_span!(parent: &span, "queue_task", task = %message.info);
let _enter = span.enter();
let crate::engine::Message { info, callback } = message;
let package_name = PackageName::from(info.package());
let workspace_info =
self.package_graph
.package_info(&package_name)
.ok_or_else(|| Error::MissingPackage {
package_name: package_name.clone(),
task_id: info.clone(),
})?;
let package_task_event =
PackageTaskEventBuilder::new(info.package(), info.task()).with_parent(telemetry);
let command = workspace_info.package_json.scripts.get(info.task());
match command {
Some(cmd) if info.package() == ROOT_PKG_NAME && turbo_regex().is_match(cmd) => {
package_task_event.track_error(TrackedErrors::RecursiveError);
let (span, text) = cmd.span_and_text("package.json");
return Err(Error::RecursiveTurbo {
task_name: info.to_string(),
command: cmd.to_string(),
span,
text,
});
}
_ => (),
}
let task_definition = engine
.task_definition(&info)
.ok_or(Error::MissingDefinition)?;
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)?;
let task_hash_telemetry = package_task_event.child();
let task_hash = self.task_hasher.calculate_task_hash(
&info,
task_definition,
task_env_mode,
workspace_info,
dependency_set,
task_hash_telemetry,
)?;
debug!("task {} hash is {}", info, task_hash);
// We do this calculation earlier than we do in Go due to the `task_hasher`
// being !Send. In the future we can look at doing this right before
// task execution instead.
let execution_env =
self.task_hasher
.env(&info, task_env_mode, task_definition, &self.global_env)?;
let task_cache = self.run_cache.task_cache(
task_definition,
workspace_info,
info.clone(),
&task_hash,
);
// Drop to avoid holding the span across an await
drop(_enter);
// here is where we do the logic split
match self.dry {
true => {
let dry_run_exec_context =
factory.dry_run_exec_context(info.clone(), task_cache);
let tracker = self.run_tracker.track_task(info.into_owned());
tasks.push(tokio::spawn(async move {
dry_run_exec_context.execute_dry_run(tracker).await
}));
}
false => {
let takes_input = task_definition.interactive || task_definition.persistent;
let Some(mut exec_context) = factory.exec_context(
info.clone(),
task_hash,
task_cache,
execution_env,
takes_input,
self.task_access.clone(),
)?
else {
// TODO(gsoltis): if/when we fix https://github.com/vercel/turborepo/issues/937
// the following block should never get hit. In the meantime, keep it after
// hashing so that downstream tasks can count on the hash existing
//
// bail if the script doesn't exist or is empty
continue;
};
let vendor_behavior =
Vendor::infer().and_then(|vendor| vendor.behavior.as_ref());
let output_client = if let Some(handle) = &self.ui_sender {
TaskOutput::UI(handle.task(info.to_string()))
} else {
TaskOutput::Direct(self.output_client(&info, vendor_behavior))
};
let tracker = self.run_tracker.track_task(info.clone().into_owned());
let spaces_client = self.run_tracker.spaces_task_client();
let parent_span = Span::current();
let execution_telemetry = package_task_event.child();
tasks.push(tokio::spawn(async move {
exec_context
.execute(
parent_span.id(),
tracker,
output_client,
callback,
spaces_client,
&execution_telemetry,
)
.await
}));
}
}
}
// Wait for the engine task to finish and for all of our tasks to finish
engine_handle.await.expect("engine execution panicked")?;
// This will poll the futures until they are all completed
let mut internal_errors = Vec::new();
while let Some(result) = tasks.next().await {
if let Err(e) = result.unwrap_or_else(|e| panic!("task executor panicked: {e}")) {
internal_errors.push(e);
}
}
drop(factory);
if !self.is_watch {
if let Some(handle) = &self.ui_sender {
handle.stop().await;
}
}
if !internal_errors.is_empty() {
return Err(Error::InternalErrors(
internal_errors.into_iter().map(|e| e.to_string()).join(","),
));
}
// Write out the traced-config.json file if we have one
self.task_access.save().await;
let errors = Arc::into_inner(errors)
.expect("only one strong reference to errors should remain")
.into_inner()
.expect("mutex poisoned");
Ok(errors)
}
/// Finishes visiting the tasks, creates the run summary, and either
/// prints, saves, or sends it to spaces.
#[allow(clippy::too_many_arguments)]
#[tracing::instrument(skip(
self,
packages,
global_hash_inputs,
engine,
env_at_execution_start
))]
pub(crate) async fn finish(
self,
exit_code: i32,
packages: &HashSet<PackageName>,
global_hash_inputs: GlobalHashableInputs<'_>,
engine: &Engine,
env_at_execution_start: &EnvironmentVariableMap,
pkg_inference_root: Option<&AnchoredSystemPath>,
) -> Result<(), Error> {
let Self {
package_graph,
color_config: ui,
run_opts,
repo_root,
global_env_mode,
task_hasher,
is_watch,
..
} = self;
let global_hash_summary = GlobalHashSummary::try_from(global_hash_inputs)?;
// output any warnings that we collected while running tasks
if let Ok(warnings) = self.warnings.lock() {
if !warnings.is_empty() {
eprintln!();
warn!("finished with warnings");
eprintln!();
PlatformEnv::output_header(global_env_mode == EnvMode::Strict, self.color_config);
for warning in warnings.iter() {
PlatformEnv::output_for_task(
warning.missing_platform_env().to_owned(),
warning.task_id(),
self.color_config,
)
}
}
}
Ok(self
.run_tracker
.finish(
exit_code,
&package_graph,
ui,
repo_root,
pkg_inference_root,
run_opts,
packages,
global_hash_summary,
global_env_mode,
engine,
task_hasher.task_hash_tracker(),
env_at_execution_start,
is_watch,
)
.await?)
}
fn sink(run_opts: &RunOpts) -> OutputSink<StdWriter> {
let (out, err) = if run_opts.should_redirect_stderr_to_stdout() {
(std::io::stdout().into(), std::io::stdout().into())
} else {
(std::io::stdout().into(), std::io::stderr().into())
};
OutputSink::new(out, err)
}
fn output_client(
&self,
task_id: &TaskId,
vendor_behavior: Option<&VendorBehavior>,
) -> OutputClient<impl std::io::Write> {
let behavior = match self.run_opts.log_order {
crate::opts::ResolvedLogOrder::Stream if self.run_tracker.spaces_enabled() => {
turborepo_ui::OutputClientBehavior::InMemoryBuffer
}
crate::opts::ResolvedLogOrder::Stream => {
turborepo_ui::OutputClientBehavior::Passthrough
}
crate::opts::ResolvedLogOrder::Grouped => turborepo_ui::OutputClientBehavior::Grouped,
};
let mut logger = self.sink.logger(behavior);
if let Some(vendor_behavior) = vendor_behavior {
let group_name = if self.run_opts.single_package {
task_id.task().to_string()
} else {
format!("{}:{}", task_id.package(), task_id.task())
};
let header_factory = (vendor_behavior.group_prefix)(group_name.to_owned());
let footer_factory = (vendor_behavior.group_suffix)(group_name.to_owned());
logger.with_header_footer(Some(header_factory), Some(footer_factory));
let (error_header, error_footer) = (
vendor_behavior
.error_group_prefix
.map(|f| f(group_name.to_owned())),
vendor_behavior
.error_group_suffix
.map(|f| f(group_name.to_owned())),
);
logger.with_error_header_footer(error_header, error_footer);
}
logger
}
fn prefix<'b>(&self, task_id: &'b TaskId) -> Cow<'b, str> {
match self.run_opts.log_prefix {
crate::opts::ResolvedLogPrefix::Task if self.run_opts.single_package => {
task_id.task().into()
}
crate::opts::ResolvedLogPrefix::Task => {
format!("{}:{}", task_id.package(), task_id.task()).into()
}
crate::opts::ResolvedLogPrefix::None => "".into(),
}
}
// Task ID as displayed in error messages
fn display_task_id(&self, task_id: &TaskId) -> String {
match self.run_opts.single_package {
true => task_id.task().to_string(),
false => task_id.to_string(),
}
}
fn prefixed_ui<W: Write>(
color_config: ColorConfig,
is_github_actions: bool,
stdout: W,
stderr: W,
prefix: StyledObject<String>,
) -> PrefixedUI<W> {
let mut prefixed_ui = PrefixedUI::new(color_config, stdout, stderr)
.with_output_prefix(prefix.clone())
// TODO: we can probably come up with a more ergonomic way to achieve this
.with_error_prefix(
Style::new().apply_to(format!("{}ERROR: ", color_config.apply(prefix.clone()))),
)
.with_warn_prefix(prefix);
if is_github_actions {
prefixed_ui = prefixed_ui
.with_error_prefix(Style::new().apply_to("[ERROR] ".to_string()))
.with_warn_prefix(Style::new().apply_to("[WARN] ".to_string()));
}
prefixed_ui
}
/// Only used for the hashing comparison between Rust and Go. After port,
/// should delete
pub fn into_task_hash_tracker(self) -> TaskHashTrackerState {
self.task_hasher.into_task_hash_tracker_state()
}
pub fn dry_run(&mut self) {
self.dry = true;
// No need to start a UI on dry run
self.ui_sender = None;
}
}
fn turbo_regex() -> &'static Regex {
static RE: OnceLock<Regex> = OnceLock::new();
RE.get_or_init(|| Regex::new(r"(?:^|\s)turbo(?:$|\s)").unwrap())
}