"Index was outside the bounds of the array" from dotnet CLI
- Dominant language
- C#
- Stars
- 4.2k
- Forks
- 778
- Avg merge
- 3h 37m
- Merged PRs (30d)
- 21
Description
I'm running Cake 0.23.0 locally on Windows 10 Pro 64 bit version.
In my Cake script I've been using "" for the project in order to let the CLI tools figure out things. I basically want to build everything.
```
DotNetCoreBuild("", new DotNetCoreBuildSettings
{
Configuration = configuration,
MSBuildSettings = msBuildSettings
});
```
This worked fine with the 1.1 tooling, but fails with the 2.0 tooling.
Turning on diagnostics information, I see that following command is being called
`"C:/Program Files/dotnet/dotnet.exe" build "" --configuration Release /property:AssemblyVersion=1.0.2.1 /property:FileVersion=1.0.2.1 /property:PackageVersion=1.0.2.1
`
Note the "" after build. If I remove them, things work fine.
If I try to pass inn null in the Cake script, I get an ArgumentNullException.
I had a look at the code and think I've spotted the problem in [https://github.com/cake-build/cake/blob/develop/src/Cake.Common/Tools/DotNetCore/Build/DotNetCoreBuilder.cs](https://github.com/cake-build/cake/blob/develop/src/Cake.Common/Tools/DotNetCore/Build/DotNetCoreBuilder.cs)
The Build() method has a null check for project, but when you look at the GetArguments() method, you can see that also has a null check for project. If it's not defined, you don't escape an empty string.
From what I can tell, it should only be a matter of removing the null check from the Build() method.
```
public void Build(string project, DotNetCoreBuildSettings settings)
{
if (project == null)
{
throw new ArgumentNullException(nameof(project));
}
if (settings == null)
{
throw new ArgumentNullException(nameof(settings));
}
RunCommand(settings, GetArguments(project, settings));
}
private ProcessArgumentBuilder GetArguments(string project, DotNetCoreBuildSettings settings)
{
var builder = CreateArgumentBuilder(settings);
builder.Append("build");
// Specific path?
if (project != null)
{
builder.AppendQuoted(project);
}
// Output directory
if (settings.OutputDirectory != null)
{
builder.Append("--output");
builder.AppendQuoted(settings.OutputDirectory.MakeAbsolute(_environment).FullPath);
}
```
~~~sh
========================================
Build
========================================
Executing task: Build
Executing: "C:/Program Files/dotnet/dotnet.exe" build "" --configuration Release /property:AssemblyVersion=1.0.2.1 /prop
erty:FileVersion=1.0.2.1 /property:PackageVersion=1.0.2.1
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.String.get_Chars(Int32 index)
at Microsoft.DotNet.Cli.CommandLine.StringExtensions.HasPrefix(String arg)
at Microsoft.DotNet.Cli.CommandLine.StringExtensions.d__7.MoveNext()
at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source, Int32& length)
at System.Collections.Generic.Queue`1..ctor(IEnumerable`1 collection)
at Microsoft.DotNet.Cli.CommandLine.Parser.Parse(IReadOnlyCollection`1 rawArgs, Boolean isProgressive)
at Microsoft.DotNet.Cli.CommandLine.Parser.Parse(String[] args)
at Microsoft.DotNet.Cli.ParserExtensions.ParseFrom(Parser parser, String context, String[] args)
at Microsoft.DotNet.Cli.Program.ProcessArgs(String[] args, ITelemetry telemetryClient)
at Microsoft.DotNet.Cli.Program.Main(String[] args)
An error occurred when executing task 'Build'.
Error: System.AggregateException: One or more errors occurred. ---> Cake.Core.CakeException: .NET Core CLI: Process retu
rned an error (exit code 1).
at Cake.Core.Tooling.Tool`1.ProcessExitCode(Int32 exitCode)
at Cake.Core.Tooling.Tool`1.Run(TSettings settings, ProcessArgumentBuilder arguments, ProcessSettings processSettings
, Action`1 postAction)
at Cake.Common.Tools.DotNetCore.Build.DotNetCoreBuilder.Build(String project, DotNetCoreBuildSettings settings)
at Submission#0.DotNetCoreBuild(String project, DotNetCoreBuildSettings settings)
at Submission#0.<>b__0_2()
at Cake.Core.CakeTaskBuilderExtensions.<>c__DisplayClass8_0.b__0(ICakeContext x)
at Cake.Core.ActionTask.d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Cake.Core.DefaultExecutionStrategy.d__4.MoveNext()
~~~
Contributor guide
Research direction
Start in src/Cake.Common/Tools/DotNetCore/Build/DotNetCoreBuilder.cs, especially DotNetCoreBuilder.Build and GetArguments, and reproduce the empty-project invocation with the .NET Core 2.0 tooling. Done means an empty project argument no longer produces an empty quoted CLI argument, while an explicit project path continues to work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- cli, tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100