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, another

View on GitHub (pinned to 668b1b85e0)

Solutions

  1. 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.
  2. If files are gathered by glob/directory scan, deduplicate the file list before parsing so the same document isn't parsed twice.
  3. If fragments come from multiple sources, namespace fragment names per source (e.g. Prefix_fragmentName) to avoid collisions.
  4. 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

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


AI-assisted analysis of facebook/relay@668b1b85e0 (2026-09-02). Data as JSON: /api/errors/d027130c4ce3eb9f. Report an issue: GitHub.