Skip to content

Commit

Permalink
shitty fix for import cycles
Browse files Browse the repository at this point in the history
  • Loading branch information
ForsakenHarmony committed Jul 10, 2023
1 parent d6aafbf commit 41f9e62
Show file tree
Hide file tree
Showing 20 changed files with 280 additions and 87 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/turbo-tasks-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ pub fn rerun_if_glob(globs: &str, root: &str) {
let cwd = env::current_dir().unwrap();
let globs = cwd.join(globs.replace('/', PATH_SEP.to_string().as_str()));
let root = cwd.join(root.replace('/', PATH_SEP.to_string().as_str()));
println!("cargo:rerun-if-changed={}", root.display());
let mut seen = HashSet::from([root]);
for entry in glob(globs.to_str().unwrap()).unwrap() {
let path = entry.unwrap();
Expand Down
10 changes: 10 additions & 0 deletions crates/turbo-tasks-bytes/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ pub enum SingleValue<T> {
Single(T),
}

impl<T: fmt::Debug> fmt::Debug for SingleValue<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SingleValue::None => f.debug_struct("SingleValue::None").finish(),
SingleValue::Multiple => f.debug_struct("SingleValue::Multiple").finish(),
SingleValue::Single(v) => f.debug_tuple("SingleValue::Single").field(v).finish(),
}
}
}

impl<T: Clone + Send, S: StreamTrait<Item = T> + Send + Unpin + 'static> From<S> for Stream<T> {
fn from(source: S) -> Self {
Self::new_open(vec![], Box::new(source))
Expand Down
66 changes: 65 additions & 1 deletion crates/turbo-tasks/src/join_iter_ext.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::future::{Future, IntoFuture};
use std::{
future::{Future, IntoFuture},
pin::Pin,
task::Poll,
};

use anyhow::Result;
use futures::{
Expand Down Expand Up @@ -108,3 +112,63 @@ where
}
}
}

pin_project! {
/// Future for the [TryFlatJoinIterExt::try_flat_join] method.
pub struct TryFlatJoin<F>
where
F: Future,
{
#[pin]
inner: JoinAll<F>,
}
}

impl<T, F> Future for TryFlatJoin<F>
where
F: Future<Output = Result<Option<T>>>,
{
type Output = Result<Vec<T>>;

fn poll(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Self::Output> {
match self.project().inner.poll_unpin(cx) {
Poll::Ready(res) => Poll::Ready(
res.into_iter()
.filter_map(|r| match r {
Ok(Some(v)) => Some(Ok(v)),
Ok(None) => None,
Err(e) => Some(Err(e)),
})
.collect::<Result<Vec<_>>>(),
),
Poll::Pending => Poll::Pending,
}
}
}

pub trait TryFlatJoinIterExt<T, F>: Iterator
where
F: Future<Output = Result<Option<T>>>,
{
/// Returns a future that resolves to a vector of the outputs of the futures
/// in the iterator, or to an error if one of the futures fail.
///
/// It also filters out any `Option::None`s
///
/// Unlike `Futures::future::try_join_all`, this returns the Error that
/// occurs first in the list of futures, not the first to fail in time.
fn try_flat_join(self) -> TryFlatJoin<F>;
}

impl<T, F, IF, It> TryFlatJoinIterExt<T, F> for It
where
F: Future<Output = Result<Option<T>>>,
IF: IntoFuture<Output = Result<Option<T>>, IntoFuture = F>,
It: Iterator<Item = IF>,
{
fn try_flat_join(self) -> TryFlatJoin<F> {
TryFlatJoin {
inner: join_all(self.map(|f| f.into_future())),
}
}
}
2 changes: 1 addition & 1 deletion crates/turbo-tasks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub use id::{
pub use invalidation::{
DynamicEqHash, InvalidationReason, InvalidationReasonKind, InvalidationReasonSet,
};
pub use join_iter_ext::{JoinIterExt, TryJoinIterExt};
pub use join_iter_ext::{JoinIterExt, TryFlatJoinIterExt, TryJoinIterExt};
pub use manager::{
dynamic_call, emit, get_invalidator, mark_finished, mark_stateful, run_once,
run_once_with_reason, spawn_blocking, spawn_thread, trait_call, turbo_tasks, Invalidator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,13 @@ function interopEsm(
getters[key] = createGetter(raw, key);
}
}

// this is not really correct
// we should set the `default` getter if the imported module is a `.cjs file`
if (!(allowExportDefault && "default" in getters)) {
getters["default"] = () => raw;
}

esm(ns, getters);
return ns;
}
Expand Down
1 change: 1 addition & 0 deletions crates/turbopack-ecmascript/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ bench = false
[dependencies]
anyhow = { workspace = true }
async-trait = { workspace = true }
futures = { workspace = true }
indexmap = { workspace = true }
indoc = { workspace = true }
lazy_static = { workspace = true }
Expand Down
6 changes: 3 additions & 3 deletions crates/turbopack-ecmascript/src/chunk/placeable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use turbopack_core::{
};

use super::{item::EcmascriptChunkItemVc, EcmascriptChunkingContextVc};
use crate::references::{async_module::OptionAsyncModuleOptionsVc, esm::EsmExportsVc};
use crate::references::{async_module::OptionAsyncModuleVc, esm::EsmExportsVc};

#[turbo_tasks::value_trait]
pub trait EcmascriptChunkPlaceable: ChunkableModule + Module + Asset {
fn as_chunk_item(&self, context: EcmascriptChunkingContextVc) -> EcmascriptChunkItemVc;
fn get_exports(&self) -> EcmascriptExportsVc;
fn get_async_module_options(&self) -> OptionAsyncModuleOptionsVc {
OptionAsyncModuleOptionsVc::cell(None)
fn get_async_module(&self) -> OptionAsyncModuleVc {
OptionAsyncModuleVc::none()
}
}

Expand Down
18 changes: 8 additions & 10 deletions crates/turbopack-ecmascript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ use crate::{
code_gen::CodeGenerateable,
references::{
analyze_ecmascript_module,
async_module::{OptionAsyncModuleOptionsReadRef, OptionAsyncModuleOptionsVc},
async_module::{OptionAsyncModuleReadRef, OptionAsyncModuleVc},
},
transform::remove_shebang,
};
Expand Down Expand Up @@ -139,7 +139,7 @@ struct MemoizedSuccessfulAnalysis {
operation: RawVc,
references: AssetReferencesReadRef,
exports: EcmascriptExportsReadRef,
async_module_options: OptionAsyncModuleOptionsReadRef,
async_module: OptionAsyncModuleReadRef,
}

pub struct EcmascriptModuleAssetBuilder {
Expand Down Expand Up @@ -317,13 +317,13 @@ impl EcmascriptModuleAssetVc {
// We need to store the ReadRefs since we want to keep a snapshot.
references: result_value.references.await?,
exports: result_value.exports.await?,
async_module_options: result_value.async_module_options.await?,
async_module: result_value.async_module.await?,
}));
} else if let Some(MemoizedSuccessfulAnalysis {
operation,
references,
exports,
async_module_options,
async_module,
}) = &*this.last_successful_analysis.get()
{
// It's important to connect to the last operation here to keep it active, so
Expand All @@ -333,7 +333,7 @@ impl EcmascriptModuleAssetVc {
references: ReadRef::cell(references.clone()),
exports: ReadRef::cell(exports.clone()),
code_generation: result_value.code_generation,
async_module_options: ReadRef::cell(async_module_options.clone()),
async_module: ReadRef::cell(async_module.clone()),
successful: false,
}
.cell());
Expand Down Expand Up @@ -454,10 +454,8 @@ impl EcmascriptChunkPlaceable for EcmascriptModuleAsset {
}

#[turbo_tasks::function]
async fn get_async_module_options(
self_vc: EcmascriptModuleAssetVc,
) -> Result<OptionAsyncModuleOptionsVc> {
Ok(self_vc.failsafe_analyze().await?.async_module_options)
async fn get_async_module(self_vc: EcmascriptModuleAssetVc) -> Result<OptionAsyncModuleVc> {
Ok(self_vc.failsafe_analyze().await?.async_module)
}
}

Expand Down Expand Up @@ -530,7 +528,7 @@ impl EcmascriptChunkItem for ModuleChunkItem {
) -> Result<EcmascriptChunkItemContentVc> {
let this = self_vc.await?;
let content = this.module.module_content(this.context, availability_info);
let async_module_options = this.module.get_async_module_options();
let async_module_options = this.module.get_async_module().module_options();

Ok(EcmascriptChunkItemContentVc::new(
content,
Expand Down
Loading

0 comments on commit 41f9e62

Please sign in to comment.