dotnet / dotnet/msbuild

[Feature Request]: Add a Defined('propname') condition

Open
#9,106 10 comments 1 reaction 0 assignees View on GitHub
Feature Request gathering-feedback triaged
Dominant language
C#
Stars
5.5k
Forks
1.5k
Avg merge
1d 8h
Merged PRs (30d)
141

Description

### Summary

The proposal is about adding a `Defined('propname')` condition in addition to available [ones](https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-conditions?view=vs-2022). The condition returns true if the property is present among project properties. More specifically it should evaluate to true if [`Microsoft.Build.Evaluation.Project.GetProperty(name)`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.build.evaluation.project.getproperty?view=msbuild-17-netcore#microsoft-build-evaluation-project-getproperty(system-string)) returns a non null value.

### Background and Motivation

I'm trying to add preprocessor definitions and conditionally exclude files in a CSharp project, based on these definitions. A documented [approach](https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-conditional-constructs?view=vs-2022#code-try-1), which basically suggest to to use regex directly on `$(DefineConstants)`, becomes unworkable for me even for simple use cases.

Most build systems provides the following facilities to ease such tasks:
- Coercion of strings to boolean (eg. CMake)
- Check for definition of external properties.

MSBuild provides none of the above two. The current approach I'm using is defining externally some `FEATURE_A=1`, `FEATURE_B=1`, etc. properties (`1` is just an arbitrary value), then using these conditionals in the CSharp project:

```




```

### Proposed Feature

Since I'm not seeing MSBuild to implement coercion of properties to booleans, I suggest adding a `Defined('propname')` condition that will allow to implement other commonly used approaches when conditionally including/excluding stuff in msbuild projects. The example above would become:

```




```

This both allows to avoid defining properties with arbitrarily values and makes the condition much more readable.

### Alternative Designs

_No response_

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.