Sublime Forum

Use of relative path in .sublime-project files

#1

hello SublimeText Forum members!

SublimeText automatically adds folders to the project using absolute paths. I am manually editing this file which is working great with relative paths. However, ST3 overrides this file and converts all path into absolute paths once I add a folder to the project using the GUI. Also, it gets rid of any comment I placed there :frowning:

Using relative paths is very important to me since ST3 is cross platform and also portable.

Not sure if there is currently a workaround? If not I would suggest this as a feature request.

0 Likes

#2

Sublime doesnā€™t always add folders to projects with absolute names; from observation it looks like it tries to keep the paths that it adds relative to the location that the sublime-project file is stored in, but if that location isnā€™t a common ancestor (i.e. the path needs to include ../) it swaps it to an absolute path instead. Though as youā€™ve noticed it will work with such a path on its own.

The canonical example of this is that if you create a sublime-project in a folder and then add that folder to the project, the path is always ..

As another example, if the project is /home/tmartin/sample.sublime-project, any folder that I add thatā€™s somewhere in my home directory is automatically relative to /home/tmartin, but adding ../../tmp/bob gets rewritten to /tmp/bob. Similarly on Windows, with sample.sublime-project sitting on my desktop, folders somewhere on my desktop are relative, but other paths are rewritten.

As such, you can potentially partially solve your problem by changing the location that you store your sublime-project files and putting them in a location thatā€™s a common ancestor to all of the paths youā€™re adding, although this is not generally a particular neat solution.

As far as I know, the only workaround to having Sublime automatically rewrite your project file is to not take actions that cause Sublime (or some plugin) to try and modify the project data on your behalf. In particular that means:

  • When you want to add a new folder to the project, you should use Project > Edit Project to open the project file and manually add an entry to the folders key.
  • If you want to change the order the folders appear in the side bar, use the same as the above to manually shift the entries instead of dragging and dropping them.
  • Be wary of plugins that might touch your project for you, say by allowing you to add exclusion patterns to your project (I think SideBarEnhancements does this for example, but I canā€™t recall for sure).
0 Likes

#3

hello @OdatNurd,

Thank you for your reply. I can recreate this behavior based on your description on Linux. However, it still happens sometimes (on Linux) that ALL relative paths are recreated as absolute, even if thereā€™s a common ancestor.

This is very smart of ST. However, as a feature request, (and without knowing the nitty gritty details under the hood) I would suggest that the evaluation to swap should not perform on existing paths.

Since I am aware of this limitation I successfully applied your suggested workarounds for many years now. In very rare cases I change this by accident.

On Windows it is not behaving as you describe:

Test environment:

OS: Windows 8.1
ST3, Build 3143, Build 3176

Observation

ST3 recreates project paths as absolute / relative based on its own criteria.
Paths that are of one type can be changed into another. These changes are not
persistent, however.

Consider the following project (testcase 1):

{
    "folders":
    [
        {
            "path": "../../../Project"
        },
        {
            "path": "../../../Project/doc"
        },
        {
            "path": "../../../utils"
        }
    ]
}

The paths had been manually defined. These MUST remain relative in order to
use them on multiple platforms / root directories.

Now, add a new folder to the project, ā€œsrcā€:

  1. Project -> Add Folder to Projectā€¦
  2. navigate to ā€¦/ā€¦/ā€¦/Project/src
  3. press Ok

The folder paths result in the following:

{
    "folders":
    [
        {
            "path": "C:\\test\\testcase\\Project"
        },
        {
            "path": "C:\\test\\testcase\\Project\\doc"
        },
        {
            "path": "C:\\test\\testcase\\utils"
        },
        {
            "path": "C:\\test\\testcase\\Project\\src"
        }
    ]
}

ST3 only creates relative paths if sample.sublime-project is the root directory (consider testcase 2 in Attachments).

It would be nice if ST3 preserves paths as relative that are 2 or even 1 level below ``${project_path}.

Attachment

0 Likes

#4

Iā€™d buy that; my own evidence is ancedotal at best. My guess would be that the project data as loaded from the file is stored in memory in a format other than how it appears in the file, so when it needs to recreate the file thereā€™s no guarantee that the result is identical even if it still logically works the same way.

To be fair my use case is to keep the sublime-project file at the root of all of the folders that the project contains and inside source control, so once the project is set up it never really changes for me (even when I switch between Linux, MacOS and Windows), and being in source control allows me to detect the rare case where something happens to it.

To my memory Iā€™ve never seen the folders in the list change, and in fact itā€™s very rare that itā€™s not just ..

2 Likes

#5

Someone could move this post into ā€œIdeas and Feature Requestsā€, please.

0 Likes

#6

Iā€™m having this same problem where my sublime project file is getting modified. Sometimes gets quite annoying actually. I feel this is more of a ā€œbugā€ than a ā€œfeatureā€.

Iā€™ve written up my project file settings, and sublime just modified it for some odd reason. Would it be possible if this behavior can be revisited, and possibly fixed somehow?

Thanks for your attention all!

0 Likes

#7

use the package Project Manager and you get three advantages:
1.just save it project saved, you donā€™t need to care about saving project file
2.you have an option to open project in current project or in a new project
3.switching project and you get open status and edting satus, it wonā€™t lost.

0 Likes