Unable to do refactoring on my Swift file in Xcode 9

Viewed 10808

I am using Xcode 9 and I am trying to do Refactoring on my Swift based file but every time I am getting below error:

Refactoring engine ranges didn't match initial ranges

enter image description here

Why isn't it matching the initial range?

10 Answers

Workaround: Restart Xcode.

This has not been resolved yet as of January 2018 (Xcode 9.2).

Build your project (Command ⌘ + B) and it will fix the error. After doing it, I could rename my file successfully.

Update For some reason, it's not happening for me anymore. I noticed also whenever I don't let indexing finishes before trying to start doing a refactor or tap on refactor many times, still see the error, but not permanent anymore.

I asked an engineer at WWDC 2018 about this issue. This issue was happening for me in only one project in my workspace. Other projects in the same workspace works fine. At the moment, there's no solution to this issue. If you want to help Apple to fix this, you can close your Xcode and run following command in terminal:

SOURCEKIT_SERVICE_LOG=3 /Applications/Xcode.app/Contents/MacOS/Xcode 2>&1 | tee /tmp/sk-log.txt

And then try to reproduce the issue and send them the log file (/tmp/sk-log.txt) so they can narrow it down and hopefully fix it in future Xcode versions.

Notice This is project related issue and won't be fixed with OS updates, Xcode updates, or any number of restarting applications, at least the one that I'm having.

I have Xcode's project created using Xcode8 long time ago. For some reason I have to upgrade to Xcode9 (9.4.1 exactly). Then I experienced that error only on that old project, not the new one created using Xcode9.

So i think that error related to project issue. So I decided to compare the settings between old and new one. There are some differences, and after several tries, by changing Optimization Level for Debug solved refactor issue.

  1. In project editor, select your Target, Build Settings
  2. Set Optimization Level (Debug) = No optimization [-Onone]
  3. Delete DerivedData folder related to your project in /Users/YourMacUsername/Library/Developer/Xcode/DerivedData
  4. Clean project shiftcommandk.
  5. Build project commandb.

enter image description here

I have the same issue in Xcode 10.3. Refactoring didn't work when I right clicked → refactor → rename on the class name in the class definition. However it did work when I did the same somewhere in code where I use that class.

Product -> Clean Build Folder -> Quit Xcode -> Reopen Project -> Build

I update to Xcode 10 and finally refactoring now it works again after a year without refactoring

I am on Mac M1 and using Xcode Version 12.5 beta 3.
I was facing the same issue when trying to rename ViewController.
Before renaming it, I moved it to a new group, and my code was in a running state. Not sure if that was the reason for the issue.
But I restarted the Xcode and the issue was resolved for me.

I was experiencing the exact same issue. It turned out that I had my build configuration set to Release mode. Changing it to Debug, cleaning build folder and recompiling fixed the issue for me.

Remove DerivedData

This worked for me for this error and other kind of refactoring errors.

  1. Close Xcode
  2. Remove DerivedData:

rm -rf ~/Library/Developer/Xcode/DerivedData

Related