Skip to content

Commit

Permalink
fixing command-line parsing in self-contained builds (#528)
Browse files Browse the repository at this point in the history
  • Loading branch information
brettsam committed Jul 14, 2021
1 parent 5504943 commit 3e65d91
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 29 deletions.
6 changes: 1 addition & 5 deletions src/DotNetWorker.Grpc/DotNetWorker.Grpc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<AssemblyName>Microsoft.Azure.Functions.Worker.Grpc</AssemblyName>
<RootNamespace>Microsoft.Azure.Functions.Worker.Grpc</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<MinorProductVersion>2</MinorProductVersion>
<MinorProductVersion>3</MinorProductVersion>
</PropertyGroup>

<Import Project="..\..\build\Common.props" />
Expand Down Expand Up @@ -39,8 +39,4 @@
<Protobuf Update=".\protobuf\src\proto\identity\ClaimsIdentityRpc.proto" Access="Internal" />
<Protobuf Update=".\protobuf\src\proto\shared\NullableTypes.proto" Access="Internal" />
</ItemGroup>

<ItemGroup>
<Folder Include="Binding\" />
</ItemGroup>
</Project>
8 changes: 7 additions & 1 deletion src/DotNetWorker.Grpc/GrpcServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ public static IServiceCollection AddGrpc(this IServiceCollection services)
GrpcWorkerStartupOptions arguments = argumentsOptions.Value;
GrpcChannel grpcChannel = GrpcChannel.ForAddress($"http://{arguments.Host}:{arguments.Port}", new GrpcChannelOptions()
string uriString = $"http://{arguments.Host}:{arguments.Port}";
if (!Uri.TryCreate(uriString, UriKind.Absolute, out Uri? grpcUri))
{
throw new InvalidOperationException($"The gRPC channel URI '{uriString}' could not be parsed.");
}
GrpcChannel grpcChannel = GrpcChannel.ForAddress(grpcUri, new GrpcChannelOptions()
{
MaxReceiveMessageSize = arguments.GrpcMaxMessageLength,
MaxSendMessageSize = arguments.GrpcMaxMessageLength,
Expand Down
2 changes: 1 addition & 1 deletion src/DotNetWorker/DotNetWorker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<AssemblyName>Microsoft.Azure.Functions.Worker</AssemblyName>
<RootNamespace>Microsoft.Azure.Functions.Worker</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<MinorProductVersion>3</MinorProductVersion>
<MinorProductVersion>4</MinorProductVersion>
</PropertyGroup>

<Import Project="..\..\build\Common.props" />
Expand Down
20 changes: 17 additions & 3 deletions src/DotNetWorker/Hosting/WorkerHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,24 @@ public static IHostBuilder ConfigureFunctionsWorkerDefaults(this IHostBuilder bu

internal static void RegisterCommandLine(IConfigurationBuilder builder, string[] cmdLine)
{
if (cmdLine.Length > 0 &&
!cmdLine[0].StartsWith("--"))
// If either of the first two arguments do not begin with '--', wrap them in
// quotes. On Linux, either of these first two arguments can be the path to the
// assembly, which begins with a '/' and is interpreted as a switch.
for (int i = 0; i <= 1; i++)
{
cmdLine[0] = $"\"{cmdLine[0]}\"";
if (cmdLine.Length <= i)
{
break;
}

string arg = cmdLine[i];

if (arg.StartsWith("--"))
{
break;
}

cmdLine[i] = $"\"{arg}\"";
}

builder.AddCommandLine(cmdLine);
Expand Down
42 changes: 23 additions & 19 deletions test/DotNetWorkerTests/WorkerHostBuilderExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Collections.Generic;
using System;
using System.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Configuration.EnvironmentVariables;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xunit;
using System.Linq;
using Microsoft.Extensions.Configuration.EnvironmentVariables;
using System;

namespace Microsoft.Azure.Functions.Worker.Tests
{
public class WorkerHostBuilderExtensionsTests
{
[Theory]
[InlineData("/home/usr/", "\"/home/usr/\"")]
[InlineData(null, "--host")]
public void QuoteFirstArg(string firstArg, string expected)
[InlineData("--host", "127.0.0.1", "--port", "45040")]
[InlineData("/home/usr/a.dll", "--host", "127.0.0.1", "--port", "45040")]
[InlineData("/home/usr/a.dll", "/home/usr/a.dll", "--host", "127.0.0.1", "--port", "45040")]
public void QuoteFirstArg(params string[] args)
{
var cmdLineList = new List<string> { "--host", "127.0.0.1" };
var configBuilder = new ConfigurationBuilder();
WorkerHostBuilderExtensions.RegisterCommandLine(configBuilder, args);

if (firstArg != null)
{
cmdLineList.Insert(0, firstArg);
}
var config = configBuilder.Build();

var cmdLine = cmdLineList.ToArray();
Assert.Equal("127.0.0.1", config["host"]);
}

var configBuilder = new ConfigurationBuilder();
WorkerHostBuilderExtensions.RegisterCommandLine(configBuilder, cmdLine);
[Theory]
[InlineData(0)]
[InlineData(1)]
public void RegisterCommandLine_NoArgs(int count)
{
var args = Enumerable.Repeat<string>("test", count).ToArray();

Assert.Equal(expected, cmdLine[0]);
var configBuilder = new ConfigurationBuilder();
WorkerHostBuilderExtensions.RegisterCommandLine(configBuilder, args);

var config = configBuilder.Build();
Assert.Equal("127.0.0.1", config["host"]);
// Ensures we don't throw an IndexOutOfRangeException; no assert necessary.
configBuilder.Build();
}

[Fact]
Expand Down

0 comments on commit 3e65d91

Please sign in to comment.