48

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
CodeChanger
  • 7,953
  • 5
  • 49
  • 80

14 Answers14

29

Workaround: Restart Xcode.

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Cannoliopsida
  • 3,044
  • 5
  • 36
  • 61
  • 1
    In Xcode 9.2, close the project and open it again (don't have to restart Xcode). Worked for me – auspicious99 May 08 '18 at 09:38
  • 1
    I think clearing out derived data will make the indexing working once again and you shouldn't need to restart Xcode... Altought paradox is that restarting Xcode is faster :D – Dominik Bucher Dec 14 '18 at 09:16
25

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

shim
  • 9,289
  • 12
  • 69
  • 108
Jose Antonio
  • 376
  • 4
  • 6
7

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.

manman
  • 4,743
  • 3
  • 30
  • 42
7

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

axunic
  • 2,256
  • 18
  • 18
4

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.

WalterBeiter
  • 2,021
  • 3
  • 23
  • 48
3

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

Richard Poutier
  • 207
  • 2
  • 10
2

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

pableiros
  • 14,932
  • 12
  • 99
  • 105
2

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.

Amit Baderia
  • 4,454
  • 3
  • 27
  • 19
0

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.

0

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

Pedro Trujillo
  • 1,559
  • 18
  • 19
0

I experienced this on a Mac M1, using Xcode Version 14.1 (14B47b).

This helped: Clean project: shift-command-k. Build project: command-b.

bravestLimit
  • 33
  • 1
  • 9
0

Xcode 14.2: New project and tried to refactor the name of the ViewController and got the rename error pop up. Saved the the project and did a build. Refactoring the file name worked after that.

Bob Taylor
  • 61
  • 1
  • 5
0

In case none of the above works (usually does for me too, except for this one time when it didn't), this is what worked for me:

  1. I remove reference of the file (but still keep the actual file). Remove reference window option
  2. I open the folder where I have the file I previously removed and I drag and drop the file in the same place I needed it to be. Make sure Copy items if needed in unselected. Drag-and-drop window option
  3. Clean build folder and Build.
  4. Wait for indexing to finish.
  5. Refactor.

Note: I'm using Xcode 14.3

0

None of the below worked for me. But I could do it. This is my solution.

My case was like:

variable = [self functionName];

and "functionName" failed to refactor > rename. What I did is took this function call in brackets like this:

variable = ([self functionName]);

After that I could rename this function.

Kibernetik
  • 2,947
  • 28
  • 35