Skip to content

Fix some linker warnings, report others #24553

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

Merged
merged 4 commits into from
Aug 4, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Threading;
Expand Down Expand Up @@ -47,14 +48,14 @@ public static IWebHostBuilder CaptureStartupErrors(this IWebHostBuilder hostBuil
/// <param name="hostBuilder">The <see cref="IWebHostBuilder"/> to configure.</param>
/// <param name="startupAssemblyName">The name of the assembly containing the startup type.</param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
[RequiresUnreferencedCode("Types and members the loaded assembly depends on might be removed.")]
public static IWebHostBuilder UseStartup(this IWebHostBuilder hostBuilder, string startupAssemblyName)
{
if (startupAssemblyName == null)
{
throw new ArgumentNullException(nameof(startupAssemblyName));
}


return hostBuilder
.UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName)
.UseSetting(WebHostDefaults.StartupAssemblyKey, startupAssemblyName);
Expand Down
8 changes: 5 additions & 3 deletions src/Hosting/Abstractions/src/HostingStartupAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

namespace Microsoft.AspNetCore.Hosting
Expand All @@ -16,7 +17,7 @@ public sealed class HostingStartupAttribute : Attribute
/// Constructs the <see cref="HostingStartupAttribute"/> with the specified type.
/// </summary>
/// <param name="hostingStartupType">A type that implements <see cref="IHostingStartup"/>.</param>
public HostingStartupAttribute(Type hostingStartupType)
public HostingStartupAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type hostingStartupType)
{
if (hostingStartupType == null)
{
Expand All @@ -35,6 +36,7 @@ public HostingStartupAttribute(Type hostingStartupType)
/// The implementation of <see cref="IHostingStartup"/> that should be loaded when
/// starting an application.
/// </summary>
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
public Type HostingStartupType { get; }
}
}
}
7 changes: 4 additions & 3 deletions src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ public IWebHostBuilder UseStartup([DynamicallyAccessedMembers(StartupLinkerOptio
return this;
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern", Justification = "We need to call a generic method on IHostBuilder.")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The case where this may be unsafe is if IHostBuilder.ConfigureContainer ever puts a DynamicallyAccessedMembers attribute on the TContainerBuilder type. The linker won't be able to see that you are filling in the TContainerBuilder with containerType.

Looking at the implementation In Microsoft.Extensions.Hosting, I don't see HostBuilder needing this, so I think this is safe.

private void UseStartup([DynamicallyAccessedMembers(StartupLinkerOptions.Accessibility)] Type startupType, HostBuilderContext context, IServiceCollection services, object instance = null)
{
var webHostBuilderContext = GetWebHostBuilderContext(context);
Expand Down Expand Up @@ -275,12 +276,12 @@ private void UseStartup([DynamicallyAccessedMembers(StartupLinkerOptions.Accessi
var actionType = typeof(Action<,>).MakeGenericType(typeof(HostBuilderContext), containerType);

// Get the private ConfigureContainer method on this type then close over the container type
var configureCallback = GetType().GetMethod(nameof(ConfigureContainer), BindingFlags.NonPublic | BindingFlags.Instance)
var configureCallback = typeof(GenericWebHostBuilder).GetMethod(nameof(ConfigureContainerImpl), BindingFlags.NonPublic | BindingFlags.Instance)
.MakeGenericMethod(containerType)
.CreateDelegate(actionType, this);

// _builder.ConfigureContainer<T>(ConfigureContainer);
typeof(IHostBuilder).GetMethods().First(m => m.Name == nameof(IHostBuilder.ConfigureContainer))
typeof(IHostBuilder).GetMethod(nameof(IHostBuilder.ConfigureContainer))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously LINQ was used to get the first ConfigureContainer method for some reason. Do we know why?

I'm guessing there is only a single ConfigureContainer on IHostBuilder.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously LINQ was used to get the first ConfigureContainer method for some reason. Do we know why?

Lazy and defensive. Unless we decide to break the interface, this is "safe". 😄

I'm guessing there is only a single ConfigureContainer on IHostBuilder.

Correct.

.MakeGenericMethod(containerType)
.InvokeWithoutWrappingExceptions(_builder, new object[] { configureCallback });
}
Expand Down Expand Up @@ -310,7 +311,7 @@ private void UseStartup([DynamicallyAccessedMembers(StartupLinkerOptions.Accessi
});
}

private void ConfigureContainer<TContainer>(HostBuilderContext context, TContainer container)
private void ConfigureContainerImpl<TContainer>(HostBuilderContext context, TContainer container)
{
var instance = context.Properties[_startupKey];
var builder = (ConfigureContainerBuilder)context.Properties[typeof(ConfigureContainerBuilder)];
Expand Down
1 change: 1 addition & 0 deletions src/Hosting/Hosting/src/Internal/StartupLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ void RunPipeline(TContainerBuilder containerBuilder)
}
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "We're warning at the entry point. This is an implementation detail.")]
public static Type FindStartupType(string startupAssemblyName, string environmentName)
{
if (string.IsNullOrEmpty(startupAssemblyName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ public virtual Task<PublishedApplication> Publish(DeploymentParameters deploymen
// https://stackoverflow.com/a/37983587/102052
//
// 2. If "dotnet publish" does hang indefinitely for some reason, tests should fail fast with an error message.
const int timeoutMinutes = 5;
if (hostProcess.WaitForExit(milliseconds: timeoutMinutes * 60 * 1000))
var timeout = deploymentParameters.PublishTimeout ?? TimeSpan.FromMinutes(5);

if (hostProcess.WaitForExit(milliseconds: (int)timeout.TotalMilliseconds))
{
if (hostProcess.ExitCode != 0)
{
Expand All @@ -91,7 +92,7 @@ public virtual Task<PublishedApplication> Publish(DeploymentParameters deploymen
}
else
{
var message = $"{DotnetCommandName} publish failed to exit after {timeoutMinutes} minutes";
var message = $"{DotnetCommandName} publish failed to exit after {timeout.TotalMinutes} minutes";
logger.LogError(message);
throw new Exception(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ public DeploymentParameters(DeploymentParameters parameters)
/// </summary>
public Action<DeploymentParameters> UserAdditionalCleanup { get; set; }

/// <summary>
/// Timeout for publish
/// </summary>
public TimeSpan? PublishTimeout { get; set; }

public override string ToString()
{
return string.Format(
Expand Down
1 change: 1 addition & 0 deletions src/Hosting/test/FunctionalTests/LinkedApplicationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public async Task LinkedApplicationWorks()
ApplicationType = ApplicationType.Standalone,
PublishApplicationBeforeDeployment = true,
RestoreDependencies = true,
PublishTimeout = TimeSpan.FromMinutes(10), // Machines are slow (these tests restore)
StatusMessagesEnabled = false
};

Expand Down