I have this structure:
\
MySolution.sln
Directory.Build.props (1)
\src
Directory.Build.props (2-src)
\Project1
\Project2
\test
Directory.Build.props (2-test)
\Project1Tests
\Project2Tests
I have common properties for all projects (1), common properties for src projects (2-src), and common properties for test projects (2-test).
For (2-src) and (2-test) to import (1), according to advice given at the repo, I added to each of them:
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props'))" />
That doesn't work (I get an error that the import causes a circular dependency.). So I tried:
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '../'))" />
Which also doesn't work for the same reason. But this does work:
<Import Project="../Directory.Build.props" />
I prefer to use the msbuild commands (which support a deeper directory structure), rather than a hardcoded value. Is that possible?
You can work around it by using the folder name of the current file ($(MSBuildThisFileDirectory)
):
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)..\'))" />
This is required because relative paths are interpreted as relative to the project so just using ../
as the second parameter would always be "one up from the csproj" file regardless of the location of the file this statement is in.
Worked for me, with a minor improvement:
<PropertyGroup>
<ParentDirectoryBuildPropsPath>$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)..\'))</ParentDirectoryBuildPropsPath>
</PropertyGroup>
<ImportGroup>
<Import Project="$(ParentDirectoryBuildPropsPath)" />
</ImportGroup>
Condition accordingly, of course.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With