From 6d6addc8bd3a9f2d7ec9002d0f781bf40bfe36b8 Mon Sep 17 00:00:00 2001 From: Aaron Adams Date: Thu, 1 Aug 2024 09:14:43 +0800 Subject: [PATCH] feat: Add lua-language-server to CI (#2611) - Implement lua-language-server linter in pre-commit - Install via Nix due to lack of Ubuntu package - Apply patch for --check CLI bug using Nix overlay - Centralize linting config for VSCode, pre-commit, and CI Fixes #2576 ## Checklist - [ ] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [ ] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [ ] I have not broken the cheatsheet --------- Co-authored-by: Pokey Rule <755842+pokey@users.noreply.github.com> --- .github/actions/lint-lua-ls/action.yml | 13 ++++++++ .github/workflows/pre-commit.yml | 3 ++ .github/workflows/test.yml | 2 ++ .luarc.json | 6 ++++ .vscode/settings.json | 4 --- flake.nix | 27 +++++++++++++++-- scripts/lint-lua-ls.sh | 42 ++++++++++++++++++++++++++ 7 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 .github/actions/lint-lua-ls/action.yml create mode 100644 .luarc.json create mode 100755 scripts/lint-lua-ls.sh diff --git a/.github/actions/lint-lua-ls/action.yml b/.github/actions/lint-lua-ls/action.yml new file mode 100644 index 0000000000..58a98e16a6 --- /dev/null +++ b/.github/actions/lint-lua-ls/action.yml @@ -0,0 +1,13 @@ +name: "Lua Language Server Lint" +description: "Lints all lua files with lua-language-server" +runs: + using: "composite" + steps: + - uses: cachix/install-nix-action@v27 + with: + nix_path: nixpkgs=channel:nixos-unstable + - uses: DeterminateSystems/magic-nix-cache-action@v2 + - run: nix profile install --accept-flake-config .#lua-language-server + shell: bash + - run: scripts/lint-lua-ls.sh + shell: bash diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index daa024e3ce..42e589bc2d 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -8,11 +8,14 @@ on: types: [opened, synchronize, reopened] merge_group: branches: [main] + workflow_dispatch: jobs: pre-commit: name: Pre-commit runs-on: ubuntu-latest + env: + CURSORLESS_REPO_ROOT: ${{ github.workspace }} steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2f3b14fbed..62c323d9e6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -66,6 +66,8 @@ jobs: NEOVIM_PATH: ${{ steps.vim.outputs.executable }} - uses: ./.github/actions/test-neovim-lua/ if: runner.os == 'Linux' && matrix.app_version == 'stable' + - uses: ./.github/actions/lint-lua-ls/ + if: runner.os == 'Linux' && matrix.app_version == 'stable' - name: Create vscode dist that can be installed locally run: pnpm -F @cursorless/cursorless-vscode populate-dist --local-install if: runner.os == 'Linux' && matrix.app_version == 'stable' diff --git a/.luarc.json b/.luarc.json new file mode 100644 index 0000000000..a58b4564ad --- /dev/null +++ b/.luarc.json @@ -0,0 +1,6 @@ +{ + "runtime.version": "Lua 5.1", + "diagnostics.ignoredFiles": "Disable", + "diagnostics.globals": ["vim", "talon", "it", "describe"], + "workspace.ignoreDir": ["data/playground/lua/", ".luarocks", ".lua"] +} diff --git a/.vscode/settings.json b/.vscode/settings.json index 7922d2b067..c5ff74b927 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -26,10 +26,6 @@ "typescript.enablePromptUseWorkspaceTsdk": true, "typescript.tsdk": "node_modules/typescript/lib", "eslint.workingDirectories": [{ "pattern": "packages/*/" }], - "Lua.runtime.version": "Lua 5.1", - "Lua.diagnostics.globals": ["vim", "talon", "it", "describe"], - "Lua.diagnostics.ignoredFiles": "Disable", - "Lua.workspace.ignoreDir": ["data/playground/lua/"], "[lua]": { "editor.defaultFormatter": "JohnnyMorganz.stylua" }, diff --git a/flake.nix b/flake.nix index 0b53089e89..514d47cdef 100644 --- a/flake.nix +++ b/flake.nix @@ -22,6 +22,7 @@ pkgs = import nixpkgs { inherit system; overlays = [ + # Updated neovim-node-client is pending merge: # https://github.com/NixOS/nixpkgs/pull/317333 (final: prev: { nodePackages = prev.nodePackages // { @@ -45,7 +46,21 @@ }; }; neovim = prev.neovim.override { withNodeJs = true; }; - + # There is a recent bug that prevents cli --check invocation: + # See #2613 + lua-language-server = prev.lua-language-server.overrideAttrs { + postPatch = + let + patch = prev.fetchurl { + url = "https://github.com/LuaLS/lua-language-server/pull/2775.patch"; + sha256 = "sha256-5hjuNzBHLp9kiD6O8jTL5YlvaqR8IuJPHchIZE2/p/Q="; + }; + in + '' + patch -p1 < ${patch} + '' + + prev.lua-language-server.postPatch; + }; }) ]; @@ -57,6 +72,12 @@ ] (nixpkgs.lib.importTOML ./pyproject.toml).tool.ruff.target-version; in { + packages = forEachSupportedSystem ( + { pkgs }: + { + lua-language-server = pkgs.lua-language-server; + } + ); devShells = forEachSupportedSystem ( { pkgs }: { @@ -69,6 +90,8 @@ [ pkgs.corepack pkgs.vsce + pkgs.nodejs + # https://github.com/NixOS/nixpkgs/pull/251418 (pkgs.pre-commit.overrideAttrs (previousAttrs: { makeWrapperArgs = '' @@ -76,12 +99,12 @@ ''; })) python + pkgs.lua-language-server # language server used by pre-commit hooks pkgs.neovim pkgs.luajitPackages.busted # for lua testing pkgs.luarocks # pre-commit doesn't auto-install luarocks pkgs.ps - pkgs.nodejs ]; # To prevent weird broken non-interactive bash terminal buildInputs = [ pkgs.bashInteractive ]; diff --git a/scripts/lint-lua-ls.sh b/scripts/lint-lua-ls.sh new file mode 100755 index 0000000000..7d8b5da137 --- /dev/null +++ b/scripts/lint-lua-ls.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash +set -euo pipefail + +# lua-language-server should be installed automatically by the flake.nix dev shell +# or .github/workflows/pre-commit.yml +if ! type lua-language-server &>/dev/null; then + echo "ERROR: lua-language-server is not installed. Please run 'nix develop' or install it manually." + exit 1 +fi + +if [ ! -e "${CURSORLESS_REPO_ROOT-nonexistent}" ]; then + CURSORLESS_REPO_ROOT=$(git rev-parse --show-toplevel) +fi + +function check_file() { + local file="$1" + logpath="$(mktemp -d)" + rm -rf "$logpath/check.json" + result=$(lua-language-server --check "$file" \ + --checklevel="Warning" \ + --configpath="${CURSORLESS_REPO_ROOT}/.luarc.json" \ + --logpath="$logpath") + if [[ ! "$result" == *"no problems found"* ]]; then + if [ -e "$logpath/check.json" ]; then + cat "$logpath/check.json" + else + echo "ERROR: lua-language-server failed to run." + echo "$result" + fi + return 1 + + fi + return 0 +} + +# lua-language-server doesn't support single file parsing, so check entire folder +exit_code=0 +if ! check_file "${CURSORLESS_REPO_ROOT}"; then + exit_code=1 +fi + +exit $exit_code