Skip to content

Handling Functions Runtime Environment env var when set by user #4535

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

Closed
wants to merge 5 commits into from
Closed
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
1 change: 1 addition & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
- Add `func pack` support for in-proc functions (#4529)
- Default to remote build for `func pack` for python apps (#4530)
- Update `func init` to default to the .NET 8 template for in-proc apps (#4557)
- Fix exception when `AZURE_FUNCTIONS_ENVIRONMENT` variable is set by user (#2465)
4 changes: 2 additions & 2 deletions src/Cli/func/Actions/HostActions/StartHostAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ private async Task<IWebHost> BuildWebHost(ScriptApplicationHostOptions hostOptio
.Build();
}

private async Task<IDictionary<string, string>> GetConfigurationSettings(string scriptPath, Uri uri)
internal async Task<IDictionary<string, string>> GetConfigurationSettings(string scriptPath, Uri uri)
{
var settings = _secretsManager.GetSecrets();
settings.Add(Constants.WebsiteHostname, uri.Authority);
Expand All @@ -300,7 +300,7 @@ private async Task<IDictionary<string, string>> GetConfigurationSettings(string

// when running locally in CLI we want the host to run in debug mode
// which optimizes host responsiveness
settings.Add("AZURE_FUNCTIONS_ENVIRONMENT", "Development");
settings.TryAdd("AZURE_FUNCTIONS_ENVIRONMENT", "Development");
Copy link
Member

Choose a reason for hiding this comment

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

There are a lot of side effects if we allow for this to be overridden. The WebJobs SDK and host behaviors for polling intervals, logging, and other things will change, and this could significantly impact the local debugging experience in surprising ways.


// Inject the .NET Worker startup hook if debugging the worker
if (DotNetIsolatedDebug != null && DotNetIsolatedDebug.Value)
Expand Down
35 changes: 35 additions & 0 deletions test/Cli/Func.UnitTests/ActionsTests/StartHostActionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,41 @@ public async Task ValidateHostRuntimeAsync_MatchesExpectedResults(WorkerRuntime
Assert.False(expectException, "Expected validation failure.");
}

[Fact]
public async Task GetConfigurationSettings_DoesNotOverwriteAzFuncEnvironment_WhenAlreadyInSecrets()
{
// Arrange
var secretsDict = new Dictionary<string, string>
{
["AZURE_FUNCTIONS_ENVIRONMENT"] = "UserEnv"
};

var mockSecretsManager = new Mock<ISecretsManager>();
mockSecretsManager.Setup(s => s.GetSecrets())
.Returns(() => new Dictionary<string, string>(secretsDict));

// Return an empty set of connection strings of the expected type
mockSecretsManager.Setup(s => s.GetConnectionStrings())
.Returns(Array.Empty<ConnectionString>);

// Initialize globals if required by your setup
GlobalCoreToolsSettings.Init(mockSecretsManager.Object, []);

var action = new StartHostAction(mockSecretsManager.Object, Mock.Of<IProcessManager>())
{
DotNetIsolatedDebug = false,
EnableJsonOutput = false,
VerboseLogging = false,
HostRuntime = "default"
};

// Act
var result = await action.GetConfigurationSettings("some/path", new Uri("https://example.com"));

// Assert
Assert.Equal("UserEnv", result["AZURE_FUNCTIONS_ENVIRONMENT"]);
}

public void Dispose()
{
FileSystemHelpers.Instance = null;
Expand Down