Skip to content

FileInfoFactory and DirectoryInfoFactory Wrap implementations don't match the interface for nullability #975

Closed

Description

@mikeeheler

Describe the bug
The implementations for FileInfoFactory.Wrap and DirectoryInfoFactory.Wrap require non-null references, while the interfaces are defined as [return: NotNullIfNotNull("fileInfo")] IFileInfoFactory.Wrap(FileInfo? fileInfo) and likewise for IDirectoryInfoFactory.Wrap.

To Reproduce
Steps to reproduce the behavior:

  1. new FileSystem().FileInfo.Wrap(null);
  2. The analyzer and compiler accept the syntax since IFileInfoFactory.Wrap accepts FileInfo? and returns IFileInfo?
  3. An ArgumentNullException is thrown

Expected behavior
I expect to get a null IFileInfo? back if the input is null, and no exception thrown.

Additional context
Fixing this will help compatibility with System.CommandLine where defining options as FileInfo? is a common practice, i.e.:

var inputFileOption = new Option<FileInfo?>("--input");
var command = new Command("test") { inputFileOption };
command.SetHandler(
    (FileInfo? inputFile) => {
        var fileSystem = new FileSystem();
        var concreteCommand = new ConcreteCommand(fileSystem:fileSystem) {
            InputFile = fileSystem.FileInfo.Wrap(inputFile)
        }
        concreteCommand.Run();
    },
    inputFileOption
);

Activity

  1. vbreuss commented on Apr 22, 2023

    @vbreuss
    Member

    Thanks for creating this issue, @mikeeheler.
    I already created a pull request (#976) to fix this bug!

  2. added 2 commits that reference this issue on Apr 23, 2023
    1fcc5cd
    554e97c
  3. github-actions commented on Apr 24, 2023

    @github-actions

    This is addressed in release v19.2.17.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    state: needs discussionIssues that need further discussionstate: releasedIssues that are releasedtype: bugIssues that describe misbehaving functionality

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions