Apollo-ios: Trailing newline thrash with Xcode 10.2

Created on 1 May 2019  Ā·  8Comments  Ā·  Source: apollographql/apollo-ios

I’m setting up Apollo in an app for the first time, and I have noticed that Apollo and Xcode 10.2 disagree about whether a file should end in a trailing newline, causing the file to be modified every time it is opened or the project is built.

Intended outcome:
Apollo should generate an API.swift with a newline \n character at the end of the file.

Actual outcome:
Apollo generates an API.swift that does not have a newline \n character at the end of the file. Every time Xcode 10.2 opens the file, it adds a newline to the end of the file. Every time the project is built, Apollo removes the trailing newline.

How to reproduce the issue:

  • Use Apollo 0.10.1 and Xcode 10.2
  • Add your Apollo-generated API.swift to your Xcode project
  • Open API.swift in Xcode, and see that Xcode adds a newline to API.swift
  • Build the project, and see that Apollo regenerates API.swift without a newline
codegen

Most helpful comment

This has been addressed by switching to the updated code generation in 0.13.0!

All 8 comments

What Text Editing Preferences are you using? I could not reproduce this with Xcode Version 10.2.1 (10E1001).

Screenshot 2019-05-02 at 11 14 19

Anyone else seeing this issue?

It looks like, while Xcode automatically modifies the file when it’s opened (see screenshot indicating modified file), I still have to save the file for the additional whitespace to be reflected on the filesystem.

modified file

I have ā€œAutomatically trim trailing whitespaceā€ and ā€œIncluding whitespace-only linesā€ enabled, but ā€œConvert existing files on saveā€ disabled. However, I still experience the issue if I copy your settings exactly. I also experience the issue when I use Xcode 10.1.

Xcode Text Editing Preferences

To better illustrate my issue, here’s the end of the output of cat API.swift as generated by Apollo:

Apollo-generated

And here’s the end of the output of cat API.swift after Xcode appends a trailing newline:

Xcode-modified

Note how the last brace of the Apollo-generated file appears on the same line as my command prompt, while the last brace of the Xcode-modified file appears on its own line, as expected.

According to the POSIX standard, each line in a file, including the last line, should end in a newline \n character. GNU and Unix tools like cat expect this behavior, so they don’t behave nicely when a file does not end in a trailing newline.

Thank you for the detailed follow-up. Sounds like I should be able to reproduce it. However, there’s some subtlety to it, a freshly generated file looks fine here.

Screenshot 2019-05-03 at 11 49 49

When I use Vim with :set list!, my freshly generated file shows a newline $ too, but if I save the file from Vim, Git shows that there’s now a new line. It looks like Vim also inserts the newline if it isn’t already present.

I think that’s default behavior, but just in case, here’s my ~/.vimrc:

syntax enable

set incsearch
set hlsearch

autocmd FileType gitcommit setlocal spell textwidth=72

Confirmed šŸ˜…

Screenshot 2019-05-03 at 19 18 54

Although, from a quick glance, I reckon this is not Swift specific, I will try expose this issue with a test for apollographql/apollo-tooling/packages/apollo-codegen-swift.

I guess writing TypeScript is a thing I do now. šŸ¤·ā€ā™‚ļø

This has been addressed by switching to the updated code generation in 0.13.0!

Was this page helpful?
0 / 5 - 0 ratings