Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

TextEditor.getPath only providing realpath #9879

Closed

Description

@dirk-thomas

Currently TextEditor.getPath returns the realpath of the file. When a project directory contains symlinks somewhere under it it becomes impossible to decide which project root a specific file belongs to. Please see noseglid/atom-build#245 for a longer explanation of the use case as well as an example folder hierarchy.

The only way to make a correlation between a TextEditor and a project root requires the TextEditor to provide the non-real path of the file.

Getting the realpath from the non-real path is trivial (fs.realpath())) but the other way around is simply not possible.

Activity

  1. thedaniel commented on Dec 3, 2015

    @thedaniel
    Contributor

    @atom/feedback thoughts on this? I'm not familiar with a foolproof way to associate a TextEditor to a project path - I can see the benefit of adding a getter for something like nonRealPath / literalPath (naming is hard), or perhaps even better, a method that returns the project root, if any, that the file in the editor belongs to. My gut tells me that a lot of people are inferring project root from the file path.. i certainly have in packages before.

  2. lee-dohm commented on Dec 3, 2015

    @lee-dohm
    Contributor

    If we could get the project root and project-relative path (both I think would be useful in other contexts too) from a TextEditor, that should fix this issue I think?

    But this is going to be a really, really deep rabbit hole, I feel. The problem is that there can be an infinite number of paths that can reach a file. Should the TextEditor instance have to know about all of them? What if one of the n paths changes after the TextEditor is created? Say one of the non-real (imaginary?) paths to the file in question is a/foo/c.txt and gets renamed to a/bar/c.txt. Do we have to have file watchers on everything to manage this?

  3. maxbrunsfeld commented on Dec 3, 2015

    @maxbrunsfeld
    Contributor

    I agree with @dirk-thomas that we should simply not compute the real path, and leave that to the caller. Does anyone know of circumstances where that would break things in a way that wasn't trivially fixable?

  4. lee-dohm commented on Dec 4, 2015

    @lee-dohm
    Contributor

    Does anyone know of circumstances where that would break things in a way that wasn't trivially fixable?

    I'm pretty sure it was this way to begin with (because it was easier), but it changed for some reason ¯_(ツ)_/¯ I'll see if I can backtrace it.

  5. lee-dohm commented on Dec 4, 2015

    @lee-dohm
    Contributor

    So I devised a test just to see how consistent this is:

    1. Created a new repository https://github.com/lee-dohm/test-atom-9879
    2. Created a directory real-dir inside it
    3. Created a symlink to real-dir named fake-dir
    4. Created a file inside real-dir named test.md

    Then I attempted to open test.md in multiple ways from both real-dir and fake-dir and recorded their getPath() result with the command: atom.workspace.getActiveTextEditor().getPath(). Here are the results:

    • real-dir/test.md
      • Open from tree-view: /Users/Lee/Source/test-atom-9879/real-dir/test.md
      • Open from fuzzy-finder: /Users/Lee/Source/test-atom-9879/real-dir/test.md
      • Open from Cmd+O: /Users/Lee/Source/test-atom-9879/real-dir/test.md
      • Open via drag-and-drop: /Users/Lee/Source/test-atom-9879/real-dir/test.md (New window appears)
    • fake-dir/test.md
      • Open from tree-view: /Users/Lee/Source/test-atom-9879/real-dir/test.md
      • Open from fuzzy-finder: Does not show up in list
      • Open from Cmd+O: /Users/Lee/Source/test-atom-9879/real-dir/test.md
      • Open from drag-and-drop: /Users/Lee/Source/test-atom-9879/real-dir/test.md (New window appears)

    Ok, 100% consistent. Which tells me that the code for all of this is almost assuredly in one place ... most likely the opener mechanism.

    So then I delved into the Wayback Machine and followed the opener code all the way back to November 2013. As far as I can see, the opener code (which migrated between Project and Workspace over the span) has always* called Project::resolvePath or Project::resolve on the path passed to it. And the resolve twins have always* called fs.absolute() on the path passed to them. Which you can still see here:

    atom/src/project.coffee

    Lines 212 to 219 in 2696f49

    if fs.isAbsolute(uri)
    path.normalize(fs.absolute(uri))
    # TODO: what should we do here when there are multiple directories?
    else if projectPath = @getPaths()[0]
    path.normalize(fs.absolute(path.join(projectPath, uri)))
    else
    undefined

    So I was wrong and completely fabricated my previously mentioned memory.

    Now, where does that leave us? Well, it appears that Project::resolvePath has never been part of the public API and Project::resolve was deprecated back on January 9th, 2015. So at least we don't have to worry about third-party packages depending on this as an API. But since TextEditors have always* returned absolute paths ... we may want to be careful about breaking that implicit contract.

    * For values of "always" back to November 2013

  6. dirk-thomas commented on Dec 5, 2015

    @dirk-thomas
    ContributorAuthor

    I do understand that changing the existent behavior is risky. But do we agree that we should somehow expose the non-realpath with a different method (however clumsy the name might be)?

    Regarding the absolute() function in fs-plus: I find it rather unintuitive that it resolves symlinks. Based on how similar functions in other languages work (e.g. in Python: https://docs.python.org/3/library/os.path.html#os.path.abspath) I expect the function to:

    • prefix a relative path with a "cwd"
    • collapse .. within the path (normalize X/path/../Y to X/Y)
    • replace stuff like ~

    But I would not expect the function to perform a realpath().

  7. lee-dohm commented on Dec 5, 2015

    @lee-dohm
    Contributor

    But do we agree that we should somehow expose the non-realpath with a different method (however clumsy the name might be)?

    Sure, we should. The question is when? Is this no big deal and we can take it now? Or is it something that should wait until v2.0?

  8. dirk-thomas commented on Dec 5, 2015

    @dirk-thomas
    ContributorAuthor

    Obviously I would like to have the feature rather sooner then later 😉 in order to actually make the build commands (referenced in the description) work. Without this feature there is simply no way to make it work for projects with symlinks. Out of curiosity: what is the rough time frame for Atom 2.0?

    Let me try to describe what changes would be required (also for me to understand the Atom internals a bit better) and what that might affect:

    • Modify Project.resolvePath() to return non-realpath but normalized absolute path instead.
    • Consider the current usage of the function:
      • Project.isPathModified() depends on what Buffer.getPath() returns. If it will return a non-realpath nothing needs to be done. Otherwise Project.isPathModified() needs to invoke realpath on the argument before passing it to findBufferForPath().
      • Project.bufferForPathSync() same as for the previous bullet.
      • Workspace.open() this is where we actually want the non-realpath since from here the uri / path gets passed throughout the pipeline until it ends up in the TextEditor.
        • The function also determines if the file is already open in a pane (itemForURI()). This is based on TextBuffer.getUri() which uses getPath(). If getPath() continues to return a realpath the argument to itemForURI() needs to be a realpath.
      • Workspace.openSync() same as for the previous bullet.
      • Workspace.openTextFile() calls fs.getSizeSync() which might need a realpath (?).
      • Is there a way to find out if other code invokes this non-public API?
    • Modify TextEditor.getPath() to call realpath() before returning the value and introduce a new method TextEditor.getNonRealPath().

    If we ensure that existing API like TextEditor.getPath() or TextBuffer.getPath() still return the same realpath as before this might be a backward compatible change? I am happy to hear your thoughts...

  9. dirk-thomas commented on Dec 20, 2015

    @dirk-thomas
    ContributorAuthor

    It would be great if someone could provide guidance regarding my proposed changes. I am happy to work on a patch if the direction is clear.

  10. winstliu commented on Dec 22, 2015

    @winstliu
    Contributor

    Was going through a few old issues and noticed this one that seemed really similar: #3110.

  11. dirk-thomas commented on Jan 13, 2016

    @dirk-thomas
    ContributorAuthor

    ping 😉

  12. joefitzgerald commented on Jan 29, 2016

    @joefitzgerald
    Contributor

    @50Wliu yep, this is exactly what #3110 is about. I'm going to close it in deference to this.

  13. dirk-thomas commented on Sep 7, 2016

    @dirk-thomas
    ContributorAuthor

    I have updated #10856 to provide additional API exposing the non-realpath. The existing API should not change behavior (except that a TextBuffer now internally stores the non-realpath). Any feedback is welcome.

  14. maxbrunsfeld commented on Jan 5, 2017

    @maxbrunsfeld
    Contributor

    This will be fixed in Atom 1.14. We'll preserve whatever path the project was initialized with. Sorry for the very very long turnaround time.

  15. dirk-thomas commented on Jan 5, 2017

    @dirk-thomas
    ContributorAuthor

    👍 for supporting this with the current API without the need for additional API.

  16. lock commented on Apr 3, 2018

    @lock

    This issue has been automatically locked since there has not been any recent activity after it was closed. If you can still reproduce this issue in Safe Mode then please open a new issue and fill out the entire issue template to ensure that we have enough information to address your issue. Thanks!

  17. locked and limited conversation to collaborators on Apr 3, 2018
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions