dotnet / dotnet/msbuild

[Bug]: Inconsistent behavior when using Choose element for conditional package references in MSBuild

Open
#10,149 2 comments 1 reaction 0 assignees View on GitHub
Area: Documentation Area: Language backlog Priority:2 triaged
Dominant language
C#
Stars
5.5k
Forks
1.5k
Avg merge
1d 8h
Merged PRs (30d)
141

Description

### Issue Description

There is an observed inconsistency in MSBuild when attempting to conditionally include NuGet package references using the `Choose` element with the `When` condition. Specifically, using `Choose` for this purpose does not work as expected, whereas using the `Condition` attribute directly on `ItemGroup` functions correctly.

### Steps to Reproduce

1. Create a new .NET project.

2. Create a Directory.Build.props file in the root directory.

3. Add the following content to the Directory.Build.props file using the `Choose` element:
```







```

4. Mark the project as a test project by setting the `IsTestProject` property to true in its .csproj file:
```


true

```
5. Run `dotnet restore` and observe that the xunit package is not included.

6. Modify the Directory.Build.props to use the `Condition` attribute directly on `ItemGroup`:
```



```

7. Run `dotnet restore` again and observe that the xunit package is now included.

### Expected Behavior

Both the `Choose` element with `When` conditions and the `Condition` attribute on `ItemGroup` should enable or disable the inclusion of NuGet package references based on the specified condition.

### Actual Behavior

- Using the `Choose` element with `When` condition does not reliably include the specified NuGet package reference when the condition evaluates to `true`.
- Using the `Condition` attribute directly on `ItemGroup` successfully includes the specified NuGet package reference when the condition evaluates to `true`.

### Analysis

_No response_

### Versions & Configurations

17.9.8.16306

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.