I'm generating some .cs files using node.exe before the compilation, which I then include in my project.
I call node.exe and define the steps for the generation via CSProj. After migrating the CSProj to the new format <Project Sdk="Microsoft.NET.Sdk"> the files are constantly being generated, erased and generated again while Visual Studio is open (tested with VS2017 and VS2019).
This is my targets file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
...
</PropertyGroup>
<Target Name="SetVariables">
<ItemGroup>
<_NodeJsExe Include="$(MSBuildThisFileDirectory)..\..\..\Node.js.redist\*\tools\win-x64\node.exe"/>
</ItemGroup>
<PropertyGroup>
<__NodeJsExe>#(_NodeJsExe);</__NodeJsExe>
<NodeJsExe>$(__NodeJsExe.Substring(0, $(__NodeJsExe.IndexOf(';'))))</NodeJsExe>
</PropertyGroup>
</Target>
<Target Name="ProblemHere" BeforeTargets="TransformDuringBuild;PrepareForBuild;CoreCompile" DependsOnTargets="SetVariables;CleanFilesBeforeBuild">
<Exec Command=""$(NodeJsExe)"..." />
</Target>
<Target Name="CleanFilesBeforeBuild">
<ItemGroup>
<Ts2LangFiles Include="$(GeneratedFolder)\**\*.cs" />
</ItemGroup>
<Delete Files="#(Ts2LangFiles)"/>
</Target>
</Project>
Anyone knows what the problem is?
Found out that Visual Studio is building some targets at design-time.
If I add this to my custom target, the files stop being constantly generated by MSBuild:
<Target Name="ProblemHere" Condition="$(DesignTimeBuild) != true And $(BuildingProject) == true" BeforeTargets="TransformDuringBuild;PrepareForBuild;CoreCompile" DependsOnTargets="SetVariables;CleanFilesBeforeBuild">
I just have yet to understand if this is a bug in Visual Studio or if the problem is mine and I'm using the targets like I'm not supposed to.
Related
is it possible to run a project with an argument of a dll from another project?
My project structure and dependencies look like this
As you can see there are no direct dependencies between a mod and the game implementation directly.
When I click on Debug -> Start New Instance, F5 or use the button () I want to run ./ExampleGameInEngineA.exe GameModC.dll.
I know that you can set an Executable in the project settings
But I am looking for a more generic way that is automatically compiling ExampleGameInEngineA and putting it in the same directory as GameModC.
Hardcoded directory paths are also not great (tho I could use relative paths for this).
For testing purposes we can safely ignore GameInEngineB but I still don't want any hard references to GameInEngineA.
Okay, I kinda solved it by myself by using a PreBuild-Event-Target:
GameModC.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<RunPostBuildEvent>OnOutputUpdated</RunPostBuildEvent>
</PropertyGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent" Condition="">
<MSBuild Projects="$(SolutionDir)ExampleGameInEngineA\ExampleGameInEngineA.csproj..." />
<ItemGroup>
<GameEngineAOutputFolder Include="$(SolutionDir)ExampleGameInEngineA\$(OutDir)*.*" />
</ItemGroup>
<Copy SourceFiles="#(GameEngineAOutputFolder)" DestinationFolder="$(OutDir)" SkipUnchangedFiles="false" />
</Target>
<ItemGroup>
<ProjectReference Include="..\GameApi\GameModdingApi.csproj" />
</ItemGroup>
</Project>
GameInEngineA.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\GameApi\GameModdingApi.csproj" />
</ItemGroup>
</Project>
Please note the AppendTargetFrameworkToOutputPath to remove the netcoreapp3.1 or netstandard2.1.
But there are still problems with the solution:
It does not recognize changes made in ExampleGameInEngineA (manual rebuild required)
The Condition when to add ExampleGameInEngineA is always met (because I have no good idea for a condition)
I am still open for better solutions.
I set CopyLocalLockFileAssemblies to true and want to filter the output. So I used the following code:
<Target Name="FilterCopyLocalItems" AfterTargets="ResolveLockFileCopyLocalProjectDeps">
<ItemGroup>
<ReferenceCopyLocalPaths Remove="#(ReferenceCopyLocalPaths)" Condition="'%(Filename)' == 'Microsoft.Extensions.DependencyInjection.Abstractions'" />
</ItemGroup>
</Target>
But this code did not work, how can I put a filter on the output?
Your target FilterCopyLocalItems is to remove the reference dll from the output folder.
I wonder if you means that the target cannot be executed.
For me, I used the below xml code in my net core project which installed the nuget package Microsoft.Extensions.DependencyInjection.Abstractions.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
<Target Name="FilterCopyLocalItems" AfterTargets="ResolveLockFileCopyLocalProjectDeps">
<ItemGroup>
<ReferenceCopyLocalPaths Remove="#(ReferenceCopyLocalPaths)" Condition="'%(Filename)' == 'Microsoft.Extensions.DependencyInjection.Abstractions'" />
</ItemGroup>
</Target>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.6" />
</ItemGroup>
</Project>
You can find the target under the Detailed output build log.
Enter Tools-->Options-->Projects and Solutions-->Build and Run-->set MSBuild project build output verbosity to Detailed.
And you can see the target by searching its name under the detailed output log while you build it.
It will prevent the Microsoft.Extensions.DependencyInjection.Abstractions.dll being generated in the output folder.
Update 1
Actually, you may do some extra operation which causes the target ResolveLockFileCopyLocalProjectDeps not to be triggered. Due to the lack of your detailed project structure and CSPROJ file, I did not notice that.
For your situation, the target ResolvePackageDependenciesForBuild works well.
So in your side, you should use this:
<Target Name="FilterCopyLocalItems" AfterTargets="ResolvePackageDependenciesForBuild">
<ItemGroup>
<ReferenceCopyLocalPaths Remove="#(ReferenceCopyLocalPaths)" Condition="'%(Filename)' == 'Microsoft.Extensions.DependencyInjection.Abstractions'" />
</ItemGroup>
</Target>
Besides, when you execute the target, please do not add <ExcludeAssets>Runtime</ExcludeAssets> under the PackageReference of your nuget package, its effect is actually the role of your target FilterCopyLocalItems. At runtime, remove the related package.dll in the output folder. See this document.
So you should delete it to avoid reuse.
The issue:
If I change the file extension from the visual studio, the file Build Action will be according to the previous Build Action and not according to the new file extension.
There is a way to enforce the visual studio always takes the Build Action from the file extension?
Example:
Create a new .Net Core 3.1 Class library. There is a single file (Class1.cs) The csproj will be like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>
Change the name of Class1.cs to Class1.txt from the visual studio. The result, Class1.txt is with Build Action Compile although it is a text file. The content of csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<None Remove="Class1.txt" />
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.txt" />
</ItemGroup>
</Project>
Additional information:
I'm using Visual Studio 2019.
If I change the file extension from the file system the issue doesn't appear.
Update:
A possible solution is to change the file Build Action after each file rename, but I want an automatic way.
I'm trying to use the target event "BeforeBuild" in .csproj (vs2017), but it's not working. Someone would know what is wrong:
<Project DefaultTargets="BeforeBuild" Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<Target Name="BeforeBuild">
<Message Text="Test123"></Message>
</Target>
</Project>
The expected result is a message: Test123 on output.
[]s
BeforeBuild dosen't working in csproj
That because Before/AfterTarget in csproj gets overridden by SDKs target file.
if you're using the new Sdk attribute on the Project element, it's not possible to put a target definition after the default .targets import. This can lead to targets that people put in their project files unexpectedly not running, with no indication why unless you examine the log file and see the message that the target has been overridden.
dsplaisted have filed Microsoft/msbuild#1680 for this issue. As a workaround, you can do the following:
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
<PreBuildEvent />
</PropertyGroup>
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
<Target Name="BeforeBuild">
<Message Text="Test123"></Message>
</Target>
Or:
<Target Name="test" BeforeTargets="Build">
<Message Text="Test123" />
</Target>
From the official docs:
Warning
Be sure to use different names than the predefined targets listed in
the table in the previous section (for example, we named the custom
build target here CustomAfterBuild, not AfterBuild), since those
predefined targets are overridden by the SDK import which also defines
them. You don't see the import of the target file that overrides those
targets, but it is implicitly added to the end of the project file
when you use the Sdk attribute method of referencing an SDK.
I have several XSLTs used in my ASP.NET web application.
I want these files to be compiled to dll whenever I build the project.
Currently, I'm compiling the xslts manually by invoking xsltc.exe from vs2010 tools command prompt.
How can I add msbuild task for xsltc.exe so that it will generate assembly whenevr i build my project?
I'm using .NET 4.0.
That works but doesn't really wrap the tool in a MSBuild friendly way.
I came up with this (which was good enough to get by).
<!-- The Transform File Names... -->
<ItemGroup>
<XsltcTransform Include="Transform1.xslt">
<!-- And the generated .Net Class name. -->
<Class>Transform1Class</Class>
</XsltcTransform>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- Sadly using $(OutDir) MUST come after the Import of CSharp.targets -->
<PropertyGroup>
<XSLTCOutputDll>$(OutDir)xslts.dll</XSLTCOutputDll>
</PropertyGroup>
<Target Name="FindXSLTC">
<PropertyGroup>
<XSLTC>"$(TargetFrameworkSDKToolsDirectory)xsltc.exe"</XSLTC>
</PropertyGroup>
</Target>
<Target Name="XSLTC" Inputs="#(XsltcTransform)" Outputs="$(XSLTCOutputDll)" DependsOnTargets="FindXSLTC">
<Exec Command="$(XSLTC) /out:"$(XSLTCOutputDll)" #(XsltcTransform -> ' /class:%(Class) %(FullPath) ')" />
</Target>
<Target Name="BeforeResolveReferences" DependsOnTargets="XSLTC">
</Target>
These targets will let you compile multiple transforms into one DLL.
Running XSLTC before "BeforeResolveRefereneces" is necessary so that you can have an assembly reference to the generated DLL.
<PropertyGroup>
<WinSDK>C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin</WinSDK>
</PropertyGroup>
<Target Name="Build">
<Exec Command="%22$(WinSDK)\xsltc.exe%22 /out:$(OutputPath)\_PublishedWebsites\xyzapp\bin\Xslts.dll /class:ABC %22$(MSBuildProjectDirectory)\xyzapp\a.xslt%22 /class:DEF %22$(MSBuildProjectDirectory)\xyzapp\b.xslt%22 /class:GHI %22$(MSBuildProjectDirectory)\xyzapp\c.xslt%22"/>
</Target>