The “GetReferenceNearestTargetFrameworkTask” task was not found - visual-studio

Given: My local system references MSBuild at "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSbuild.exe". My automated build server (Jenkins) does not have this version of MSBuild so I used chocolatey to download microsoft-build-tools.
Problem: My visual studio project compiles successfully locally but won't compile successfully on my automated build server (Jenkins) due to a ReferenceAssembly error:
C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(1179,5): error MSB3644: The reference assemblies for framework ".NETFramework,Version=v4.6.2" were not found. To resolve this, install the SDK or Targeting Pack for this framework version or retarget your application to a version of the framework for which you have the SDK or Targeting Pack installed. Note that assemblies will be resolved from the Global Assembly Cache (GAC) and will be used in place of reference assemblies. Therefore your assembly may not be correctly targeted for the framework you intend.
I've searched online and found a possible solution https://developercommunity.visualstudio.com/content/problem/137779/the-getreferencenearesttargetframeworktask-task-wa.html where the powershell script modifies Microsoft Build Tools installer:
Start-Process "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vs_installer.exe" -ArgumentList 'modify --installPath "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools" --quiet --add Microsoft.VisualStudio.Component.NuGet.BuildTools --add Microsoft.Net.Component.4.6.2.TargetingPack --norestart --force' -Wait -PassThru
I ran the script and I could see that it modifies the installer to select the specified target 4.6.2. However, the target package 4.6.2 is not present in the path "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework". I think it successfully downloaded the package one time but I deleted it to see if it was reproducible in my build server (Jenkins).
I don't want to manually paste the v4.6.2 target package from my local system to the build server. Any ideas on how to resolve this reference assembly problem?

For "GetReferenceNearestTargetFrameworkTask":
You can use powershell to call installer.exe, and install the components you lack.
Also, you can start vs installer in your server and modify your build tools to install packages below:
Any ideas on how to resolve this reference assembly problem?
If they are unchecked, check them and click modify button to install them by vs installer.exe.
And if they are checked, uncheck them and click modify to remove completely. After that, check them to reinstall the 4.6.2 framework.
Update:
After my check the modify in the Get Tools and Features can't help resolve assembly while they should.This option can only work to install new framework(which not once installed) but not repair the 4.6.2 once installed but now broken.
Download this developer pack manually and run it can help resolve the missing packages.
In addition:I delete the v4.6.2 folder in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework and get it back by run the pack to repair.

Related

What runtime dependencies does TLBIMP have?

Setting up a clean build machine I have only installed VS2019 so far, including Windows 10 SDK, as far as 3rd-party software is concerned.
An ODL file has custom compile step:
midl /out ..\dll %(FullPath)
TLBIMP ../dll/%(Filename).tlb /namespace:MyOrg.COM /keyfile:../include/MyOrg.snk /out:../dll/%(Filename).dll
This gives the build error:
1>'TLBIMP' is not recognized as an internal or external command,
1>operable program or batch file. 1>C:\Program Files (x86)\Microsoft
Visual
Studio\2019\Professional\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets(231,5):
error MSB6006: "cmd.exe" exited with code 9009.
Clearly MIDL is working, what additional Visual Studio/Windows components do I need to install for TMPLIB?
As Simon points out in comments, TLMIMP.exe is a standard VS tool. Depending which Windows SDK is being used it should be found in:
C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\TlbImp.exe
C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools
You might have multiple versions installed, which is used during a build will depend on your %PATH variable. This would normally be set whenever you run Visual Studio Developer Command Prompt (I assume if you build within the IDE it's done automatically).
My issue was actually that I'm building with Azure Devops and it doesn't automatically add this folder to the build path.

Nuget Restore in a Docker Container

I am trying run nuget restore mySolution.sln in a docker container.
For the most part it seems to work just fine. The packages seem to restore fine, but before they do, it shows this error:
C:\src\myProject\mySolution.csproj(317,11):
error MSB4226: The imported project "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\Microsoft\VisualStudio\v15.0\WebApplications\Microsoft.WebApplication.targets" was not found.
Also, tried to find "WebApplications\Microsoft.WebApplication.targets" in the fallback search path(s) for $(VSToolsPath)
- "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v15.0" .
These search paths are defined in "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\bin\msbuild.exe.Config". Confirm that the path in the declaration is correct, and that the file exists on disk in one of the search paths.
From what I read, this means I need the nuget package MSBuild.Microsoft.VisualStudio.Web.targets. I did a nuget install of that, but the error does not go away. (It just drops it where ever I run it. I tried putting it with my solution and in the packages folder. Neither had any effect.)
How can I fix this error using command line tools? (No GUI because this is a docker container).
I assume what you're trying to build is a web-application, but you haven't installed corresponding workloads in your build tools package.And that's the cause of this issue. (Agree with imps)
Open the Visual Studio Installer in Start menu and you can modify the workloads of your msbuild package:
I only use the msbuild build tools package to build C++ desktop projects in server, so I only install the Visual C++ build tools workload. For you, you have to install the build tools for Web applications so that you can get the necessary files to build web-applications.
Update:
error: The imported project "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\Microsoft\VisualStudio\v15.0\WebApplications\Microsoft.WebApplication.targets" was not found.
After I install the Web development build tools, I now get the newly created WebApplications folder in path C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\Microsoft\VisualStudio\v15.0.

How to install vstest.console.exe offline

I am supposed to do an offline installation of vstest.console.exe in the folder path as follows: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow
I have tried using offline packages such as vs_testagent.exe and vs_testcontroller.exe according to the following link: https://learn.microsoft.com/en-us/visualstudio/install/create-a-network-installation-of-visual-studio. However, these packages do not contain vstest.console.exe.
I am also unable to do a full installation of VS2017 due to space issues. Do anyone have any suggestions? Thanks.
Download the nuget package: Microsoft.TestPlatform, rename it a zip file. Open the zip file, you will find all you need from this folder: .\tools\net451\Common7\IDE\Extensions\TestPlatform, including vstest.console.exe, testhost.**.exe, QTAgent.exe, datacollector.exe.
If you added the nuget package to project, it just downloads the package to packages folder and won't complie to bin, so you have to add a Post-Build event to copy files to bin folder from the packages folder.
You are looking for "Build Tools for Visual Studio" (Microsoft login required). It will give you an installer "vs_BuildTools.exe", which will install by default the build tools in:
C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools
(by default)
Two versions of vstest console were installed when I ran it:
...\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe
...\Common7\IDE\Extensions\TestPlatform\vstest.console.exe
I do not know why, nor how they are different. I use the first one.
I used the chocolatey package visualstudio2019testagent to install VS 2019 Test Agent.
Not sure if this would help someone in the future.
Download the Visual Studio Build Tools installer from https://my.visualstudio.com/Downloads?q=build%20tools%20for%20visual%20studio
Run the installer, click the tab Individual components and select Testing tool core features - Build Tools
After installation the vstest.console.exe is located in folder
C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\Common7\IDE\CommonExtensions\Microsoft\TestWindow

The "GetReferenceNearestTargetFrameworkTask" task was not found

I have a problem with autocompletion in VS 2017 Community.
Previously I had VS 2017 Enterprise from school, but the key expired so I moved to Community.
Before, everything works great, but now it doesn't work at all.
I found a solution on Stack Overflow here but it doesn't work, so found another solution at GitHub here.
And I got this:
Build FAILED.
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(1601,5): error MSB4036:
The "GetReferenceNearestTargetFrameworkTask" task was not found. Check the following:
1.) The name of the task in the project file is the same as the name of the task class.
2.) The task class is "public" and implements the Microsoft.Build.Framework.ITask interface.
3.) The task is correctly declared with in the project file, or in the *.tasks files located in the
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin" directory.
0 Warning(s)
1 Error(s)
Time Elapsed 00:00:00.11
Do you know what to do next?
I had the same error message but for a different problem.
So, I'm using MSBuild to automate build/deploy process for Azure Functions. Everything was working fine until I updated both Visual Studio 2017 and Visual Studio Build Tools 2017 at which point I started getting this error. To be precise I got this error only when using MSBuild, building project manually from VS was working fine.
In my case I already had everything set as per #Programmer's answer.
But as I was using MSBuild it turned out that I also had to install NuGet targets and build tasks which are part of Visual Studio Build Tools.
Follow these to fix that error. This applies to VS 2017:
Fix 1:
1.Install Nuget PackageManager from here.
2.Restart Visual Studio.
If the problem is still there, continue below
Fix 2:
1.Download and start/run the Visual Studio Installer again.
2.While the Visual Studio Installer is still running, go to the "Individual Components" tab
3.Tick the "NuGet package manager" check-box that is under "Code tools" option.
4.Click Install to install it.
Screenshot of where this is located:
That should fix the error you see in this question. Restart Visual Studio and test the auto-completion function. If it's not working, see the answers from this question as that is a whole different issue.
I was seeing this issue with msbuild 15.6.82 on a build environment that does not have VisualStudio 2017, only VS Build Tools.
Here's a PowerShell script that resolves this issue, it pretty much does the equivalent of previous answers in the VS Installer, but silently and waiting for completion.
Start-Process "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vs_installer.exe" -ArgumentList 'modify --installPath "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools" --quiet --add Microsoft.VisualStudio.Component.NuGet.BuildTools --add Microsoft.Net.Component.4.5.TargetingPack --norestart --force' -Wait -PassThru
I'm working with a headless build server on server core, resolved by installing chocolatey package choco install visualstudio2017-workload-webbuildtools from here:
https://chocolatey.org/packages?q=msbuild
I'm reporting an answer from a Microsoft techician
This usually indicates one of two things:
A failed VS installation. If that's the case, I would recommend running a repair on your VS install.
You have msbuild assemblies in the GAC. If that's the case, please ungac them.
Livar Cunha [MSFT]
I got the same problem in Visual Studio 2019, I solved it by simply install unity package

Where is Microsoft.Web.Publishing.targets?

My MSBuild proj file is referencing Microsoft.Web.Publishing.targets. Even though microsoft says MSBuild is standalone installation, i dont think this file is part of MSBuild.
I am trying to setup a build server. and i don't see this file at this location. We have installed .Net 4.5.2 installed on that server.
C:\Program Files
(x86)\MSBuild\Microsoft\VisualStudio\vXX.X\Web\Microsoft.Web.Publishing.targets
After researching I found, I have to install VisualStudio to get this at that location.
Questions
Is there any way to install this Target (and other Targets at this location) without having to install visual studio?
Yes, you can use the MSBuild.Microsoft.VisualStudio.Web.targets package.

Resources