Building VS Solution via command prompt - c#

I have an solution with one project. Able to build it via VS GUI.
I want to build it via command prompt. But getting errors:
Command used:
"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe" "C:\Users\xxx\Desktop\WebApplication1\WebApplication1.sln" /t:build
error:
error MSB4019: The imported
project "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications\Microsoft.WebApplication.targets"
was not found. Confirm that the path in the declaration is correct, and that the file exists on disk.
How to resolve this. Please suggest.

This can happen when Visual Studio is not installed on the machine on which you're attempting to build the project (e.g. a build agent).
Verify VS is installed, or if this isn't an option you could run the following command in the package manager console:
Install-Package MSBuild.Microsoft.VisualStudio.Web.targets
This package will update the .csproj file of your project to use the VS version of the targets if they exist, otherwise a local version contained in the packages folder.

The path of MSBuild should be "C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe" instead of "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe".
As per Visual Studio Blog: MSBuild is now part of Visual Studio!:
Starting with Visual Studio 2013, the 2013 version of MSBuild will ship as a part of Visual Studio instead of the .NET Framework. This transition allows us to more rapidly evolve MSBuild.
So when you build project via command prompt in Visual Studio 2015, the path of MSBuild should be "C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe".
Hope this can help you.

Related

msbuild fails with type or namespace name missing

I have a C# project (csproj) and a solution (sln), which I want to build via msbuild.exe.
$ msbuild.exe foo.sln /m /t:Build /p:Configuration="Release"
This fails with several errors like these here:
(CoreCompile target) ->
AppComboCommand.cs(9,7): error CS0246: The type or namespace name 'EnvDTE' could not be found
(are you missing a using directive or an assembly reference?) [foo.csproj]
These errors go away after I opened the project in Visual Studio 2019 and then build again through command line again. VS seems to generate a few files while opening the project, and I am wondering what I miss here. Is this maybe a dependency problem?
Any help is highly appreciated
EnvDTE is just a vs sdk dll for extend visual studio functionality and it is usually stored in the vs local folder.
First, you should make sure that you have installed the Visual Studio extension development workloads first. If you use Build Tool for VS2019, you should also remember to check it.
Please follow these:
1) If you reference this dll in the local area, please make sure that you have referenced it by Right-click on project-->Add Reference-->reference C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\PublicAssemblies\envdte.dll.
This below approach is not recommended, and for vs SDK DLL references, using nuget is risky:
If you use nuget to install it the EnvDTE which does exist in nuget.org, when you build it from command line, you should run nuget restore MySolution.sln first. MSBuild Command line will not restore the missing packages and it is not its job.
So please download nuget.exe first and then add this in script:
$nuget="xxxxx\nuget.exe(the local path of the nuget.exe)"
$nuget restore `xxxxx\MySolution.sln`
Then execute the build process.
2) I found that you use a powershell script to build the project, so please make sure that you use MSBuild for VS2019 not the one of Net Framework(C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe).
$msbuild.exe="C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe"
If you install Build Tool for VS2019, you should use this path:
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin\MSBuild.exe

C# Predefined type 'System.Object' is not defined or imported

In .NET core project, I changed the all projects name than after reloading all project that time many errors were showing approx 3225. And mainly all errors are related to system namespace like
System.Object not defined or imported
System.Boolean not defined or imported
Task does not exist
namespace ArgumentNullException could not be found
How do I resolve all those errors?
close VS
delete the bin, obj and .vs (may be hidden) folders
manually run dotnet restore
reopen the project in VS
That fixes most things.
I was hitting this in VS Code. In Windows Terminal, dotnet build and dotnet run ran fine, but I was getting the errors in VS Code. The delete/restore/reopen answer did not fix it.
It seems it was an issue where I had an outdated version of Visual Studio confusing the C#/OmniSharp extension in VS Code.
The OmniSharp log included something like this:
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
Located 2 MSBuild instance(s)
1: Visual Studio Enterprise 2019 16.8.30907.101 16.8.3 - "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin"
2: StandAlone 17.0.0 - "c:\Users\foo\.vscode-insiders\extensions\ms-dotnettools.csharp-1.23.16\.omnisharp\1.37.16\.msbuild\Current\Bin"
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
Registered MSBuild instance: Visual Studio Enterprise 2019 16.8.30907.101 16.8.3 - "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin"
That version of Visual Studio 2019 did not have the .NET 6.0 SDK installed, and I couldn't find it in the VS Installer.
So I installed Visual Studio 2022 and the .NET 6.0 SDK as part of that install. Now my log looks like this:
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
Located 3 MSBuild instance(s)
1: Visual Studio Enterprise 2022 17.0.31912.275 17.0.0 - "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin"
2: Visual Studio Enterprise 2019 16.8.30907.101 16.8.3 - "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin"
3: StandAlone 17.0.0 - "c:\Users\foo\.vscode-insiders\extensions\ms-dotnettools.csharp-1.23.16\.omnisharp\1.37.16\.msbuild\Current\Bin"
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
Registered MSBuild instance: Visual Studio Enterprise 2022 17.0.31912.275 17.0.0 - "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin"
And the errors are gone!
Disclaimer: I work for Microsoft.
In my case, Error List window had hundreds of errors. But when rebuild the solution, used to get the message Rebuild All Succeeded. And even the project was able to run. But entire code was in red.
Just closing Visual Studio and deleting vs folder did the job.
Gazi's solution above solved my problem which was exactly what she/he described. I had tried updating VS, cleaning and rebuilding solution, restarting VS. Nothing helped. Removing the hidden vs folder at the solution level did the trick. Hope it helps someone!
This can also be caused by a circular dependency between projects in a solution.
error MSB4006: There is a circular dependency in the target dependency graph involving target "_GenerateRestoreProjectPathWalk"
Just like citelao I've got this issue in VS code when created new .NET 6.0 isolated AZ Function using wizard.
In OmniSharp logs I had about the same.
Just in case, in order to see the omni sharp logs, you have to open 'output' panel, and there on the right click on a drop-down:
I did not want to install VS 2022 as my target was to work in VS code for this project. I found the answer here: omnisharp-roslyn #2247
Basically the solution was to add omnisharp.json file in the project root with the following contents:
{
"msbuild": {
"useBundledOnly": true
}
}
After that I've closed VS Code, deleted bin and obj folders and started VS Code back.

The "GetReferenceNearestTargetFrameworkTask" task was not found in visual studio code [duplicate]

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

Build succeeded with projects Ctrl + Shift + B that MSBuild can't build

Does anyone know where I can take a look at my project if after I make a build with MSBuild through MSBuild console I get this warning:
MSB4078: The project file 'MyProject.csproj' is not supported by
MSBuild and cannot be built?
My project is running with Target Framework .Net Core 2.0. The MSBuild version I am using is 14.0.25420.1
This is the cs.proj
You are using the new csproj file format (see Project sdk=...)
You will need to use MSBuild 15 in order for it to build. You have two options:
1) Download Build Tools for Visual Studio 2017 and install. Then the path will be:
C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin
2) If you have VS installed, the path will be:
C:\Program Files (x86)\Microsoft Visual Studio\2017\<VS Version>\MSBuild\15.0\Bin
Using MSBuild 15 should fix the error.
Please check what version of MSBuild are you running. I tested with the version 15.0, it runs without any error.
Generally, make sure that you have configured a build agent for your team project, and that you have Visual Studio 2017 installed on the agent machine and that you installed the latest Visual Studio update.
Also make sure you have select the MSbuild version 15.0 for the build.
See Build your ASP.NET Core app for details.
Reference this related thread: Support for .NET Core .csproj files
I suggest to use MSBuild in more reliable way.
Download Build Tools for Visual Studio 2017 from Visual Studio Downloads page, it includes latest MSBuild 15.* (direct link).
Command-line arguments documented here: Use command-line parameters to install
Visual Studio 2017.
All workloads and components are listed here: Visual Studio Build Tools 2017 component directory.
Use PowerShell module VSSetup to find MSBuild. Download it or install from here: Github: Microsoft/Visual Studio Setup PowerShell Module
Find MS Build
Import-Module $PSScriptRoot\VSSetup\VSSetup.psd1
$msBuildPath = (Get-VSSetupInstance | Select-VSSetupInstance -Version 15.0 -Product Microsoft.VisualStudio.Product.BuildTools).InstallationPath
if ([System.IntPtr]::Size -eq 8)
{
$global:msbuildPath = Join-Path $msBuildPath 'MSBuild\15.0\Bin\amd64'
}
else
{
$global:msbuildPath = Join-Path $msBuildPath 'MSBuild\15.0\Bin'
}
Write-Output "Using MSBuild from $global:msbuildPath"
Write-Output "MSBuild /version"
$msbuild = Join-Path $global:msbuildPath msbuild
& $msbuild /version

VS2017 command line build (missing "%VS150COMNTOOLS%VSVars32.bat")

I have installed VS2017 and call
call "%VS120COMNTOOLS%VSVars32.bat"
from the command line but all I get is
'"%VS150COMNTOOLS%VSVars32.bat"' is not recognized as an internal or external command, operable program or batch file.
If I run "set" from the command line I can see VS120COMNTOOLS (for VS2013) & VS140COMNTOOLS (for VS2015) but there is no VS150COMNTOOLS. How can I build from the command line?
I submitted this as a tech support issue to Microsoft who accepted it as a bug in the install ("there is no VSVars32.bat in the C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools directory.").
However, there is a work-around:
From MS:
At least, since there is a VsDevCmd.bat, there is a Visual Studio 2017 Developer Command Prompt, which also sets, modifies environment variables (Framework40Verion, FrameworkDir, FrameworkDIR64, FrameworkVersion, FrameworkVersion64, INCLUDE, IPCPATH, PATH, VCINSTALLDIR, VCToolsInstallDir, ...)
C:\Program Files (x86)\Microsoft Visual Studio\2017\EDITION\Common7\Tools\VsDevCmd.bat
Run C:\Program Files (x86)\Microsoft Visual Studio\2017\EDITION\Common7\Tools\VsDevCmd.bat where EDITION is the type of VS2017 install, i.e. C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools\VsDevCmd.bat for the Enterprise install. This sets up the needed environment variables and batch builds will work.
Hope this helps anyone struggling with the same issue.
VS2017 has reworked its directory structure and filenames. The file is no longer titled "VsVars32.bat", you should look for the file 'vcvars.bat', which can be located in ..\Program Files (x86)\Microsoft Visual Studio\2017\EDITION\Common7\Tools\vsdevcmd\ext\vcvars.bat (using Adam's syntax, replace EDITION with the version of Visual Studio you use).
For simplicity, you can use %VSAPPIDDIR% in your call to point to the IDE folder where devenv.exe is located, and go back one folder to define your path to the batch file. For example:
call "%VSAPPIDDIR%..\Tools\vsdevcmd\ext\vcvars.bat"
If its any help to anyone...
If you modify your Visual Studio 2017 installation to install the component:
"VC++ 2015.3 v140 toolset for Desktop (x86,x64)"
then the 'VSVars32.bat' file which is missing, will be installed (as its a component of VC++ 2015, but not VC++ 2017).

Categories