question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

Summary

The proposal is about adding a Defined('propname') condition in addition to available ones. 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) 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, 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:

  <ItemGroup Condition="$(FEATURE_A) == ''">
    <None Include="FeatureA\*.cs" />
    <Compile Remove="FeatureA\*.cs" />
  </ItemGroup>

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:

  <ItemGroup Condition="!Defined('FEATURE_A')">
    <None Include="FeatureA\*.cs" />
    <Compile Remove="FeatureA\*.cs" />
  </ItemGroup>

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

Alternative Designs

No response

Issue Analytics

  • State:open
  • Created 2 months ago
  • Reactions:1
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
AR-Maycommented, Aug 10, 2023

Team triage: we would like to collect community interest for this issue.

1reaction
jrdoddscommented, Aug 12, 2023

Instead of being limited to a Condition attribute, I’d like to see this implemented as an MSBuild property function.

e.g.

$([MSBuild]::IsPropertyDefined(string propertyName))
Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom properties
You can modify any custom property defined in your source code from within CloudBees Feature Management. You can change the type of a...
Read more >
Trigger condition - Add (assignee) to 'Current user'
In Trigger conditions 'Current user' has (agent), (end-user), and all agents listed by name. When I want to create a trigger using condition...
Read more >
eval() - JavaScript - MDN Web Docs - Mozilla
The eval() function evaluates JavaScript code represented as a string and returns its completion value. The source is parsed as a script.
Read more >
What Is a Feature Request? Definition and Examples
A feature request is a comment, message, or ask from a user to implement a specific feature into your product. Feature requests can...
Read more >
Interfaces - TypeScript: Handbook
In TypeScript, interfaces fill the role of naming these types, and are a powerful way of defining contracts within your code as well...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found