# Why can't the same filename be used twice

**URL:** <https://forums.swift.org/t/why-cant-the-same-filename-be-used-twice/69791>\
**Category:** Using Swift\
**Tags:** packagemanager\
**Created:** [February 2, 2024, 10:11am UTC](https://forums.swift.org/t/why-cant-the-same-filename-be-used-twice/69791 "2024-02-02T10:11:21Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![Lancelotbronner](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lancelotbronner/32/22071_2.png) [@Lancelotbronner](https://forums.swift.org/u/Lancelotbronner)\
**Post date:** [July 4, 2025, 9:06pm UTC](https://forums.swift.org/t/why-cant-the-same-filename-be-used-twice/69791/22 "2025-07-04T21:06:42Z")

</div>

> [@jrose](#):
>
> Can still lead to very subtle errors.

I think there's a point where we can accept that everyone else is doing it and if they're not having issues with it maybe we'll be fine.

Also if we store them in caches and such by relative path, which we should've always done, this is a non-issue.  
If someone is using `#fileID` for something critical instead of just debugging then it was probably a bad idea to begin with.

---

<div class="post-metadata">

**Author:** ![nicksloan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nicksloan/32/35193_2.png) [@nicksloan](https://forums.swift.org/u/nicksloan)\
**Post date:** [July 5, 2025, 3:58am UTC](https://forums.swift.org/t/why-cant-the-same-filename-be-used-twice/69791/23 "2025-07-05T03:58:28Z")

</div>

Would a compiler flag be a reasonable option here? It could be introduced off by default, allowing people to opt in to the multi-file behavior, and then eventually be switched to enabled by default. Library code like NSCoding could warn when the flag is enabled to alert users of the potential for data loss or other consequences.

A flag obviously comes with a maintenance burden, but it is something that could put the language on a path to phasing out unwanted old behavior.

---

<div class="post-metadata">

**Author:** ![Lancelotbronner](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lancelotbronner/32/22071_2.png) [@Lancelotbronner](https://forums.swift.org/u/Lancelotbronner)\
**Post date:** [July 7, 2025, 2:06pm UTC](https://forums.swift.org/t/why-cant-the-same-filename-be-used-twice/69791/24 "2025-07-07T14:06:19Z")

</div>

It's used by `NSCoding`? That explains why it matters so much, and why I never encountered it!

Thank you for that insight!

---

<div class="post-metadata">

**Author:** ![nicksloan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nicksloan/32/35193_2.png) [@nicksloan](https://forums.swift.org/u/nicksloan)\
**Post date:** [July 7, 2025, 4:52pm UTC](https://forums.swift.org/t/why-cant-the-same-filename-be-used-twice/69791/25 "2025-07-07T16:52:35Z")

</div>

That came from @jrose's blog posts. One of the difficulties of changing how names are mangled is that the mangled names have been used in archives created with NSCoding, which means the mangled names have trickled all the way to end-user space. It sounds like great pains have to be taken to make changes to the mangled names to avoid end-user facing breakages.

It's a real bummer of a situation.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [July 7, 2025, 5:09pm UTC](https://forums.swift.org/t/why-cant-the-same-filename-be-used-twice/69791/26 "2025-07-07T17:09:50Z")

</div>

> [@jrose](#):
>
> Can still lead to very subtle errors. What if you have three files with the same name, and then over time delete one, and then another?

There could be a more of less safe way out:

1. the project doesn't use duplicated file names.  
Let's consider it doesn't use NSCoding (we could consider that use case later)
2. a file with a duplicated name is introduced to the project.
3. at this point Xcode complains and prompts: "you've just added a file with a duplicated name. That's an error unless you enable this new compiler flag"
4. user enables that compile flag - Xcode now allows the compilation.
5. user adds NSCoding - archives are now archived using a new method because of the new compiler flag
6. user deletes the duplicated file name (or renames the file)
7. archives are still using the new method of archiving (due to the setting that is still on)
8. Only when the user switches that compiler flag off – there would be a trouble.

IOW there could be a new compilation flag that affects NSCoding archives, and duplicated file names are only allowed when this compilation flag is on.

---

<div class="post-metadata">

**Author:** ![nicksloan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nicksloan/32/35193_2.png) [@nicksloan](https://forums.swift.org/u/nicksloan)\
**Post date:** [July 7, 2025, 6:25pm UTC](https://forums.swift.org/t/why-cant-the-same-filename-be-used-twice/69791/27 "2025-07-07T18:25:37Z")

</div>

That's pretty much what I was thinking.

Bonus points for a migration strategy for old use cases like existing NSCoding archives. Bonus bonus points if we can ensure that the new mangled names can no longer trickle into user space.

---

<div class="post-metadata">

**Author:** ![nicksloan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nicksloan/32/35193_2.png) [@nicksloan](https://forums.swift.org/u/nicksloan)\
**Post date:** [December 7, 2025, 3:09pm UTC](https://forums.swift.org/t/why-cant-the-same-filename-be-used-twice/69791/28 "2025-12-07T15:09:30Z")

</div>

I think the real solution here is to hard break the existing code. Hear me out.

NSCoding uses mangled file names in its output. That means that renaming a source file will break those archives too. That’s a really unexpected behavior that should be recognized as a bug. Any other code that uses the mangled names may have similar bugs. We should make the mangled names internal to the compiler and break all of this buggy code.

Effected libraries should change to either reimplement the mangled names or replace them with something more sensible.

In this approach no one will break user data by building on a later swift version, but they (or the libraries developers they depend on) will have to make a choice about how to proceed.

We punt this problem to user space where it belongs and can unshackle the compiler.

I really don’t think there is a better solution, but the current situation is a house of cards and must be addressed.

---

<div class="post-metadata">

**Author:** ![Lancelotbronner](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lancelotbronner/32/22071_2.png) [@Lancelotbronner](https://forums.swift.org/u/Lancelotbronner)\
**Post date:** [December 9, 2025, 5:41pm UTC](https://forums.swift.org/t/why-cant-the-same-filename-be-used-twice/69791/29 "2025-12-09T17:41:59Z")

</div>

> [@nicksloan](#):
>
> I think the real solution here is to hard break the existing code. Hear me out.

I very much agree with this suggestion.

[Previous page](https://forums.swift.org/t/why-cant-the-same-filename-be-used-twice/69791.md?page=1)
