-
Notifications
You must be signed in to change notification settings - Fork 204
Avoid executing source generators outside of an Azure Functions project #2119
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
da2fc36
Checking generators are running inside valid azure functions project
kshyju 30bfa86
Checking generators are running inside valid azure functions project
kshyju 4e232e0
Merge branch 'shkr/fix-exten-startup-gen' of https://github.com/Azure…
kshyju 1a86b21
Minor cleanup
kshyju a5f12c4
Minor cleanup on release notes.
kshyju 95e3e24
Added comment to props file about the usage of "FunctionsExecutionMod…
kshyju 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
23 changes: 23 additions & 0 deletions
23
sdk/Sdk.Generators/Extensions/GeneratorExecutionContextExtensions.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,23 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| using Microsoft.CodeAnalysis; | ||
|
|
||
| namespace Microsoft.Azure.Functions.Worker.Sdk.Generators | ||
| { | ||
| internal static class GeneratorExecutionContextExtensions | ||
| { | ||
| /// <summary> | ||
| /// Returns true if the source generator is running in the context of an "Azure Function" project. | ||
| /// </summary> | ||
| internal static bool IsRunningInAzureFunctionProject(this GeneratorExecutionContext context) | ||
| { | ||
| if (context.AnalyzerConfigOptions.GlobalOptions.TryGetValue(Constants.BuildProperties.FunctionsExecutionModel, out var value)) | ||
| { | ||
| return string.Equals(value, Constants.ExecutionModel.Isolated); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
| } |
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
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
48 changes: 48 additions & 0 deletions
48
test/Sdk.Generator.Tests/ExtensionStartup/NotGeneratedTests.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,48 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| using System.Threading.Tasks; | ||
| using Microsoft.Azure.Functions.Tests.WorkerExtensionsSample; | ||
| using Microsoft.Azure.Functions.Worker.Sdk.Generators; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Xunit; | ||
| namespace Microsoft.Azure.Functions.SdkGeneratorTests | ||
| { | ||
| public partial class ExtensionStartupRunnerGeneratorTests | ||
| { | ||
| public sealed class NotGeneratedTests | ||
| { | ||
| const string InputCode = """ | ||
| public class Foo | ||
| { | ||
| } | ||
| """; | ||
|
|
||
| [Theory] | ||
| [InlineData(LanguageVersion.CSharp7_3)] | ||
| [InlineData(LanguageVersion.CSharp8)] | ||
| [InlineData(LanguageVersion.CSharp9)] | ||
| [InlineData(LanguageVersion.CSharp10)] | ||
| [InlineData(LanguageVersion.CSharp11)] | ||
| [InlineData(LanguageVersion.Latest)] | ||
| public async Task NotGeneratedWhenNotRunningInAnAzureFunctionsProject(LanguageVersion languageVersion) | ||
| { | ||
| var referencedExtensionAssemblies = new[] | ||
| { | ||
| typeof(SampleExtensionStartup).Assembly, | ||
| }; | ||
|
|
||
| string? expectedGeneratedFileName = null; | ||
| string? expectedOutput = null; | ||
|
|
||
| await TestHelpers.RunTestAsync<ExtensionStartupRunnerGenerator>( | ||
| referencedExtensionAssemblies, | ||
| InputCode, | ||
| expectedGeneratedFileName, | ||
| expectedOutput, | ||
| languageVersion: languageVersion, | ||
| runInsideAzureFunctionProject: false); | ||
| } | ||
| } | ||
| } | ||
| } |
74 changes: 74 additions & 0 deletions
74
test/Sdk.Generator.Tests/FunctionExecutor/NotGeneratedTests.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,74 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| using System.Reflection; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Azure.Functions.Worker; | ||
| using Microsoft.Azure.Functions.Worker.Sdk.Generators; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Extensions.Logging; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Azure.Functions.SdkGeneratorTests | ||
| { | ||
| public partial class FunctionExecutorGeneratorTests | ||
| { | ||
| public sealed class NotGeneratedTests | ||
| { | ||
| private readonly Assembly[] _referencedAssemblies = new[] | ||
| { | ||
| typeof(HttpTriggerAttribute).Assembly, | ||
| typeof(FunctionAttribute).Assembly, | ||
| typeof(LoggingServiceCollectionExtensions).Assembly, | ||
| typeof(ServiceProviderServiceExtensions).Assembly, | ||
| typeof(ServiceCollection).Assembly, | ||
| typeof(ILogger).Assembly, | ||
| typeof(IConfiguration).Assembly, | ||
| typeof(HostBuilder).Assembly, | ||
| typeof(IHostBuilder).Assembly | ||
| }; | ||
|
|
||
| [Theory] | ||
| [InlineData(LanguageVersion.CSharp7_3)] | ||
| [InlineData(LanguageVersion.CSharp8)] | ||
| [InlineData(LanguageVersion.CSharp9)] | ||
| [InlineData(LanguageVersion.CSharp10)] | ||
| [InlineData(LanguageVersion.CSharp11)] | ||
| [InlineData(LanguageVersion.Latest)] | ||
| public async Task NotGeneratedWhenNotRunningInAnAzureFunctionsProject(LanguageVersion languageVersion) | ||
| { | ||
| string inputCode = """ | ||
| using System; | ||
| using Microsoft.Azure.Functions.Worker; | ||
| using Microsoft.Azure.Functions.Worker.Http; | ||
|
|
||
| namespace MyCompany.MyApp.Functions | ||
| { | ||
| public static class HttpTriggerSimple | ||
| { | ||
| [Function(nameof(HttpTriggerSimple))] | ||
| public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
| } | ||
| } | ||
| """; | ||
| string? expectedGeneratedFileName = null; | ||
| string? expectedOutput = null; | ||
|
|
||
| await TestHelpers.RunTestAsync<FunctionExecutorGenerator>( | ||
| _referencedAssemblies, | ||
| inputCode, | ||
| expectedGeneratedFileName, | ||
| expectedOutput, | ||
| languageVersion: languageVersion, | ||
| runInsideAzureFunctionProject: false); | ||
| } | ||
|
|
||
| } | ||
| } | ||
| } |
71 changes: 71 additions & 0 deletions
71
test/Sdk.Generator.Tests/FunctionMetadataProviderGeneratorTests/NotGeneratedTests.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,71 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| using System.Reflection; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Azure.Functions.Worker; | ||
| using Microsoft.Azure.Functions.Worker.Sdk.Generators; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Extensions.Logging; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Azure.Functions.SdkGeneratorTests | ||
| { | ||
| public partial class FunctionMetadataProviderGeneratorTests | ||
| { | ||
| public sealed class NotGeneratedTests | ||
| { | ||
| private readonly Assembly[] _referencedExtensionAssemblies = new[] | ||
| { | ||
| typeof(HttpTriggerAttribute).Assembly, typeof(FunctionAttribute).Assembly, | ||
| typeof(LoggingServiceCollectionExtensions).Assembly, | ||
| typeof(ServiceProviderServiceExtensions).Assembly, typeof(ServiceCollection).Assembly, | ||
| typeof(ILogger).Assembly, typeof(IConfiguration).Assembly, typeof(HostBuilder).Assembly, | ||
| typeof(IHostBuilder).Assembly | ||
| }; | ||
|
|
||
| [Theory] | ||
| [InlineData(LanguageVersion.CSharp7_3)] | ||
| [InlineData(LanguageVersion.CSharp8)] | ||
| [InlineData(LanguageVersion.CSharp9)] | ||
| [InlineData(LanguageVersion.CSharp10)] | ||
| [InlineData(LanguageVersion.CSharp11)] | ||
| [InlineData(LanguageVersion.Latest)] | ||
| public async Task NotGeneratedWhenNotRunningInAnAzureFunctionsProject(LanguageVersion languageVersion) | ||
| { | ||
| string inputCode = """ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using Microsoft.Azure.Functions.Worker; | ||
| using Microsoft.Azure.Functions.Worker.Http; | ||
|
|
||
| namespace MyCompany.MyApp.Functions | ||
| { | ||
| public static class HttpTriggerSimple | ||
| { | ||
| [Function(nameof(HttpTriggerSimple))] | ||
| public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| string? expectedGeneratedFileName = null; | ||
| string? expectedOutput = null; | ||
|
|
||
| await TestHelpers.RunTestAsync<FunctionMetadataProviderGenerator>( | ||
| _referencedExtensionAssemblies, | ||
| inputCode, | ||
| expectedGeneratedFileName, | ||
| expectedOutput, | ||
| languageVersion: languageVersion, | ||
| runInsideAzureFunctionProject: false); | ||
| } | ||
| } | ||
| } | ||
| } |
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
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.