lsp-mode icon indicating copy to clipboard operation
lsp-mode copied to clipboard

ts-ls: lsp-execute-code-action erases entire buffer

Open codingquark opened this issue 1 year ago • 4 comments

Thank you for the bug report

  • [X] I am using the latest version of lsp-mode related packages.
  • [X] I checked FAQ and Troubleshooting sections
  • [X] You may also try reproduce the issue using clean environment using the following command: M-x lsp-start-plain

Bug description

lsp-mode config:

(use-package lsp-mode
  :init
  (setq lsp-keymap-prefix "s-l")
  :commands (lsp lsp-deferred)
  :hook ((python-ts-mode . lsp)
	 (typescript-ts-mode . lsp))
  :config
  (setq lsp-idle-delay 0.500)
  (setq lsp-enable-symbol-highlighting nil)
  (setq lsp-modeline-diagnostics-enable nil)
  (setq lsp-modeline-code-actions-enable nil)
  (setq lsp-enable-snippet nil)
  (setq lsp-headerline-breadcrumb-enable nil))

lsp-mode checked out commit: 8df561298

I am in nestjs project. When executing lsp-execute-code-action on a class that does not exist, the buffer is erased entirely. The function works correctly when the class can be imported / exists.

Steps to reproduce

https://github.com/emacs-lsp/lsp-mode/assets/456712/a9f732a8-c82b-4f81-a5b8-1223d42bc50a

  1. Open .ts file from a nestjs project
  2. Use some class that is not yet in import section
  3. Execute lsp-execute-code-action

Expected behavior

Do not erase buffer.

Which Language Server did you use?

ts-ls

OS

MacOS

Error callstack

[Trace - 03:04:06 PM] Sending request 'textDocument/codeAction - (165)'.
Params: {
  "textDocument": {
    "uri": "file:///Users/codingquark/workspace/nestproject/patrika-backend/src/categories/categories.module.ts"
  },
  "range": {
    "start": {
      "line": 6,
      "character": 51
    },
    "end": {
      "line": 6,
      "character": 51
    }
  },
  "context": {
    "diagnostics": [
      {
        "range": {
          "start": {
            "line": 6,
            "character": 34
          },
          "end": {
            "line": 6,
            "character": 52
          }
        },
        "message": "Cannot find name 'CategoriesServices'.",
        "severity": 1,
        "code": 2304,
        "source": "typescript",
        "tags": []
      }
    ]
  }
}


[Trace - 03:04:06 PM] Received response 'textDocument/codeAction - (165)' in 41ms.
Result: [
  {
    "title": "Move to a new file",
    "kind": "refactor.move",
    "command": {
      "title": "Move to a new file",
      "command": "_typescript.applyRefactoring",
      "arguments": [
        {
          "file": "/Users/codingquark/workspace/nestproject/patrika-backend/src/categories/categories.module.ts",
          "startLine": 7,
          "startOffset": 52,
          "endLine": 7,
          "endOffset": 52,
          "refactor": "Move to a new file",
          "action": "Move to a new file"
        }
      ]
    }
  }
]


[Trace - 03:04:06 PM] Sending request 'workspace/executeCommand - (166)'.
Params: {
  "command": "_typescript.applyRefactoring",
  "arguments": [
    {
      "file": "/Users/codingquark/workspace/nestproject/patrika-backend/src/categories/categories.module.ts",
      "startLine": 7,
      "startOffset": 52,
      "endLine": 7,
      "endOffset": 52,
      "refactor": "Move to a new file",
      "action": "Move to a new file"
    }
  ]
}


[Trace - 03:04:06 PM] Sending notification 'textDocument/didChange'.
Params: {
  "textDocument": {
    "uri": "file:///Users/codingquark/workspace/nestproject/patrika-backend/src/categories/categories.module.ts",
    "version": 141
  },
  "contentChanges": [
    {
      "range": {
        "start": {
          "line": 4,
          "character": 0
        },
        "end": {
          "line": 8,
          "character": 32
        }
      },
      "rangeLength": 125,
      "text": ""
    }
  ]
}


[Trace - 03:04:06 PM] Sending notification 'textDocument/didChange'.
Params: {
  "textDocument": {
    "uri": "file:///Users/codingquark/workspace/nestproject/patrika-backend/src/categories/categories.module.ts",
    "version": 142
  },
  "contentChanges": [
    {
      "range": {
        "start": {
          "line": 2,
          "character": 0
        },
        "end": {
          "line": 3,
          "character": 0
        }
      },
      "rangeLength": 60,
      "text": ""
    }
  ]
}


[Trace - 03:04:06 PM] Sending notification 'textDocument/didChange'.
Params: {
  "textDocument": {
    "uri": "file:///Users/codingquark/workspace/nestproject/patrika-backend/src/categories/categories.module.ts",
    "version": 143
  },
  "contentChanges": [
    {
      "range": {
        "start": {
          "line": 1,
          "character": 0
        },
        "end": {
          "line": 2,
          "character": 0
        }
      },
      "rangeLength": 41,
      "text": ""
    }
  ]
}


[Trace - 03:04:06 PM] Sending notification 'textDocument/didChange'.
Params: {
  "textDocument": {
    "uri": "file:///Users/codingquark/workspace/nestproject/patrika-backend/src/categories/categories.module.ts",
    "version": 144
  },
  "contentChanges": [
    {
      "range": {
        "start": {
          "line": 0,
          "character": 0
        },
        "end": {
          "line": 1,
          "character": 0
        }
      },
      "rangeLength": 44,
      "text": ""
    }
  ]
}


[Trace - 03:04:06 PM] Sending notification 'textDocument/didOpen'.
Params: {
  "textDocument": {
    "uri": "file:///Users/codingquark/workspace/nestproject/patrika-backend/src/categories/CategoriesModule.4.ts",
    "languageId": "typescript",
    "version": 0,
    "text": ""
  }
}


[Trace - 03:04:06 PM] Sending notification 'textDocument/didChange'.
Params: {
  "textDocument": {
    "uri": "file:///Users/codingquark/workspace/nestproject/patrika-backend/src/categories/CategoriesModule.4.ts",
    "version": 1
  },
  "contentChanges": [
    {
      "range": {
        "start": {
          "line": 0,
          "character": 0
        },
        "end": {
          "line": 0,
          "character": 0
        }
      },
      "rangeLength": 0,
      "text": "import { HttpModule } from '@nestjs/axios';\nimport { Module } from '@nestjs/common';\nimport { CategoriesResolver } from './categories.resolver';\n\n\n@Module({\n    imports: [HttpModule],\n    providers: [CategoriesResolver, CategoriesServices],\n})\nexport class CategoriesModule {\n}\n"
    }
  ]
}


[Trace - 03:04:06 PM] Received request 'workspace/applyEdit - (10).
Params: {
  "edit": {
    "changes": {
      "file:///Users/codingquark/workspace/nestproject/patrika-backend/src/categories/categories.module.ts": [
        {
          "range": {
            "start": {
              "line": 4,
              "character": 0
            },
            "end": {
              "line": 8,
              "character": 33
            }
          },
          "newText": ""
        },
        {
          "range": {
            "start": {
              "line": 2,
              "character": 0
            },
            "end": {
              "line": 3,
              "character": 0
            }
          },
          "newText": ""
        },
        {
          "range": {
            "start": {
              "line": 1,
              "character": 0
            },
            "end": {
              "line": 2,
              "character": 0
            }
          },
          "newText": ""
        },
        {
          "range": {
            "start": {
              "line": 0,
              "character": 0
            },
            "end": {
              "line": 1,
              "character": 0
            }
          },
          "newText": ""
        }
      ],
      "file:///Users/codingquark/workspace/nestproject/patrika-backend/src/categories/CategoriesModule.4.ts": [
        {
          "range": {
            "start": {
              "line": 0,
              "character": 0
            },
            "end": {
              "line": 0,
              "character": 0
            }
          },
          "newText": "import { HttpModule } from '@nestjs/axios';\nimport { Module } from '@nestjs/common';\nimport { CategoriesResolver } from './categories.resolver';\n\n\n@Module({\n    imports: [HttpModule],\n    providers: [CategoriesResolver, CategoriesServices],\n})\nexport class CategoriesModule {\n}\n"
        }
      ]
    }
  }
}


[Trace - 03:04:06 PM] Sending response 'workspace/applyEdit - (10)'. Processing request took 649ms
Params: {
  "jsonrpc": "2.0",
  "id": 10,
  "result": {
    "applied": true
  }
}


[Trace - 03:04:06 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "uri": "file:///Users/codingquark/workspace/nestproject/patrika-backend/src/categories/categories.module.ts",
  "diagnostics": []
}


[Trace - 03:04:06 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "uri": "file:///Users/codingquark/workspace/nestproject/patrika-backend/src/categories/CategoriesModule.ts",
  "diagnostics": [
    {
      "range": {
        "start": {
          "line": 7,
          "character": 36
        },
        "end": {
          "line": 7,
          "character": 54
        }
      },
      "message": "Cannot find name 'CategoriesServices'.",
      "severity": 1,
      "code": 2304,
      "source": "typescript",
      "tags": []
    }
  ]
}


[Trace - 03:04:06 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "uri": "file:///Users/codingquark/workspace/nestproject/patrika-backend/src/app.module.ts",
  "diagnostics": []
}


[Trace - 03:04:06 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "uri": "file:///Users/codingquark/workspace/nestproject/patrika-backend/src/locations/locations.module.ts",
  "diagnostics": []
}


[Trace - 03:04:06 PM] Received response 'workspace/executeCommand - (166)' in 715ms.
Result: null


[Trace - 03:04:07 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "uri": "file:///Users/codingquark/workspace/nestproject/patrika-backend/src/categories/CategoriesModule.4.ts",
  "diagnostics": [
    {
      "range": {
        "start": {
          "line": 5,
          "character": 1
        },
        "end": {
          "line": 8,
          "character": 2
        }
      },
      "message": "Unable to resolve signature of class decorator when called as an expression.\n  The runtime will invoke the decorator with 2 arguments, but the decorator expects 1.",
      "severity": 1,
      "code": 1238,
      "source": "typescript",
      "tags": []
    },
    {
      "range": {
        "start": {
          "line": 7,
          "character": 36
        },
        "end": {
          "line": 7,
          "character": 54
        }
      },
      "message": "Cannot find name 'CategoriesServices'.",
      "severity": 1,
      "code": 2304,
      "source": "typescript",
      "tags": []
    }
  ]
}


[Trace - 03:04:09 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "uri": "file:///Users/codingquark/workspace/nestproject/patrika-backend/src/stories/stories.service.ts",
  "diagnostics": [
    {
      "range": {
        "start": {
          "line": 72,
          "character": 4
        },
        "end": {
          "line": 72,
          "character": 10
        }
      },
      "message": "'filter' is declared but its value is never read.",
      "severity": 4,
      "code": 6133,
      "source": "typescript",
      "tags": [
        1
      ]
    },
    {
      "range": {
        "start": {
          "line": 73,
          "character": 4
        },
        "end": {
          "line": 73,
          "character": 8
        }
      },
      "message": "'skip' is declared but its value is never read.",
      "severity": 4,
      "code": 6133,
      "source": "typescript",
      "tags": [
        1
      ]
    },
    {
      "range": {
        "start": {
          "line": 74,
          "character": 4
        },
        "end": {
          "line": 74,
          "character": 14
        }
      },
      "message": "'channel_id' is declared but its value is never read.",
      "severity": 4,
      "code": 6133,
      "source": "typescript",
      "tags": [
        1
      ]
    },
    {
      "range": {
        "start": {
          "line": 75,
          "character": 4
        },
        "end": {
          "line": 75,
          "character": 14
        }
      },
      "message": "'sortFilter' is declared but its value is never read.",
      "severity": 4,
      "code": 6133,
      "source": "typescript",
      "tags": [
        1
      ]
    },
    {
      "range": {
        "start": {
          "line": 76,
          "character": 4
        },
        "end": {
          "line": 76,
          "character": 13
        }
      },
      "message": "'isSiteMap' is declared but its value is never read.",
      "severity": 4,
      "code": 6133,
      "source": "typescript",
      "tags": [
        1
      ]
    },
    {
      "range": {
        "start": {
          "line": 78,
          "character": 8
        },
        "end": {
          "line": 78,
          "character": 17
        }
      },
      "message": "'limit_str' is declared but its value is never read.",
      "severity": 4,
      "code": 6133,
      "source": "typescript",
      "tags": [
        1
      ]
    }
  ]
}


[Trace - 03:04:10 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "uri": "file:///Users/codingquark/workspace/nestproject/patrika-backend/src/stories/entities/story.entity.ts",
  "diagnostics": [
    {
      "range": {
        "start": {
          "line": 7,
          "character": 6
        },
        "end": {
          "line": 7,
          "character": 14
        }
      },
      "message": "'Category' is declared but never used.",
      "severity": 4,
      "code": 6196,
      "source": "typescript",
      "tags": [
        1
      ]
    },
    {
      "range": {
        "start": {
          "line": 13,
          "character": 6
        },
        "end": {
          "line": 13,
          "character": 9
        }
      },
      "message": "'Tag' is declared but never used.",
      "severity": 4,
      "code": 6196,
      "source": "typescript",
      "tags": [
        1
      ]
    },
    {
      "range": {
        "start": {
          "line": 19,
          "character": 6
        },
        "end": {
          "line": 19,
          "character": 11
        }
      },
      "message": "'Topic' is declared but never used.",
      "severity": 4,
      "code": 6196,
      "source": "typescript",
      "tags": [
        1
      ]
    },
    {
      "range": {
        "start": {
          "line": 25,
          "character": 6
        },
        "end": {
          "line": 25,
          "character": 10
        }
      },
      "message": "'Type' is declared but never used.",
      "severity": 4,
      "code": 6196,
      "source": "typescript",
      "tags": [
        1
      ]
    }
  ]
}


[Trace - 03:04:10 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "uri": "file:///Users/codingquark/workspace/nestproject/patrika-backend/src/categories/entities/category.entity.ts",
  "diagnostics": []
}


[Trace - 03:04:10 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "uri": "file:///Users/codingquark/workspace/nestproject/patrika-backend/src/locations/locations.module.ts",
  "diagnostics": []
}


[Trace - 03:04:10 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "uri": "file:///Users/codingquark/workspace/nestproject/patrika-backend/src/categories/categories.module.ts",
  "diagnostics": []
}


[Trace - 03:04:10 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "uri": "file:///Users/codingquark/workspace/nestproject/patrika-backend/src/app.module.ts",
  "diagnostics": []
}


[Trace - 03:04:10 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "uri": "file:///Users/codingquark/workspace/nestproject/patrika-backend/src/categories/CategoriesModule.ts",
  "diagnostics": [
    {
      "range": {
        "start": {
          "line": 7,
          "character": 36
        },
        "end": {
          "line": 7,
          "character": 54
        }
      },
      "message": "Cannot find name 'CategoriesServices'.",
      "severity": 1,
      "code": 2304,
      "source": "typescript",
      "tags": []
    }
  ]
}


[Trace - 03:04:10 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "uri": "file:///Users/codingquark/workspace/nestproject/patrika-backend/src/categories/CategoriesModule.1.ts",
  "diagnostics": [
    {
      "range": {
        "start": {
          "line": 7,
          "character": 36
        },
        "end": {
          "line": 7,
          "character": 54
        }
      },
      "message": "Cannot find name 'CategoriesServices'.",
      "severity": 1,
      "code": 2304,
      "source": "typescript",
      "tags": []
    }
  ]
}


[Trace - 03:04:10 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "uri": "file:///Users/codingquark/workspace/nestproject/patrika-backend/src/categories/CategoriesModule.2.ts",
  "diagnostics": [
    {
      "range": {
        "start": {
          "line": 7,
          "character": 36
        },
        "end": {
          "line": 7,
          "character": 54
        }
      },
      "message": "Cannot find name 'CategoriesServices'.",
      "severity": 1,
      "code": 2304,
      "source": "typescript",
      "tags": []
    }
  ]
}


[Trace - 03:04:10 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "uri": "file:///Users/codingquark/workspace/nestproject/patrika-backend/src/categories/CategoriesModule.3.ts",
  "diagnostics": [
    {
      "range": {
        "start": {
          "line": 7,
          "character": 36
        },
        "end": {
          "line": 7,
          "character": 54
        }
      },
      "message": "Cannot find name 'CategoriesServices'.",
      "severity": 1,
      "code": 2304,
      "source": "typescript",
      "tags": []
    }
  ]
}


[Trace - 03:04:10 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "uri": "file:///Users/codingquark/workspace/nestproject/patrika-backend/src/categories/CategoriesModule.4.ts",
  "diagnostics": [
    {
      "range": {
        "start": {
          "line": 7,
          "character": 36
        },
        "end": {
          "line": 7,
          "character": 54
        }
      },
      "message": "Cannot find name 'CategoriesServices'.",
      "severity": 1,
      "code": 2304,
      "source": "typescript",
      "tags": []
    }
  ]
}

Anything else?

No response

codingquark avatar Nov 06 '23 09:11 codingquark

This also happens a lot to me. I think this is when the only code action is something like "move definition into a separate file". Lsp mode creates a new file moves everything in there, changes all other files using the current definitions, does not save the changes, and leaves you with an empty buffer, quite annoying.

This usually happens to me when I want to import the definition at point, but somehow the import action is not available.

dschrempf avatar Nov 12 '23 09:11 dschrempf

Indeed, I noticed many files being created as well as a side effect. Something somewhere also messes up import statements in other files (at least in nestjs projects).

codingquark avatar Nov 13 '23 06:11 codingquark

You can either:

  1. (setq lsp-auto-execute-action nil) to avoid auto execution
  2. filter out refactor.move if you don't need it:
(advice-add 'lsp--select-action :filter-args
            (defun get-rid-of/refactor.move (actions_args)
              (list 
               (seq-remove (lambda (action)
                             (string= "refactor.move" (plist-get action :kind)))
                           (seq-first actions_args)))))

cxa avatar Jan 01 '24 02:01 cxa

Thanks, I didn't know about the first option.

dschrempf avatar Jan 04 '24 12:01 dschrempf