{"record":{"id":"79f8eb369f535e88","repo":"emberjs/ember.js","slug":"attempted-to-set-the-global-context-twice-this-sh","errorCode":null,"errorMessage":"Attempted to set the global context twice. This should only be set once.","messagePattern":"Attempted to set the global context twice\\. This should only be set once\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/@glimmer/global-context/index.ts","lineNumber":175,"sourceCode":"  getPath: (obj: object, path: string) => unknown;\n  setPath: (obj: object, prop: string, value: unknown) => void;\n  warnIfStyleNotTrusted: (value: unknown) => void;\n  assert: (test: unknown, msg: string, options?: { id: string }) => asserts test;\n  deprecate: (\n    msg: string,\n    test: unknown,\n    options: {\n      id: string;\n    }\n  ) => void;\n}\n\nlet globalContextWasSet = false;\n\nexport default function setGlobalContext(context: GlobalContext) {\n  if (DEBUG) {\n    if (globalContextWasSet) {\n      throw new Error('Attempted to set the global context twice. This should only be set once.');\n    }\n\n    globalContextWasSet = true;\n  }\n\n  scheduleRevalidate = context.scheduleRevalidate;\n  scheduleDestroy = context.scheduleDestroy;\n  scheduleDestroyed = context.scheduleDestroyed;\n  toIterator = context.toIterator;\n  toBool = context.toBool;\n  getProp = context.getProp;\n  setProp = context.setProp;\n  getPath = context.getPath;\n  setPath = context.setPath;\n  warnIfStyleNotTrusted = context.warnIfStyleNotTrusted;\n  assert = context.assert;\n  deprecate = context.deprecate;\n}","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/emberjs/ember.js/blob/26f97246a8bf2e28edf26ac3093da2e86c04ffc5/packages/@glimmer/global-context/index.ts#L157-L193","documentation":"`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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Remove the second `setGlobalContext()` call and set the context in exactly one place (e.g. a single app initializer)","Deduplicate @glimmer/global-context in node_modules / bundler config so only one module instance exists","Use `testOverrideGlobalContext()` for tests instead of calling `setGlobalContext()` again","Check for multiple versions of the glimmer packages with a lockfile/dependency audit"],"exampleFix":"// before\nsetGlobalContext(ctx); // initializer\nsetGlobalContext(ctx); // test setup — throws\n// after\nsetGlobalContext(ctx); // initializer only\ntestOverrideGlobalContext(partialCtx); // test setup","handlingStrategy":"validation","validationCode":"let contextSet = false;\nfunction setupGlimmer(ctx) {\n  if (!contextSet) { setGlobalContext(ctx); contextSet = true; }\n}","typeGuard":"const isGlobalContextSet = () => contextSet; // track in a wrapper module","tryCatchPattern":"try { setGlobalContext(ctx); } catch (e) { if (!/global context twice/.test(e.message)) throw e; /* ignore: already set by another copy — verify which instance won */ }","preventionTips":["Call setGlobalContext exactly once, in a single app initializer","Deduplicate @glimmer/global-context in the bundler/lockfile","Use testOverrideGlobalContext for tests instead of re-setting"],"tags":["global-context","initialization","glimmer"],"backgroundTag":"global-context-set-twice","analyzedSha":"26f97246a8bf2e28edf26ac3093da2e86c04ffc5","analyzedAt":"2026-09-01T05:01:28.182Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}