Skip to content

Commit 854fa07

Browse files
jaredparJared Parsonschsienki
authored
Remove host specific args from CommandLineArgs (#68918)
This changes our build tasks to remove the host specific args, namely the `exec ...\csc.dll`, from the `CommandLineArgs` property. That `ITaskItem[]` is meant for the IDE consumption and doesn't need host specific information. As a part of fixing this bug I cleaned up a lot of unnecessary member inheritance in the build tasks. There were several `virtual` members where every implementation had the same code. Moved all that up to the base and cleaned up the comments to make the intent clearer. closes #67102 Co-authored-by: Jared Parsons <[email protected]> Co-authored-by: Chris Sienkiewicz <[email protected]>
1 parent 5731c5f commit 854fa07

File tree

13 files changed

+322
-207
lines changed

13 files changed

+322
-207
lines changed

src/Compilers/Core/MSBuildTask/Csc.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ protected override string ToolNameWithoutExtension
197197
/// <summary>
198198
/// Fills the provided CommandLineBuilderExtension with those switches and other information that can go into a response file.
199199
/// </summary>
200-
protected internal override void AddResponseFileCommands(CommandLineBuilderExtension commandLine)
200+
protected override void AddResponseFileCommands(CommandLineBuilderExtension commandLine)
201201
{
202202
commandLine.AppendSwitchIfNotNull("/lib:", AdditionalLibPaths, ",");
203203
commandLine.AppendPlusOrMinusSwitch("/unsafe", _store, nameof(AllowUnsafeBlocks));

src/Compilers/Core/MSBuildTask/Csi.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ public class Csi : InteractiveCompiler
2525
#endregion
2626

2727
#region Interactive Compiler Members
28+
29+
protected override void AddCommandLineCommands(CommandLineBuilderExtension commandLine)
30+
{
31+
// Nothing to add
32+
}
33+
2834
protected override void AddResponseFileCommands(CommandLineBuilderExtension commandLine)
2935
{
3036
commandLine.AppendSwitchIfNotNull("/lib:", AdditionalLibPaths, ",");

src/Compilers/Core/MSBuildTask/InteractiveCompiler.cs

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -184,60 +184,22 @@ public ITaskItem? Source
184184

185185
#region Tool Members
186186

187-
// See ManagedCompiler.cs on the logic of this property
188-
private bool HasToolBeenOverridden => !(string.IsNullOrEmpty(ToolPath) && ToolExe == ToolName);
189-
190-
protected sealed override bool IsManagedTool => !HasToolBeenOverridden;
191-
192-
protected sealed override string PathToManagedTool => Utilities.GenerateFullPathToTool(ToolName);
193-
194-
protected sealed override string PathToNativeTool => Path.Combine(ToolPath ?? "", ToolExe);
195-
196187
protected override int ExecuteTool(string pathToTool, string responseFileCommands, string commandLineCommands)
197188
{
198189
if (ProvideCommandLineArgs)
199190
{
200-
CommandLineArgs = GetArguments(commandLineCommands, responseFileCommands).Select(arg => new TaskItem(arg)).ToArray();
191+
CommandLineArgs = GenerateCommandLineArgsTaskItems(responseFileCommands);
201192
}
202193

203194
return (SkipInteractiveExecution) ? 0 : base.ExecuteTool(pathToTool, responseFileCommands, commandLineCommands);
204195
}
205196

206-
public string GenerateCommandLineContents() => GenerateCommandLineCommands();
207-
208-
protected sealed override string ToolArguments
209-
{
210-
get
211-
{
212-
var builder = new CommandLineBuilderExtension();
213-
AddCommandLineCommands(builder);
214-
return builder.ToString();
215-
}
216-
}
217-
218-
public string GenerateResponseFileContents() => GenerateResponseFileCommands();
219-
220-
protected sealed override string GenerateResponseFileCommands()
221-
{
222-
var commandLineBuilder = new CommandLineBuilderExtension();
223-
AddResponseFileCommands(commandLineBuilder);
224-
return commandLineBuilder.ToString();
225-
}
226-
227197
#endregion
228198

229-
/// <summary>
230-
/// Fills the provided CommandLineBuilderExtension with those switches and other information that can't go into a response file and
231-
/// must go directly onto the command line.
232-
/// </summary>
233-
protected virtual void AddCommandLineCommands(CommandLineBuilderExtension commandLine)
234-
{
235-
}
236-
237199
/// <summary>
238200
/// Fills the provided CommandLineBuilderExtension with those switches and other information that can go into a response file.
239201
/// </summary>
240-
protected virtual void AddResponseFileCommands(CommandLineBuilderExtension commandLine)
202+
protected override void AddResponseFileCommands(CommandLineBuilderExtension commandLine)
241203
{
242204
commandLine.AppendSwitch("/i-");
243205

@@ -269,15 +231,5 @@ protected virtual void AddResponseFileCommands(CommandLineBuilderExtension comma
269231
}
270232
}
271233
}
272-
273-
/// <summary>
274-
/// Get the command line arguments to pass to the compiler.
275-
/// </summary>
276-
private string[] GetArguments(string commandLineCommands, string responseFileCommands)
277-
{
278-
var commandLineArguments = CommandLineUtilities.SplitCommandLineIntoArguments(commandLineCommands, removeHashComments: true);
279-
var responseFileArguments = CommandLineUtilities.SplitCommandLineIntoArguments(responseFileCommands, removeHashComments: true);
280-
return commandLineArguments.Concat(responseFileArguments).ToArray();
281-
}
282234
}
283235
}

src/Compilers/Core/MSBuildTask/ManagedCompiler.cs

Lines changed: 5 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -477,15 +477,6 @@ public bool ReportIVTs
477477

478478
#endregion
479479

480-
// ToolExe delegates back to ToolName if the override is not
481-
// set. So, if ToolExe == ToolName, we know ToolExe is not
482-
// explicitly overridden. So, if both ToolPath is unset and
483-
// ToolExe == ToolName, we know nothing is overridden, and
484-
// we can use our own csc.
485-
private bool HasToolBeenOverridden => !(string.IsNullOrEmpty(ToolPath) && ToolExe == ToolName);
486-
487-
protected sealed override bool IsManagedTool => !HasToolBeenOverridden;
488-
489480
/// <summary>
490481
/// Method for testing only
491482
/// </summary>
@@ -494,10 +485,6 @@ public string GeneratePathToTool()
494485
return GenerateFullPathToTool();
495486
}
496487

497-
protected sealed override string PathToManagedTool => Utilities.GenerateFullPathToTool(ToolName);
498-
499-
protected sealed override string PathToNativeTool => Path.Combine(ToolPath ?? "", ToolExe);
500-
501488
protected override int ExecuteTool(string pathToTool, string responseFileCommands, string commandLineCommands)
502489
{
503490
using var logger = new CompilerServerLogger($"MSBuild {Process.GetCurrentProcess().Id}");
@@ -508,8 +495,7 @@ internal int ExecuteTool(string pathToTool, string responseFileCommands, string
508495
{
509496
if (ProvideCommandLineArgs)
510497
{
511-
CommandLineArgs = GetArguments(commandLineCommands, responseFileCommands)
512-
.Select(arg => new TaskItem(arg)).ToArray();
498+
CommandLineArgs = GenerateCommandLineArgsTaskItems(responseFileCommands);
513499
}
514500

515501
if (SkipCompilerExecution)
@@ -526,7 +512,7 @@ internal int ExecuteTool(string pathToTool, string responseFileCommands, string
526512
string? tempDirectory = BuildServerConnection.GetTempPath(workingDirectory);
527513

528514
if (!UseSharedCompilation ||
529-
HasToolBeenOverridden ||
515+
!IsManagedTool ||
530516
!BuildServerConnection.IsCompilerServerSupported)
531517
{
532518
LogCompilationMessage(logger, requestId, CompilationKind.Tool, $"using command line tool by design '{pathToTool}'");
@@ -550,7 +536,7 @@ internal int ExecuteTool(string pathToTool, string responseFileCommands, string
550536
var buildRequest = BuildServerConnection.CreateBuildRequest(
551537
requestId,
552538
Language,
553-
GetArguments(ToolArguments, responseFileCommands).ToList(),
539+
GenerateCommandLineArgsList(responseFileCommands),
554540
workingDirectory: workingDirectory,
555541
tempDirectory: tempDirectory,
556542
keepAlive: null,
@@ -804,66 +790,19 @@ private void LogCompilationMessage(ICompilerServerLogger logger, Guid requestId,
804790
}
805791
}
806792

807-
public string GenerateResponseFileContents()
808-
{
809-
return GenerateResponseFileCommands();
810-
}
811-
812-
/// <summary>
813-
/// Get the command line arguments to pass to the compiler.
814-
/// </summary>
815-
private string[] GetArguments(string commandLineCommands, string responseFileCommands)
816-
{
817-
var commandLineArguments =
818-
CommandLineUtilities.SplitCommandLineIntoArguments(commandLineCommands, removeHashComments: true);
819-
var responseFileArguments =
820-
CommandLineUtilities.SplitCommandLineIntoArguments(responseFileCommands, removeHashComments: true);
821-
return commandLineArguments.Concat(responseFileArguments).ToArray();
822-
}
823-
824-
/// <summary>
825-
/// Returns the command line switch used by the tool executable to specify the response file
826-
/// Will only be called if the task returned a non empty string from GetResponseFileCommands
827-
/// Called after ValidateParameters, SkipTaskExecution and GetResponseFileCommands
828-
/// </summary>
829-
protected override string GenerateResponseFileCommands()
830-
{
831-
CommandLineBuilderExtension commandLineBuilder = new CommandLineBuilderExtension();
832-
AddResponseFileCommands(commandLineBuilder);
833-
return commandLineBuilder.ToString();
834-
}
835-
836-
/// <summary>
837-
/// Method for testing only
838-
/// </summary>
839-
public string GenerateCommandLine()
840-
{
841-
return GenerateCommandLineCommands();
842-
}
843-
844-
protected sealed override string ToolArguments
845-
{
846-
get
847-
{
848-
var builder = new CommandLineBuilderExtension();
849-
AddCommandLineCommands(builder);
850-
return builder.ToString();
851-
}
852-
}
853-
854793
/// <summary>
855794
/// Fills the provided CommandLineBuilderExtension with those switches and other information that can't go into a response file and
856795
/// must go directly onto the command line.
857796
/// </summary>
858-
protected internal virtual void AddCommandLineCommands(CommandLineBuilderExtension commandLine)
797+
protected override void AddCommandLineCommands(CommandLineBuilderExtension commandLine)
859798
{
860799
commandLine.AppendWhenTrue("/noconfig", _store, nameof(NoConfig));
861800
}
862801

863802
/// <summary>
864803
/// Fills the provided CommandLineBuilderExtension with those switches and other information that can go into a response file.
865804
/// </summary>
866-
protected internal virtual void AddResponseFileCommands(CommandLineBuilderExtension commandLine)
805+
protected override void AddResponseFileCommands(CommandLineBuilderExtension commandLine)
867806
{
868807
// If outputAssembly is not specified, then an "/out: <name>" option won't be added to
869808
// overwrite the one resulting from the OutputAssembly member of the CompilerParameters class.

0 commit comments

Comments
 (0)