scalameta / metals-zed

Zed plugin for Metals
Apache License 2.0
56 stars 12 forks source link

Metals configuration with Zed #21

Closed vasigorc closed 3 weeks ago

vasigorc commented 2 months ago

Hello,

it's more of a question, rather than an issue. If there is a better alternative to finding out things about how to use the extension (Stakoverflow tag, maybe something else) - please let me know.

Context

It's my first time trying to set-up a Scala project with Zed. Here is the relevant part from ~/.config/zed/settings.json:

  "lsp": {
    "metals": {
      "settings": {
        "inlayHints": {
          "inferredTypes": {
            "enable": true
          }
        },
        "javaHome": "~/.sdkman/candidates/java/current",
        "sbtScript": "~/.sdkman/candidates/sbt/current/bin/sbt",
        "scalafmtConfigPath": "project/.scalafmt.conf",
        "scalafixConfigPath": "project/.scalafix.conf",
        "inlayHints.inferredTypes.enable": true,
        "enableIndentOnPaste": true,
        "autoImportBuild": "all"
      },
      "binary": {
        "arguments": ["-Dmetals.http=on"]
      }
    }
  },
  "languages": {
    "Scala": {
      "tab_size": 2,
      "language_servers": ["metals"]
    }
  }

Here is the last line from my ~/.zshrc file:

export PATH="$PATH:~/Library/Application Support/Coursier/bin/metals"

I did install metals via Coursier as suggested. I am on MacOS Sonoma 14.6.1 (23G93)

Problem

When I am opening an existing Scala project, there is a pop-up telling me abot failure to connect to metals server:

Failed to connect with build server, no functionality will work. See logs for more details.

Here are (what I thought were) relevant entries from ~/Library/Logs/Zed/Zed.log:

2024-09-09T10:00:27.996395-04:00 [INFO] starting language server. binary path: "<MY_HOME>/Library/Application Support/Zed/node/node-v22.5.1-darwin-arm64/bin/node", working directory: "<MY_HOME>/.config/zed", args: ["<MY_HOME>/Library/Application Support/Zed/languages/json-language-server/node_modules/vscode-langservers-extracted/bin/vscode-json-language-server", "--stdio"]
2024-09-09T10:06:26.483166-04:00 [ERROR] oneshot canceled
2024-09-09T10:06:26.483564-04:00 [ERROR] oneshot canceled
2024-09-09T10:06:27.675811-04:00 [WARN] request completed with error: failed to connect to the server
2024-09-09T10:06:27.702738-04:00 [ERROR] no worktrees when constructing ProjectLspAdapterDelegate
2024-09-09T10:06:31.490095-04:00 [INFO] building git repository, `.git` path in the worktree: ".git"
2024-09-09T10:06:31.519638-04:00 [INFO] Initializing default prettier with plugins {}
2024-09-09T10:06:31.519779-04:00 [INFO] starting language servers for Scala: metals
2024-09-09T10:06:31.51988-04:00 [INFO] starting language server "metals", path: "<MY_HOME>/repos/<MY_PROJECT>", id: 3
2024-09-09T10:06:31.523516-04:00 [ERROR] No path or contents found for buffer
2024-09-09T10:06:31.523642-04:00 [ERROR] No worktree for path: "<MY_HOME>/.config/zed/settings.json"
2024-09-09T10:06:32.901265-04:00 [INFO] starting language server. binary path: "<MY_HOME>/Library/Application Support/Coursier/bin/metals", working directory: "<MY_HOME>/repos/<MY_PROJECT>", args: ["-Dmetals.http=on"]

ℹ️ I've replaced sensitive information with pseudo tags <MY_HOME> and <MY_PROJECT>

vasigorc commented 3 weeks ago

Closing this, the problem was that I had this line "scalafmtConfigPath": ".scalafmt.conf" in my ~/.confid/zed/settings.json but scalafmt wasn't configured for the project in question. Keeping this config only in settings file of projects that do use Scalafmt fixed the issue. I found this out by looking into metals.log:

2024.11.05 12:18:27 ERROR Failed to connect with build server, no functionality will work.
java.nio.file.NoSuchFileException: <MY_PROJECT>/.scalafmt.conf
    at sun.nio.fs.UnixException.translateToIOException(UnixException.java:92)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116)
    at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:219)
    at java.nio.file.Files.newByteChannel(Files.java:371)
    at java.nio.file.Files.newByteChannel(Files.java:422)
    at java.nio.file.Files.readAllBytes(Files.java:3206)
    at scala.meta.internal.io.PlatformFileIO$.slurp(PlatformFileIO.scala:42)
    at scala.meta.internal.io.FileIO$.slurp(FileIO.scala:18)