Build failure in Azure DevOps – scenario 1

Challenge/Issue: .NET Project was building successfully but fails on azure build server. Following is the error.
Error CS8652 The feature ‘top-level statements’ is currently in Preview and unsupported. To use Preview features, use the ‘preview’ language version.
Error CS1513 } expected
Error CS1022 Type or namespace definition, or end-of-file expected

Solution: Locally we usually build with Debug mode. In Azure it builds with release mode as configured. So, we tried build the solution with release mode and the issue is reproduced. It was related to conditional compiler constants which was declared for Debug mode only. We gave for release mode and it is able to build successfully in local as well as build server in release mode.

DefineConstants (.NET) – Defines conditional compiler constants.
Symbol/value pairs are separated by semicolons and are specified by using the following syntax:
symbol1 = value1 ; symbol2 = value2
The property is equivalent to the /define compiler switch.

Analysis/Steps to reproduce the issue:
Create sample console application. So, you have below code.

using System;
 namespace ConsoleApp1
 {
     class Program
     {
         static void Main(string[] args)
         {
             Console.WriteLine("Hello World!");
         }
     }
 }

Now select the project node, Project > Project properties. Click “Build” tab. Provide some value like my_first_project as shown below.

This will add entries in csproj as below.

Let’s say we used these constants for determining namespace as below.

using System;
 if my_first_project
 namespace ConsoleApp1
 elif my_second_project
 namespace ConsoleApp2
 endif
 {
     class Program
     {
         static void Main(string[] args)
         {
             Console.WriteLine("Hello World!");
         }
     }
 }

This builds successfully in debug mode. Now change the build mode to release.

Now the issue is reproduced. You should see the errors in Error List window.

To resolve this go to Project properties > Build tab. Change configuration to Release and provide constant in Conditional compilation symbols as shown below. Save it. You should now see no error, be able to build successfully.

This will update csproj as below.

Please let me know your feedback in comments below.

HTH

Leave a comment