Unverified Commit caa595eb authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: handle removed routes (#77890)

### Why?

Before `get_endpoints` gives you a lot of `Vc<Endpoint>`, which we keep around and subscribe to. That is a problem as endpoint can also vanish at any time. e. g. when the folder is removed.
`Vc<Endpoint>` was not able to represent this state. The subscription throw an error as the Vc is no longer existant. So the error was technically correct here (not a bug in turbo-tasks).
But in practice we don't want to have this error. We want to have a clean end of the subscription without any error. That's why I change it to `Vc<Option<Endpoint>>`, which allows to represent the non-existance of an endpoint.
This can (resp. need to be) handled in subscriptions, which can report e. g. a `NotFound` result or a change event (for server_changed).

### What?

avoid having inactive Vcs when routes are removed.

instead use an `Option<Endpoint>` to allow respresenting removed endpoints and handle these in subscriptions correctly.
parent 87d7d8eb
...@@ -3,10 +3,11 @@ use std::{ops::Deref, sync::Arc}; ...@@ -3,10 +3,11 @@ use std::{ops::Deref, sync::Arc};
use anyhow::Result; use anyhow::Result;
use napi::{bindgen_prelude::External, JsFunction}; use napi::{bindgen_prelude::External, JsFunction};
use next_api::{ use next_api::{
operation::OptionEndpoint,
paths::ServerPath, paths::ServerPath,
route::{ route::{
endpoint_server_changed_operation, endpoint_write_to_disk_operation, Endpoint, endpoint_client_changed_operation, endpoint_server_changed_operation,
EndpointOutputPaths, endpoint_write_to_disk_operation, EndpointOutputPaths,
}, },
}; };
use tracing::Instrument; use tracing::Instrument;
...@@ -71,7 +72,7 @@ impl From<Option<EndpointOutputPaths>> for NapiWrittenEndpoint { ...@@ -71,7 +72,7 @@ impl From<Option<EndpointOutputPaths>> for NapiWrittenEndpoint {
server_paths: server_paths.into_iter().map(From::from).collect(), server_paths: server_paths.into_iter().map(From::from).collect(),
..Default::default() ..Default::default()
}, },
None => Self { Some(EndpointOutputPaths::NotFound) | None => Self {
r#type: "none".to_string(), r#type: "none".to_string(),
..Default::default() ..Default::default()
}, },
...@@ -85,10 +86,10 @@ impl From<Option<EndpointOutputPaths>> for NapiWrittenEndpoint { ...@@ -85,10 +86,10 @@ impl From<Option<EndpointOutputPaths>> for NapiWrittenEndpoint {
// some async functions (in this case `endpoint_write_to_disk`) can cause // some async functions (in this case `endpoint_write_to_disk`) can cause
// higher-ranked lifetime errors. See https://github.com/rust-lang/rust/issues/102211 // higher-ranked lifetime errors. See https://github.com/rust-lang/rust/issues/102211
// 2. the type_complexity clippy lint. // 2. the type_complexity clippy lint.
pub struct ExternalEndpoint(pub VcArc<Box<dyn Endpoint>>); pub struct ExternalEndpoint(pub VcArc<OptionEndpoint>);
impl Deref for ExternalEndpoint { impl Deref for ExternalEndpoint {
type Target = VcArc<Box<dyn Endpoint>>; type Target = VcArc<OptionEndpoint>;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.0 &self.0
...@@ -105,7 +106,7 @@ struct WrittenEndpointWithIssues { ...@@ -105,7 +106,7 @@ struct WrittenEndpointWithIssues {
#[turbo_tasks::function(operation)] #[turbo_tasks::function(operation)]
async fn get_written_endpoint_with_issues_operation( async fn get_written_endpoint_with_issues_operation(
endpoint_op: OperationVc<Box<dyn Endpoint>>, endpoint_op: OperationVc<OptionEndpoint>,
) -> Result<Vc<WrittenEndpointWithIssues>> { ) -> Result<Vc<WrittenEndpointWithIssues>> {
let write_to_disk_op = endpoint_write_to_disk_operation(endpoint_op); let write_to_disk_op = endpoint_write_to_disk_operation(endpoint_op);
let (written, issues, diagnostics, effects) = let (written, issues, diagnostics, effects) =
...@@ -214,7 +215,7 @@ impl Eq for EndpointIssuesAndDiags {} ...@@ -214,7 +215,7 @@ impl Eq for EndpointIssuesAndDiags {}
#[turbo_tasks::function(operation)] #[turbo_tasks::function(operation)]
async fn subscribe_issues_and_diags_operation( async fn subscribe_issues_and_diags_operation(
endpoint_op: OperationVc<Box<dyn Endpoint>>, endpoint_op: OperationVc<OptionEndpoint>,
should_include_issues: bool, should_include_issues: bool,
) -> Result<Vc<EndpointIssuesAndDiags>> { ) -> Result<Vc<EndpointIssuesAndDiags>> {
let changed_op = endpoint_server_changed_operation(endpoint_op); let changed_op = endpoint_server_changed_operation(endpoint_op);
...@@ -241,13 +242,6 @@ async fn subscribe_issues_and_diags_operation( ...@@ -241,13 +242,6 @@ async fn subscribe_issues_and_diags_operation(
} }
} }
#[turbo_tasks::function(operation)]
fn endpoint_client_changed_operation(
endpoint_op: OperationVc<Box<dyn Endpoint>>,
) -> Vc<Completion> {
endpoint_op.connect().client_changed()
}
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")] #[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn endpoint_client_changed_subscribe( pub fn endpoint_client_changed_subscribe(
#[napi(ts_arg_type = "{ __napiType: \"Endpoint\" }")] endpoint: External<ExternalEndpoint>, #[napi(ts_arg_type = "{ __napiType: \"Endpoint\" }")] endpoint: External<ExternalEndpoint>,
......
...@@ -9,7 +9,8 @@ use napi::{ ...@@ -9,7 +9,8 @@ use napi::{
use next_api::{ use next_api::{
entrypoints::Entrypoints, entrypoints::Entrypoints,
operation::{ operation::{
EntrypointsOperation, InstrumentationOperation, MiddlewareOperation, RouteOperation, EntrypointsOperation, InstrumentationOperation, MiddlewareOperation, OptionEndpoint,
RouteOperation,
}, },
project::{ project::{
DefineEnv, DraftModeOptions, PartialProjectOptions, Project, ProjectContainer, DefineEnv, DraftModeOptions, PartialProjectOptions, Project, ProjectContainer,
...@@ -580,7 +581,7 @@ pub struct NapiRoute { ...@@ -580,7 +581,7 @@ pub struct NapiRoute {
impl NapiRoute { impl NapiRoute {
fn from_route(pathname: String, value: RouteOperation, turbo_tasks: &NextTurboTasks) -> Self { fn from_route(pathname: String, value: RouteOperation, turbo_tasks: &NextTurboTasks) -> Self {
let convert_endpoint = |endpoint: OperationVc<Box<dyn Endpoint>>| { let convert_endpoint = |endpoint: OperationVc<OptionEndpoint>| {
Some(External::new(ExternalEndpoint(VcArc::new( Some(External::new(ExternalEndpoint(VcArc::new(
turbo_tasks.clone(), turbo_tasks.clone(),
endpoint, endpoint,
......
This diff is collapsed.
...@@ -10,7 +10,7 @@ use turbopack_core::{ ...@@ -10,7 +10,7 @@ use turbopack_core::{
output::OutputAssets, output::OutputAssets,
}; };
use crate::{paths::ServerPath, project::Project}; use crate::{operation::OptionEndpoint, paths::ServerPath, project::Project};
#[derive( #[derive(
TraceRawVcs, TraceRawVcs,
...@@ -97,17 +97,36 @@ async fn endpoint_output_assets_operation( ...@@ -97,17 +97,36 @@ async fn endpoint_output_assets_operation(
} }
#[turbo_tasks::function(operation)] #[turbo_tasks::function(operation)]
pub fn endpoint_write_to_disk_operation( pub async fn endpoint_write_to_disk_operation(
endpoint: OperationVc<Box<dyn Endpoint>>, endpoint: OperationVc<OptionEndpoint>,
) -> Vc<EndpointOutputPaths> { ) -> Result<Vc<EndpointOutputPaths>> {
endpoint_write_to_disk(endpoint.connect()) Ok(if let Some(endpoint) = *endpoint.connect().await? {
endpoint_write_to_disk(*endpoint)
} else {
EndpointOutputPaths::NotFound.cell()
})
}
#[turbo_tasks::function(operation)]
pub async fn endpoint_server_changed_operation(
endpoint: OperationVc<OptionEndpoint>,
) -> Result<Vc<Completion>> {
Ok(if let Some(endpoint) = *endpoint.connect().await? {
endpoint.server_changed()
} else {
Completion::new()
})
} }
#[turbo_tasks::function(operation)] #[turbo_tasks::function(operation)]
pub fn endpoint_server_changed_operation( pub async fn endpoint_client_changed_operation(
endpoint: OperationVc<Box<dyn Endpoint>>, endpoint: OperationVc<OptionEndpoint>,
) -> Vc<Completion> { ) -> Result<Vc<Completion>> {
endpoint.connect().server_changed() Ok(if let Some(endpoint) = *endpoint.connect().await? {
endpoint.client_changed()
} else {
Completion::new()
})
} }
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
...@@ -131,6 +150,7 @@ pub enum EndpointOutputPaths { ...@@ -131,6 +150,7 @@ pub enum EndpointOutputPaths {
server_paths: Vec<ServerPath>, server_paths: Vec<ServerPath>,
client_paths: Vec<RcStr>, client_paths: Vec<RcStr>,
}, },
NotFound,
} }
/// The routes as map from pathname to route. (pathname includes the leading /// The routes as map from pathname to route. (pathname includes the leading
......
...@@ -50,6 +50,8 @@ describe(`app-dir-hmr`, () => { ...@@ -50,6 +50,8 @@ describe(`app-dir-hmr`, () => {
// The new page should be rendered // The new page should be rendered
const newHTML = await next.render('/folder-renamed') const newHTML = await next.render('/folder-renamed')
expect(newHTML).toContain('Hello') expect(newHTML).toContain('Hello')
expect(next.cliOutput).not.toContain('FATAL')
} finally { } finally {
// Rename it back // Rename it back
await next.renameFolder('app/folder-renamed', 'app/folder') await next.renameFolder('app/folder-renamed', 'app/folder')
...@@ -89,7 +91,6 @@ describe(`app-dir-hmr`, () => { ...@@ -89,7 +91,6 @@ describe(`app-dir-hmr`, () => {
// details are unimportant. // details are unimportant.
expect(fastRefreshLogs).toEqual( expect(fastRefreshLogs).toEqual(
expect.arrayContaining([ expect.arrayContaining([
{ source: 'log', message: '[Fast Refresh] rebuilding' },
{ {
source: 'log', source: 'log',
message: expect.stringContaining('[Fast Refresh] done in '), message: expect.stringContaining('[Fast Refresh] done in '),
...@@ -102,6 +103,8 @@ describe(`app-dir-hmr`, () => { ...@@ -102,6 +103,8 @@ describe(`app-dir-hmr`, () => {
await retry(async () => { await retry(async () => {
expect(await browser.elementByCss('p').text()).toBe('mac') expect(await browser.elementByCss('p').text()).toBe('mac')
}) })
expect(next.cliOutput).not.toContain('FATAL')
}) })
it.each(['node', 'node-module-var', 'edge', 'edge-module-var'])( it.each(['node', 'node-module-var', 'edge', 'edge-module-var'])(
...@@ -118,7 +121,7 @@ describe(`app-dir-hmr`, () => { ...@@ -118,7 +121,7 @@ describe(`app-dir-hmr`, () => {
expect(logs).toEqual( expect(logs).toEqual(
expect.arrayContaining([ expect.arrayContaining([
expect.objectContaining({ expect.objectContaining({
message: '[Fast Refresh] rebuilding', message: expect.stringContaining('[Fast Refresh] done'),
source: 'log', source: 'log',
}), }),
]) ])
...@@ -143,6 +146,8 @@ describe(`app-dir-hmr`, () => { ...@@ -143,6 +146,8 @@ describe(`app-dir-hmr`, () => {
await retry(async () => { await retry(async () => {
expect(await browser.elementByCss('p').text()).toBe('mac') expect(await browser.elementByCss('p').text()).toBe('mac')
}) })
expect(next.cliOutput).not.toContain('FATAL')
} }
) )
...@@ -168,10 +173,6 @@ describe(`app-dir-hmr`, () => { ...@@ -168,10 +173,6 @@ describe(`app-dir-hmr`, () => {
// TODO: Should assert on all logs but these are cluttered with logs from our test utils (e.g. playwright tracing or webdriver) // TODO: Should assert on all logs but these are cluttered with logs from our test utils (e.g. playwright tracing or webdriver)
expect(logs).toEqual( expect(logs).toEqual(
expect.arrayContaining([ expect.arrayContaining([
{
message: '[Fast Refresh] rebuilding',
source: 'log',
},
{ {
message: expect.stringContaining('[Fast Refresh] done in'), message: expect.stringContaining('[Fast Refresh] done in'),
source: 'log', source: 'log',
...@@ -187,6 +188,8 @@ describe(`app-dir-hmr`, () => { ...@@ -187,6 +188,8 @@ describe(`app-dir-hmr`, () => {
) )
// No MPA navigation triggered // No MPA navigation triggered
expect(await browser.eval('window.__TEST_NO_RELOAD')).toEqual(true) expect(await browser.eval('window.__TEST_NO_RELOAD')).toEqual(true)
expect(next.cliOutput).not.toContain('FATAL')
}) })
}) })
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment