Post Build event with condition - visual-studio

Having the need to add a condition to the Post Build event in my Visual Studio 2013 project, I ended up with the following:
<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">
<PostBuildEvent>"$(SolutionDir)..\Deploy\Build\sign-bin.cmd"</PostBuildEvent>
</PropertyGroup>
I've added the condition Condition=" '$(OS)' == 'Windows_NT' " manually with a text editor to the CSPROJ file.
This works as expected, my solution builds successfully both in Visual Studio as well as in Travis CI.
What doesn't work:
When editing the Post Build event inside Visual Studio in the graphical editor, it seems to ignore the condition and simply stores the event twice:
<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">
<PostBuildEvent>"$(SolutionDir)..\Deploy\Build\sign-bin.cmd"</PostBuildEvent>
</PropertyGroup>
<PropertyGroup>
<PostBuildEvent>"$(SolutionDir)..\Deploy\Build\sign-bin.cmd"</PostBuildEvent>
</PropertyGroup>
It doesn't matter whether I add the condition to the <PropertyGroup> node or the <PostBuildEvent> node.
In both cases the nodes are duplicated by Visual Studio upon saving.
My questions:
Any chance to somehow add the ability to ignore my Post Build event (which is the reason for the condition) on Travis?
Any chance to somehow tell Visual Studio to not duplicate the node with condition upon saving?

Related

The OutputPath property is not set for project ... / VS arbirtrarily changing configuration of projects in solution from Debug to Release

Last last week I started having an issue with every solution I've tried to build with Visual Studio. I was using VS 2019 and our organization had just pushed down some windows updates (not sure if that's related) but after the updates I was on VS 2019 version 16.8.4, .Net version 4.8.03752.
Whenever I try to build one of our solutions (I tried 3 different solutions after doing a free pull from the main branch of our repo) I get over 1000 errors which seem to be cascade errors from the OutputPath property not being set which seems to be related to several of the projects being arbitrarily changed from Debug to Release even though the solution is set to Debug | Any CPU.
Furthermore, if I examine the build tab on the project properties (or examine them in configuration manager) NO DEBUG CONFIGURATION IS SHOWN AS AVAILABLE. If I close Visual Studio it prompts to me save changes and if I do a comparison shows that it did indeed change the configuration of several projects form Debug to Release.
I have reproduced this on 2 separate machines, with fresh installs of Visual Studio 2019 (and 2017) and pulling fresh code from the repos. Other developers in my org an build the solutions just fine and their configurations show Debug | Any CPU across the board (i.e. I seem to be the only person in the world having this problem and I'm having it on every machine I've tried and with multiple versions of Visual Studio).
I also see errors saying that projects contains nuget references that are "missing" but if I restore nuget pacakges I get a messages saying they are all installed and there is nothing to restore. I also see the "missing" packages in the packages folder.
We use relative paths for the packages so it can't be the typical literal-path issue some people have. Besides which, my folder structure hasn't changed and we all use the same folder structure in our ord.
I am completely at a loss as to what to try next. My googlefoo has completely failed me in this regard.
I'm desperately hoping someone in the Stack Exchange world can offer a solution.
Thanks,
Greg.
Please check if your project's csproj file has missed these content:
You could directly add these at the bottom of the csproj file:
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
After that, delete bin and obj folder.
Then, run update-package -reinstall under Tools-->Options-->Nuget Package Manager--> Package Manager Console to reinstall all nuget packages.

.csproj/msbuild PropertyGroup configuration for TF Build Agent

How can I make a project build behave different on 1) (local) compile inside Visual Studio and 2) triggered by TFS Build Server on TF Build Agent?
I tried (both without success)
<PropertyGroup Condition="'$(TF_BUILD)' == 'true' AND '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PropertyGroup Condition="'$(BuildingInsideVisualStudio)' != 'true' AND '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
I have (in both cases) created 2 such property groups with == and != swapped on the first condition (the one that should result in different behavior) and different content in the PropertyGroup (which is what I am really aiming for).
I am using VS 2017 and TFS 2017.
edit/update:
I found the mistake in my ways, it was that I edited the AnyCPU PropertyGoup and built x86.
Actually, all of this works, including Daniel Mann's suggestion. I was using it on the wrong propertygroup.
Since I found this information hard to find, I hope this summary helps someone in the future.
Different property values on TF and local (VS) build can be written like
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
...
<Optimize Condition="'$(BUILD_SOURCESDIRECTORY)' != ''">true</Optimize>
<Optimize Condition="'$(TF_BUILD)' != 'true'">false</Optimize>
..redundant..
<Optimize Condition="'$(BuildingInsideVisualStudio)' == 'false'">true</Optimize>
...
</PropertyGroup>
So on TFS Build the following conditions should be true (and false in Visual Studio):
$(BUILD_SOURCESDIRECTORY) != ''
$(TF_BUILD) == 'true'
$(BuildingInsideVisualStudio)' != 'true'

Visual studio 2012 overwrites properties in my .csproj project file

I am using Visual Studio 2012. I have many projects each containing at least one .csproj file. The .csproj files contain this section:
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<IntermediateOutputPath>Assembly\Release\</IntermediateOutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<IntermediateOutputPath>Assembly\Debug\</IntermediateOutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<IntermediateOutputPath>Assembly\x86\Debug\</IntermediateOutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<IntermediateOutputPath>Assembly\x86\Release\</IntermediateOutputPath>
</PropertyGroup>
For some reason, whenever I open the project in Visual Studio, saving it changes the .csproj file to look like this:
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<IntermediateOutputPath>C:\Users\user\AppData\Local\Temp\vs9475.tmp\Release\</IntermediateOutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<IntermediateOutputPath>C:\Users\user\AppData\Local\Temp\vs9475.tmp\Debug\</IntermediateOutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<IntermediateOutputPath>C:\Users\user\AppData\Local\Temp\vs9475.tmp\x86\Debug\</IntermediateOutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<IntermediateOutputPath>C:\Users\user\AppData\Local\Temp\vs9475.tmp\x86\Release\</IntermediateOutputPath>
</PropertyGroup>
I have tried several things including adding a BaseIntermediateOutputPath element as well as creating separate conditional IntermediateOutputPath elements. The problem is that this messes with our version control and also generates errors when someone other than myself loads it (because of the local path). Can anyone tell me how to stop visual studio from changing the file every time we load it? This happens to everyone who opens it in Visual Studio 2012. It worked in 2010 but we are transitioning and need to fix this. It is a 2012 project.

Is OutputPath a required field in every PropertyGroup?

My projects appear to all have three <PropertyGroup> items.
One:
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Two: <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
Three: <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
FinalBuilder has been failing. When I go into the project file and add <OutputPath>bin\Debug\</OutputPath> to the first element in the .csproj file (an MSBuild file as I understand it), the build succeeds.
The remaining two elements already have <OutputPath> defined.
Is this a required field for all three elements? Why is it missing from the first element in my project files?
When MSBuild compiles a project, it takes OutputPath as an argument, to where it should place the build output.
The .csproj file has some default settings. It's in the first <PropertyGroup>.
In the conditional PropertyGroups, there are specific properties, to different configurations and platforms:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
Properties inside this node overwrites the default one, so it can specialize the build.
To hit the different PropertyGroups, MSBuild takes some argument, for example, hitting "Release|x86", the command looks like this:
msbuild /p:Configuration="Release" /p:Platform="x86"
MSBuild will use the properties from the default PropertyGroup, and overwrite/use properties from the PropertyGroups that meet the conditions, in this example "Release|x86" to compile the code.
Your problem sounds like MSBuild does not have the right arguments to evaluate the right PropertyGroups.

How do I change the build folders in VS2010?

I'm getting an error in a VS2010 DB project that indicates I have too many charachters in my build path.
How can I change my default build path for all project types?
Something like
c:\build\$(projectname)\......
Thanks!
EDIT:
I've moved my project to the root of the C: drive and I still get the error with my DB project. I get this error when I try to right click the project and select properties
An error occurred trying to load the project properties window. Close the window and try again.
Cannot evaluate the item metadata "%(FullPath)". The item metadata "%(FullPath)" cannot be applied to the path "obj\Debug|Any CPU\TASS.DB.dbschema". Illegal characters in path. C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets
The first thing that jumps out to me here is that your platform and configuration are being fused together to form "Debug|Any CPU" and a string is being made from that--the pipe is the character it's referencing there when it says there are illegal characters. I'm not sure how much your database project really differs with respect to debug/release and for architecture, but you may not even need to include them in the path.
Since you can't open the project property pages, you'll need to edit the msbuild directly by unloading it and selecting "Edit..." from the context menu (sorry if you know this already).
From there, assuming you're realling running up on the windows path length ceiling, you could use some msbuild trickery to maximize your headroom in there. Specifically, doing something similar to what you suggest: use the C:\ drive wherever possible.
To do this, look inside the PropertyGroups with the conditions for your Configuration & Platform configurations, and inside them replace the OutputPath and IntermediateOutputPath properties so that they're as short as possible, for example:
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<OutputPath>$(SystemDrive)\D\A</OutputPath>
<IntermediateOutputPath>$(SystemDrive)\o\D\A</IntermediateOutputPath>
</PropertyGroup>
This saves some valuable characters in that instead of "Debug" you're using "D", "A" for "AnyCPU" and "o" for "obj".
Probably most importantly you're using C:\o\ for the intermediate build directory instead of C:\whatever-the-whole-path-is-to-your-project-file\obj. As well, this property isn't configurable from the property pages, from what I recall.
Some added flexibility there using SystemDrive instead of a hard-coded C:, not that I would really expect it to be different.
Finally, concerning your property pages load problem, I don't know how the Debug|AnyCPU got in your path (I don't know of any properties that store the concatenated flavor like that), but you should be able to pick it out pretty easily once you open up the file. Hopefully it's similar to load errors in something like the winforms designer where you change one line and suddenly the whole thing works again.
Hope this helps!
I don't think it's possible to set a default build path for all projects, only the standard Debug/Release folders within the project itself. The only suggestion I would have is to simply move the project folder to location with a shorter path.
EDIT: As per the new edit, have a look here:
http://connect.microsoft.com/VisualStudio/feedback/details/594333/database-project-template-files-corrupt
I updated a project from VS 2005 to VS 2010 and got the same error message.
"The item metadata "%(Filename)" cannot be applied to the path "obj\Debug|x86\Debug\DemoCSharp.pdb". Illegal characters in path." The problem is that Visual Studio 2010 fails in converting the csproj file to the new format, but it does not tell us where exactly the error is.
In my VS 2005 csproj file there is the following XML code:
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">Debug|x86</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{05F88317-0CA7-4FE5-8520-35422402941A}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>DemoCSharp</RootNamespace>
<AssemblyName>DemoCSharp</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>true</DebugSymbols>
<OutputPath>..\output32\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<OutputPath>..\output32\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
<DebugSymbols>true</DebugSymbols>
<OutputPath>..\output64\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
<OutputPath>..\output64\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
Visual Studio does NOT tell us which line produces the problem. But I found it by "try and error".
The cause of the error message is clearly a bug in the Visual Studio conversion wizard because VS 2005 has no problem loading this csproj file while VS 2010 fails to convert it.
So you have to manually edit and fix this file and then load it anew in VS2010.
In my case the line that triggers the bug is the 3. line with <Platform Condition. The bug is that VS tries to take the value ("Debug|x86") of this platform condition XML node and embed it into a path on disk (like "...\obj\Debug|x86\..."). But as pipe characters are illegal in paths, it later complains and aborts the conversion.
So how to solve the problem ?
I simply replaced the third line
<Platform Condition=" '$(Platform)' == '' ">Debug|x86</Platform>
with
<Platform Condition=" '$(Platform)' == '' ">Debug</Platform>
which eliminates the pipe character and the project converted without errors.
NOTE: It is also possible to completely delete this line.
ATTENTION:
It is possible that in YOUR case the same error messages needs another fix than in my case. Please study the csproj file and look for the pipe characters, then find out with try and error how to modify it. This error can even appear in other conditions than converting a project.
But what they all have in common is that this is a Visual Studio bug (or in case of 'littlechris' a software extension bug) that tries to embed a pipe character into the path.
XML node: "Debug|x86" -> path "...\obj\Debug|x86\..."
I received this messeage because the absolute path of one of the files in my project exceeded 260 characters. Once I reduced the path length, I was able to build the project.

Resources