facebook/relay · error
Duplicate fragment definitions named {}: first one: {:?} s
Error message
Duplicate fragment definitions named {}:
first one: {:?}
second one: {:?}
What it means
This panic comes from ProgramWithDependencies::from_definitions when two fragment definitions share the same name. The function builds a lookup of fragments by name and panics if a name is inserted twice, since the dependency-resolution pass requires fragment names to be unique to resolve references unambiguously.
Source
Thrown at compiler/crates/program-with-dependencies/src/program_with_dependencies.rs:136
ExecutableDefinition::Operation(operation) => {
let loc = operation.name.location;
let name = operation.name.item;
if let Some(another) = seen_operation_loc.insert(name, loc) {
panic!(
"\nDuplicate operation definitions named {}: \nfirst one: {:?}\nsecond one: {:?}\n",
name, loc, another
);
}
scoped_operations.push(Arc::new(operation)); // Keep the order the operations same as inputs.
}
ExecutableDefinition::Fragment(fragment) => {
let loc = fragment.name.location;
let name = fragment.name.item;
let fragment_ref = Arc::new(fragment);
if let Some(another) =
seen_fragments_loc.insert(name, fragment_ref.name.location)
{
panic!(
"\nDuplicate fragment definitions named {}: \nfirst one: {:?}\nsecond one: {:?}\n",
name, loc, another
);
}
scoped_fragments.insert(name, fragment_ref.clone()); // Keep the order the fragments same as inputs.
}
}
}
// Ensure there are no duplicate fragments referenced in the dependencies.
for (fragment_name, signature) in &dependencies {
let loc = signature.name.location;
if let Some(another) = seen_fragments_loc.insert(*fragment_name, loc)
&& another != loc
{
panic!(
"\nDuplicate fragment definitions named {}: \nfirst one: {:?}\nsecond one: {:?}\n",
fragment_name, loc, anotherView on GitHub (pinned to 668b1b85e0)
Solutions
- Find the two fragment definitions with the reported locations and delete or rename one so fragment names are unique across the whole set passed to from_definitions.
- If files are gathered by glob/directory scan, deduplicate the file list before parsing so the same document isn't parsed twice.
- If fragments come from multiple sources, namespace fragment names per source (e.g. Prefix_fragmentName) to avoid collisions.
- Upgrade to a compiler version or add a pre-pass that reports duplicates as diagnostics instead of panicking.
Example fix
// before
fragment UserFields on User { name }
// in another file, duplicate
fragment UserFields on User { name }
// after
fragment UserFields on User { name }
fragment AdminUserFields on User { name }
Defensive patterns
Strategy: validation
Validate before calling
fn validate_unique_fragments<'a>(frags: impl IntoIterator<Item = &'a FragmentDefinition>) -> Result<(), String> {
let mut seen = std::collections::HashSet::new();
for f in frags {
if !seen.insert(f.name.item.0) {
return Err(format!("duplicate fragment definition: {}", f.name.item.0));
}
}
Ok(())
}
Prevention
- Run a pre-pass validating fragment name uniqueness before invoking from_definitions.
- Use unique per-file fragment naming conventions to avoid collisions across sources.
- Deduplicate parsed documents when collecting .graphql files via glob.
- Treat compiler panics as build failures and surface the two reported locations in CI output.
When it happens
Trigger: Calling ProgramWithDependencies::from_definitions with an iterator of FragmentDefinition where two fragments have identical names (checked via seen_fragments_loc while walking the input fragments).
Common situations: Merging schema/operation sources that both define the same fragment (e.g. duplicated .graphql files included twice via include! or glob), a script concatenating documents, or copy-pasting a fragment into a second file.
Related errors
- @__metadata directive should have only one argument!
- unexpected value for @defer if argument: {other:?}
- unexpected value for @stream if argument: {other:?}
- Unexpected custom directives: {:#?}
- Expected a scalar field.
AI-assisted analysis of facebook/relay@668b1b85e0 (2026-09-02).
Data as JSON: /api/errors/d027130c4ce3eb9f.
Report an issue: GitHub.