本文记录一个 ReSharper 已知问题,在在 Directory.Build.props 属性求值结果不能应用到项目里,此行为将导致 ReSharper 获取的构建文件与 VisualStudio 不同,导致了可能存在的冲突
此问题我已经报告给 ReSharper 官方,详细请看: https://youtrack.jetbrains.com/issue/RSRP-500675
以下是我的报告内容:
ReSharper Reports ‘Ambiguous reference’ Error Incorrectly in Project with Cross - Project References
If a project simultaneously marks and references the code of another project or conducts project references, an “Ambiguous reference” error may appear in ReSharper. However, the project can actually be built successfully in Visual Studio.
The project structure to reproduce this issue is a bit complex, but don’t worry. I’ve built it. I’ve placed the minimal reproducible code on GitHub, and you can find all the code via this link https://github.com/lindexi/lindexi_gd/tree/40067cf83fece460a7d80c3d2445e6ab652531b3/Workbench/QeedekallheweWhanenurnellere . Here are the steps to reproduce:
- Create three new projects: Lib, AllInOne, and App projects.
- Put the Directory.Build.props file in the folder where the sln file is located.
- Establish the association between projects: The AllInOne project depends on the Lib project, and the App project depends on the AllInOne project.
After creating the projects, fill in the necessary content.
Add the following custom attribute content to the Directory.Build.props file:
<PropertyGroup>
<PackAllInOne>true</PackAllInOne>
<PackAllInOne Condition="'$(Configuration)'=='debug'">false</PackAllInOne>
</PropertyGroup>
Add an empty Foo type to the Lib project with no code inside, as only the type is needed to reproduce this issue.
namespace Lib;
public class Foo
{
}
Write the Program.cs file in the App project to create a Foo object. The code is as follows: csharp
using Lib;
var foo = new Foo();
Up to this point, everything works fine. This is because the App project indirectly depends on the Lib project through the AllInOne project and can access the Foo type.
The final step is to edit the content of the AllInOne’s csproj file. Configure the AllInOne project to decide whether to reference the Lib project or the code inside the Lib project through the PackAllInOne property. The code is as follows:
<ItemGroup Condition=" $(PackAllInOne) == true ">
<Compile Include="..\Lib\**\*.cs" />
</ItemGroup>
<ItemGroup Condition=" $(PackAllInOne) != true ">
<ProjectReference Include="..\Lib\Lib.csproj" />
</ItemGroup>
After completing this step, you’ll see that the Program.cs
file, which originally worked fine in the App project, now shows an “Ambiguous reference” error in ReSharper. Even though the project can still be built successfully in Visual Studio, it means ReSharper’s error message is an incorrect false alarm.
You can find all my code in https://github.com/lindexi/lindexi_gd/tree/40067cf83fece460a7d80c3d2445e6ab652531b3/Workbench/QeedekallheweWhanenurnellere
本文会经常更新,请阅读原文: https://blog.lindexi.com/post/ReSharper-%E5%B7%B2%E7%9F%A5%E9%97%AE%E9%A2%98-%E5%9C%A8-Directory.Build.props-%E5%B1%9E%E6%80%A7%E6%B1%82%E5%80%BC%E7%BB%93%E6%9E%9C%E4%B8%8D%E8%83%BD%E5%BA%94%E7%94%A8%E5%88%B0%E9%A1%B9%E7%9B%AE%E9%87%8C.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。
如果你想持续阅读我的最新博客,请点击 RSS 订阅,推荐使用RSS Stalker订阅博客,或者收藏我的博客导航
本作品采用
知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议
进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接:
https://blog.lindexi.com
),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请
与我联系
。
无盈利,不卖课,做纯粹的技术博客
以下是广告时间
推荐关注 Edi.Wang 的公众号
欢迎进入 Eleven 老师组建的 .NET 社区
以上广告全是友情推广,无盈利