Setting BaseIntermediateOutputPath correctly in a SDK-based project is hard
NuGet restore drops the project.assets.json file to the $(BaseIntermediateOutpath). If a user customizes that by setting the property in a SDK-based project like so:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<BaseIntermediateOutputPath>C:\blah</BaseIntermediateOutputPath>
</PropertyGroup>
</Project>
Then the project.assets.json gets dropped to that folder correctly. However if there are nuget packages in this project that have tasks\targets then the generated project.nuget.g.props\targets are imported by Microsoft.Common.Props. Therefore BaseIntermediateOutputPath needs to be defined before the import of the common props and for that they have to know to expand the SDK import:
<Project>
<PropertyGroup>
<BaseIntermediateOutputPath>C:\blah</BaseIntermediateOutputPath>
</PropertyGroup>
<Import Sdk="Microsoft.NET.Sdk" Project="Sdk.props" />
...
<Import Sdk="Microsoft.NET.Sdk" Project="Sdk.targets" />
</Project>
I don't know what we can do to fix this but logging an issue here so that atleast this serves as documentation for people running into this issue.
An easier way is to use the new Sdk attribute for import elements:
<Project>
<PropertyGroup>
<BaseIntermediateOutputPath>C:\blah</BaseIntermediateOutputPath>
</PropertyGroup>
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk/1.0.0" />
...
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk/1.0.0" />
</Project>
See #1493
Has that syntax been implemented already?
According to this diff it was: https://github.com/Microsoft/msbuild/pull/1492/files#diff-325c8a74f9ae27c1b3f8870e9cb64678L2310
For posterity: another option is using a Directory.Build.props to set this. See https://github.com/dotnet/sdk/issues/802.
Is there are "correct" solution for this? All I'm trying to do is move the bin\ and obj\ folders that .NET Core projects dump in the project folder into a Build\ directory at the solution level so they're separate from the source code. I added this:
<PropertyGroup>
<OutputPath>$(SolutionDir)\Build\$(ProjectName)\bin\$(Configuration)</OutputPath>
<BaseIntermediateOutputPath>$(SolutionDir)\Build\$(ProjectName)\obj</BaseIntermediateOutputPath>
</PropertyGroup>
to the top of our .csproj files, but it appears that $(ProjectName) is not set at this point? Is there some correct way to accomplish this as it's driving me crazy. :) Note that it appears that $(SolutionDir) works fine. It also looks like OutputPath always has the netcoreapp1.1 or netstandard1.6.1 folder appended as well.
Both moving the imports as in https://github.com/Microsoft/msbuild/issues/1603#issuecomment-275271522 and using a Directory.Build.props as in https://github.com/Microsoft/msbuild/issues/1603#issuecomment-277726334 are "correct", @Ziflin. Chose whichever meets your needs better.
I don't have an imports section. This is just a simple .NET Core Console App and Lib solution. So the console app uses:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
</Project>
And I added the PropertyGroup in my first comment above this PropertyGroup. Is $(ProjectName) supposed to be valid or is there some other variable name I can use that is for the project's name? It's basically treating it like it's not set.
Sorry I'm not clear on how to use the Directory.Build.props from that other comment/issue..
@Ziflin your two options are:
Create a file named Directory.Build.props in a folder above your projects (maybe next to your solution? you know your repo layout best) that sets the properties you want to set. It will be automatically included in any project below it in the directory structure.
Or change your project file from the implicit imports model to explicit imports, so that you can control order. These are exactly identical:
-<Project Sdk="Microsoft.NET.Sdk">
+<Project>
+ <Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
+ <Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
</Project>
After you've made the implicit imports explicit, you can add or move things around them to affect relative order.
@rainersigwald Thanks for the help, but neither of those methods seem to have any effect on the fact that the $(ProjectName) variable is not set which makes it very difficult to do something like:
<BaseIntermediateOutputPath>$(SolutionDir)\Build\$(ProjectName)\obj</BaseIntermediateOutputPath>
And have it create a Build\MyProject\obj folder under the solution. If I hardcode a name for $(ProjectName):
<BaseIntermediateOutputPath>$(SolutionDir)\Build\MyProject\obj</BaseIntermediateOutputPath>
then it works as expected, but then I am unable to use a single Directory.Build.props file for all the projects in the solution. The $(SolutionDir) and $(Configuration) variables seem to work fine so I wasn't sure if I just had the name wrong for the project name variable or if it was a bug.
Would $(MSBuildProjectName), which is a well-known property automatically populated by MSBuild based on the file name, work for you?
That works perfectly! Sorry for the confusion. I saw that $(SolutionDir) was working and assumed $(ProjectName) would as well.
From @srivatsn
BaseIntermediateOutputPathneeds to be defined before the import of the common props and for that they have to know to expand the SDK import.
Even easier way is to generalize implicit imports placement.
<Project Sdk="Custom.Sdk">
<PropertyGroup Evaluation="BeforeImplicitProps">
<BaseIntermediateOutputPath>..\..\Build</BaseIntermediateOutputPath>
</PropertyGroup>
...
<PropertyGroup Evaluation="AfterImplicitTargets">
<SomeImportantPropertyAfterTargets>Value!!!</SomeImportantPropertyAfterTargets>
</PropertyGroup>
</Project>
would translate to this
<Project>
<PropertyGroup>
<BaseIntermediateOutputPath>..\..\Build</BaseIntermediateOutputPath>
</PropertyGroup>
<Import Project="Sdk.props" Sdk="Custom.Sdk/1.0.0" />
...
<Import Project="Sdk.targets" Sdk="Custom.Sdk/1.0.0" />
<PropertyGroup>
<SomeImportantPropertyAfterTargets>Value!!!</SomeImportantPropertyAfterTargets>
</PropertyGroup>
</Project>
Here, assume Custom Sdk uses common Sdk. This is helpful in creating custom .proj file with Sdk story to them like I can use any custom Sdk that uses common Sdk or itself. See Issue #1686
This is one way to fix the problem and It does it even before all the props and after all the targets, which would be useful for many debugging and logging scenarios.
@Nirmal4G Directory.Build.props is imported before BaseIntermediateOutputPath is set so I would recommend you just use that.
@jeffkl I know!
But I want to set some properties (within the Project file) even before all the implicit props and have some targets after all the implicit targets, something along those lines!
Where in the SDK props do we implicitly import .props from nuget packages? I wanted to look at it and see if I can come up with any other clever tricks for my Sdk props (trying to avoid Directory.Build.props for the time being), but I can't find the actual place where we look at the project assets and import them.
See the lines here, the comments will tell you everything!
https://github.com/dotnet/msbuild/blob/b38e4ceeaaec36c5237ae698041e9b9f18c84876/src/Tasks/Microsoft.Common.props#L22-L63
And in the targets...
https://github.com/dotnet/msbuild/blob/b38e4ceeaaec36c5237ae698041e9b9f18c84876/src/Tasks/Microsoft.Common.targets#L116-L147
That is how nuget and other package managers (paket, etc) import the restored assets!
You can modify MSBuildProjectExtensionsPath to generalize your assets output!
It's not just MSBuildProjectExtensionsPath though, as I wrote for https://stackoverflow.com/questions/45575280/msbuild-nuget-restoreoutputpath-how-to-make-it-work, there are more properties that need to work together:
BaseIntermediateOutputPath- used to construct:ProjectAssetsFile(SDK targets)MSBuildProjectExtensionsPathif unset (MSBuild - Microsoft.Common.props)RestoreOutputPath(NuGet targets)
MSBuildProjectExtensionsPath- could theoretically be set to something differentRestoreOutputPath- tells restore where to drop assets file and extension targets.
If those three don't point to the same directory -> 💩
The dangerous part is that those three properties are coming from different components so if you're not careful about which one is set where (=> set base..path early or set all), you won't have a good time.
I had the same experience, and that was the solution that saved me, thank you for that, but if you are using the latest tools you can get away with those problems.
But if you are using props/targets that are before the ProjectExtensions logic, It's better to shim up (by detecting ImportProjectExtensionProps) those in Directory.Build.props/targets so that you can use them with your old toolsets ensuring forward compatibility.
Thanks for the summary of the interrelated properties, @dasMulli.
MSBuildProjectExtensionsPath is the only one of these properties that is used in the common props, before the body of the project. So I think a good solution would be:
- Use
MSBuildProjectExtensionsPathinstead ofBaseIntermediateOutputPathto construct theRestoreOutputPath. This would mean it would be OK to changeBaseIntermediateOutputPathin the body of a project, and it would mean that the intermediate build output would go in the specified folder, but the NuGet assets, props, and targets files would continue to go in the defaultobjfolder. - Add an errror to MSBuild if the
MSBuildProjectExtensionsPathis modified between when it's used in the common props and when it's used in the common targets. This would catch anyone who tries to override this property in the body of their project file.