emberjs/ember.js · error · Error

Attempted to set the global context twice. This should only

Error message

Attempted to set the global context twice. This should only be set once.

What it means

`setGlobalContext()` from @glimmer/global-context configures the host's scheduleRevalidate etc., and may only be invoked once per application lifetime in DEBUG builds. The module keeps a `globalContextWasSet` boolean and throws if a second set is attempted, because the context is treated as an application-wide singleton. This prevents conflicting hosts or duplicate module instances from stomping each other's configuration.

Source

Thrown at packages/@glimmer/global-context/index.ts:175

  getPath: (obj: object, path: string) => unknown;
  setPath: (obj: object, prop: string, value: unknown) => void;
  warnIfStyleNotTrusted: (value: unknown) => void;
  assert: (test: unknown, msg: string, options?: { id: string }) => asserts test;
  deprecate: (
    msg: string,
    test: unknown,
    options: {
      id: string;
    }
  ) => void;
}

let globalContextWasSet = false;

export default function setGlobalContext(context: GlobalContext) {
  if (DEBUG) {
    if (globalContextWasSet) {
      throw new Error('Attempted to set the global context twice. This should only be set once.');
    }

    globalContextWasSet = true;
  }

  scheduleRevalidate = context.scheduleRevalidate;
  scheduleDestroy = context.scheduleDestroy;
  scheduleDestroyed = context.scheduleDestroyed;
  toIterator = context.toIterator;
  toBool = context.toBool;
  getProp = context.getProp;
  setProp = context.setProp;
  getPath = context.getPath;
  setPath = context.setPath;
  warnIfStyleNotTrusted = context.warnIfStyleNotTrusted;
  assert = context.assert;
  deprecate = context.deprecate;
}

View on GitHub (pinned to 26f97246a8)

Solutions

  1. Remove the second `setGlobalContext()` call and set the context in exactly one place (e.g. a single app initializer)
  2. Deduplicate @glimmer/global-context in node_modules / bundler config so only one module instance exists
  3. Use `testOverrideGlobalContext()` for tests instead of calling `setGlobalContext()` again
  4. Check for multiple versions of the glimmer packages with a lockfile/dependency audit

Example fix

// before
setGlobalContext(ctx); // initializer
setGlobalContext(ctx); // test setup — throws
// after
setGlobalContext(ctx); // initializer only
testOverrideGlobalContext(partialCtx); // test setup
Defensive patterns

Strategy: validation

Validate before calling

let contextSet = false;
function setupGlimmer(ctx) {
  if (!contextSet) { setGlobalContext(ctx); contextSet = true; }
}

Type guard

const isGlobalContextSet = () => contextSet; // track in a wrapper module

Try / catch

try { setGlobalContext(ctx); } catch (e) { if (!/global context twice/.test(e.message)) throw e; /* ignore: already set by another copy — verify which instance won */ }

Prevention

When it happens

Trigger: Importing @glimmer/global-context from two different copies (e.g. duplicate dependency versions in node_modules or both the app bundle and a test bundle) so the module is instantiated twice, or explicitly calling `setGlobalContext()` twice — e.g. in both an initializer and a test setup.

Common situations: Bundler/deduplication problems where @glimmer/global-context appears twice; test harnesses that re-run app boot code; addons each trying to set the context themselves.

Related errors


AI-assisted analysis of emberjs/ember.js@26f97246a8 (2026-09-01). Data as JSON: /api/errors/79f8eb369f535e88. Report an issue: GitHub.