-
Notifications
You must be signed in to change notification settings - Fork 600
Make nested option containers trimming-safe #3376
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
Open
Larry Osterman (LarryOsterman)
wants to merge
7
commits into
main
Choose a base branch
from
larryosterman-option-container-trimming-safety
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
950f2c5
Make nested option containers trimming-safe
LarryOsterman 02f5fb4
Add trimmed option test app to solutions
LarryOsterman 4e647df
Remove obsolete option container attribute
LarryOsterman c5540bd
Remove option container metadata interface
Copilot f826708
Fix build break and apply review feedback
LarryOsterman ccca0f7
Merge branch 'main' into larryosterman-option-container-trimming-safety
alzimmermsft cd20554
Merge in main and resolve merge conflict
alzimmermsft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
core/Microsoft.Mcp.Core/src/Options/OptionContainerAttribute.cs
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
core/Microsoft.Mcp.Core/src/Options/OptionContainerAttribute`1.cs
|
LarryOsterman marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace Microsoft.Mcp.Core.Options; | ||
|
|
||
| /// <summary> | ||
| /// Identifies a complex option container and preserves the members required to discover and bind its options. | ||
| /// </summary> | ||
| /// <typeparam name="TContainer">The type of the option container.</typeparam> | ||
| [AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)] | ||
| public sealed class OptionContainerAttribute< | ||
| [DynamicallyAccessedMembers(ContainerMembers)] TContainer> : Attribute | ||
| { | ||
| private const DynamicallyAccessedMemberTypes ContainerMembers = | ||
| DynamicallyAccessedMemberTypes.PublicProperties | | ||
| DynamicallyAccessedMemberTypes.PublicParameterlessConstructor; | ||
|
|
||
| /// <summary> | ||
| /// The prefix to use for the options in the container. | ||
| /// If null, the property name (in kebab-case) is used as the prefix. | ||
| /// </summary> | ||
| public string? Prefix { get; init; } | ||
|
|
||
| [DynamicallyAccessedMembers(ContainerMembers)] | ||
| public Type ContainerType => typeof(TContainer); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
core/Microsoft.Mcp.Core/tests/Microsoft.Mcp.Core.Tests/Options/OptionBinderTrimmedTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Diagnostics; | ||
| using System.Reflection; | ||
| using System.Runtime.InteropServices; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Mcp.Core.Tests.Options; | ||
|
|
||
| public sealed class OptionBinderTrimmedTests | ||
| { | ||
| [Fact] | ||
| public async Task TrimmedPublish_PreservesNestedOptionContainerProperties() | ||
| { | ||
| var sourcePath = Assembly.GetExecutingAssembly() | ||
| .GetCustomAttributes<AssemblyMetadataAttribute>() | ||
| .Single(attribute => attribute.Key == "SourcePath") | ||
| .Value!; | ||
| var projectPath = Path.GetFullPath( | ||
| Path.Combine(sourcePath, "..", "TrimmedOptionContainerApp", "TrimmedOptionContainerApp.csproj")); | ||
| var publishPath = Path.Combine(Path.GetTempPath(), nameof(OptionBinderTrimmedTests), Guid.NewGuid().ToString("N")); | ||
|
|
||
| try | ||
| { | ||
| var publishResult = await RunProcessAsync( | ||
| "dotnet", | ||
| [ | ||
| "publish", | ||
| projectPath, | ||
| "--configuration", "Release", | ||
| "--runtime", RuntimeInformation.RuntimeIdentifier, | ||
| "--self-contained", "true", | ||
| "--output", publishPath, | ||
| "/p:PublishTrimmed=true" | ||
| ]); | ||
|
|
||
| Assert.True( | ||
| publishResult.ExitCode == 0, | ||
| $"Trimmed publish failed.{Environment.NewLine}{publishResult.StandardOutput}{Environment.NewLine}{publishResult.StandardError}"); | ||
|
|
||
| var executableName = OperatingSystem.IsWindows() | ||
| ? "TrimmedOptionContainerApp.exe" | ||
| : "TrimmedOptionContainerApp"; | ||
| var executablePath = Path.Combine(publishPath, executableName); | ||
| var executionResult = await RunProcessAsync(executablePath, []); | ||
|
|
||
| Assert.True( | ||
| executionResult.ExitCode == 0, | ||
| $"Trimmed executable did not discover and bind nested retry options.{Environment.NewLine}" + | ||
| $"{executionResult.StandardOutput}{Environment.NewLine}{executionResult.StandardError}"); | ||
| } | ||
| finally | ||
| { | ||
| if (Directory.Exists(publishPath)) | ||
| { | ||
| Directory.Delete(publishPath, recursive: true); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static async Task<(int ExitCode, string StandardOutput, string StandardError)> RunProcessAsync( | ||
| string fileName, | ||
| string[] arguments) | ||
| { | ||
| var startInfo = new ProcessStartInfo | ||
| { | ||
| FileName = fileName, | ||
| UseShellExecute = false, | ||
| RedirectStandardOutput = true, | ||
| RedirectStandardError = true, | ||
| CreateNoWindow = true | ||
| }; | ||
|
|
||
| foreach (var argument in arguments) | ||
| { | ||
| startInfo.ArgumentList.Add(argument); | ||
| } | ||
|
|
||
| using var process = Process.Start(startInfo) | ||
| ?? throw new InvalidOperationException($"Failed to start '{fileName}'."); | ||
| var standardOutput = process.StandardOutput.ReadToEndAsync(); | ||
| var standardError = process.StandardError.ReadToEndAsync(); | ||
|
|
||
| await process.WaitForExitAsync(TestContext.Current.CancellationToken); | ||
| return (process.ExitCode, await standardOutput, await standardError); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.