dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.28k stars 1.58k forks source link

Move class to file error #57398

Closed FMorschel closed 2 days ago

FMorschel commented 3 days ago

I got the following when refactoring my package with "Move class to file".

image

I was trying to move a class to another file while I already had an export for it (the new not yet existing file) on the source file. I did that some times before and after that error so there might be something else to it. I've not yet been able to repro but I'll keep trying.

Here are the instrumentation logs for around the time when this happened.

InstrumentationLog.zip


CC: @DanTup

DanTup commented 3 days ago

The exception is this:

1731956245397:Ex:An error occurred while handling workspace/executeCommand request
Null check operator used on a null value
#0      MoveTopLevelToFile.compute (package::analysis_server/src/services/refactoring/move_top_level_to_file.dart::156::39)
<asynchronous suspension>
#1      RefactorCommandHandler._performRefactor (package::analysis_server/src/lsp/handlers/commands/refactor_command_handler.dart::116::18)
<asynchronous suspension>
#2      RefactorCommandHandler.handle.<anonymous closure> (package::analysis_server/src/lsp/handlers/commands/refactor_command_handler.dart::76::16)
<asynchronous suspension>
#3      ErrorOr.mapResult (package::analysis_server/src/lsp/error_or.dart::88::11)
<asynchronous suspension>
#4      ServerStateMessageHandler.handleMessage (package::analysis_server/src/lsp/handlers/handlers.dart::499::20)
<asynchronous suspension>
#5      LspAnalysisServer._handleRequestMessage (package::analysis_server/src/lsp/lsp_analysis_server.dart::1139::18)
<asynchronous suspension>
#6      LspAnalysisServer.handleMessage.<anonymous closure>.<anonymous closure> (package::analysis_server/src/lsp/lsp_analysis_server.dart::530::15)
<asynchronous suspension>
#7      OperationPerformanceImpl.runAsync (package::analyzer/src/util/performance/operation_performance.dart::174::14)
<asynchronous suspension>
#8      LspAnalysisServer.handleMessage.<anonymous closure> (package::analysis_server/src/lsp/lsp_analysis_server.dart::510::11)
<asynchronous suspension>

It occurred on code inside move_to_top_level that does this:

for (var element2 in analyzer.movingDeclarations) {
  var element = element2.asElement!;

So it seems like one of the elements being moved returned null for asElement. I'll see if there's enough info in the log to figure out which.

DanTup commented 2 days ago

@FMorschel from the log, it seems like it occurred when you were trying to move extension AddDays to another file. I'm unable to reproduce this, however I see you were using an SDK at d75c493e27f9bf2a89043860132462be665065c2 and I see that later that day 5cd0517bd66050b4edc7c52fd0f3fa3bd0f37a6d made changes to the asElement implementation. I wonder if it may have been fixed by that change?

FMorschel commented 2 days ago

Maybe, I've not yet updated. I was working on this PR https://github.com/FMorschel/due_date/pull/26 (this specific commit https://github.com/FMorschel/due_date/pull/26/commits/be709b5990373e1300004de83626630abc60f257).

I'll see if I can repro this and I'll let you know. I'll also test after updating the SDK to see if this is fixed.

FMorschel commented 2 days ago

I can't even reproduce this myself with that commit. I'm not sure what I have done differently. In any case, I'll close this then as it seems to have been a one-off caused by a probably fixed error. Thanks for all the help!