So if I create an alias of a folder in Finder and hand the alias to my app (I also moved the alias file to a new folder, but I did not move the original folder)...
Please file a bug on this and then post the bug number back here. I can explain what's going on, but URLByResolvingAliasFileAtURL should always resolve to the same location as the Finder.
Only seems to happen if the folder I move the alias to happens to contain a sibling folder that has the same title as the original folder.
So, making things concrete, the actual issue here is the following configuration sequence:
(1)
Create an alias file to the target "beside" that target:
target
target alias
NewDir
target
(2)
Move the object into a new directory "beside" another object with the same name as the target:
target
NewDir
target alias
target
What's going on here is that bookmark resolution is driven by heuristics, one factor of which is the relative position of the bookmark and its target. Those heuristics are what allow resolution to succeed even when the original target object no longer exists— for example, when a directory is deleted and then recreated or when an alias and its target are moved to a different volume.
The problem here is that the Finder and URLByResolvingAliasFileAtURL aren't using identical heuristics (and they should be). You can see this for yourself if you start with the configuration above, starting with URLByResolvingAliasFileAtURL resolving to this:
.../NewDir/target
NOW, rename that object so you end up with this:
target
NewDir
target_old
target alias
And resolution now returns:
.../target
Note that this isn't actually about a simple name match either, as this configuration:
foobar
NewDir
target_old
target alias
Resolves to this:
.../foobar
But then changing to this:
foobar
NewDir
target
target alias
Goes back to this:
.../NewDir/target
In other words, the issue isn't that URLByResolvingAliasFileAtURL can't find the target, it's that it's preferring the "local" name match over the other resolution path.
So then once my app touches the alias with +URLByResolvingAliasFileAtURL: the alias now resolves to this new (wrong) location (even in Finder).
Is your code writing out new alias files for stale files (which would do exactly what you're describing)?
If not, can you post the EXACT steps you did to make this happen? I've replicated the general issue you've described, but I haven't been able to get the Finder to switch resolve targets.
So it appears to be an issue with the way Finder makes aliases.
I haven't looked at the code in depth but I wouldn't be surprised if the Finder was writing a slightly different format. The term "alias" actually comes from an original, classic macOS, data format. Bookmarks replaced that format in ~10.6, but I wouldn't be surprised if the Finder still had some level of support to provide backward compatibility.
__
Kevin Elliott
DTS Engineer, CoreOS/Hardware