sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Exception when attempting to compute the file changes for the file move: Server error: The `edit.getRefactoring` request was cancelled."

Open ghost opened this issue 3 years ago • 14 comments

@liudonghua123 commented on Jul 15, 2020, 6:38 AM UTC:

Thanks for the feedback! If your issue is related to the Flutter framework itself, please open an issue at github.com/flutter/flutter.

Steps to Reproduce

  1. open project in android studio
  2. move/refactor some dart files under lib from a directory to another directory, both directory are under lib.
  3. move expection dialog shown Exception when attempting to compute the file changes for the file move: Server error: The edit.getRefactoring request was cancelled." image
  4. The dart files moved, but the imports are not changed, so the code broken.

Please tell us what you were doing and what went wrong

Version info

D:\code\flutter\colorful_classroom_app>flutter doctor -v
[✓] Flutter (Channel master, 1.20.0-8.0.pre.101, on Microsoft Windows [Version 10.0.19041.388], locale en-US)
    • Flutter version 1.20.0-8.0.pre.101 at D:\apps\flutter
    • Framework revision fcb5806933 (54 minutes ago), 2020-07-14 22:41:01 -0700
    • Engine revision 99c2b3a245
    • Dart version 2.9.0 (build 2.9.0-21.0.dev 20bf2fcf56)
    • Pub download mirror https://pub.flutter-io.cn
    • Flutter download mirror https://storage.flutter-io.cn

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at D:\android\Android_SDK
    • Platform android-29, build-tools 29.0.2
    • ANDROID_HOME = D:\android\Android_SDK
    • Java binary at: D:\apps\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[✓] Visual Studio - develop for Windows (Visual Studio Community 2019 16.5.4)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.5.30011.22
    • Windows 10 SDK version 10.0.18362.0

[✓] Android Studio (version 4.0)
    • Android Studio at D:\apps\Android Studio
    • Flutter plugin version 47.1.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[✓] VS Code, 32-bit edition (version 1.46.1)
    • VS Code at C:\Program Files (x86)\Microsoft VS Code
    • Flutter extension version 3.12.1

[✓] Connected device (4 available)
    • Windows (desktop) • windows    • windows-x64    • Microsoft Windows [Version 10.0.19041.388]
    • Web Server (web)  • web-server • web-javascript • Flutter Tools
    • Chrome (web)      • chrome     • web-javascript • Google Chrome 84.0.4147.89
    • Edge (web)        • edge       • web-javascript • Microsoft Edge 83.0.478.61

• No issues found!

This issue was moved by stevemessick from flutter/flutter-intellij#4702.

ghost avatar Jul 15 '20 20:07 ghost

@lrhn When will this be fixed? It is impossible to work with this.

alexeylopukh-aloteq avatar Oct 29 '20 12:10 alexeylopukh-aloteq

I'm not on the analyzer team, so I wouldn't be able to answer that. Let's see if the analyzer team has a comment.

lrhn avatar Oct 29 '20 13:10 lrhn

Any updates on this? This is still happening on flutter 2.0.5 and android studio 4.1.3.

knyghtryda avatar Apr 21 '21 02:04 knyghtryda

Flutter 2.2.1, Android Studio 4.2.1, still an issue.

CattoDoesCode avatar Jun 01 '21 12:06 CattoDoesCode

I believe this happens when you move multiple files that are interdependent. So there must be a logic to move the files that are not dependent first, change their reference, and then move the others.

An alternative is move file by file (one by one)

idkq avatar Jul 27 '21 13:07 idkq

This behavior happens only on windows. I've tested with linux an it worked as expected (except when you move the whole folder, refactoring doesn't work (neither windows nor linux)).

aliyazdi75 avatar Feb 18 '22 08:02 aliyazdi75

@DanTup Is this something you could look at?

bwilkerson avatar Feb 18 '22 17:02 bwilkerson

@bwilkerson is there a way to enable the instrumentation log in Android Studio? I tried searching the settings but can't find anywhere to add additional args.

I can't reproduce this specific error message, but it does fail for me on Windows in Android Studio with the following error:

Dart analysis issue: FileSystemException(path=M:\Dev\TestStuff\FlutterTestApp\lib\folder2\file2.dart; message=Cannot open file)

My feeling is that these messages could be caused by:

  • A race caused by AS sending concurrent requests to rename files (right now the analysis server only supports a single refactor at a time, and a second one will cancel the first)
  • AS maybe renaming the file(s) before all of the refactor requests have completed

In VS Code, the renames go through a queue so they won't be sent concurrently (since VS Code may also call us multiple times if multiple items are dragged) - should AS do the same?

Related: I have an open change for supporting renaming folders (https://dart-review.googlesource.com/c/sdk/+/158005) which I'm hoping to be able to merge soon (I'm waiting for a new VS Code LSP client to be published for some final testing/tweaking of cancellation). It doesn't help with multiple items being renamed/moved together but that's probably a logical next step (either by extending the protocol to support multiple items, or handling concurrent renames).

DanTup avatar Feb 22 '22 10:02 DanTup

is there a way to enable the instrumentation log in Android Studio?

Yes. Open "Help > Find Action...". Search for "Registry..." and run the action. Search for "dart.server.additional.arguments" and edit it to add command-line arguments that will be sent to the server. Finally, restart the server.

bwilkerson avatar Feb 22 '22 15:02 bwilkerson

Thanks!

I still can't repro the original error message, but I see that report is quite old now. @aliyazdi75 can you confirm if you're seeing the same error noted in the title ("request was cancelled") or something else (perhaps the same issue I'm seeing?).

@bwilkerson for the error I'm seeing (log below), the issue seems to be that when computing edits for a rename, the references to that file are checked to see if they're package URIs or not, and that involves reading their content. However if they were renamed just before their content was read, the read fails.

Eg.:

  • file1 references file2, file2 references file1
  • in editor, drag both to a new folder
  • editor sends MOVE_FILE for file1 and gets the edits
  • edit performs the rename on disk around the same time as sending MOVE_FILE for file2
  • analyzer is computing edits for file2 and tries to read file1s content (which has just been renamed on disk) and fails

I can't think of a simple fix, although I suspect retrying the refactor after this happened (perhaps after a short delay) would succeed, as the renamed file would've been handled and the new rename would be looking at the reference from its new location, not the old one.

# Request for edits to move file1
1645547999993:Req:{"id"::"16","method"::"edit.getRefactoring","params"::{"kind"::"MOVE_FILE","file"::"M::\\Dev\\TestStuff\\FlutterTestApp\\lib\\folder1\\file1.dart","offset"::0,"length"::0,"validateOnly"::true,"options"::{"newFile"::"M::\\Dev\\TestStuff\\FlutterTestApp\\lib\\folder2\\file1.dart"}},"clientRequestTime"::1645547999991}
1645548000023:Res:{"id"::"16","result"::{"initialProblems"::[],"optionsProblems"::[],"finalProblems"::[]}}
1645548000035:Req:{"id"::"17","method"::"edit.getRefactoring","params"::{"kind"::"MOVE_FILE","file"::"M::\\Dev\\TestStuff\\FlutterTestApp\\lib\\folder1\\file1.dart","offset"::0,"length"::0,"validateOnly"::false,"options"::{"newFile"::"M::\\Dev\\TestStuff\\FlutterTestApp\\lib\\folder2\\file1.dart"}},"clientRequestTime"::1645548000035}
1645548000084:Res:{"id"::"17","result"::{"initialProblems"::[],"optionsProblems"::[],"finalProblems"::[],"change"::{"message"::"","edits"::[{"file"::"M::\\Dev\\TestStuff\\FlutterTestApp\\lib\\folder1\\file2.dart","fileStamp"::0,"edits"::[{"offset"::7,"length"::45,"replacement"::"'package::flutter_test_app/folder2/file1.dart'"}]}],"linkedEditGroups"::[]}}}

# Edits applied to file2 to update import
1645548000128:Req:{"id"::"18","method"::"analysis.updateContent","params"::{"files"::{"M::\\Dev\\TestStuff\\FlutterTestApp\\lib\\folder1\\file2.dart"::{"type"::"add","content"::"import 'package::flutter_test_app/folder2/file1.dart';\n\nclass File2 {}\n\nFile1? a;"}}},"clientRequestTime"::1645548000127}


# Begin request to rename file 2
1645548000128:Req:{"id"::"19","method"::"edit.getRefactoring","params"::{"kind"::"MOVE_FILE","file"::"M::\\Dev\\TestStuff\\FlutterTestApp\\lib\\folder1\\file2.dart","offset"::0,"length"::0,"validateOnly"::true,"options"::{"newFile"::"M::\\Dev\\TestStuff\\FlutterTestApp\\lib\\folder2\\file2.dart"}},"clientRequestTime"::1645548000127}
1645548000142:Res:{"id"::"18","result"::{}}
1645548000151:Res:{"id"::"19","result"::{"initialProblems"::[],"optionsProblems"::[],"finalProblems"::[]}}
1645548000169:Req:{"id"::"20","method"::"edit.getRefactoring","params"::{"kind"::"MOVE_FILE","file"::"M::\\Dev\\TestStuff\\FlutterTestApp\\lib\\folder1\\file2.dart","offset"::0,"length"::0,"validateOnly"::false,"options"::{"newFile"::"M::\\Dev\\TestStuff\\FlutterTestApp\\lib\\folder2\\file2.dart"}},"clientRequestTime"::1645548000152}

# File 1 is renamed on disk
1645548000199:Watch:<unknown>:M::\Dev\TestStuff\FlutterTestApp\lib\folder1\file1.dart:remove
1645548000201:Noti:{"event"::"analysis.flushResults","params"::{"files"::["M::\\Dev\\TestStuff\\FlutterTestApp\\lib\\folder1\\file1.dart"]}}
1645548000202:Watch:<unknown>:M::\Dev\TestStuff\FlutterTestApp\lib\folder2\file1.dart:add

# Error while computing edits for file2.. Tried to go to disk for modification stamp but the file has gone
1645548000227:Ex:FileSystemException(path=M::\Dev\TestStuff\FlutterTestApp\lib\folder1\file1.dart; message=Cannot retrieve modification time):
#0      _PhysicalFile.modificationStamp (package::analyzer/file_system/physical_file_system.dart::116::7)
#1      _OverlayFile.modificationStamp (package::analyzer/file_system/overlay_file_system.dart::146::18)
#2      FileSource.contentsFromFile (package::analyzer/src/source/source_resource.dart::68::14)
#3      FileSource.contents (package::analyzer/src/source/source_resource.dart::55::12)
#4      MoveFileRefactoringImpl._isPackageReference (package::analysis_server/src/services/refactoring/move_file.dart::179::34)
#5      MoveFileRefactoringImpl._computeNewUri (package::analysis_server/src/services/refactoring/move_file.dart::160::9)
#6      MoveFileRefactoringImpl.createChange.<anonymous closure> (package::analysis_server/src/services/refactoring/move_file.dart::148::22)
#7      ChangeBuilderImpl.addDartFileEdit (package::analyzer_plugin/src/utilities/change_builder/change_builder_core.dart::145::20)
<asynchronous suspension>
#8      MoveFileRefactoringImpl.createChange (package::analysis_server/src/services/refactoring/move_file.dart::147::7)
<asynchronous suspension>
#9      _RefactoringManager.getRefactoring.<anonymous closure> (package::analysis_server/src/edit/edit_domain.dart::1035::23)
<asynchronous suspension>

Edit: I also realise why VS Code doesn't see this - we currently make no attempt to handle multi-item renames in LSP (the "queue" was pre-LSP) so the refactor never runs when dragging multiple files there.

DanTup avatar Feb 22 '22 17:02 DanTup

I think your analysis is correct. I can think of two possible solutions.

The first is to re-order the events on the client side. If we were to make all of the changes to each file before asking to move the next then we'd simulate a series of individual file moves, which is what server currently supports.

The second is to support multi-file moves in server directly. This is probably more work, but might be a little faster.

I don't remember enough about how this is implemented to know what the risks of these two approaches are.

bwilkerson avatar Feb 22 '22 17:02 bwilkerson

The second is to support multi-file moves in server directly. This is probably more work, but might be a little faster.

I think this is very similar to the in-progress changes to support directories (since we just collect all the files in the directory and enumerate through them). It would need an update to the protocol (for Android Studio - LSP already accepts multiple) and an update to the Android Studio plugin, but it probably is the best option (and something I'd like to do after landing the other changes).

The directory work is partly blocked for LSP (waiting on a new LSP client release, but also trying to handle more cases where we have inconsistent LineInfos for similar reasons to these), but I think it could be landed for non-LSP (although there will also need to be some work on the client to take advantage of it - passing folders over, but also ensuring the user can cancel if they decide it's taking too long).

Edit: That said, if we go down that route, it should probably be documented that clients should not send MOVE_FILES while also renaming files on disk in this way, since this error would remain a possibility.

DanTup avatar Feb 22 '22 17:02 DanTup

@DanTup Yes, I can still reproduce this on only Windows. It is so strange. It only happens on specific files with specific imports. I still couldn't reproduce this on some simple projects, but it happened on one of my big projects for specific files. If I can find a simple way I will inform you. This is the error message: image

aliyazdi75 avatar Feb 22 '22 18:02 aliyazdi75

Still happens to me on MacOS and Flutter 3. Exactly same error message.

javiermrz avatar Aug 03 '22 08:08 javiermrz

I'm also facing this problem and with every new version of Android Studio, I say to myself "maybe they solved the problem in this version", but no 😅

However, there is a way for me to solve this problem in a very simple and easy manner from within Android Studio (or vs code)

When I:

  • Change the name of the folder.
  • Or moving more than one file to another directory
  • Or moving a directory to another one

This problem occurs.

What is the solution for me?

Suppose we have a directory like: lib>view>screens, and we want to move it to the path lib/presentation>view>screens. In this case, I'll move the view directory into a new one called presentation.

However, after moving, the error dialog mentioned in above will pop up.

In this case, I will select package:malazhariy/view/ in any file affected by the problem and then modify the path according to the change I made, in this case, it would be: package:malazhariy/presentation/view/

  • Before: package:malazhariy/view/
  • After: .. package:malazhariy/presentation/view/

I will repeat this step for all affected files once by using the Replace in files command and its shortcut in AD is Ctrl+Shift+R for Windows. And I will modify the path of all files and once I press the Replace all button, the problem should be solved.


Note: When importing a file, avoid importing it like this ../../../../utils/styles.dart and make sure to import it like this 'package:flutter_restaurant/utils/styles.dart' to avoid any future problems of this kind.

This was my way of dealing with this problem 😁

https://user-images.githubusercontent.com/87443208/204137757-11026a6a-32e7-43be-911f-ceda8d7e2c99.mp4

MAlazhariy avatar Nov 27 '22 13:11 MAlazhariy

Same problem for me. On my Windows machine I get the mentioned error message, on a newly installed Ubuntu VM I don't get the message, but the result is the same: The depending import paths will not be updated...

S-Man42 avatar Jan 13 '23 15:01 S-Man42

This is a very unfortunate thing. The only thing that saves is moving one file at a time. Then everything works as expected

PackRuble avatar Jan 21 '23 10:01 PackRuble

still on android studio 2022.1.1 with macOS 13.1 and flutter 3.3.8

zxlbxx avatar Jan 28 '23 08:01 zxlbxx

Still a problem; macOS, flutter 3.10, studio flamingo. 😢

romatallinn avatar May 28 '23 16:05 romatallinn

Still a problem; Windows 11, Flutter 3.10, Android Studio Flamingo

ifsygn avatar Jun 27 '23 17:06 ifsygn

Why is this problem never solved...

est7 avatar Jun 29 '23 06:06 est7

I started using Android Studio for Flutter, then switched to IntelliJ some time after and my experience has been great ever since. Haven't had errors like this, and you also have a Device Manager in IntelliJ for the emulator, so you don't need to open Android Studio. Hope this helps some people ♥️

javiermrz avatar Jul 09 '23 11:07 javiermrz

Seriously why is this still a problem. What's a developer who cant refactor.

cyberpwnn avatar Jul 09 '23 14:07 cyberpwnn

Still facing same problem in Android Studio Giraffe | 2022.3.1

Sumit258000 avatar Aug 16 '23 05:08 Sumit258000

Seeing this on Windows, Android Studio Iguana 2023.2.1 Canary 1. Almost every file I move now runs into this error and I have to go manually change every reference. Bit annoying :/

StephenBrough avatar Sep 03 '23 18:09 StephenBrough

Hello, and it's 3.5 years later. I mean, how important does the issue have to be to get it fixed? It's the refactoring that does not work. And there is no workaround except you can try the lottery - refactoring files one by one. So, should we be getting an IDE from another vendor?

Moreover, the error causes havoc that you can't undo. Because, of course, it will show dialog that says there was an error and it too far gone now, it can't undo the mess. It's the worst-case scenario! Android Studio-Cannot Undo-0926 0248 @2x

You have "one job". This is the job of the IDE, to refactor. If not this, then what? What are you doing releasing all those Hedgehogs and Iguanas if you're not fixing the main functionality of the IDE??

romanr avatar Sep 25 '23 17:09 romanr

It's Almost the end of 2023 and this Is still an issue. Facing the Same issue with Android Studio Giraffe | 2022.3.1 and Flutter 3.10.6

ernestlipson avatar Oct 02 '23 12:10 ernestlipson

@jcollins-g @idkq Was this issue mistakenly evaluated as P2 (likely to work on it) and not P1 (will work on it) ?
Looking at all the P1 issues in comparison, I struggle to find any issue that is more breaking than this one.

romanr avatar Oct 02 '23 12:10 romanr

facing same issue when it will be solved?

Anksji avatar Oct 11 '23 02:10 Anksji

we can't able to refract the code?

Anksji avatar Oct 11 '23 02:10 Anksji