The current implementation does not attempt to repair the string if it isn't valid Unicode.
As I mentioned in the alternatives considered section, I'm anticipating that most consumers of this API will immediately shove it in a String if it isn't already a String. We do that with the program's command-line arguments (CommandLine.arguments) too, and there's a good chance that the argument list of a program chosen at random will include one or more file system paths.
It doesn't feel quite right to me to offer an additional executablePathCString: ContiguousArray<CChar>-shaped property, but it's easy enough to do so if the community needs it. See also my comments about swift-system below.
My intent is that you'll get back an absolute path. There may be platform-specific corner cases I'm not aware of where you'll get back something else, so please file bugs if you run into such problems.
Microsoft's documentation states:
The string returned will use the same format that was specified when the module was loaded. Therefore, the path can be a long or short file name, and can use the prefix "?". For more information, see Naming a File.
I'm not sure if that answers your question?
This is not part of the proposal, but it's my opinion that the standard library should include a type representing a file path distinct from a Unicode string. If there were such a type, it would definitely change the calculus here. ![]()
swift-system is fairly high in the stack and not part of the (public interface of the) Swift toolchain. That means that components in the toolchain can't rely on it being present and can't link to it. There are multiple components in the toolchain that do need the executable path. The ones I know of off the top of my head are:
- Foundation
- Swift Testing
- Swift Argument Parser
- Swift Package Manager
These components can't link to swift-system, so each one needs to implement its own logic to find the executable path. A single implementation in the standard library would allow each of these components to share a common implementation, bug fixes and all.
I'm aware there's a pitch up to move swift-system into the toolchain. If executablePath is in the standard library, swift-system would be able to expose it as a FilePath too. Changes to swift-system are not part of this proposal, but let's assume that as a toolchain component, swift-system could get the value without a trip through Unicode Land.