Skip to content

Make System.OsString.Posix.fromBytes and Systen.OsString.Windows.encodeFS total #6

Closed

Description

@Bodigrim

Currently we have symmetric types:

System.OsString.Posix.fromBytes :: MonadThrow m => ByteString -> m PosixString
System.OsString.Windows.fromBytes :: MonadThrow m => ByteString -> m WindowsString

System.OsString.Posix.encodeFS :: String -> IO PosixString
System.OsString.Windows.encodeFS :: String -> IO WindowsString

It's not uncommon to work on a single-platform application or an area, which utilises only one flavour of OsString on both platforms. Recent work on tar is a prominent example, where PosixString is used as an internal representation even on Windows.

IMO it would be better to break the symmetry and provide

System.OsString.Posix.fromBytes :: ByteString -> PosixString
System.OsString.Windows.encodeFS :: String -> WindowsString

The lack of total conversions is the biggest friction point in my experience so far; if we can alleviate it at least in some scenarios, it's worth doing.

Activity

  1. hasufell commented on Dec 12, 2023

    @hasufell
    Member

    This would be a breaking change.

  2. hasufell commented on Dec 12, 2023

    @hasufell
    Member

    How would we migrate or deprecate? This is invasive and will require another major bump for two packages, as well as patches for directory at least, potentially with CPP.

  3. Bodigrim commented on Dec 13, 2023

    @Bodigrim
    ContributorAuthor

    I'm not attached to names. Maybe just add two new functions:

    System.OsString.Posix.fromByteString :: ByteString -> PosixString

    and, say,

    System.OsString.Windows.fromString :: String -> WindowsString

    The latter function suggests that there could exist a well-behaved instance IsString WindowsString...

  4. hasufell commented on Dec 15, 2023

    @hasufell
    Member

    It's probably no secret that I am no friend of IsString class: it has bugs and unclear semantics. So I'd rather avoid it, otherwise people will get hopes that OsString may get it.

    Instead, GHC SC has to come up with a version of OverloadedStrings that is not broken.

  5. Bodigrim commented on Dec 15, 2023

    @Bodigrim
    ContributorAuthor

    Fair enough. I'm not wed to any particular names, anything with pure type signature will do.

  6. added a commit that references this issue on Dec 26, 2023
    5e50cfd
  7. hasufell commented on Dec 26, 2023

    @hasufell
    Member
  8. BurningWitness commented on May 17, 2024

    @BurningWitness

    It's probably no secret that I am no friend of IsString class: it has bugs and unclear semantics. So I'd rather avoid it, otherwise people will get hopes that OsString may get it.

    Would IsString OsString be about as unsafe as IsString Text currently is or am I missing some massive usage caveat?

  9. hasufell commented on May 17, 2024

    @hasufell
    Member

    It's probably no secret that I am no friend of IsString class: it has bugs and unclear semantics. So I'd rather avoid it, otherwise people will get hopes that OsString may get it.

    Would IsString OsString be about as unsafe as IsString Text currently is or am I missing some massive usage caveat?

    There are two caveats:

    1. OsString is not guaranteed to be unicode (unlike Text). So the IsString instance conveys a false sense of "this type is unicode-ish" (although the osstr quasi-quoter encodes as UTF-8/16, but it's more explicit, easier to document and can reject surrogates)
    2. Yes, it has the surrogate problem as well
  10. BurningWitness commented on May 17, 2024

    @BurningWitness

    OsString is not guaranteed to be unicode (unlike Text).

    But encoding itself is still deterministic on a given platform, so it is possible to make a total conversion function that encodes surrogates into either other surrogates or replacement characters, right?

  11. hasufell commented on May 17, 2024

    @hasufell
    Member

    OsString is not guaranteed to be unicode (unlike Text).

    But encoding itself is still deterministic on a given platform, so it is possible to make a total conversion function that encodes surrogates into either other surrogates or replacement characters, right?

    I'm not sure what definition of "total" you are proposing, but encode . decode == id does not hold.

    What does "other surrogates" mean? Proper UTF-8 does not allow surrogate range. If you're proposing PEP-383, then the unicode consortium has already voiced its displeasure: https://unicode.org/L2/L2009/09236-pep383-problems.html

  12. BurningWitness commented on May 17, 2024

    @BurningWitness

    The point I'm leading to is that IsString, while being a runtime-evaluated codec typeclass (bad in three different ways, I fully agree), is overwhelmingly only used for hardcoding strings. So in spirit the encode . decode == id rule only matters in regards to encoding valid Unicode points, handling invalid ones may as well be considered undefined behavior (though for performance reasons it should probably encode invalid points into other invalid points as it sees convenient).

    The current lack of an IsString OsString instance means that instead of a pleasant "honk" I have to input either [osstr|honk|] or unsafeEncodeUtf "honk", and it gets even stupider with lists. There is no upside to this, other than perhaps confusing newcomers, who most probably don't even know what UTF surrogates are.

    So ideally in a proper API I would expect:

    • An IsString instance with documentation stating usafe limits and that manual fromString use is never needed;

    • unsafeEncodeUtf in a clearly unsafe module (perhaps System.OsString.Unsafe) and matching the behavior of fromString.

    • (optional) encodeUtf/decodeUtf should return either Maybes or meaningful Eithers. The current exception format I assume is text cargo cult; a simple "character offset at which the error occurred" format would be far more useful.

  13. hasufell commented on May 17, 2024

    @hasufell
    Member

    I have to re-iterate:

    • the quasiquoter is safe, clearly documented and can even be made more explicit if need be (e.g. by renaming to osstrUnicode or exposing it through a .Unicode module)
    • we can also provide ascii and other quasi quoter variants => this approach is clearly superior
    • IsString is a misdesigned broken class that the community should stop using
    • OverloadedStrings as an extension most certainly could be fixed or redesigned... I have no problem with string literals! Maybe it should be called OverloadedUnicodeStrings.
    • the fact that users have to look for how to construct OsString is a good thing and means people are starting to become aware of the intricacies that surround strings and especially filepaths

    I have written two large blog posts about Haskell filepath and Haskell strings:

    The whole point of AFPP/OsPath/OsString was to be more correct than String (yes, in those pesky little corner cases too). Providing an IsString instance is a blow to that vision. People can still use String and FilePath if they don't care that much about correctness or write orphan instances how they see fit.

    Since ByteString instance of IsString is also problematic, we already have incentive of fixing OverloadedStrings. I'm not on GHC SC, but I can surely help promote and support such attempts.

  14. BurningWitness commented on May 17, 2024

    @BurningWitness

    I know all the points you stated and I agree with all of them (except the "clearly superior" thing, else I wouldn't be writing this), but the community at large, including me, will only stop using IsString when better IsString arrives. Knowing how slow and painful GHC updates are, we're not going to see anything like that before year 2030. Until that day, every user of this library will be forced to bear with the nine-character-wide Template-Haskell-ridden quasiquoter overhead that doesn't seem to do anything and isn't even highlit properly in the editor.

  15. hasufell commented on May 18, 2024

    @hasufell
    Member

    Until that day, every user of this library will be forced to bear with the nine-character-wide Template-Haskell-ridden quasiquoter overhead

    I'm not sure I understand what the argument here is. That the identifier is too long to type?

    That's a rather poor argument in my book. There are ways to configure your editor with snippets and macros to make it easier. It may even be possible to write an HLS plugin to aid with that.

    The real caveats of quasiquoters are:

    • slows down compilation time
    • some tooling might get confused (e.g. buggy formatters)

    Also, you can just define an orphan IsString instance and then deal with the fallout.

    This library was not written to be the most convenient String type. It was written to be the most correct one wrt OS API.

    And reading all this, I'm considering to make it more prominent that the quasi quoter is UTF-8/16.

    If we reach a point where people just randomly throw OsString literals around without understanding that these might not always be correct (e.g. bc the encoding of filepaths is actually not UTF), then we didn't gain much. Equality checks might suddenly fail, because there's no getFileSystemEncoding involved anymore (which is used for FilePath for instance, but it's also just an approximation).

  16. BurningWitness commented on May 18, 2024

    @BurningWitness

    There are ways to configure your editor with snippets and macros to make it easier.

    I don't need to do that for any other part of the language and this library does not seem different enough to justify this. People who don't want to use IsString can simply choose not to use OverloadedStrings, which is opt-in.

    e.g. bc the encoding of filepaths is actually not UTF

    How about encoding as in truncating characters to bytes then? This is consistent with how ByteStrings already behave, matches what most people care about (hardcoding ASCII strings) and predictably fails for anyone who chooses to manually encode characters beyond ASCII.

  17. hasufell commented on May 18, 2024

    @hasufell
    Member

    I don't need to do that for any other part of the language and this library does not seem different enough to justify this. People who don't want to use IsString can simply choose not to use OverloadedStrings, which is opt-in.

    I don't want to provide broken API.

    How about encoding as in truncating characters to bytes then? This is consistent with how ByteStrings already behave, matches what most people care about (hardcoding ASCII strings) and predictably fails for anyone who chooses to manually encode characters beyond ASCII.

    Are you suggesting the IsString instance should truncate like ByteString? Absolutely not. That is even worse, as we have seen evidence here: haskell/bytestring#140


    I'm also not sure my example came across the way I wanted. Say, you have filepaths encoded via Shift JIS and now you create a literal of an expected filepath to search for in a directory. The bytes will never match. When dealing with String, you don't have that issue, because you're not comparing bytes, but already decoded Unicode Code Points. getFileSystemEncoding is the trick that the base library uses at the outer FFI layer to turn CString into FilePath. So it happen to work, if your filepaths match the encoding that getFileSystemEncoding (locale lookup) returns. That is a gamble too (e.g. when dealing with network drives or whatnot).

    This is why os-string has a rich interface to deal with encodings:

    And it also provides the same base trick via encodeFS.

    In some cases you might even want to construct a specific byte sequence as a literal, instead of going through String.

    All these strategies have trade offs. There is no one right way. UTF-8 is not the one right way either, even if people want to believe that. If we did believe that, then this package would be useless.

  18. BurningWitness commented on May 18, 2024

    @BurningWitness

    truncate like ByteString? Absolutely not.

    Yes, I am suggesting exactly that, as it would be as consistently broken as every other IsString instance and its convenience and readability would far outweigh said brokenness (this is a trade-off like any other).

    Say, you have filepaths encoded via Shift JIS and now you create a literal of an expected filepath to search for in a directory. The bytes will never match.

    Yes, and with this approach non-ASCII hardcoding will almost never match locally either, consistently failing all relevant tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions