Skip to content
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

Initializing Dockerfile for .NET isolated project does not honor existing TFM #3800

11 changes: 11 additions & 0 deletions src/Azure.Functions.Cli/Actions/LocalActions/InitAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,17 @@ private static async Task WriteLocalSettingsJson(WorkerRuntime workerRuntime, Pr

private static async Task WriteDockerfile(WorkerRuntime workerRuntime, string language, string targetFramework, bool csx)
{
if (WorkerRuntimeLanguageHelper.IsDotnet(workerRuntime) && string.IsNullOrEmpty(targetFramework))
{
var functionAppRoot = ScriptHostHelpers.GetFunctionAppRootDirectory(Environment.CurrentDirectory);
string projectFilePath = ProjectHelpers.FindProjectFile(functionAppRoot);
VineethReyya marked this conversation as resolved.
Show resolved Hide resolved
if (projectFilePath != null)
{
string projectFileName = Path.GetFileName(projectFilePath);
targetFramework = await DotnetHelpers.DetermineTargetFramework(Path.GetDirectoryName(projectFilePath), projectFileName);
}
}

if (workerRuntime == Helpers.WorkerRuntime.dotnet)
{
if (csx)
Expand Down
13 changes: 11 additions & 2 deletions src/Azure.Functions.Cli/Helpers/DotnetHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,23 @@ public static void EnsureDotnet()
/// e.g. in Directory.Build.props
/// </summary>
/// <param name="projectDirectory">Directory containing the .csproj file</param>
/// <param name="projectFilename">Name of the .csproj File</param>
/// <returns>Target framework, e.g. net8.0</returns>
/// <exception cref="CliException"></exception>
public static async Task<string> DetermineTargetFramework(string projectDirectory)
public static async Task<string> DetermineTargetFramework(string projectDirectory, string projectFilename = null)
{
EnsureDotnet();
if (projectFilename == null)
{
var projectFilePath = ProjectHelpers.FindProjectFile(projectDirectory);
if (projectFilePath != null)
{
projectFilename = Path.GetFileName(projectFilePath);
}
}
var exe = new Executable(
VineethReyya marked this conversation as resolved.
Show resolved Hide resolved
"dotnet",
"build -getproperty:TargetFramework",
$"build {projectFilename} -getproperty:TargetFramework",
workingDirectory: projectDirectory,
environmentVariables: new Dictionary<string, string>
{
Expand Down
27 changes: 27 additions & 0 deletions test/Azure.Functions.Cli.Tests/E2E/InitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -877,5 +877,32 @@ public Task init_python_app_generates_getting_started_md()
},
}, _output);
}

[Theory]
[InlineData("dotnet-isolated","4", "net6.0")]
[InlineData("dotnet-isolated", "4", "net7.0")]
[InlineData("dotnet-isolated","4", "net8.0")]
public Task init_docker_only_for_existing_project_TargetFramework(string workerRuntime, string version, string TargetFramework)
{
var TargetFrameworkstr = TargetFramework.Replace("net", string.Empty);
return CliTester.Run(new RunConfiguration
{
Commands = new[]
{
$"init . --worker-runtime {workerRuntime} --target-framework {TargetFramework}",
$"init . --docker-only",
},
CheckFiles = new[]
{
new FileResult
{
Name = "Dockerfile",
ContentContains = new[] { $"FROM mcr.microsoft.com/azure-functions/{workerRuntime}:{version}-{workerRuntime}{TargetFrameworkstr}" }
}
},
OutputContains = new[] { "Dockerfile" },
CommandTimeout = TimeSpan.FromSeconds(300)
}, _output);
}
}
}
Loading