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

Error log the invalid the target name #9050

Merged
merged 7 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/MSBuild.UnitTests/CommandLineSwitches_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,26 @@ public void ProcessGraphBuildSwitch(string[] parameters, GraphBuildOptions expec
}
}

/// <summary>
/// Verifies that the /target switch is parsed properly with invalid characters.
/// </summary>
[Fact]
public void ProcessInvalidTargetSwitch()
{
string projectContent = """
<Project>
</Project>
""";
using TestEnvironment testEnvironment = TestEnvironment.Create();
string project = testEnvironment.CreateTestProjectWithFiles("project.proj", projectContent).ProjectFile;

#if FEATURE_GET_COMMANDLINE
MSBuildApp.Execute(@"msbuild.exe " + project + " /t:foo.bar").ShouldBe(MSBuildApp.ExitType.SwitchError);
#else
MSBuildApp.Execute(new[] { @"msbuild.exe", project, "/t:foo.bar" }).ShouldBe(MSBuildApp.ExitType.SwitchError);
#endif
}

/// <summary>
/// Verifies that when the /profileevaluation switch is used with invalid filenames an error is shown.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions src/MSBuild/MSBuild.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@
<Compile Include="..\Shared\Modifiers.cs">
<ExcludeFromStyleCop>true</ExcludeFromStyleCop>
</Compile>
<Compile Include="..\Shared\XMakeElements.cs">
<ExcludeFromStyleCop>true</ExcludeFromStyleCop>
</Compile>
<Compile Include="..\Shared\BufferedReadStream.cs" />
<Compile Include="..\Shared\CopyOnWriteDictionary.cs" />
<Compile Include="..\Shared\IKeyed.cs" />
Expand Down
7 changes: 6 additions & 1 deletion src/MSBuild/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1379,6 +1379,12 @@
<data name="UnsupportedSwitchForSolutionFiles" Visibility="Public">
<value>The '{0}' switch is not supported for solution files.</value>
</data>
<data name="NameInvalid" Visibility="Public">
<value>MSBUILD: error MSB5016: The name "{0}" contains an invalid character "{1}".</value>
<comment>
{StrBegin="MSBUILD : error MSB5016: "}
</comment>
</data>
<!-- **** TerminalLogger strings begin **** -->
<data name="RestoreComplete" xml:space="preserve">
<value>Restore complete ({0}s)</value>
Expand Down Expand Up @@ -1482,7 +1488,6 @@
</comment>
</data>
<!-- **** TerminalLogger strings end **** -->

<!--
The command line message bucket is: MSB1001 - MSB1999

Expand Down
7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.cs.xlf

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

7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.de.xlf

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

7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.es.xlf

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

7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.fr.xlf

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

7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.it.xlf

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

7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.ja.xlf

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

7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.ko.xlf

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

7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.pl.xlf

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

7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.pt-BR.xlf

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

7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.ru.xlf

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

7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.tr.xlf

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

7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.zh-Hans.xlf

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

7 changes: 7 additions & 0 deletions src/MSBuild/Resources/xlf/Strings.zh-Hant.xlf

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

8 changes: 8 additions & 0 deletions src/MSBuild/XMake.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3266,6 +3266,14 @@ internal static int GetLengthOfSwitchIndicator(string unquotedSwitch)
/// <returns>List of target names.</returns>
private static string[] ProcessTargetSwitch(string[] parameters)
{
foreach (string parameter in parameters)
{
int indexOfSpecialCharacter = parameter.IndexOfAny(XMakeElements.InvalidTargetNameCharacters);
if (indexOfSpecialCharacter >= 0)
{
CommandLineSwitchException.Throw("NameInvalid", nameof(XMakeElements.target), parameter, parameter[indexOfSpecialCharacter].ToString());
}
}
return parameters;
}

Expand Down