Skip to content

SchemaPrinter generates different results on Windows #2973

Closed

Description

@ajkerr

Describe the bug

The SchemaPrinter utility generates slightly different results for schemas that contain multi-line descriptions when run on Windows vs Linux/macOS.

For some reason, \r\n line endings are used for multi-line descriptions only, and an extra blank line is inserted after the opening """.

To Reproduce

Using a schema like so:

type Query {
  """
  This does
  something.
  """
  myQuery: String
}

The SchemaPrinter will generate something like this on Windows (I'm using Java literals here for illustration - the actual results contain binary carriage returns and line feeds):

type Query {\n
  """\n
  \r\n
  This does\r\n
  something.\r\n
  """\n
  myQuery: String\n
}\n

On Unix-based systems, the results are as expected.

Activity

  1. bbakerman commented on Oct 5, 2022

    @bbakerman
    Member

    I suspect its based on the printf behavior in Java

    We have lots of code like

            out.printf("%s\n", AstPrinter.printAst(definition));
            if (extensions != null) {
                for (TypeDefinition<?> extension : extensions) {
                    out.printf("\n%s\n", AstPrinter.printAst(extension));
                }
            }
            out.print('\n');
    

    So perhaps the printf code uses the Windows \r\n (which is standard windows newline versus the Unix convention of just \n)

    \r\n is standard new line on Windows (if I recall back 10 years to when I last worked in Windows)

    So my question is - is this a problem for you in practice. What is not working as expected for you with this behavior?

  2. ajkerr commented on Oct 5, 2022

    @ajkerr
    Author

    So interestingly, there were already some fixes done for this back in #2705. This PR removed usage of println which does emit OS-specific line ending characters.

    I've scanned the code and I can't find anywhere that is generating \r characters, either directly or indirectly, but I have a sneaking suspicion in may be related to how Antlr is parsing the schema.

    As mentioned in the issue description, these \r characters only appear in multi-line description strings, which is why I suspect the parser.

    At any rate, here's my use case.

    We commit to version control a copy of the full schema, generated using SchemaPrinter. The purpose of this file is to act as a "schema lock" file that we can use to compare new changes against.

    We have a utility that uses SchemaDiff that developers can run to see if their changes contain breaking changes to the schema. This utility also regenerates an updated copy of the schema lock file if there are no breaking changes or the developer has indicated that breaking changes are ok.

    In our CI, we then compare the generated schema against the committed schema lock file to make sure the files are identical.

    If the diff/lock file generation tool is run on Windows, the generated file from SchemaPrinter is different if the schema contains multi-line descriptions.

    For now, we are hacking around it by using a regex search and replace to strip the weird carriage returns and extra line that appears after the opening """.

  3. bbakerman commented on Oct 6, 2022

    @bbakerman
    Member

    The challenge we would have it testing any changes - we dont have easy access to Windows machines with the IDEA etc.. to develop a fix

  4. ajkerr commented on Oct 6, 2022

    @ajkerr
    Author

    The challenge we would have it testing any changes - we dont have easy access to Windows machines with the IDEA etc.. to develop a fix

    I'm in the same boat, unfortunately. Feel free to close this out. If I have some spare time to investigate with a Windows VM I'll try to run things through a debugger to fine the source of these carriage returns, and if I get anywhere, I can reopen with more information (or submit a pull request).

  5. miklcct commented on Nov 6, 2024

    @miklcct

    Please reopen.

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