Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse invalid property under target #8190

Merged
merged 18 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion documentation/wiki/ChangeWaves-Dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Surround your feature with the following:

If you need to condition a Task or Target, use the built in `AreFeaturesEnabled` function.
```xml
<Target Name="SomeRiskyChange" Condition="$([MSBuild]::AreFeaturesEnabled('17.4'))"">
<Target Name="SomeRiskyChange" Condition="$([MSBuild]::AreFeaturesEnabled('17.4'))">
rainersigwald marked this conversation as resolved.
Show resolved Hide resolved
<!-- Where '17.4' is the change wave assigned to your feature. -->
```

Expand Down
1 change: 1 addition & 0 deletions documentation/wiki/ChangeWaves.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ A wave of features is set to "rotate out" (i.e. become standard functionality) t
## Current Rotation of Change Waves

### 17.6
- [Parse invalid property under target](https://github.com/dotnet/msbuild/pull/8190)
- [Eliminate project string cache](https://github.com/dotnet/msbuild/pull/7965)
- [Log an error when no provided search path for an import exists](https://github.com/dotnet/msbuild/pull/8095)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
using System.IO;
using System.Xml;
using Microsoft.Build.Construction;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using Shouldly;
using Xunit;

using InvalidProjectFileException = Microsoft.Build.Exceptions.InvalidProjectFileException;
using Xunit;

#nullable disable

Expand Down Expand Up @@ -339,6 +343,51 @@ public void SetReturns()
Assert.True(project.HasUnsavedChanges);
}

/// <summary>
/// Parse invalid property under target
/// </summary>
[Theory]
[InlineData(true)]
[InlineData(false)]
public void ReadInvalidPropertyUnderTarget(bool enableNewBehavior)
{
using (TestEnvironment env = TestEnvironment.Create())
{
ChangeWaves.ResetStateForTests();

if (!enableNewBehavior)
{
env.SetEnvironmentVariable("MSBUILDDISABLEFEATURESFROMVERSION", ChangeWaves.Wave17_6.ToString());
rainersigwald marked this conversation as resolved.
Show resolved Hide resolved
BuildEnvironmentHelper.ResetInstance_ForUnitTestsOnly();
}

string projectFile = @"
<Project>
<Target Name='t'>
<test>m</test>
</Target>
</Project>";

TransientTestFile file = env.CreateFile("proj.csproj", projectFile);
ProjectCollection collection = new ProjectCollection();
var error = Assert.Throws<InvalidProjectFileException>(() =>
{
collection.LoadProject(file.Path).Build().ShouldBeTrue();
});

var expectedString = "If you intended this to be a property, it must be inside a <PropertyGroup> element";

if (ChangeWaves.AreFeaturesEnabled(ChangeWaves.Wave17_6))
{
error.Message.ShouldMatch(expectedString);
}
else
{
error.ErrorCode.ShouldNotMatch(expectedString);
}
}
}

/// <summary>
/// Helper to get an empty ProjectTargetElement with various attributes and two tasks
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/Build/Evaluation/ProjectParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Build.Shared;
using System;
using System.Collections.Generic;
using System.Xml;
JaynieBai marked this conversation as resolved.
Show resolved Hide resolved

using Expander = Microsoft.Build.Evaluation.Expander<Microsoft.Build.Evaluation.ProjectProperty, Microsoft.Build.Evaluation.ProjectItem>;
using ProjectXmlUtilities = Microsoft.Build.Internal.ProjectXmlUtilities;
Expand Down Expand Up @@ -634,6 +635,14 @@ private ProjectTargetElement ParseProjectTargetElement(XmlElementWithLocation el
if (onError != null)
{
ProjectErrorUtilities.ThrowInvalidProject(onError.Location, "NodeMustBeLastUnderElement", XMakeElements.onError, XMakeElements.target, childElement.Name);
}
if (ChangeWaves.AreFeaturesEnabled(ChangeWaves.Wave17_6))
{
if (childElement.ChildNodes.Count == 1 && childElement.FirstChild.NodeType == XmlNodeType.Text)
{
// If the element has inner text and no other child elements except text, then this should be a property and throw invalid child element of <Target>
ProjectErrorUtilities.ThrowInvalidProject(childElement.Location, "PropertyOutsidePropertyGroupInTarget", childElement.Name, childElement.ParentNode.Name);
}
}

child = ParseProjectTaskElement(childElement, target);
Expand Down
4 changes: 4 additions & 0 deletions src/Build/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,10 @@
<value>MSB4067: The element &lt;{0}&gt; beneath element &lt;{1}&gt; is unrecognized.</value>
<comment>{StrBegin="MSB4067: "}</comment>
</data>
<data name="PropertyOutsidePropertyGroupInTarget" xml:space="preserve" Condition="$([MSBuild]::AreFeaturesEnabled('17.6'))">
<value>MSB4067: The element &lt;{0}&gt; beneath element &lt;{1}&gt; is unrecognized. If you intended this to be a property, it must be inside a &lt;PropertyGroup&gt; element.</value>
Copy link
Member

@danmoseley danmoseley Dec 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<value>MSB4067: The element &lt;{0}&gt; beneath element &lt;{1}&gt; is unrecognized. If you intended this to be a property, it must be inside a &lt;PropertyGroup&gt; element.</value>
<value>MSB4067: The element &lt;{0}&gt; beneath element &lt;{1}&gt; is unrecognized. If you intended this to be a property, enclose it within a &lt;PropertyGroup&gt; element.</value>

Otherwise you might interpret this to mean properties can't be inside targets, when they can.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

<comment>{StrBegin="MSB4067: "}</comment>
</data>
<data name="InvalidChildElementDueToDuplication" xml:space="preserve">
<value>MSB4173: The element &lt;{0}&gt; beneath element &lt;{1}&gt; is invalid because a child element with that name already exists</value>
<comment>{StrBegin="MSB4173: "}</comment>
Expand Down
7 changes: 6 additions & 1 deletion src/Build/Resources/xlf/Strings.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/Build/Resources/xlf/Strings.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/Build/Resources/xlf/Strings.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/Build/Resources/xlf/Strings.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/Build/Resources/xlf/Strings.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/Build/Resources/xlf/Strings.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/Build/Resources/xlf/Strings.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/Build/Resources/xlf/Strings.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.