You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This issue is about how to control what changes Visual Studio automatically introduces to a project when having custom MSBuild items.
Description
Basically, I have a library which handles .cshtml files by passing them to a Roslyn source generator. I'd like to do it the clean way by mapping those files to a custom MSBuild item (of type RazorBlade), and then passing those as AdditionalFiles items to Roslyn. Note that the Razor SDK is not supposed to be used in this scenario.
The library provides the following .props file, which maps .cshtml files in the project to RazorBlade items, unless the EnableDefaultRazorBladeItems property is set to false:
The goal here is to use all .cshtml files by default, or let the users customize them if needed by manipulating the RazorBlade items themselves.
Unfortunately, this code causes Visual Studio to remove RazorBlade items when adding a new file: if I create NewFile.cshtml using the UI, Visual Studio will automatically add the following to the .csproj file:
The inital issue also reported that after removing the code that VS inserted, the "Build Action" property was inconsistent (which I can understand given that two different MSBuild item types reference the same file):
The build action was also not set correctly, sometimes getting set to Content, sometimes to C# analyzer file
Add a NewFile.cshtml file using the Visual Studio UI (Add -> New item...)
Notice the change in the project file
Question
I'd like to know which is the correct way to handle this.
I suppose I'll need to use $(DesignTimeBuild) somewhere, or move the mapping to AdditionalFiles to an MSBuild target. But what would be the idiomatic way to proceed in order to avoid surprises with what Visual Studio does with the .csproj file? Also, how does it behave when several MSBuild items are mapped to the same file?
More generally, I think this should ideally be documented, as it is not straightforward, and there are quite a few libraries doing this kind of things, using different approaches.
Details
Reproduced with VS 17.12.3
Here are some related issues I could find, but none of them provide a clear solution to the problem:
All of this seemed necessary in order to get the proper behavior, but I'm leaving this issue open as I'm not sure if this is the best way to solve the issue, and would appreciate a comment on that.
Summary
I am trying to resolve ltrzesniewski/RazorBlade#16 but I don't know which is the best way to proceed.
This issue is about how to control what changes Visual Studio automatically introduces to a project when having custom MSBuild items.
Description
Basically, I have a library which handles
.cshtml
files by passing them to a Roslyn source generator. I'd like to do it the clean way by mapping those files to a custom MSBuild item (of typeRazorBlade
), and then passing those asAdditionalFiles
items to Roslyn. Note that the Razor SDK is not supposed to be used in this scenario.The library provides the following
.props
file, which maps.cshtml
files in the project toRazorBlade
items, unless theEnableDefaultRazorBladeItems
property is set to false:Then, the following
.targets
file configures them and generatesAdditionalFiles
items:The goal here is to use all
.cshtml
files by default, or let the users customize them if needed by manipulating theRazorBlade
items themselves.Unfortunately, this code causes Visual Studio to remove
RazorBlade
items when adding a new file: if I createNewFile.cshtml
using the UI, Visual Studio will automatically add the following to the.csproj
file:This is not exactly what I'd like to happen. 😅
The inital issue also reported that after removing the code that VS inserted, the "Build Action" property was inconsistent (which I can understand given that two different MSBuild item types reference the same file):
Repro
Open the following project file:
Add a
NewFile.cshtml
file using the Visual Studio UI (Add -> New item...)Notice the change in the project file
Question
I'd like to know which is the correct way to handle this.
I suppose I'll need to use
$(DesignTimeBuild)
somewhere, or move the mapping toAdditionalFiles
to an MSBuild target. But what would be the idiomatic way to proceed in order to avoid surprises with what Visual Studio does with the.csproj
file? Also, how does it behave when several MSBuild items are mapped to the same file?More generally, I think this should ideally be documented, as it is not straightforward, and there are quite a few libraries doing this kind of things, using different approaches.
Details
Reproduced with VS 17.12.3
Here are some related issues I could find, but none of them provide a clear solution to the problem:
The text was updated successfully, but these errors were encountered: