dotnet / msbuild

The Microsoft Build Engine (MSBuild) is the build platform for .NET and Visual Studio.
https://docs.microsoft.com/visualstudio/msbuild/msbuild
MIT License
5.21k stars 1.35k forks source link

Csc task cannot be found when using custom toolset #3002

Open dfederm opened 6 years ago

dfederm commented 6 years ago

Steps to reproduce

Minimal repro located here: https://github.com/dfederm/msbuild/tree/csc-not-found-custom-toolset/src/Samples/CscNotFoundWithCustomToolset

After building, run something like:

"D:\msbuild\artifacts\Debug\bin\Samples\CscNotFoundWithCustomToolset\net46\CscNotFoundWithCustomToolset.exe" "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\"

Expected behavior

The Compile target succeeds

Actual behavior

Output:

__________________________________________________
Project "D:\msbuild\src\Samples\CscNotFoundWithCustomToolset\Test.csproj" (Compile target(s)):

C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error MSB4062: The "Microsoft.CodeAnalysis.BuildTasks.Csc" task could not be loaded from the assembly \Microsoft.Build.Tasks.CodeAnalysis.dll. Could not load file or assembly 'file:///D:\Microsoft.Build.Tasks.CodeAnalysis.dll' or one of its dependencies. The system cannot find the file specified. Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.
Done building project "Test.csproj" -- FAILED.
Compile failed

Environment data

Using the latest version of Microsoft.Build.Runtime (15.5.180.1)

Other

Talked with @cdmihai and one guess to a solution would be to change Microsoft.Common.tasks from:

<UsingTask TaskName="Microsoft.CodeAnalysis.BuildTasks.Csc" AssemblyFile="Microsoft.Build.Tasks.CodeAnalysis.dll" />

to

<UsingTask TaskName="Microsoft.CodeAnalysis.BuildTasks.Csc" AssemblyFile="$(RoslynTargetsPath)\Microsoft.Build.Tasks.CodeAnalysis.dll" />

That way it always looks next to Roslyn adjacent to the same place it imported the targets from.

Or actually I wonder why the Roslyn tasks aren't defined as a <UsingTask> in the Roslyn targets in the first place like any other task. IMHO the Csc task doesn't need to be "special".

manish10sharma commented 6 years ago

Hello I am having the same issue. MSB4062 The "Microsoft.CodeAnalysis.BuildTasks.Csc" task could not be loaded from the assembly C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Roslyn\Microsoft.Build.Tasks.CodeAnalysis.dll. Could not load file or assembly 'Microsoft.Build.Utilities.Core, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. Confirm that the declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. MSBuild C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Roslyn\Microsoft.CSharp.Core.targets 52 false Error

Can Anyone tell me solution if found? I am using 2017 15.8.2 Version.

Please help. Thanks

rainersigwald commented 6 years ago

@manish10sharma I think that is a distinct problem. Are you also using a custom MSBuild host executable? If so, I suspect you need to add binding redirects for MSBuild assemblies to the 15.0 versions. Using the Microsoft.Build.Locator package can help with that.

manish10sharma commented 6 years ago

I have tried that even, But it throwing same error. I am attaching my packages file. May be something conflicting.

packagesForTest.txt

rainersigwald commented 6 years ago

@manish10sharma I think the Locator package only adds the binding redirects for you if you use PackageReference style NuGet references. Did you try manually adding binding redirects like the ones we use in MSBuild.exe?

https://github.com/Microsoft/msbuild/blob/6244c69aa4732353bbc0a2c9adfd2e5f79de5516/src/MSBuild/app.config#L13-L33

manish10sharma commented 6 years ago

Yes, I already tried that all. See the errors, I am facing When I hit Build Command

exceptionafterbuild exceptionafterbuild2