{"record":{"id":"2fc8172d0e832334","repo":"gitbutlerapp/gitbutler","slug":"no-provider-found-for-token-tostring","errorCode":null,"errorMessage":"No provider found for ${token.toString()}","messagePattern":"No provider found for (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/context.ts","lineNumber":55,"sourceCode":"\tsetContext(token._key, value);\n}\n\n/**\n * Provides many injectables in one call.\n */\nexport function provideAll(entries: [InjectionToken<any>, any][]) {\n\tfor (const [token, value] of entries) {\n\t\tprovide(token, value);\n\t}\n}\n\n/**\n * An injector for use with `InjectionToken` rather than `Constructor`.\n */\nexport function inject<T>(token: InjectionToken<T>): T {\n\tconst value = svelteGetContext<T>(token._key);\n\tif (value === undefined) {\n\t\tthrow new Error(`No provider found for ${token.toString()}`);\n\t}\n\treturn value;\n}\n\n/**\n * Injects a value using an injection token with a fallback\n * Returns the default value if the token is not found\n */\nexport function injectOptional<T>(token: InjectionToken<T>, defaultValue: T): T {\n\tconst value = svelteGetContext<T>(token._key);\n\treturn value !== undefined ? value : defaultValue;\n}\n","sourceCodeStart":37,"sourceCodeEnd":68,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/packages/core/src/context.ts#L37-L68","documentation":"packages/core's inject() retrieves a value from Svelte's context system by an InjectionToken's key and throws when nothing was provided for that token. Every inject(token) call must be matched by a setContext(token, value) in an ancestor component; the throw fires when the token has no provider in the current component tree path.","triggerScenarios":"Calling inject(stackService)/inject(uiState)/inject(projectsService) etc. from a component rendered outside the subtree where setContext was called — e.g. a component mounted via a portal, a module-level singleton instantiation outside component initialisation, tests rendering components without the provider wrapper, or a typo/different token instance than the one provided.","commonSituations":"Unit tests mounting a component without the app's provider hierarchy; Svelte lifecycle violation (calling inject outside component init, so getContext returns undefined); rendering UI outside the root layout where services are provided; duplicate package versions creating two distinct token instances so provide/inject keys don't match.","solutions":["Ensure setContext(token, value) for the token is called in an ancestor component before the consuming component initialises.","Wrap test renderings in a provider component (or a context wrapper helper) that provides all required services.","Make sure inject() is called during component initialisation (top level of the component), not in async callbacks or event handlers.","Verify provide and consume use the exact same token instance (same import path, no duplicate package installs)."],"exampleFix":"// before\n// Component.ts: outside provider tree\nconst projects = inject(projectsService); // throws: No provider found\n\n// after\n// App root: provide first\nsetContext(projectsService_token, new ProjectsService());\n// Component.ts (child):\nconst projects = inject(projectsService); // resolves","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"let projects: ProjectsService;\ntry {\n  projects = inject(projectsService);\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith(\"No provider found\")) {\n    projects = new ProjectsService(); // or fail fast with a clear setup error\n  } else throw e;\n}","preventionTips":["Always provide tokens via setContext in a root/ancestor component before children initialise.","Call inject() only during synchronous component initialisation, never in handlers or async code.","In tests, use a shared provider wrapper component that supplies all app services.","Guard against duplicate package installs so token instances (and their keys) match."],"tags":["svelte","dependency-injection","context"],"backgroundTag":"missing-dependency","analyzedSha":"58e5313667b857ef39a730e380af31816a7b1768","analyzedAt":"2026-09-18T06:50:32.052Z","contentChangedAt":"2026-09-18T06:50:32.052Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}