-
Notifications
You must be signed in to change notification settings - Fork 28.2k
/
Copy pathutils.rs
508 lines (458 loc) · 15.7 KB
/
utils.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
use std::{future::Future, ops::Deref, path::PathBuf, sync::Arc, time::Duration};
use anyhow::{anyhow, Context, Result};
use napi::{
bindgen_prelude::{External, ToNapiValue},
threadsafe_function::{ThreadSafeCallContext, ThreadsafeFunction, ThreadsafeFunctionCallMode},
JsFunction, JsObject, JsUnknown, NapiRaw, NapiValue, Status,
};
use rustc_hash::FxHashMap;
use serde::Serialize;
use turbo_tasks::{
get_effects, task_statistics::TaskStatisticsApi, trace::TraceRawVcs, Effects, OperationVc,
ReadRef, TaskId, TryJoinIterExt, TurboTasks, TurboTasksApi, UpdateInfo, Vc, VcValueType,
};
use turbo_tasks_backend::{
default_backing_storage, noop_backing_storage, DefaultBackingStorage, GitVersionInfo,
NoopBackingStorage,
};
use turbo_tasks_fs::FileContent;
use turbopack_core::{
diagnostics::{Diagnostic, DiagnosticContextExt, PlainDiagnostic},
error::PrettyPrintError,
issue::{
IssueDescriptionExt, IssueSeverity, PlainIssue, PlainIssueSource, PlainSource, StyledString,
},
source_pos::SourcePos,
};
use crate::util::log_internal_error_and_inform;
#[derive(Clone)]
pub enum NextTurboTasks {
Memory(Arc<TurboTasks<turbo_tasks_backend::TurboTasksBackend<NoopBackingStorage>>>),
PersistentCaching(
Arc<TurboTasks<turbo_tasks_backend::TurboTasksBackend<DefaultBackingStorage>>>,
),
}
impl NextTurboTasks {
pub fn dispose_root_task(&self, task: TaskId) {
match self {
NextTurboTasks::Memory(turbo_tasks) => turbo_tasks.dispose_root_task(task),
NextTurboTasks::PersistentCaching(turbo_tasks) => turbo_tasks.dispose_root_task(task),
}
}
pub fn spawn_root_task<T, F, Fut>(&self, functor: F) -> TaskId
where
T: Send,
F: Fn() -> Fut + Send + Sync + Clone + 'static,
Fut: Future<Output = Result<Vc<T>>> + Send,
{
match self {
NextTurboTasks::Memory(turbo_tasks) => turbo_tasks.spawn_root_task(functor),
NextTurboTasks::PersistentCaching(turbo_tasks) => turbo_tasks.spawn_root_task(functor),
}
}
pub async fn run_once<T: TraceRawVcs + Send + 'static>(
&self,
future: impl Future<Output = Result<T>> + Send + 'static,
) -> Result<T> {
match self {
NextTurboTasks::Memory(turbo_tasks) => turbo_tasks.run_once(future).await,
NextTurboTasks::PersistentCaching(turbo_tasks) => turbo_tasks.run_once(future).await,
}
}
pub fn spawn_once_task<T, Fut>(&self, future: Fut) -> TaskId
where
T: Send,
Fut: Future<Output = Result<Vc<T>>> + Send + 'static,
{
match self {
NextTurboTasks::Memory(turbo_tasks) => turbo_tasks.spawn_once_task(future),
NextTurboTasks::PersistentCaching(turbo_tasks) => turbo_tasks.spawn_once_task(future),
}
}
pub async fn aggregated_update_info(
&self,
aggregation: Duration,
timeout: Duration,
) -> Option<UpdateInfo> {
match self {
NextTurboTasks::Memory(turbo_tasks) => {
turbo_tasks
.aggregated_update_info(aggregation, timeout)
.await
}
NextTurboTasks::PersistentCaching(turbo_tasks) => {
turbo_tasks
.aggregated_update_info(aggregation, timeout)
.await
}
}
}
pub async fn get_or_wait_aggregated_update_info(&self, aggregation: Duration) -> UpdateInfo {
match self {
NextTurboTasks::Memory(turbo_tasks) => {
turbo_tasks
.get_or_wait_aggregated_update_info(aggregation)
.await
}
NextTurboTasks::PersistentCaching(turbo_tasks) => {
turbo_tasks
.get_or_wait_aggregated_update_info(aggregation)
.await
}
}
}
pub async fn stop_and_wait(&self) {
match self {
NextTurboTasks::Memory(turbo_tasks) => turbo_tasks.stop_and_wait().await,
NextTurboTasks::PersistentCaching(turbo_tasks) => turbo_tasks.stop_and_wait().await,
}
}
pub fn task_statistics(&self) -> &TaskStatisticsApi {
match self {
NextTurboTasks::Memory(turbo_tasks) => turbo_tasks.task_statistics(),
NextTurboTasks::PersistentCaching(turbo_tasks) => turbo_tasks.task_statistics(),
}
}
}
pub fn create_turbo_tasks(
output_path: PathBuf,
persistent_caching: bool,
_memory_limit: usize,
dependency_tracking: bool,
) -> Result<NextTurboTasks> {
Ok(if persistent_caching {
let version_info = GitVersionInfo {
describe: env!("VERGEN_GIT_DESCRIBE"),
dirty: option_env!("CI").is_none_or(|value| value.is_empty())
&& env!("VERGEN_GIT_DIRTY") == "true",
};
NextTurboTasks::PersistentCaching(TurboTasks::new(
turbo_tasks_backend::TurboTasksBackend::new(
turbo_tasks_backend::BackendOptions {
storage_mode: Some(if std::env::var("TURBO_ENGINE_READ_ONLY").is_ok() {
turbo_tasks_backend::StorageMode::ReadOnly
} else {
turbo_tasks_backend::StorageMode::ReadWrite
}),
dependency_tracking,
..Default::default()
},
default_backing_storage(&output_path.join("cache/turbopack"), &version_info)?,
),
))
} else {
NextTurboTasks::Memory(TurboTasks::new(
turbo_tasks_backend::TurboTasksBackend::new(
turbo_tasks_backend::BackendOptions {
storage_mode: None,
dependency_tracking,
..Default::default()
},
noop_backing_storage(),
),
))
})
}
/// A helper type to hold both a Vc operation and the TurboTasks root process.
/// Without this, we'd need to pass both individually all over the place
#[derive(Clone)]
pub struct VcArc<T> {
turbo_tasks: NextTurboTasks,
/// The Vc. Must be unresolved, otherwise you are referencing an inactive operation.
vc: OperationVc<T>,
}
impl<T> VcArc<T> {
pub fn new(turbo_tasks: NextTurboTasks, vc: OperationVc<T>) -> Self {
Self { turbo_tasks, vc }
}
pub fn turbo_tasks(&self) -> &NextTurboTasks {
&self.turbo_tasks
}
}
impl<T> Deref for VcArc<T> {
type Target = OperationVc<T>;
fn deref(&self) -> &Self::Target {
&self.vc
}
}
pub fn serde_enum_to_string<T: Serialize>(value: &T) -> Result<String> {
Ok(serde_json::to_value(value)?
.as_str()
.context("value must serialize to a string")?
.to_string())
}
/// The root of our turbopack computation.
pub struct RootTask {
#[allow(dead_code)]
turbo_tasks: NextTurboTasks,
#[allow(dead_code)]
task_id: Option<TaskId>,
}
impl Drop for RootTask {
fn drop(&mut self) {
// TODO stop the root task
}
}
#[napi]
pub fn root_task_dispose(
#[napi(ts_arg_type = "{ __napiType: \"RootTask\" }")] mut root_task: External<RootTask>,
) -> napi::Result<()> {
if let Some(task) = root_task.task_id.take() {
root_task.turbo_tasks.dispose_root_task(task);
}
Ok(())
}
pub async fn get_issues<T: Send>(source: OperationVc<T>) -> Result<Arc<Vec<ReadRef<PlainIssue>>>> {
let issues = source.peek_issues_with_path().await?;
Ok(Arc::new(issues.get_plain_issues().await?))
}
/// Reads the [turbopack_core::diagnostics::Diagnostic] held
/// by the given source and returns it as a
/// [turbopack_core::diagnostics::PlainDiagnostic]. It does
/// not consume any Diagnostics held by the source.
pub async fn get_diagnostics<T: Send>(
source: OperationVc<T>,
) -> Result<Arc<Vec<ReadRef<PlainDiagnostic>>>> {
let captured_diags = source.peek_diagnostics().await?;
let mut diags = captured_diags
.diagnostics
.iter()
.map(|d| d.into_plain())
.try_join()
.await?;
diags.sort();
Ok(Arc::new(diags))
}
#[napi(object)]
pub struct NapiIssue {
pub severity: String,
pub stage: String,
pub file_path: String,
pub title: serde_json::Value,
pub description: Option<serde_json::Value>,
pub detail: Option<serde_json::Value>,
pub source: Option<NapiIssueSource>,
pub documentation_link: String,
pub sub_issues: Vec<NapiIssue>,
}
impl From<&PlainIssue> for NapiIssue {
fn from(issue: &PlainIssue) -> Self {
Self {
description: issue
.description
.as_ref()
.map(|styled| serde_json::to_value(StyledStringSerialize::from(styled)).unwrap()),
stage: issue.stage.to_string(),
file_path: issue.file_path.to_string(),
detail: issue
.detail
.as_ref()
.map(|styled| serde_json::to_value(StyledStringSerialize::from(styled)).unwrap()),
documentation_link: issue.documentation_link.to_string(),
severity: issue.severity.as_str().to_string(),
source: issue.source.as_ref().map(|source| source.into()),
title: serde_json::to_value(StyledStringSerialize::from(&issue.title)).unwrap(),
sub_issues: issue
.sub_issues
.iter()
.map(|issue| (&**issue).into())
.collect(),
}
}
}
#[derive(Serialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum StyledStringSerialize<'a> {
Line {
value: Vec<StyledStringSerialize<'a>>,
},
Stack {
value: Vec<StyledStringSerialize<'a>>,
},
Text {
value: &'a str,
},
Code {
value: &'a str,
},
Strong {
value: &'a str,
},
}
impl<'a> From<&'a StyledString> for StyledStringSerialize<'a> {
fn from(value: &'a StyledString) -> Self {
match value {
StyledString::Line(parts) => StyledStringSerialize::Line {
value: parts.iter().map(|p| p.into()).collect(),
},
StyledString::Stack(parts) => StyledStringSerialize::Stack {
value: parts.iter().map(|p| p.into()).collect(),
},
StyledString::Text(string) => StyledStringSerialize::Text { value: string },
StyledString::Code(string) => StyledStringSerialize::Code { value: string },
StyledString::Strong(string) => StyledStringSerialize::Strong { value: string },
}
}
}
#[napi(object)]
pub struct NapiIssueSource {
pub source: NapiSource,
pub range: Option<NapiIssueSourceRange>,
}
impl From<&PlainIssueSource> for NapiIssueSource {
fn from(
PlainIssueSource {
asset: source,
range,
}: &PlainIssueSource,
) -> Self {
Self {
source: (&**source).into(),
range: range.as_ref().map(|range| range.into()),
}
}
}
#[napi(object)]
pub struct NapiIssueSourceRange {
pub start: NapiSourcePos,
pub end: NapiSourcePos,
}
impl From<&(SourcePos, SourcePos)> for NapiIssueSourceRange {
fn from((start, end): &(SourcePos, SourcePos)) -> Self {
Self {
start: (*start).into(),
end: (*end).into(),
}
}
}
#[napi(object)]
pub struct NapiSource {
pub ident: String,
pub content: Option<String>,
}
impl From<&PlainSource> for NapiSource {
fn from(source: &PlainSource) -> Self {
Self {
ident: source.ident.to_string(),
content: match &*source.content {
FileContent::Content(content) => match content.content().to_str() {
Ok(str) => Some(str.into_owned()),
Err(_) => None,
},
FileContent::NotFound => None,
},
}
}
}
#[napi(object)]
pub struct NapiSourcePos {
pub line: u32,
pub column: u32,
}
impl From<SourcePos> for NapiSourcePos {
fn from(pos: SourcePos) -> Self {
Self {
line: pos.line,
column: pos.column,
}
}
}
#[napi(object)]
pub struct NapiDiagnostic {
pub category: String,
pub name: String,
#[napi(ts_type = "Record<string, string>")]
pub payload: FxHashMap<String, String>,
}
impl NapiDiagnostic {
pub fn from(diagnostic: &PlainDiagnostic) -> Self {
Self {
category: diagnostic.category.to_string(),
name: diagnostic.name.to_string(),
payload: diagnostic
.payload
.iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect(),
}
}
}
pub struct TurbopackResult<T: ToNapiValue> {
pub result: T,
pub issues: Vec<NapiIssue>,
pub diagnostics: Vec<NapiDiagnostic>,
}
impl<T: ToNapiValue> ToNapiValue for TurbopackResult<T> {
unsafe fn to_napi_value(
env: napi::sys::napi_env,
val: Self,
) -> napi::Result<napi::sys::napi_value> {
let mut obj = napi::Env::from_raw(env).create_object()?;
let result = T::to_napi_value(env, val.result)?;
let result = JsUnknown::from_raw(env, result)?;
if matches!(result.get_type()?, napi::ValueType::Object) {
// SAFETY: We know that result is an object, so we can cast it to a JsObject
let result = unsafe { result.cast::<JsObject>() };
for key in JsObject::keys(&result)? {
let value: JsUnknown = result.get_named_property(&key)?;
obj.set_named_property(&key, value)?;
}
}
obj.set_named_property("issues", val.issues)?;
obj.set_named_property("diagnostics", val.diagnostics)?;
Ok(obj.raw())
}
}
pub fn subscribe<T: 'static + Send + Sync, F: Future<Output = Result<T>> + Send, V: ToNapiValue>(
turbo_tasks: NextTurboTasks,
func: JsFunction,
handler: impl 'static + Sync + Send + Clone + Fn() -> F,
mapper: impl 'static + Sync + Send + FnMut(ThreadSafeCallContext<T>) -> napi::Result<Vec<V>>,
) -> napi::Result<External<RootTask>> {
let func: ThreadsafeFunction<T> = func.create_threadsafe_function(0, mapper)?;
let task_id = turbo_tasks.spawn_root_task(move || {
let handler = handler.clone();
let func = func.clone();
Box::pin(async move {
let result = handler().await;
let status = func.call(
result.map_err(|e| {
log_internal_error_and_inform(&e);
napi::Error::from_reason(PrettyPrintError(&e).to_string())
}),
ThreadsafeFunctionCallMode::NonBlocking,
);
if !matches!(status, Status::Ok) {
let error = anyhow!("Error calling JS function: {}", status);
eprintln!("{}", error);
return Err::<Vc<()>, _>(error);
}
Ok(Default::default())
})
});
Ok(External::new(RootTask {
turbo_tasks,
task_id: Some(task_id),
}))
}
// Await the source and return fatal issues if there are any, otherwise
// propagate any actual error results.
pub async fn strongly_consistent_catch_collectables<R: VcValueType + Send>(
source_op: OperationVc<R>,
) -> Result<(
Option<ReadRef<R>>,
Arc<Vec<ReadRef<PlainIssue>>>,
Arc<Vec<ReadRef<PlainDiagnostic>>>,
Arc<Effects>,
)> {
let result = source_op.read_strongly_consistent().await;
let issues = get_issues(source_op).await?;
let diagnostics = get_diagnostics(source_op).await?;
let effects = Arc::new(get_effects(source_op).await?);
let result = if result.is_err() && issues.iter().any(|i| i.severity <= IssueSeverity::Error) {
None
} else {
Some(result?)
};
Ok((result, issues, diagnostics, effects))
}