diff --git a/editor/src/messages/portfolio/document/document_message_handler.rs b/editor/src/messages/portfolio/document/document_message_handler.rs index b5babf92b71..057385a2864 100644 --- a/editor/src/messages/portfolio/document/document_message_handler.rs +++ b/editor/src/messages/portfolio/document/document_message_handler.rs @@ -4922,6 +4922,8 @@ mod document_message_handler_tests { let frontend_messages = editor.editor.handle_message(Message::Wake); assert!(!frontend_messages.contains(&FrontendMessage::UpdateRenderStatus { status: RenderStatus::Error })); + // Erroring nodes get hidden so the rest still renders, so the render alone doesn't show the graph compiled + assert!(editor.active_document().network_interface.resolved_types.node_graph_errors.is_empty()); } #[tokio::test] diff --git a/editor/src/messages/portfolio/document/node_graph/node_graph_message_handler.rs b/editor/src/messages/portfolio/document/node_graph/node_graph_message_handler.rs index 4b791c09739..346ea1a203d 100644 --- a/editor/src/messages/portfolio/document/node_graph/node_graph_message_handler.rs +++ b/editor/src/messages/portfolio/document/node_graph/node_graph_message_handler.rs @@ -3846,7 +3846,7 @@ impl NodeGraphMessageHandler { fn node_graph_errors(&self, network_interface: &mut NodeNetworkInterface, breadcrumb_network_path: &[NodeId]) -> Vec { let depth = breadcrumb_network_path.len(); let inside = |path: &[NodeId]| path.starts_with(breadcrumb_network_path) && path.len() > depth; - let graph_errors: Vec<_> = network_interface + let graph_errors = network_interface .resolved_types .node_graph_errors .iter() @@ -3854,18 +3854,14 @@ impl NodeGraphMessageHandler { .map(|error| match error.node_path.len() == depth + 1 { true => (error.node_path[depth], format!("{:?}", error.error)), false => (error.node_path[depth], "Node graph type error within this node".to_string()), - }) - .collect(); - // Evaluation errors come from the last graph that compiled, so a type error hides them - let mut errors: Vec<_> = match graph_errors.is_empty() { - true => self - .evaluation_errors - .iter() - .filter(|error| inside(&error.node_path)) - .map(|error| (error.node_path[depth], error.message.clone())) - .collect(), - false => graph_errors, - }; + }); + // Nodes with type errors get hidden so the rest of the graph still evaluates, so its evaluation errors are just as current + let evaluation_errors = self + .evaluation_errors + .iter() + .filter(|error| inside(&error.node_path)) + .map(|error| (error.node_path[depth], error.message.clone())); + let mut errors: Vec<_> = graph_errors.chain(evaluation_errors).collect(); let warnings = network_interface .resolved_types .node_graph_warnings diff --git a/editor/src/node_graph_executor.rs b/editor/src/node_graph_executor.rs index 983adaeab1f..28b152b8cc4 100644 --- a/editor/src/node_graph_executor.rs +++ b/editor/src/node_graph_executor.rs @@ -70,6 +70,11 @@ pub type NodeErrorTrace = Box<[NodePaths]>; pub type NodePaths = Box<[Box<[NodeId]>]>; impl EvaluationErrors { + /// An error that isn't traced back to any node. + fn untraced(error: impl Into>) -> Self { + Self(Box::new([(Arc::clone(graphene_std::NodeError::new(error).error()), NodeErrorTrace::default())])) + } + pub(crate) fn iter(&self) -> impl Iterator { self.0.iter().map(|(error, trace)| (&**error, trace)) } diff --git a/editor/src/node_graph_executor/runtime.rs b/editor/src/node_graph_executor/runtime.rs index 3ad44a815b7..161ecc59c28 100644 --- a/editor/src/node_graph_executor/runtime.rs +++ b/editor/src/node_graph_executor/runtime.rs @@ -47,8 +47,10 @@ pub struct NodeRuntime { pending_export: Option, pending_eyedropper: Option, pending_thumbnails: Option, - /// The network the executor last compiled successfully, with previews applied. + /// The network the executor last compiled successfully, with previews applied and its erroring nodes hidden. old_graph: Option, + /// Whether the latest network failed to compile, so the executor still holds an older one that mustn't be rendered as if it were current. + compile_failed: bool, async_runtime: Option, editor_api: Arc, @@ -135,6 +137,7 @@ impl NodeRuntime { pending_eyedropper: None, pending_thumbnails: None, old_graph: None, + compile_failed: false, resources: ResourceRegistry::default(), async_runtime: None, @@ -218,8 +221,8 @@ impl NodeRuntime { self.resources = resources; self.node_graph_errors.clear(); - self.node_graph_warnings.clear(); - let result = self.update_network(network.clone()); + let result = self.update_network(&mut network); + self.compile_failed = result.is_err(); if result.is_ok() { self.thumbnails.update_sources(&layers, &network, &self.executor); self.old_graph = Some(network); @@ -351,6 +354,10 @@ impl NodeRuntime { render_config.export_format = ExportFormat::Svg; } + if self.compile_failed { + return Some(ExecutionResult::Error(EvaluationErrors::untraced("The node graph failed to compile"))); + } + let context = render_config.into_context_with_generations(self.async_runtime().snapshot()); let result = match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| self.executor.tree().eval_tagged_value(self.executor.output(), &context))) { Ok(Ok(Poll::Pending)) => ExecutionResult::Pending, @@ -361,7 +368,7 @@ impl NodeRuntime { ExecutionResult::Fallback(value, self.resolve_errors(&error)) } Ok(Ok(Poll::Error(error))) => ExecutionResult::Error(self.resolve_errors(&error)), - Ok(Err(error)) => ExecutionResult::Error(EvaluationErrors(Box::new([(Arc::clone(NodeError::new(error).error()), NodeErrorTrace::default())]))), + Ok(Err(error)) => ExecutionResult::Error(EvaluationErrors::untraced(error)), Err(payload) => { let response = ExecutionResponse { execution_id, @@ -406,11 +413,39 @@ impl NodeRuntime { Raster::new_gpu(texture).convert(Footprint::BOUNDLESS, executor.clone()).await } - fn update_network(&mut self, graph: NodeNetwork) -> Result { + /// Compiles the network into the executor, hiding each node with an error so the rest of the graph can still render without it. + /// A hidden node passes its primary input through, which may in turn break the nodes downstream of it, so this goes on + /// until the network compiles or its errors are on nothing more that can be hidden. The nodes it hid stay hidden in `graph`. + fn update_network(&mut self, graph: &mut NodeNetwork) -> Result { + loop { + let (types, message, errors) = match self.compile_network(graph.clone()) { + Ok(types) => return Ok(types), + Err(failure) => failure, + }; + + let mut hid_any = false; + for error in errors { + hid_any |= graph.hide_node(&error.node_path); + if !self.node_graph_errors.contains(&error) { + self.node_graph_errors.push(error); + } + } + + if !hid_any { + return Err((types, message)); + } + } + } + + /// Compiles the network into the executor, or gives back why it couldn't along with the errors of the nodes responsible, if any. + fn compile_network(&mut self, graph: NodeNetwork) -> Result { + // Warnings are only kept from the latest attempt, since an earlier one may have had nodes that are hidden now + self.node_graph_warnings.clear(); + let mut scoped_network = wrap_network_in_scope(graph, self.editor_api.clone(), self.async_runtime().handle()); if let Err(e) = self.preprocessor.preprocess(&mut scoped_network, &|resource_id| self.resources.hash(&resource_id)) { - return Err((ResolvedDocumentNodeTypesDelta::default(), e.to_string())); + return Err((ResolvedDocumentNodeTypesDelta::default(), e.to_string(), GraphErrors::new())); } // We assume only one output @@ -419,20 +454,13 @@ impl NodeRuntime { let c = Compiler {}; let CompiledNetwork { network: proto_network, warnings } = match c.compile_single(scoped_network) { Ok(compiled_network) => compiled_network, - Err(CompileError::Graph(errors)) => { - let message = format!("{errors:?}"); - self.node_graph_errors = errors; - return Err((ResolvedDocumentNodeTypesDelta::default(), message)); - } - Err(CompileError::Message(message)) => return Err((ResolvedDocumentNodeTypesDelta::default(), message)), + Err(CompileError::Graph(errors)) => return Err((ResolvedDocumentNodeTypesDelta::default(), format!("{errors:?}"), errors)), + Err(CompileError::Message(message)) => return Err((ResolvedDocumentNodeTypesDelta::default(), message, GraphErrors::new())), }; self.node_graph_warnings = warnings; assert_ne!(proto_network.nodes.len(), 0, "No proto nodes exist?"); - let result = self.executor.update(proto_network).map_err(|(types, e)| { - self.node_graph_errors.clone_from(&e); - (types, format!("{e:?}")) - }); + let result = self.executor.update(proto_network).map_err(|(types, e)| (types, format!("{e:?}"), e)); self.async_runtime().update_sources(self.executor.sources()); result } @@ -614,6 +642,105 @@ mod tests { }); } + /// Feeds the layer's content through a Blur node, which only takes raster images, so it fails to type check with the rectangle it's given. + fn blur_layer_content(editor: &mut EditorTestUtils) -> NodeId { + use crate::messages::portfolio::document::node_graph::document_node_definitions::DefinitionIdentifier; + use crate::messages::portfolio::document::utility_types::network_interface::{InputConnector, OutputConnector}; + + let layer = editor.active_document().metadata().all_layers().next().unwrap(); + let content = InputConnector::node_at_index(layer.to_node(), 1); + let rectangle = editor.active_document().network_interface.upstream_output_connector(&content, &[]).unwrap(); + + let blur = NodeId::new(); + editor.dispatch(NodeGraphMessage::CreateNodeFromContextMenu { + node_id: Some(blur), + node_type: DefinitionIdentifier::ProtoNode(graphene_std::raster_nodes::filter::blur::IDENTIFIER), + xy: None, + add_transaction: true, + }); + editor.dispatch(NodeGraphMessage::CreateWire { + output_connector: rectangle, + input_connector: InputConnector::node_at_index(blur, 0), + }); + editor.dispatch(NodeGraphMessage::CreateWire { + output_connector: OutputConnector::primary_output(blur), + input_connector: content, + }); + blur + } + + /// Updates the runtime with the document's network and evaluates it, giving back what the editor sent the frontend in response. + async fn update_and_run(editor: &mut EditorTestUtils) -> Vec { + send_update(editor); + editor.runtime.update(); + editor.runtime.run().await; + editor.dispatch(Message::Wake) + } + + #[tokio::test] + async fn a_node_with_a_type_error_is_hidden_so_the_rest_still_renders() { + let mut editor = editor().await; + let blur = blur_layer_content(&mut editor); + + let messages = update_and_run(&mut editor).await; + assert!(messages.iter().any(|message| matches!(message, FrontendMessage::UpdateDocumentArtwork { .. }))); + assert!(!messages.contains(&FrontendMessage::UpdateRenderStatus { status: RenderStatus::Error })); + + let document = editor.active_document(); + let error_paths: Vec<_> = document.network_interface.resolved_types.node_graph_errors.iter().map(|error| error.node_path.clone()).collect(); + assert_eq!(error_paths, [vec![blur]], "The rectangle passing through should leave nothing else to fail"); + + let layer = document.metadata().all_layers().next().unwrap(); + assert!( + document.metadata().click_targets(layer).is_some_and(|targets| !targets.is_empty()), + "The rectangle should still be rendered" + ); + assert!( + document.network_interface.is_visible(&blur, &[]), + "Only the compiled network should have the node hidden, not the document" + ); + } + + #[tokio::test] + async fn a_network_that_fails_to_compile_does_not_render_the_last_one_that_did() { + use crate::messages::portfolio::document::node_graph::document_node_definitions::DefinitionIdentifier; + use crate::messages::portfolio::document::utility_types::network_interface::{InputConnector, OutputConnector}; + + let mut editor = editor().await; + assert!(update_and_run(&mut editor).await.iter().any(|message| matches!(message, FrontendMessage::UpdateDocumentArtwork { .. }))); + + // The renderer has nothing for a lone bool, and its own nodes aren't in the document to be hidden + let bool_value = NodeId::new(); + editor.dispatch(NodeGraphMessage::CreateNodeFromContextMenu { + node_id: Some(bool_value), + node_type: DefinitionIdentifier::ProtoNode(graphene_std::math_nodes::bool_value::IDENTIFIER), + xy: None, + add_transaction: true, + }); + editor.dispatch(NodeGraphMessage::CreateWire { + output_connector: OutputConnector::primary_output(bool_value), + input_connector: InputConnector::Export(0), + }); + + let messages = update_and_run(&mut editor).await; + assert!(messages.contains(&FrontendMessage::UpdateRenderStatus { status: RenderStatus::Error })); + + // Panning only asks for another render, which mustn't bring back the rectangle from before as if it were still there + let portfolio = editor.portfolio_message_handler_mut(); + let document_id = portfolio.active_document_id.unwrap(); + let (executor, documents) = (&mut portfolio.executor, &mut portfolio.documents); + let document = documents.get_mut(&document_id).unwrap(); + executor + .submit_current_node_graph_evaluation(document, document_id, UVec2::ONE, 1., Default::default(), DVec2::ZERO) + .unwrap(); + editor.runtime.update(); + editor.runtime.run().await; + + let messages = editor.dispatch(Message::Wake); + assert!(!messages.iter().any(|message| matches!(message, FrontendMessage::UpdateDocumentArtwork { .. }))); + assert!(messages.contains(&FrontendMessage::UpdateRenderStatus { status: RenderStatus::Error })); + } + /// The ID of the viewport request the runtime evaluates, which only changes once it has read a newer one. fn last_request(editor: &EditorTestUtils) -> Option { editor.runtime.pending.map(|request| request.execution_id) diff --git a/editor/src/test_utils.rs b/editor/src/test_utils.rs index 30db52feb27..76d496d8f62 100644 --- a/editor/src/test_utils.rs +++ b/editor/src/test_utils.rs @@ -16,6 +16,7 @@ use document_container::store::MemoryStore; use glam::{DVec2, UVec2}; use graph_craft::application_io::resource::HashMapResourceStorage; use graph_craft::application_io::{ApplicationIo, PlatformApplicationIo}; +use graph_craft::proto::GraphErrors; use graphene_std::raster::color::Color; use graphene_std::uuid::NodeId; use std::sync::atomic::{AtomicBool, Ordering}; @@ -83,6 +84,11 @@ impl EditorTestUtils { let frontend_messages = self.dispatch(Message::Wake); let evaluation_errors = self.portfolio_message_handler_mut().executor.take_evaluation_errors(); + // Nodes with type errors get hidden so the rest of the graph still renders, which means a successful render doesn't rule them out + let graph_errors = &self.active_document().network_interface.resolved_types.node_graph_errors; + if !graph_errors.is_empty() { + return Err(TestGraphError::Compilation(graph_errors.clone())); + } if evaluation_errors.is_none() && frontend_messages.contains(&FrontendMessage::UpdateRenderStatus { status: RenderStatus::Error }) { return Err(TestGraphError::Render); } @@ -453,6 +459,7 @@ impl FrontendMessageTestUtils for FrontendMessage { #[derive(Debug)] pub enum TestGraphError { + Compilation(GraphErrors), Evaluation(EvaluationErrors), Render, Other(String), @@ -461,6 +468,7 @@ pub enum TestGraphError { impl std::fmt::Display for TestGraphError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { + Self::Compilation(errors) => write!(f, "Graph compilation failed:\n{errors:#?}"), Self::Evaluation(errors) => write!(f, "Graph evaluation failed:\n{errors}"), Self::Render => write!(f, "Graph should render"), Self::Other(message) => write!(f, "{message}"), diff --git a/node-graph/graph-craft/src/document.rs b/node-graph/graph-craft/src/document.rs index 4d2908edc5d..856c6cf21ca 100644 --- a/node-graph/graph-craft/src/document.rs +++ b/node-graph/graph-craft/src/document.rs @@ -655,6 +655,49 @@ impl NodeNetwork { dropped } + /// Hides the node at this path from the document network, as its eye icon would, so it compiles to a passthrough of its primary input. + /// The table entries along the path are copied for just this instance first, since the other instances sharing them shouldn't lose the node too. + /// Returns false without changing anything if there's no such node or it's already hidden. + pub fn hide_node(&mut self, node_path: &[NodeId]) -> bool { + fn hide(network: &mut NodeNetwork, definitions: &mut FxHashMap, node_path: &[NodeId], copied: &mut Vec) -> bool { + let Some((node_id, deeper_path)) = node_path.split_first() else { return false }; + let Some(node) = network.nodes.get_mut(node_id) else { return false }; + + if deeper_path.is_empty() { + return std::mem::replace(&mut node.visible, false); + } + + match &mut node.implementation { + DocumentNodeImplementation::Network(nested) => hide(nested, definitions, deeper_path, copied), + DocumentNodeImplementation::Definition(id) => { + let Some(mut copy) = definitions.get(id).cloned() else { return false }; + if !hide(&mut copy, definitions, deeper_path, copied) { + return false; + } + + copied.push(*id); + *id = NetworkId::new(); + definitions.insert(*id, copy); + true + } + _ => false, + } + } + + let mut definitions = std::mem::take(&mut self.definitions); + let mut copied = Vec::new(); + let hidden = hide(self, &mut definitions, node_path, &mut copied); + self.definitions = definitions; + + // An entry that only this instance used is left with nothing naming it once copied, so it's dropped rather than kept around + if !copied.is_empty() { + let reached = self.reachable_definitions(); + self.definitions.retain(|id, _| !copied.contains(id) || reached.contains(id)); + } + + hidden + } + /// Checks that every table entry a node names exists, that no entries name each other in a cycle, and that only this network holds a table. pub fn validate_definitions(&self) -> Result<(), DefinitionError> { validate_named_definitions(self, &self.definitions, &mut Vec::new(), &mut FxHashSet::default()) diff --git a/node-graph/graph-craft/src/document/document_tests.rs b/node-graph/graph-craft/src/document/document_tests.rs index e5f1ae15255..20cb76ec3cc 100644 --- a/node-graph/graph-craft/src/document/document_tests.rs +++ b/node-graph/graph-craft/src/document/document_tests.rs @@ -224,6 +224,52 @@ fn edits_through_one_instance_reach_every_instance() { assert_eq!(network.implementation_output_count(&DocumentNodeImplementation::Definition(ADD)), 1); } +#[test] +fn hiding_a_node_in_one_instance_leaves_the_others_alone() { + let mut network = two_add_instances(); + assert!(network.hide_node(&[NodeId(10), NodeId(1)])); + + assert!(!network.nested_network(&[NodeId(10)]).unwrap().nodes[&NodeId(1)].visible); + assert!(network.nested_network(&[NodeId(11)]).unwrap().nodes[&NodeId(1)].visible, "The other instance should keep the node"); + assert_eq!(network.locate(&[NodeId(11)]), Some(Some(ADD))); + assert_eq!(network.validate_definitions(), Ok(())); + assert!(!network.hide_node(&[NodeId(10), NodeId(1)]), "The node is already hidden"); + + // Hiding another node copies this instance's entry again, and the copy from before is no longer named by anything + assert!(network.hide_node(&[NodeId(10), NodeId(0)])); + let hidden: Vec<_> = network.nested_network(&[NodeId(10)]).unwrap().nodes.values().map(|node| node.visible).collect(); + assert_eq!(hidden, [false, false]); + assert_eq!(network.definitions.len(), 2); +} + +#[test] +fn hiding_a_node_copies_each_entry_on_its_path() { + let outer = NetworkId(2); + let mut network = network_with( + [(NodeId(10), DocumentNodeImplementation::Definition(outer)), (NodeId(11), DocumentNodeImplementation::Definition(outer))], + [(ADD, add_network()), (outer, network_with([(NodeId(10), DocumentNodeImplementation::Definition(ADD))], []))], + ); + assert!(network.hide_node(&[NodeId(10), NodeId(10), NodeId(1)])); + + assert!(!network.nested_network(&[NodeId(10), NodeId(10)]).unwrap().nodes[&NodeId(1)].visible); + assert!(network.nested_network(&[NodeId(11), NodeId(10)]).unwrap().nodes[&NodeId(1)].visible); + assert_eq!(network.definitions.len(), 4, "The other instance still names both original entries"); + assert_eq!(network.validate_definitions(), Ok(())); +} + +#[test] +fn hiding_a_node_that_is_not_there_changes_nothing() { + let mut network = two_add_instances(); + assert!(!network.hide_node(&[])); + assert!(!network.hide_node(&[NodeId(12)])); + assert!(!network.hide_node(&[NodeId(10), NodeId(5)])); + assert_eq!(network, two_add_instances()); + + assert!(network.hide_node(&[NodeId(10)])); + assert!(!network.nodes[&NodeId(10)].visible); + assert_eq!(network.locate(&[NodeId(10)]), Some(Some(ADD)), "Hiding the instance itself needs no copy"); +} + #[test] fn definition_edits_change_the_network_hash() { let mut network = two_add_instances(); diff --git a/node-graph/interpreted-executor/src/dynamic_executor.rs b/node-graph/interpreted-executor/src/dynamic_executor.rs index 68185553e9f..58b67dd3bb2 100644 --- a/node-graph/interpreted-executor/src/dynamic_executor.rs +++ b/node-graph/interpreted-executor/src/dynamic_executor.rs @@ -72,9 +72,11 @@ impl DynamicExecutor { } /// Updates the existing [`BorrowTree`] to reflect the new [`ProtoNetwork`], reusing nodes where possible. + /// If the network fails to update, the executor keeps evaluating the last one that succeeded. #[cfg_attr(debug_assertions, inline(never))] pub fn update(&mut self, proto_network: ProtoNetwork) -> Result { - self.output = proto_network.output; + // The output only moves over once its node is in the tree, otherwise a failed update would leave nothing to evaluate + let output = proto_network.output; self.typing_context.update(&proto_network).map_err(|e| { // If there is an error then get types that have been resolved before the error let add = proto_network @@ -96,8 +98,11 @@ impl DynamicExecutor { (ResolvedDocumentNodeTypesDelta { add, remove: Vec::new() }, e) })?; - self.sources = proto_network.source_ids().copied().collect(); + let sources = proto_network.source_ids().copied().collect(); let (add, orphaned) = self.tree.update(proto_network, &self.typing_context).map_err(|e| (ResolvedDocumentNodeTypesDelta::default(), e))?; + self.output = output; + self.sources = sources; + let old_to_remove = core::mem::replace(&mut self.orphaned_nodes, orphaned); let mut remove = Vec::with_capacity(old_to_remove.len() - self.orphaned_nodes.len().min(old_to_remove.len())); for node_id in old_to_remove { diff --git a/node-graph/interpreted-executor/src/dynamic_executor/test.rs b/node-graph/interpreted-executor/src/dynamic_executor/test.rs index 5191b29863b..a9fd9ba2f60 100644 --- a/node-graph/interpreted-executor/src/dynamic_executor/test.rs +++ b/node-graph/interpreted-executor/src/dynamic_executor/test.rs @@ -924,6 +924,26 @@ fn executor_panics_reach_the_host_with_the_original_payload() { assert_eq!(*payload.downcast::().unwrap(), 42); } +#[test] +fn a_failed_update_keeps_evaluating_the_last_network() { + let mut executor = DynamicExecutor::new(bounding_box_network(TaggedValue::TypeDefault(item!(Vector)))).unwrap(); + + // Bounding Box has no implementation taking a bool, so this network fails to type check + let mut bounding_box_node = ProtoNode::value(ConstructionArgs::Nodes(vec![NodeId(2)]), vec![NodeId(3)]); + bounding_box_node.identifier = ProtoNodeIdentifier::new("core_types::vector::BoundingBoxNode"); + let broken = ProtoNetwork { + inputs: vec![], + output: NodeId(3), + nodes: vec![value_node(2, TaggedValue::Bool(true)), (NodeId(3), bounding_box_node)], + }; + assert!(executor.update(broken).is_err()); + + assert_eq!(executor.output(), NodeId(1)); + let context: Context = None; + let result: Option> = executor.tree().eval(executor.output(), &context); + assert!(result.is_some(), "The network from before the failed update should still evaluate"); +} + #[test] fn chained_fallbacks_both_reach_the_root() { let executor = DynamicExecutor::new(ProtoNetwork {