Skip to content

Commit a5684fa

Browse files
authored
remove warnings for single file publishing (#1606)
* resolve warnings for single file publishing * alternative argv[0] approach for root command exe path
1 parent 50c4e13 commit a5684fa

File tree

7 files changed

+40
-9
lines changed

7 files changed

+40
-9
lines changed

src/System.CommandLine.Tests/CommandLineConfigurationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation and contributors. All rights reserved.
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using FluentAssertions;

src/System.CommandLine.Tests/TrimmingTests.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.IO;
99
using System.Text;
1010
using FluentAssertions;
11+
using Xunit;
1112
using Xunit.Abstractions;
1213

1314
namespace System.CommandLine.Tests;
@@ -24,15 +25,29 @@ public TrimmingTests(ITestOutputHelper output)
2425
_systemCommandLineDllPath = typeof(Command).Assembly.Location;
2526
}
2627

27-
[ReleaseBuildOnlyFact]
28-
public void App_referencing_system_commandline_can_be_trimmed()
28+
[ReleaseBuildOnlyTheory]
29+
[InlineData("")]
30+
[InlineData("-p:PublishSingleFile=true")]
31+
public void App_referencing_system_commandline_can_be_trimmed(string additionalArgs)
2932
{
3033
var stdOut = new StringBuilder();
3134
var stdErr = new StringBuilder();
35+
36+
var workingDirectory = Path.Combine(Directory.GetCurrentDirectory(), "TrimmingTestApp");
3237

38+
Process.RunToCompletion(
39+
DotnetMuxer.Path.FullName,
40+
"clean -c Release -r win-x64",
41+
workingDirectory: workingDirectory);
42+
43+
var commandLine = string.Format(
44+
"publish -c Release -r win-x64 --self-contained -p:SystemCommandLineDllPath=\"{0}\" -p:TreatWarningsAsErrors=true -p:PublishTrimmed=true {1}",
45+
_systemCommandLineDllPath,
46+
additionalArgs);
47+
3348
var exitCode = Process.RunToCompletion(
3449
DotnetMuxer.Path.FullName,
35-
$"publish -c Release -r win-x64 --self-contained /p:PublishTrimmed=true /p:SystemCommandLineDllPath=\"{_systemCommandLineDllPath}\" /p:TreatWarningsAsErrors=true",
50+
commandLine,
3651
s =>
3752
{
3853
_output.WriteLine(s);
@@ -43,7 +58,7 @@ public void App_referencing_system_commandline_can_be_trimmed()
4358
_output.WriteLine(s);
4459
stdErr.Append(s);
4560
},
46-
workingDirectory: Path.Combine(Directory.GetCurrentDirectory(), "TrimmingTestApp"));
61+
workingDirectory);
4762

4863
stdOut.ToString().Should().NotContain("warning IL");
4964
stdErr.ToString().Should().BeEmpty();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Xunit;
5+
6+
namespace System.CommandLine.Tests.Utility;
7+
8+
public class ReleaseBuildOnlyTheoryAttribute : TheoryAttribute
9+
{
10+
public ReleaseBuildOnlyTheoryAttribute()
11+
{
12+
#if DEBUG
13+
Skip = "This test runs only on Release builds.";
14+
#endif
15+
}
16+
}

src/System.CommandLine/Command.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public IEnumerable<Symbol> Children
5757
public IReadOnlyList<Argument> Arguments => _arguments is not null ? _arguments : Array.Empty<Argument>();
5858

5959
/// <summary>
60-
/// Represents all of the options for the command, including global options.
60+
/// Represents all of the options for the command, including global options that have been applied to any of the command's ancestors.
6161
/// </summary>
6262
public IReadOnlyList<Option> Options => _options is not null ? _options : Array.Empty<Option>();
6363

src/System.CommandLine/CommandExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) .NET Foundation and contributors. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
using System.CommandLine.Builder;
54
using System.CommandLine.Invocation;
65
using System.CommandLine.Parsing;
76
using System.Linq;

src/System.CommandLine/RootCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class RootCommand : Command
2323
LazyThreadSafetyMode.PublicationOnly);
2424

2525
private static readonly Lazy<string> _executablePath =
26-
new(() => GetAssembly().Location,
26+
new(() => Environment.GetCommandLineArgs()[0],
2727
LazyThreadSafetyMode.PublicationOnly);
2828

2929
private static readonly Lazy<string> _executableName =
@@ -49,7 +49,7 @@ public RootCommand(string description = "") : base(ExecutableName, description)
4949
{
5050
}
5151

52-
internal static Assembly GetAssembly() => _assembly.Value!;
52+
internal static Assembly GetAssembly() => _assembly.Value;
5353

5454
/// <summary>
5555
/// The name of the currently running executable.

src/System.CommandLine/System.CommandLine.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1818
<IsTrimmable>true</IsTrimmable>
1919
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
20+
<EnableSingleFileAnalyzer>true</EnableSingleFileAnalyzer>
2021
</PropertyGroup>
2122

2223
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">

0 commit comments

Comments
 (0)