Preventing Visual Studio from rewriting project re

2019-08-25 16:39发布

I have a large solution in which the "root" project includes feature projects by glob:

<ProjectReference Include="..\Feature\*\*.csproj" />

This works, despite not looking great in the references list, but the issue that occasionally Visual Studio will rewrite the csproj with all of the project references resolved:

<ProjectReference Include="..\Feature\A\A.csproj" />
<ProjectReference Include="..\Feature\B\B.csproj" />

It's not clear what triggers this, but I'm guessing it might have something to do with NuGet.

Is there anyway to stop VS from doing this (akin to using DisableFastUpToDateCheck for custom MSBuild scenarios)?

2条回答
smile是对你的礼貌
2楼-- · 2019-08-25 16:57

I've done further research on this.

For testing, you can consistency reproduce the expansion by renaming any project that's included in the wildcard pattern.

Also, the easiest way to prevent the expansion is to:

  1. Move the project reference globs into Directory.Build.props or another external file
  2. Set DisableFastUpToDateCheck to true in your csproj

Using properties doesn't work and isn't required, likewise with using an Exclude.

查看更多
Summer. ? 凉城
3楼-- · 2019-08-25 17:04

It's not clear what triggers this, but I'm guessing it might have something to do with NuGet.

It should be related to the items in the ItemGroup. I have the similar issue before, but the difference is that I use wildcards to contain .cs files and your are .csproj files, looks like:

<ItemGroup>
    <Compile Include="**\*.cs" />
</ItemGroup>

When I delete one of .cs file in the <ItemGroup>, the wildcard gets expanded in the csproj file. For you case, if I deleted the the C.csproj project from Visual Studio (Add it before, reload the root project), then I got the same result as you.

For this issue, many other community members submit a user voice to Visual Studio team: VS IDE should support file patterns in project files. Now this is well supported in the new project system used by .NET Core and .NET Standard in Visual Studio 2017, but they haven't done the work to support it for existing project types.

Is there anyway to stop VS from doing this (akin to using DisableFastUpToDateCheck for custom MSBuild scenarios)?

To resolve this issue, you can use option Exclude="..." to exclude the project that you do not want to refer to:

  <ItemGroup>
    <ProjectReference Include="..\Feature\*\*.csproj" Exclude="..\Feature\C\C.csproj" />
  </ItemGroup> 

Or, if you want to delete one of project and keep the wildcard pattern, you only need to unload the root project, then delete the reference project, reload the root project, the wildcard pattern would be preserved.

Hope this helps.

查看更多
登录 后发表回答