{"record":{"id":"193f03ab28ea9cd6","repo":"PrismLibrary/Prism","slug":"resources-cyclicdependencyfound","errorCode":null,"errorMessage":"Resources.CyclicDependencyFound","messagePattern":"Resources\\.CyclicDependencyFound","errorType":"exception","errorClass":"CyclicDependencyFoundException","httpStatus":null,"severity":"error","filePath":"src/Prism.Core/Modularity/ModuleDependencySolver.cs","lineNumber":84,"sourceCode":"            }\n        }\n\n        /// <summary>\n        /// Calculates an ordered vector according to the defined dependencies.\n        /// Non-dependant modules appears at the beginning of the resulting array.\n        /// </summary>\n        /// <returns>The resulting ordered list of modules.</returns>\n        /// <exception cref=\"CyclicDependencyFoundException\">This exception is thrown\n        /// when a cycle is found in the defined dependency graph.</exception>\n        public string[] Solve()\n        {\n            List<string> skip = new List<string>();\n            while (skip.Count < dependencyMatrix.Count)\n            {\n                List<string> leaves = this.FindLeaves(skip);\n                if (leaves.Count == 0 && skip.Count < dependencyMatrix.Count)\n                {\n                    throw new CyclicDependencyFoundException(Resources.CyclicDependencyFound);\n                }\n                skip.AddRange(leaves);\n            }\n            skip.Reverse();\n\n            if (skip.Count > knownModules.Count)\n            {\n                string moduleNames = this.FindMissingModules(skip);\n                throw new ModularityException(moduleNames, string.Format(CultureInfo.CurrentCulture,\n                                                            Resources.DependencyOnMissingModule,\n                                                            moduleNames));\n            }\n\n            return skip.ToArray();\n        }\n\n        private string FindMissingModules(List<string> skip)\n        {","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Prism.Core/Modularity/ModuleDependencySolver.cs#L66-L102","documentation":"CyclicDependencyFoundException is thrown by ModuleDependencySolver.Solve when the module dependency graph contains a cycle, so no topological load order exists. Prism requires an acyclic graph to order module initialization.","triggerScenarios":"ModuleA depends on ModuleB while ModuleB depends on ModuleA (directly or via longer chains); wiring mutual dependencies in AddDependency; config files where each module lists the other as a dependency.","commonSituations":"Refactoring that accidentally reversed a dependency direction; shared-service module mistakenly depending on a consumer; copying dependency attributes between module declarations.","solutions":["Break the cycle by removing or reversing one dependency edge","Extract the shared functionality into a new lower-level module that both depend on","Use TrySolve, which returns false instead of throwing, to detect cycles gracefully"],"exampleFix":"// before\nsolver.AddDependency(\"ModuleA\", \"ModuleB\");\nsolver.AddDependency(\"ModuleB\", \"ModuleA\"); // cycle\n// after\nsolver.AddDependency(\"ModuleA\", \"ModuleB\"); // one direction only","handlingStrategy":"validation","validationCode":"bool HasCycle(Dictionary<string,string> edges)\n{\n    var visited = new HashSet<string>();\n    foreach (var n in edges.Keys)\n    {\n        var cur = n;\n        while (cur != null && edges.ContainsKey(cur))\n        {\n            if (visited.Contains(cur)) return true;\n            visited.Add(cur);\n            cur = edges[cur];\n        }\n        visited.Clear();\n    }\n    return false;\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    var order = solver.Solve();\n}\ncatch (CyclicDependencyFoundException)\n{\n    logger.LogError(\"Module dependency graph contains a cycle\");\n}","preventionTips":["Keep dependencies one-directional: higher-level modules depend on lower-level ones","Draw the dependency graph when adding new modules","Use TrySolve in production to fail gracefully"],"tags":["cyclic-dependency","topological-sort","modularity","prism"],"backgroundTag":"cyclic-dependency-found","analyzedSha":"358118cd640d9a22ff8cf21c8ad197fa038b7990","analyzedAt":"2026-09-15T15:00:31.079Z","contentChangedAt":"2026-09-15T15:00:31.079Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}