Skip to content

.NET 5 RC1 breaks string CompareTo #42234

Closed

Description

@333fred

Description

Run this code:

using System;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            // The quotes are required for this repro
            var string1 = "'delegate'";
            var string2 = "'delegate sub'";
            Console.WriteLine(string1.CompareTo(string2));
        }
    }
}

On .NET 5 P7 and .NET 472, it prints -1. On .NET 5 RC1, it prints 1. This is preventing Roslyn from updating to a new version.

Configuration

.NET 5 RC1

Regression?

From .NET 5 P7

I'm not entirely sure if this was a regression in P8 or RC1: the Roslyn move to P8 has been delayed by other issues we've been working through.

Activity

  1. Dotnet-GitSync-Bot commented on Sep 15, 2020

    @Dotnet-GitSync-Bot
    Collaborator

    I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

  2. added
    untriaged
    on Sep 15, 2020
  3. added and removed
    untriaged
    on Sep 15, 2020
  4. added this to the 5.0.0 milestone on Sep 15, 2020
  5. tarekgh commented on Sep 15, 2020

    @tarekgh
    Member

    Marked with 5.0 till we investigate.

  6. Gnbrkm41 commented on Sep 15, 2020

    @Gnbrkm41
    Contributor

    This seems related with the use of ICU. Configuring the runtime to use NLS fixes the issue:

    ❯ $env:DOTNET_SYSTEM_GLOBALIZATION_USENLS
    true
    gotos@SURFACEBOOK  ~\source\repos\icurepro                                                                   [10:38]
    ❯ dotnet run
    -1
    gotos@SURFACEBOOK  ~\source\repos\icurepro                                                                   [10:38]
    ❯ $env:DOTNET_SYSTEM_GLOBALIZATION_USENLS = "false"
    gotos@SURFACEBOOK  ~\source\repos\icurepro                                                                   [10:38]
    ❯ dotnet run
    1
    

    (Windows 10 Education Insiders, Build 20211, ko-KR)

  7. GrabYourPitchforks commented on Sep 15, 2020

    @GrabYourPitchforks
    Member

    @333fred What's the OS version and language? en-US?

  8. tarekgh commented on Sep 15, 2020

    @tarekgh
    Member

    Yes I can see it with en-US. it is the behavior change between ICU and NLS. This is by design then. I am able to repro it with 3.1 on Linux. So, this is actually is not a new behavior anyway as we had it on Linux.

    My question to Roslyn, why they are not using ordinal comparison here? having Linguistic comparison will make the behavior under the mercy of the current culture.

  9. modified the milestones: 5.0.0, 6.0.0 on Sep 15, 2020
  10. tarekgh commented on Sep 15, 2020

    @tarekgh
    Member

    moving it out of 5.0 and I'll close it soon if there is no strong push back.

  11. tarekgh commented on Sep 15, 2020

    @tarekgh
    Member

    One more note, returning 1 make more sense to me too.

  12. 333fred commented on Sep 15, 2020

    @333fred
    MemberAuthor

    My question to Roslyn, why they are not using ordinal comparison here?

    We were using the default behavior of string.Compare. It's very concerning to me that we're accepting a breaking change on the behavior of string.Compare with entirely ascii text, this seems like a massive breaking change.

    So, this is actually is not a new behavior anyway as we had it on Linux.

    Are you implying that it should have done this on Linux in .NET Core 3.1? If so, that would very much surprise me, as I use Linux day-to-day and haven't noticed test breakage.

  13. tarekgh commented on Sep 15, 2020

    @tarekgh
    Member

    It's very concerning to me that we're accepting a breaking change on the behavior of string.Compare with entirely ascii text, this seems like a massive breaking change.

    Sorting behavior can change per OS and OS version especially when using different cultures. I am still wondering why didn't/don't use Ordinal especially you are talking about ASCII characters.

    Are you implying that it should have done this on Linux in .NET Core 3.1? If so, that would very much surprise me, as I use Linux day-to-day and haven't noticed test breakage.

    Yes. just run your repro code on Linux with 3.1 and you will see getting 1 as the result.

  14. 333fred commented on Sep 15, 2020

    @333fred
    MemberAuthor

    I am still wondering why didn't/don't use Ordinal especially you are talking about ASCII character

    Literally because this is test code, and we just did the simple thing of CompareTo. I can certainly update our code to call a CompareTo overload that uses ordinal comparison. But we currently don't.

  15. tarekgh commented on Sep 15, 2020

    @tarekgh
    Member

    ok, thanks for clarifying. Could you please check Linux case and update us before I close the issue?

  16. 333fred commented on Sep 15, 2020

    @333fred
    MemberAuthor

    Could you please check Linux case and update us before I close the issue?

    Apparently I have never run the tests affected by this bug on Linux, because I do get 1 in this case. While I'm still vaguely concerned that ascii sort is breaking with .NET 5, if you all have determined that you're willing to accept this then I'll shut up. Thanks.

  17. tarekgh commented on Sep 15, 2020

    @tarekgh
    Member

    We depend on the underlying OS/OS-library behavior for sorting. We understand there is a breaking change in general but we decided to have it for the sake of the benefits of using ICU library on Windows too. We are providing a config switch to go back using NLS (as 3.1 did) but this doesn't mean Windows NLS can decide at anytime to change the behavior too. It is by design the sorting behavior can change for Windows too. by that I am closing the issue. Thanks again for your input and confirming Linux case.

  18. GrabYourPitchforks commented on Sep 15, 2020

    @GrabYourPitchforks
    Member

    It is by design the sorting behavior can change for Windows too.

    For reference, this behavior is discussed here and here. The latter link has a reference to Shawn's blog post on the subject.

    Literally because this is test code, and we just did the simple thing of CompareTo.

    Yeah, the default behaviors are painful for non-UI apps. :(
    We recently updated some analyzers to help catch these errors in runtime code, but it's typical for devs not to run analyzers over test code. I wonder if we should reconsider this stance and have some targeted analyzers enabled for test code - things that could help test maintainability?

  19. ghost locked as resolved and limited conversation to collaborators on Dec 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions