toeverything/AFFiNE · error · DuplicateDefinitionError

${stringifyIdentifier(identifier)} already exists

Error message

${stringifyIdentifier(identifier)} already exists

What it means

addFactory throws '${identifier} already exists' when registering a component factory for an identifier/variant/scope that already has one without override=true, protecting against accidental duplicate service registration in the DI framework.

Source

Thrown at packages/common/infra/src/framework/core/framework.ts:137

      override,
    }: { scope?: FrameworkScopeStack; override?: boolean } = {}
  ) {
    // convert scope to string
    const normalizedScope = stringifyScope(scope ?? ROOT_SCOPE);
    const normalizedIdentifier = parseIdentifier(identifier);
    const normalizedVariant = normalizedIdentifier.variant ?? DEFAULT_VARIANT;

    const services =
      this.components.get(normalizedScope) ??
      new Map<string, Map<ComponentVariant, ComponentFactory>>();

    const variants =
      services.get(normalizedIdentifier.identifierName) ??
      new Map<ComponentVariant, ComponentFactory>();

    // throw if service already exists, unless it is an override
    if (variants.has(normalizedVariant) && !override) {
      throw new DuplicateDefinitionError(normalizedIdentifier);
    }
    variants.set(normalizedVariant, factory);
    services.set(normalizedIdentifier.identifierName, variants);
    this.components.set(normalizedScope, services);
  }

  remove(identifier: IdentifierValue, scope: FrameworkScopeStack = ROOT_SCOPE) {
    const normalizedScope = stringifyScope(scope);
    const normalizedIdentifier = parseIdentifier(identifier);
    const normalizedVariant = normalizedIdentifier.variant ?? DEFAULT_VARIANT;

    const services = this.components.get(normalizedScope);
    if (!services) {
      return;
    }

    const variants = services.get(normalizedIdentifier.identifierName);
    if (!variants) {

View on GitHub (pinned to b4c8548c09)

Solutions

  1. Register each service identifier only once; remove the duplicate registration.
  2. Use a unique identifier for the new component if the name collides with an existing one.
  3. Check for the same provider being added twice, e.g. via both a module and manual registration.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at packages/common/infra/src/framework/core/framework.ts:137 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of toeverything/AFFiNE@b4c8548c09 (2026-08-18). Data as JSON: /api/errors/5c90af7d186a6bc1. Report an issue: GitHub.