Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Got MethodNotFound error if yaml.schemas matches filename fleet.yaml #1004

Open
1 of 4 tasks
imroc opened this issue Dec 21, 2024 · 1 comment
Open
1 of 4 tasks

Got MethodNotFound error if yaml.schemas matches filename fleet.yaml #1004

imroc opened this issue Dec 21, 2024 · 1 comment

Comments

@imroc
Copy link

imroc commented Dec 21, 2024

Describe the bug

Got MethodNotFound error if yaml.schemas matches filename fleet.yaml, no error if changed to other filename.

Image

Image

Expected Behavior

No diagonostics error.

Current Behavior

Ddiagonostics error:

Problems loading reference 'schemaservice:/Users/roc/dotfiles/kubeschemas/fleet-agones.dev-v1.json': Unable to load schema from 'schemaservice:/Users/roc/dotfiles/kubeschemas/fleet-agones.dev-v1.json': MethodNotFound. [768]

Steps to Reproduce

Neovim yamlls config matches filename fleet.yaml:

Image

No error output if changed to another filename which not equal fleet.yaml.

Environment

  • Windows
  • Mac
  • Linux
  • other (please specify)

yaml-language-server is managed by Mason which uses latest release 1.15.0:

Image

About My Neovim Usage

I write scripts to parse kubernetes YAML file, set json schema to yamlls according to apiVersion and kind, append current file path to related json schema.

The most important is to customize yamlls on_attach function, share my scripts:

local M = {}

local function remove_prefix(str, prefix)
  if str:sub(1, #prefix) == prefix then
    return str:sub(#prefix + 1)
  else
    return str
  end
end

local path_separator = package.config:sub(1, 1)

local kubeschemas_dir = os.getenv("HOME") .. path_separator .. ".config" .. path_separator .. "kubeschemas"

-- local kubeschemas = kubeschemas_dir .. path_separator .. "kubernetes.json"
local regKind = vim.regex([[\v^kind: \S+$]])
local regApiVersion = vim.regex([[\v^apiVersion: \S+$]])
local regHelm = vim.regex([[\v\{\{.+\}\}]])
local detach = function(client, bufnr)
  vim.diagnostic.enable(false, { bufnr = bufnr })
  vim.defer_fn(function()
    vim.diagnostic.reset(nil, bufnr)
    vim.lsp.buf_detach_client(bufnr, client.id)
  end, 500)
end

local get_new_settings = function(client, bufnr)
  --  Ingore kustomization.yaml, it has schema in schemastore.
  local buf_path = vim.api.nvim_buf_get_name(bufnr)
  if buf_path:match("kustomization.ya?ml$") then
    return client.config.settings
  end

  -- remove yamlls from not yaml files
  -- https://github.com/towolf/vim-helm/issues/15
  if vim.bo[bufnr].buftype ~= "" or vim.bo[bufnr].filetype == "helm" then
    -- detach(client, bufnr)
    return nil
  end

  local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
  local kind = nil
  local apiVersion = nil
  for _, line in ipairs(lines) do
    if regHelm:match_str(line) then -- ignore helm template
      detach(client, bufnr)
      return nil
    end
    if not kind then
      if regKind:match_str(line) then
        kind = remove_prefix(line, "kind: ")
        if apiVersion then
          break
        end
      end
    end
    if not apiVersion then
      if regApiVersion:match_str(line) then
        apiVersion = remove_prefix(line, "apiVersion: ")
        if kind then
          break
        end
      end
    end
  end

  if kind then
    local filename = kind
    if apiVersion then
      filename = filename .. "-" .. string.gsub(apiVersion, "/", "-")
    end
    filename = string.lower(filename)
    local jsonschema = kubeschemas_dir .. path_separator .. filename .. ".json"
    if vim.uv.fs_stat(jsonschema) then
      local schema = client.config.settings.yaml.schemas[jsonschema] or {}
      local bufuri = vim.uri_from_bufnr(bufnr)
      if not vim.tbl_contains(schema, bufuri) then
        vim.list_extend(schema, { bufuri })
        client.config.settings.yaml.schemas[jsonschema] = schema
        return client.config.settings
      end
    end
  end
end

M.on_attach = function(client, bufnr)
  if client.name ~= "yamlls" then
    return
  end

  local new_settings = get_new_settings(client, bufnr)
  if new_settings then
    client.server_capabilities.documentRangeFormattingProvider = true
    client.workspace_did_change_configuration(client.config.settings)
  end
end

return M
@imroc
Copy link
Author

imroc commented Dec 21, 2024

Oh I see, it's conflict with schemastore's fileMatch, solved by ignore fleet.yaml in schemastore:

              require("schemastore").yaml.schemas({
                ignore = {
                  "fleet.yaml",
                },
             )

fleet.yaml is Rancher Fleet configuration file in schemastore's catalog.json:

    {
      "name": "Rancher Fleet",
      "description": "Rancher Fleet fleet.yaml configuration file",
      "fileMatch": ["fleet.yaml"],
      "url": "https://json.schemastore.org/rancher-fleet-0.8.json",
      "versions": {
        "0.5": "https://json.schemastore.org/rancher-fleet-0.5.json",
        "0.8": "https://json.schemastore.org/rancher-fleet-0.8.json"
      }
    },

Anyway, MethodNotFound error is meaningless, it would be great to impreve error message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant