John Dyer
John Dyer

Reputation: 2358

MSBuild 16.9 .NET Core 3.1 - OutDir isn't searched for dependencies

I'm trying to use MSBuild in a powershell script to build many projects and solutions in a full application suite. I set the parameter for OutDir to point to a single binaries directory and from an output perspective that works.

However the documentation states that OutDir is included in AssemblySearchPaths. But looking at the logs MSBuild is clearly stuck using the hintpath from the csproj file. I've tried setting AdditionalLibPaths as well with no success. This appears to be an issue with building from Visual Studio 2019 as well. My hintpaths point to a common debug directory. A release build still looks in the debug directory. This used to work in older versions of Studio in the .NET Framework days. It worked in older TFS XAML builds setting Output Location to "SingleFolder"

I've also played around with OutDir path ending various quantities of back slashes. I suspect that this old issue is fixed.

How can I get MSBuild to use an alternate directory for the dependencies?

https://learn.microsoft.com/en-us/visualstudio/msbuild/common-msbuild-project-properties?view=vs-2019

EDIT: As per the accepted answer, adding OutDir to the AssemblySearchPaths does the trick. For me, I've created a proj file that I've added to each .NET Core csproj files. My thought is that when this gets fixed I can remove the tweak in one place.

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <AssemblySearchPaths>$(AssemblySearchPaths);$(OutDir)</AssemblySearchPaths>
</PropertyGroup>

Upvotes: 1

Views: 796

Answers (1)

Mr Qian
Mr Qian

Reputation: 23780

My hintpaths point to a common debug directory. A release build still looks in the debug directory.

The outdir is always the output folder which does not distinguish between Release and Debug. So you have to use <OutDir>C:\ttt\$(Configuration)\</OutDir> to distinguish between them.

Actually, the system msbuild properties are read earlier than the start of build task. You have to set the properties before the start of build process.

Simply modifying the system properties outdir in csproj will only take effect during the build process, but the system properties are still read before the build starts, also AssemblySearchPaths property read the previous outdir property. So you always take the default values before the modification.

You have to use Directory.Build.props file, it set the values earlier than msbuild start.

1) create a file called Directory.Build.props under your project folder.

2) add the outdir property like these into the file.

<Project>

<PropertyGroup>
   <OutDir>C:\ttt\$(Configuration)\</OutDir>
</PropertyGroup>

</Project>

3) restart VS to enable it.

However, I note that it works well in non-sdk net framework projects but it does not list under new-sdk net core projects.

non-sdk net framework projects

enter image description here

new-sdk net core projects

enter image description here

Not sure it is an issue or the Team has forgotten it. Anyway, I have reported it to our DC Forum. You can vote it or add any comments if I did not described it in detail.

As a workaround, you could try to set the new value for AssemblySearchPaths property.

In order not to lose the original value of AssemblySearchPaths, you must add it to csporj file rather than Directory.Build.props file.

Just add these into csproj file:

<PropertyGroup>
    <AssemblySearchPaths>$(AssemblySearchPaths);$(OutDir)</AssemblySearchPaths>
</PropertyGroup>

enter image description here

Update 1

I think it is an issue for net core projects.

What I said before is for VS IDE build. Now for MSBuild Command Line, it is another situation.

For non-sdk net framework projects

When I used msbuild xxx\xxx.csproj -p:outdir=c:\ttt -v:diagnostic, it shows this:

enter image description here

Well. It works perfect as we wished.

However, when we used the same command line for new-sdk net core projects, it does nothing. So I think it is quite an issue for net core projects.

And you should note that AdditionalLibPaths cannot be recognized by AssemblySearchPaths.

When I used this under :

 msbuild xxx\xxx.csproj -p:AdditionalLibPaths=c:\ttt -v:diagnostic

enter image description here

enter image description here

And you should note that there is no property for AdditionalLibPaths under the list of AssemblySearchPaths property. And it also does not work for net core projects.

In short, it is quite an issue for net core projects no doubt. I also modify the DC ticket.

Now for new-sdk net core projects,

Since you used msbuild command line to set properties, so there is no need to use Directly.Build.props file. MSbuild command line property assignment is actually the same effect of the file.

Also, AssemblySearchPaths is not ready-only. You could modify it. And actually, all msbuild properties can be overwritten and that is a flexible feature of MSBuild.

In summary, you still have to use AssemblySearchPaths.

Solution

Since The Team has some problems with this detail in the net core project, we can use the flexibility of MSbuild to manually modify to get what we want:

1) abandon using Directly.Build.props file and also keep adding these on the net core csproj file:

 <PropertyGroup>
        <AssemblySearchPaths>$(AssemblySearchPaths);$(OutDir)</AssemblySearchPaths>
  </PropertyGroup>

2) use the following command line for net core projects:

msbuild xxx\xxx.csproj -p:Outdir=c:\ttt -v:diagnostic

enter image description here

Upvotes: 1

Related Questions