Skip to content

Fix file-existence validation bypassed when --file is provided explicitly#52974

Open
Copilot wants to merge 5 commits intomainfrom
copilot/fix-defaultvaluefactory-validation
Open

Fix file-existence validation bypassed when --file is provided explicitly#52974
Copilot wants to merge 5 commits intomainfrom
copilot/fix-defaultvaluefactory-validation

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 10, 2026

DefaultValueFactory only runs when no value is provided, so placing file-existence validation there silently bypasses it when the user supplies --file nofile, causing an unhandled FileNotFoundException instead of the expected error message.

Changes

  • Stage3/Program.cs — Uses two properties together to correctly separate concerns:

    • DefaultValueFactory supplies only the default sampleQuotes.txt value when --file is omitted
    • CustomParser handles token→FileInfo conversion and file-existence validation when --file is explicitly provided; Required = true is removed since DefaultValueFactory provides the fallback
  • get-started-tutorial.md — Updates the step explanation to accurately describe each mechanism using second-person voice: DefaultValueFactory supplies the default when you don't provide --file (and doesn't run when you do); CustomParser handles conversion and validation when you do provide it

Option<FileInfo> fileOption = new("--file")
{
    DefaultValueFactory = _ =&gt; new FileInfo("sampleQuotes.txt"),
    CustomParser = result =&gt;
    {
        var file = new FileInfo(result.Tokens.Single().Value);
        if (!file.Exists)
            result.AddError("File does not exist");
        return file;
    }
};

Fixes #52979.


Internal previews

📄 File 🔗 Preview link
docs/standard/commandline/get-started-tutorial.md docs/standard/commandline/get-started-tutorial

Copilot AI changed the title [WIP] Fix incorrect use of DefaultValueFactory for file validation Fix file-existence validation bypassed when --file is provided explicitly Apr 10, 2026
Copilot AI requested a review from gewarren April 10, 2026 20:50
@gewarren gewarren marked this pull request as ready for review April 10, 2026 20:55
@gewarren gewarren requested a review from a team as a code owner April 10, 2026 20:55
Copilot AI review requested due to automatic review settings April 10, 2026 20:55
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a validation gap in the System.CommandLine “get started” tutorial where file-existence checks placed in DefaultValueFactory were bypassed when --file is explicitly provided, resulting in an unhandled FileNotFoundException instead of a parse error.

Changes:

  • Moves file-existence validation to fileOption.Validators.Add(...) so it runs for both default and user-supplied values.
  • Updates the tutorial text to describe the split between defaulting/parsing and validation.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
docs/standard/commandline/snippets/get-started-tutorial/csharp/Stage3/Program.cs Refactors option setup by separating default value creation from file-existence validation via validators.
docs/standard/commandline/get-started-tutorial.md Revises the tutorial explanation to match the intended validation mechanism.

…ct actual behavior

Agent-Logs-Url: https://github.com/dotnet/docs/sessions/b9fafcae-b41f-4097-a3e1-a91119d26d47

Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
@gewarren gewarren requested a review from adamsitnik April 10, 2026 21:07
@gewarren gewarren enabled auto-merge (squash) April 10, 2026 21:07
…aultValueFactory for default only

Agent-Logs-Url: https://github.com/dotnet/docs/sessions/e25acb40-859f-466e-849f-81c336ec617b

Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
auto-merge was automatically disabled April 10, 2026 21:13

Head branch was pushed to by a user without write access

Copilot AI requested a review from gewarren April 10, 2026 21:14
@Vladimir-Bosnjak
Copy link
Copy Markdown

Checked this PR. This resolves the issue I reported. Validation is no longer tied to DefaultValueFactory, so it no longer gets bypassed when --file is provided. We now have working parse-time error.

Execution methods should still handle I/O failures independently but for tutorial illustration this fixes it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

misleading validation and inconsistent behavior for Option<FileInfo> Incorrect use of DefaultValueFactory for custom file existence validation

4 participants