{"record":{"id":"03ef7d1eb3f31919","repo":"facebook/relay","slug":"expect-to-have-a-document-while-transforming-linke","errorCode":null,"errorMessage":"Expect to have a document while transforming linked field","messagePattern":"Expect to have a document while transforming linked field","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler/crates/relay-transforms/src/required_directive.rs","lineNumber":287,"sourceCode":"                    .annotate(\n                        \"so that it can match its parent\",\n                        required_child.required.action_location,\n                    ),\n                );\n            }\n        }\n    }\n    fn validate_semantic_non_null_for_field(\n        &mut self,\n        field: &schema::definitions::Field,\n        metadata: RequiredMetadata,\n    ) {\n        let enforce_no_throw_on_nullable = self\n            .feature_flags\n            .disallow_required_action_throw_on_semantically_nullable_fields\n            .is_enabled_for(\n                self.current_document_name\n                    .expect(\"Expect to have a document while transforming linked field\"),\n            );\n\n        if field.semantic_type().is_non_null() {\n            if metadata.action == RequiredAction::DangerouslyThrowOnSemanticallyNullableField {\n                self.errors.push(Diagnostic::error_with_data(\n                    RequiredDirectiveValidationMessageWithData::DangerousThrowActionOnNonNullableFieldWithFix,\n                    metadata.action_location,\n                ));\n            }\n        } else if metadata.action == RequiredAction::Throw && enforce_no_throw_on_nullable {\n            self.errors.push(Diagnostic::error_with_data(\n                    RequiredDirectiveValidationMessageWithData::ThrowActionOnSemanticNullableFieldWithFix,\n                    metadata.action_location,\n                ));\n        }\n    }\n\n    fn assert_compatible_required_children<T: RequireableField>(","sourceCodeStart":269,"sourceCodeEnd":305,"githubUrl":"https://github.com/facebook/relay/blob/668b1b85e06261aa3b58dabfc51f8b5524a70955/compiler/crates/relay-transforms/src/required_directive.rs#L269-L305","documentation":"While validating @required metadata for a field, validate_semantic_non_null_for_field needs the current document name to check whether the disallow_required_action_throw_on_semantically_nullable_fields feature flag is enabled for it. If self.current_document_name is None the expect panics, meaning the validation ran outside of a document context.","triggerScenarios":"get_required_metadata (linked-field handling in required_directive.rs) invoked with no current document set — typically when the transform/visitor processes an IR node without the document-scoped state being initialized, or when @required validation is invoked on a sub-selection outside a named operation/fragment.","commonSituations":"Custom tooling that reuses the required_directive visitor outside the normal transform entry point; language-server or per-node compilation paths that skip setting current_document_name; @required on a fragment compiled without an enclosing document context.","solutions":["Ensure the transform sets current_document_name before visiting (in the entry point for each operation/fragment).","Invoke @required validation through the standard Relay transform pipeline rather than calling get_required_metadata directly.","Make current_document_name an explicit parameter so the compiler enforces it (local fork fix).","File a relay compiler bug with the document that triggers it if the standard pipeline panics."],"exampleFix":"// before\nlet enforce = self.feature_flags...is_enabled_for(\n    self.current_document_name\n        .expect(\"Expect to have a document while transforming linked field\"),\n);\n// after\nlet Some(doc) = self.current_document_name else { return Ok(()) };\nlet enforce = self.feature_flags...is_enabled_for(doc);","handlingStrategy":"validation","validationCode":"assert!(self.current_document_name.is_some(), \"required_directive validation invoked outside a document context\");","typeGuard":"fn in_document(state: &TransformState) -> bool { state.current_document_name.is_some() }","tryCatchPattern":"let Some(doc) = self.current_document_name else { return Ok(()); };","preventionTips":["Set current_document_name in the visitor entry point for every document.","Do not reuse the @required visitor outside the transform pipeline.","Make document context an explicit function parameter."],"tags":["relay-compiler","required-directive","state"],"backgroundTag":"relay-compiler-internal-invariant","analyzedSha":"668b1b85e06261aa3b58dabfc51f8b5524a70955","analyzedAt":"2026-09-02T19:57:20.783Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}