Skip to content

FileSystemAccess.fileExists returns true for directory as well #1534

Closed

Description

@atanasovg

Since in Java a File object represents both a directory and a file, we need to check not only for exists but for isDirectory as well:
https://github.com/NativeScript/NativeScript/blob/master/file-system/file-system-access.android.ts#L78

Activity

  1. self-assigned this
    on Feb 11, 2016
  2. added this to the 1.6.0 milestone on Feb 11, 2016
  3. reopened this on Feb 11, 2016
  4. enchev commented on Feb 11, 2016

    @enchev
    Contributor

    Hey folks,

    It seems we were too fast! :)

    File.exists will return true for folder in both iOS and Node.js as well. It seems that this is the expected behavior!

    [UPDATE]. Finally we've decided that it will be better to return false on folder!

  5. cokobware commented on Feb 17, 2016

    @cokobware

    Thanks for fixing this guys!

  6. lhitc commented on Feb 17, 2016

    @lhitc

    This is completely contrary to virtually every other programming language or framework, not only Java or Node.js.
    The various "exists" functions check for existance regardless of file type, and directories are just files of a special type. Typically you check that at the second stage.

    So, this breaks intuitively expected behaviour and possibly also some already existing code out there.

    Just my 5 cent.

  7. NathanaelA commented on Feb 17, 2016

    @NathanaelA
  8. enchev commented on Feb 18, 2016

    @enchev
    Contributor

    Well, I was afraid of this. :) Possible option will be to restore the original behavior and provide additional optional parameter with true as default value:

     export class File extends FileSystemEntity {
            static exists(path: string, includeFolder?: boolean): boolean;
    }

    In this case you will be able to set this to false if you want to check only for files not for folders.

    var fileExists = fs.File.exists(path, false);

    Hey @atanasovg, @cokobware, what do you think?

  9. reopened this on Feb 18, 2016
  10. lhitc commented on Feb 18, 2016

    @lhitc

    If you want to implement both variants for convenience, it might be more consistent to have 3 different methods, like:
    static exists(path: string): boolean; // classic behaviour
    static isExistingRegularFile(path: string): boolean; // true if plain file
    static isExistingFolder(path: string): boolean; // true if folder

  11. atanasovg commented on Feb 18, 2016

    @atanasovg
    ContributorAuthor

    @lhitc .NET for one will NOT return true for a directory: https://msdn.microsoft.com/en-us/library/system.io.file.exists(v=vs.110).aspx. But I guess our thinking is biased due to the fact that many of the core engineering team have extensive experience with .NET.

    @enchev Given the default behavior on Node, iOS and Android is to return true for a simple exists check then any arbitrary parameters/methods added to the API seem artificial to me. If it is important for the user to check for a file explicitly he may do something like

    if(File.exists(path) && !Folder.exists(path)) {
    }
    

    So, my vote goes for reverting the behavior to its previous state.

  12. enchev commented on Feb 18, 2016

    @enchev
    Contributor

    I'm fine for reverting as well. Hey @cokobware what do you think?

  13. lhitc commented on Feb 18, 2016

    @lhitc

    @atanasovg Thanks for expanding my horizon. I admit I only checked with Java, PHP, Python, Ruby, Perl, classic C and a couple of web frameworks, which all seem to rely on the stat() system call.
    So make that "a majority of programming languages and frameworks, especially in the context of web development" ;-)

  14. cokobware commented on Feb 18, 2016

    @cokobware

    I suppose reverting is fine. Having said that, like most of the NS team, my
    experience is with .NET too and I find this result surprising given my
    experience. I guess it feels very *nixy, which is fine if that is what I
    was expecting. It makes sense to revert if that's how Node.js does it.
    Perhaps a compromise would be to enhance the documentation to point out the
    actual behaviour?

    On Thu, Feb 18, 2016, 8:33 AM Vladimir Enchev notifications@github.com
    wrote:

    I'm fine for reverting as well. Hey @cokobware
    https://github.com/cokobware what do you think?

    —
    Reply to this email directly or view it on GitHub
    #1534 (comment)
    .

  15. awebi commented on Oct 12, 2018

    @awebi

    So what is the solution to check if "isDirectory"?

  16. lock commented on Oct 12, 2019

    @lock

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  17. locked and limited conversation to collaborators on Oct 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions