dotnet / dotnet/msbuild

How to add Compile Update item to csproj?

Open
#5,244 2 comments 0 reactions 0 assignees View on GitHub
Area: API Area: Language Feature - Project File Cleanup triaged
Dominant language
C#
Stars
5.5k
Forks
1.5k
Avg merge
1d 13h
Merged PRs (30d)
133

Description

I'm trying to use Microsoft Build SDK to add a `Compile Update` item to the SDK style csproj file. What I need is to basically generate an XML like the following:

```xml

Class1.cs

```

However, I couldn't find an easy way to do this. If I simply use `ProjectItem.SetMetadataValue`, I get the following exception:

```
System.InvalidOperationException: 'Cannot modify an evaluated object originating in an imported file "C:\Program Files\dotnet\sdk\3.1.201\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.props".'
```

And if I try to use APIs like `Project.AddItem`, I have to go through this:

```cs
var item = project.AddItem("Compile", "Class1.Generated.cs", new []
{
new KeyValuePair("DependentUpon", "Class1.cs")
}).Single();
item.Xml.Include = null;
item.Xml.Update = "Class1.Generated.cs";
```

Because `Project.AddItem` requires an "unevaluated include".

Am I missing something? Is there a better way to do this?

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by examining ProjectItem.SetMetadataValue and Project.AddItem, then trace the exception for evaluated items originating in Microsoft.NET.Sdk.DefaultItems.props. Compare the requested Compile Update XML with the generated project state and determine whether an existing API supports it; done means identifying a supported approach or documenting the limitation.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, xml
Domain
build-system
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.