Search code examples
xcodepathdirectory-structurepbxproj

Xcode Locations Don't Work Properly


Whenever I add a new file to the project, it is added with the default location of "Relative to Group" and a problematic path of (for example):

../../../../../MyUsername/Folder/Foo/Foo/Foo/Bar.swift

It's not as if I put the file in another user's directory. In this case, ../myUsername resolves to .. Same with every other double dot. You could simplify it as follows:

  1. ../../../../../MyUsername/Folder/Foo/Foo/Foo/Bar.swift
  2. ../../../../Folder/Foo/Foo/Foo/Bar.swift
  3. ../../../Foo/Foo/Foo/Bar.swift
  4. ../../Foo/Foo/Bar.swift
  5. ../Foo/Bar.swift
  6. Bar.swift

Because it literally is going down the path then going back up the same way. It is equivalent to running cd ../; cd ~- in a shell.

I expected the path to be simply Bar.swift. This causes many problems when using shared code because other people have the project in different places with different usernames.

The only solution I have found so far is manually editing the project.pbxproj file. What is causing this problem and what should I do to fix it?

Note: I suspect that this might have to do something with the case of the username due to the fact that the username is as far back as the path goes.

EDIT: Here is a screenshot (red is my username, and green is the app name):

Screenshot showing the path explained above and the real path (exactly the same, except it is <code>/Users/MyUsername/</code>...

Also, cding to the directory and running realpath ../../../../../MyUsername/Folder/Foo/Foo/Foo/Bar.swift returns Bar.swift.

EDIT 2: My suspicions were right about the username being case-sensitive! Look what happens when I set the Location to Absolute Path:

Lowercase, down tree, uppercase. Full path is uppercase.

Previously, the username was only Title case (../MyUsername, /Users/MyUsername, etc.). However, now Xcode seems to go:

  1. Down the directory tree from the lowercase username (to the group folder)...
    1. Past the directory containing the project...
    2. Into the directory containing the file...
  2. Back up to the Users directory...
  3. Down the same path with the Title case username.

I think that this is a bug in Xcode. If I had to guess, what happens internally is:

  1. Xcode gets the absolute path to the file
    • From a file picker while adding
    • From a file picker while relocating by pressing the folder icon in the Location section
    • Somehow from creating a new file(?)
  2. This path uses the Title case username.
  3. Xcode gets the absolute group path
    • From its records (project.pbxproj?)
  4. For some reason, this path uses the lowercase username
  5. Xcode tries to figure out the path from #1 relative to the path from #2.
    1. For example, if the path to the file was /foo/bar/baz.swift, and the group was /foo, the intended result is bar/baz.swift.
    2. Xcode starts at the absolute path to the group (because the end path should be relative to the group).
    3. Xcode looks at both paths and compares them. /Users/myusername/Folder/Foo/Foo/Foo vs /Users/MyUsername/Folder/Foo/Foo/Foo/Bar.swift.
    4. Xcode sees that the only differences are:
      • At the end of the path (one has the filename and the other does not)
      • In the username (myusername vs MyUsername)
    5. Xcode moves up to the farthest problem up the file tree using the double dot (../). /Users/myusername/Folder/Foo/Foo/Foo/../../../../.., or simply /Users.
    6. Xcode starts down again, but this time uses the remaining portion of the absolute path for the file (Xcode never goes back down to /Users because it is the same for both paths).
    7. Xcode continues all the way to the filename, using the Title case username. /Users/myusername/Folder/Foo/Foo/Foo/../../../../../MyUsername/Folder/Foo/Foo/Foo/Bar.swift.

When location is set to Relative to Group, the path to the group is implied, so the previous path simply becomes:

../../../../../MyUsername/Folder/Foo/Foo/Foo/Bar.swift.

Familiar?


Anyways, how do I fix that?????


Solution

  • It turns out that the problem was that I had my home directory path set to a different capitalization than the actual folder.