Skip to content

Behavior changed about filesystemInfo.Attributes in .NET Core preview2/3 causes 2 tests to fail in Get-ChildItem.Tests.ps1 #4145

Closed

Description

In 2.0.0-preview1 .NET Core, filesystemInfo.Attributes raises an FileNotFoundException exception when the file is deleted or renamed during the enumration.
In 2.0.0-preview2/3 .NET Core, filesystemInfo.Attributes returns -1 without raising any exception.
This behavior change was by design -- to align with the current behavior on Full .NET. For detailed information, please see https://github.com/dotnet/corefx/issues/20456.

This behavior change causes the following tests to fail in test/powershell/Modules/Microsoft.PowerShell.Management/Get-ChildItem.Tests.ps1 (they will be marked as pending for now):

  • "Should continue enumerating a directory when a contained item is deleted"
  • "Should continue enumerating a directory when a contained item is renamed"

These 2 tests were added in PR #3806, which assumes that the FileNotFoundException will be thrown in case the file is deleted or renamed during enumeration. Since the behavior has changed in .NET Core, the fix needs to be revisited.

/cc jeffbi

Activity

  1. added this to the milestone on Jun 30, 2017
  2. daxian-dbw commented on Jun 30, 2017

    @daxian-dbw
    MemberAuthor

    Since PR #3806 was to fix #2856, I set the properties of this issue similar to 2856.

  3. jeffbi commented on Jul 4, 2017

    @jeffbi

    What I'm seeing on both platforms is that the exception is no longer thrown, but FilesystemInfo.Attributes is not returning -1. Instead it returns the attributes the file had before it was deleted, so there is no real indication that the file was deleted.

    After the item is written to the pipeline, when it is displayed, the Mode property is null. Mode is not one of the regular properties of FileSystemInfo and is added after the provider writes the object.

    I think it might be worth keeping the provider code as is (I don't think the try/catch is doing us any harm) and change the test so that it ensures that the deleted file is included in the result of Get-ChildItem.

  4. jeffbi commented on Jul 4, 2017

    @jeffbi

    I had fetched the latest source (commit ece27ff), done a new Start-PSBootStrap and Start-PSBuild -clean. Is there something else I need to do?

  5. iSazonov commented on Jul 5, 2017

    @iSazonov
    Collaborator

    I meant that we should call Refresh() or Exists() to get fresh FileAttributes.

  6. jeffbi commented on Jul 5, 2017

    @jeffbi

    Ilya (@iSazonov) You were exactly right. I hadn't realized there was a Refresh() method on FilesystemInfo.

    Assuming we want to continue with the current behavior, i.e. writing an error message and continuing the enumeration, I'm preparing a PR to do that.

  7. daxian-dbw commented on Jul 5, 2017

    @daxian-dbw
    MemberAuthor

    That means calling Refresh() or Exists() on every file system item. How expensive is that?

  8. iSazonov commented on Jul 6, 2017

    @iSazonov
    Collaborator

    Yes, it is expensive. I think we should avoid this. Now we haven't exception so maybe silently ignore this?

  9. jeffbi commented on Jul 6, 2017

    @jeffbi

    If we don't refresh and check for -1, we should end up displaying the file as if it had not (yet) been deleted. Whether that's the right thing may depend on how the cmdlet is being used.

  10. iSazonov commented on Jul 6, 2017

    @iSazonov
    Collaborator

    The file system is constantly changing, so perhaps this is the expected behavior. We can also skip new files or changing their size if it happens later we read first the list.

  11. jeffbi commented on Jul 6, 2017

    @jeffbi

    I quite agree. So long as our users understand that the file system is this dynamic, then this should be the expected behavior.

    So my take is that we leave the cmdlet code as is, with no additional call to Refresh(), and modify the tests to verify that the cmdlet does not show an error but instead retrieves the file information it can.

  12. daxian-dbw commented on Jul 6, 2017

    @daxian-dbw
    MemberAuthor

    I don't think the try/catch is doing us any harm

    I quite agree that we should keep the try/catch. However, (btw I could be totally wrong) should we keep the test hooks? Here are my thoughts:

    1. the root cause of the original issue is gone (no exception now),
    2. the test hooks won't give us code coverage for the catch blocks
    3. we pay a tax of doing two checks for every file system item.

    jeffbi Ilya (@iSazonov) what are your thoughts?

  13. jeffbi commented on Jul 6, 2017

    @jeffbi

    If we remove the test hooks, the tests themselves don't make much sense. That being said, I'm in favor of removing the test hooks and the tests.

  14. iSazonov commented on Jul 7, 2017

    @iSazonov
    Collaborator

    I agree to remove the test hooks and tests:

    1. CoreFX wants to be compatible with .Net Framework so no exception will be in future (but we have still try/catch). Maybe we should just add a comment about it.
    2. "Race conditions" for file system is normal (multiple processes add, remove and change files, permissions and attributes) and we don't need to test and to worry about this.
  15. daxian-dbw commented on Jul 7, 2017

    @daxian-dbw
    MemberAuthor

    Thanks jeffbi and Ilya (@iSazonov), then let's remove the test hooks and the corresponding tests.

  16. SteveL-MSFT commented on Aug 1, 2017

    @SteveL-MSFT
    Member

    Dongbo Wang (@daxian-dbw) is there anything more to do with this one?

  17. daxian-dbw commented on Aug 1, 2017

    @daxian-dbw
    MemberAuthor

    Nothing needs to be done. This issue was not closed when merging #4200

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

    Resolution-FixedThe issue is fixed.WG-Engine-Providersbuilt-in PowerShell providers such as FileSystem, Certificates, Registry, etc.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions