Search code examples
c#.net.net-coredependencies

Visual Studio detects dependency cycle which does not exist


So I was writing tests for my system. My system consists out of various layers within the context of a clean architecture. My Core project contains services, these services have tests. When I started writing theses tests everything went fine. But as I saw that the test project was not in the designated tests folder, I moved it. After this Visual Studio started talking about a cycle. First let me shed some light on the projects that should have these cycles; according to Visual Studio.

  • API Project (API -> Core)
  • Core (Core -> Shared)
  • API.Tests (API.Tests -> API)
  • Core.Tests (Core.Tests -> Core)

The above points are according to what I implemented, and to how the solution explorer shows project dependencies. Visual studio is giving the following exception:

enter image description here

However, there is nothing in the Core project which has a reference to Api.Tests.

enter image description here

enter image description here

Below is the Core.csproj file.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\Shared\Shared.csproj" />
  </ItemGroup>

</Project>

API Project reference:

  <ItemGroup>
    <ProjectReference Include="..\Core\Core.csproj" />
    <ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
  </ItemGroup>

API.Tests project reference:

  <ItemGroup>
    <ProjectReference Include="..\..\src\API\API.csproj" />
  </ItemGroup>

What is causing this cycle, am I missing something here, is there more information required?


Solution

  • So I "fixed" the problem. But I can not determine the exact cause. However, I can tell what happened and what I changed. The mistake might be something @ProgrammingLlama indicated, not sure.

    I created a test project in, according to VS, the tests folder. This project was named Core.Tests. When I checked the project directory I noticed that something went wrong. The Core.Tests was not placed in the corresponding tests folder. So naturally I moved it to the tests folder.

    Once the project had been moved, exceptions came to light. Exceptions about a cycle that actually did not exist. There are two other projects: Infrastructure and Shared. These two projects, completely randomly, had a reference to the API project (which makes no sense in my chosen architecture). And I also did not do this myself. So something - small suspicion in the comments - went wrong. I fixed it by removing these API references.

    I guess the only conclusion I can make is that, when such a situation occurs, just check projects for bad references (like references that don't belong there). Might as well check all the project references just to be sure.

    I basically fixed it by checking the project references, and then proceeded to fix them. Now everything works again.