{"record":{"id":"8910582a82508197","repo":"BabylonJS/Babylon.js","slug":"service-contract-tostring-has-not-been-inst","errorCode":null,"errorMessage":"Service '${contract.toString()}' has not been instantiated in the '${this._friendlyName}' container.","messagePattern":"Service '(.+?)' has not been instantiated in the '(.+?)' container\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/dev/sharedUiComponents/src/modularTool/modularity/serviceContainer.ts","lineNumber":132,"sourceCode":"        const dependencies = service.consumes?.map((contract) => this._resolveDependency(contract, service)) ?? [];\r\n\r\n        this._serviceInstances.set(service, service.factory(...dependencies));\r\n    }\r\n\r\n    /**\r\n     * Resolves a dependency by contract identity for a consuming service.\r\n     * Checks local services first, then walks up the parent chain.\r\n     * Registers the consumer as a dependent in whichever container owns the dependency.\r\n     * @param contract The contract identity to resolve.\r\n     * @param consumer The service definition that consumes this dependency.\r\n     * @returns The resolved service instance.\r\n     */\r\n    private _resolveDependency(contract: symbol, consumer: WeaklyTypedServiceDefinition): IService<symbol> & Partial<IDisposable> {\r\n        const definition = this._serviceDefinitions.get(contract);\r\n        if (definition) {\r\n            const instance = this._serviceInstances.get(definition);\r\n            if (!instance) {\r\n                throw new Error(`Service '${contract.toString()}' has not been instantiated in the '${this._friendlyName}' container.`);\r\n            }\r\n\r\n            let dependentDefinitions = this._serviceDependents.get(definition);\r\n            if (!dependentDefinitions) {\r\n                this._serviceDependents.set(definition, (dependentDefinitions = new Set()));\r\n            }\r\n            dependentDefinitions.add(consumer);\r\n\r\n            return instance;\r\n        }\r\n\r\n        if (this._parent) {\r\n            return this._parent._resolveDependency(contract, consumer);\r\n        }\r\n\r\n        throw new Error(`Service '${contract.toString()}' has not been registered in the '${this._friendlyName}' container.`);\r\n    }\r\n\r","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/BabylonJS/Babylon.js/blob/0592b347b8a4ee0236089ea86a749cacfdb266d8/packages/dev/sharedUiComponents/src/modularTool/modularity/serviceContainer.ts#L114-L150","documentation":"_resolveDependency found a registration for the contract in _serviceDefinitions but no instantiated instance in _serviceInstances for the consumer. In this container the contract is registered but its producing service was never instantiated (or was removed), so a dependent cannot be handed an instance.","triggerScenarios":"Calling container.dependencies(definition) for a contract whose producing service is registered but not yet instantiated; resolving after the producing service's instance was removed; resolving before the container's instantiation phase ran.","commonSituations":"Consuming a service before the producer's create/instantiate step completed; instantiating in one container but resolving in a peer container whose _serviceDefinitions knows the contract only via different wiring; order-of-initialization bugs in module startup.","solutions":["Instantiate the producing service (the container's instance-creation step) before calling dependencies() on consumers.","Verify the producer and consumer go through the same container (or a correct parent chain).","Check that no code removed the instance or disposed the producer before dependent resolution.","Log _serviceDefinitions vs _serviceInstances contents at failure to identify the missing instantiation."],"exampleFix":"// before\nconst deps = container.dependencies(consumerDef); // producer not instantiated yet\n// after\nawait container.instantiateServices?.(); // ensure producer instances exist first\nconst deps = container.dependencies(consumerDef);","handlingStrategy":"validation","validationCode":"// Ensure the producing service is instantiated before resolving dependents\nasync function resolveWhenReady(container, def) {\n  await container.instantiationComplete?.(); // or run instantiate step\n  return container.dependencies(def);\n}","typeGuard":null,"tryCatchPattern":"// try {\n//   return container.dependencies(def);\n// } catch (e) {\n//   if (String(e.message).includes(\"has not been instantiated\")) {\n//     await instantiateProducingServices(container);\n//     return container.dependencies(def);\n//   }\n//   throw e;\n// }","preventionTips":["Run the container's instantiation phase before any dependency resolution.","Never remove instances mid-startup; dispose in strict reverse order.","Keep producer and consumer in the same container hierarchy."],"tags":["dependency-injection","lifecycle","instantiation-order"],"backgroundTag":"service-not-instantiated","analyzedSha":"0592b347b8a4ee0236089ea86a749cacfdb266d8","analyzedAt":"2026-08-30T15:11:20.442Z","schemaVersion":2},"datasetVersion":"2026-08-30T18:17:15.746Z"}