Skip to main content

How to Resolve Error "CS1029: #error: 'text'" in C#

The Compiler Error CS1029 is a user-generated error. The message reads: "#error: 'text'".

Unlike most compiler errors which indicate a mistake in syntax or logic, CS1029 is triggered intentionally by the developer using the preprocessor directive #error. This directive is used to stop compilation and display a specific message in the Error List. It is typically used to enforce build configurations, remind developers to implement critical sections, or prevent code from running on unsupported platforms.

This guide explains why this error appears and how to address the specific conditions causing it.

Understanding the #error Directive

The #error directive allows a developer to write a custom error message that the compiler will report.

Syntax:

#error Your custom error message goes here

When the compiler encounters this line, it immediately stops the build process for that file and reports CS1029 with your text. This is useful for conditional compilation where certain branches should logically never be reached or are currently forbidden.

Scenario 1: Platform or Version Restrictions

Library authors often use #error to prevent their code from compiling on platforms that don't support necessary features.

Example of error

You are trying to compile code in a build configuration (e.g., an old .NET Framework version) that the code explicitly rejects.

#if NET35
// Code for .NET 3.5
#elif NET40
// Code for .NET 4.0
#else
// ⛔️ Error CS1029: #error: 'This library requires .NET 3.5 or 4.0'
// The developer wrote this line to stop you from building on .NET 5+,
// or an undefined framework version.
#error This library requires .NET 3.5 or 4.0
#endif

Solution: Change Target Framework

You must update your project configuration to match the requirements defined by the code.

  1. Open your .csproj file.
  2. Change the <TargetFramework> to one of the supported versions (e.g., net40).

Or, if you are the author, update the code to support the new framework:

#if NET35
// ...
#elif NET40
// ...
#else
// ✅ Correct: Add logic for newer frameworks instead of erroring out.
// Modern implementation here...
#endif

Scenario 2: "TODO" Reminders Blocking Build

Developers sometimes use #error as a "Super TODO." Unlike a comment // TODO, which is easily ignored, #error forces you to look at the code before it can ever run.

Example of error

You downloaded a template or sample code that requires manual customization (like adding an API Key) before it works.

public class ApiClient
{
private const string ApiKey = "ENTER_KEY_HERE";

#if DEBUG
// ⛔️ Error CS1029: #error: 'Please enter your API Key above and remove this line'
#error Please enter your API Key above and remove this line
#endif

public void Connect() { /* ... */ }
}

Solution: Complete the Task

  1. Read the error message text (e.g., "Please enter your API Key").
  2. Perform the required action (Edit the ApiKey variable).
  3. Delete the #error line.
public class ApiClient
{
// ✅ Correct: Key entered
private const string ApiKey = "123-456-789";

// ✅ Correct: #error line removed
public void Connect() { /* ... */ }
}

Scenario 3: Missing Configuration Symbols

This error often appears when a specific Preprocessor Symbol (like ENTERPRISE_EDITION or ENABLE_LOGGING) is expected but not defined in the build settings.

Example of error

The code expects a symbol to be defined, and errors out if it is missing.

#if !WINDOWS && !LINUX && !OSX
// ⛔️ Error CS1029: #error: 'You must define an OS symbol'
#error You must define an OS symbol (WINDOWS, LINUX, or OSX)
#endif

Solution: Define the Symbol

You need to add the required symbol to your project build options.

Option A: Edit .csproj

<PropertyGroup>
<!-- ✅ Correct: Define the symbol here -->
<DefineConstants>WINDOWS;DEBUG;TRACE</DefineConstants>
</PropertyGroup>

Option B: Visual Studio

  1. Right-click Project > Properties.
  2. Go to Build > General.
  3. Add the symbol (e.g., WINDOWS) to Conditional compilation symbols.

Conclusion

CS1029 is not a mistake you made in syntax; it is a message from the code author (or your past self).

  1. Read the Message: The text after #error tells you exactly what is wrong.
  2. Check Configuration: Ensure your Target Framework and Compilation Symbols match what the code expects.
  3. Check for Tasks: Look for placeholders or required manual edits in the code immediately preceding the error line.