Choose unique default names for func new#4929
Conversation
There was a problem hiding this comment.
Pull request overview
Updates func new to avoid suggesting a default function name that would collide with an existing function artifact, aligning behavior with VS Code’s “auto-increment” naming.
Changes:
- Compute a unique default function name before prompting (e.g.,
HttpTrigger,HttpTrigger1,HttpTrigger2, …). - Add
GetUniqueDefaultFunctionNamehelper and artifact-existence check for classic (directory) vs Node v4 (file) templates. - Add unit tests covering the incrementing behavior of the helper.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/Cli/func/Actions/LocalActions/CreateFunctionAction.cs |
Introduces unique default-name selection and artifact-existence checks for traditional vs Node v4 templates. |
test/Cli/Func.UnitTests/ActionsTests/CreateFunctionActionTests.cs |
Adds unit coverage for the default-name increment helper. |
| var defaultFunctionName = GetUniqueDefaultFunctionName( | ||
| template.Metadata.DefaultFunctionName, | ||
| functionName => FunctionArtifactExists(functionName, FileName, template)); |
There was a problem hiding this comment.
When FileName is set, FunctionArtifactExists ignores the candidate name and only checks the fixed file path. If that file already exists, every candidate returns true and GetUniqueDefaultFunctionName loops forever, hanging func new before it ever prompts. Easiest fix is to skip the uniqueness loop when FileName != null (renaming the function can't avoid a fixed-file collision anyway).
Cleanest fix:
var defaultFunctionName = FileName != null
? template.Metadata.DefaultFunctionName
: GetUniqueDefaultFunctionName(
template.Metadata.DefaultFunctionName,
functionName => FunctionArtifactExists(functionName, FileName, template));| return FileSystemHelpers.DirectoryExists(Path.Combine(Environment.CurrentDirectory, functionName)); | ||
| } | ||
|
|
||
| private static bool IsNodeV4Template(Template template) |
There was a problem hiding this comment.
This exact code/check already exists inlined in TemplatesManager.cs:145 - you should extract this a shared helper instead of duplicating
| } | ||
|
|
||
| ColoredConsole.Write($"Function name: [{template.Metadata.DefaultFunctionName}] "); | ||
| var defaultFunctionName = GetUniqueDefaultFunctionName( |
There was a problem hiding this comment.
Right now this PR only covers the traditional/Node v4 path, .NET (in-proc and isolated) and the Python v2 programming model take their own branches earlier in RunAsync and still silently prompt with a colliding default. Please extend the uniqueness behavior to those two paths as well.
-
.NET (in-proc & isolated) branch: lines 144–162 (if (WorkerRuntimeLanguageHelper.IsDotnet(_workerRuntime) && !Csx)), default name is set at line 158 (FunctionName = string.IsNullOrEmpty(FunctionName) ? TemplateName : FunctionName;).
-
Python v2 (new programming model) branch: lines 165–234 (else if (IsNewPythonProgrammingModel())), default name is resolved at lines 227–230 (if (string.IsNullOrEmpty(FunctionName)) FunctionName = providedInputs[GetFunctionNameParamId];).
Both bypass the new GetUniqueDefaultFunctionName call
|
@Genmin please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
Fixes #3262.
Summary
func newoffers a default function name, choose the first available name instead of defaulting to an existing artifactHttpTrigger,HttpTrigger1,HttpTrigger2, ... style naming for classic folder templates and Node v4 file templatesValidation
git diff --checkdotnet,csc, andmsbuildare unavailable).