Skip to content

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

@mikeeheler

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
);

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