someone-stole-my-name / yaml-companion.nvim

Get, set and autodetect YAML schemas in your buffers.
MIT License
198 stars 18 forks source link

Kubernetes completion not working #45

Open mosheavni opened 2 months ago

mosheavni commented 2 months ago

Hi, I'll start off by saying I am not sure if the issue is relevant here or on the yaml-language-server issues. I read through many issues regarding registering kubernetes schemas but not sure my issue is the same.

If I set in my lspconfig.settings.yaml.schemas.kubernetes = '/*' I get kubernetes completions, but then I get it for EVERY yaml file and it's obviously wrong. If I remove the kubernetes = '/*', then there are no Kubernetes completion, even after I select a schema.

local yaml_cfg = require('yaml-companion').setup {
  builtin_matchers = {
    -- Detects Kubernetes files based on content
    kubernetes = { enabled = true },
  },
  schemas = {
    {
      name = 'Kubernetes 1.27.12',
      uri = 'https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/v1.27.12-standalone-strict/all.json',
    },
    {
      name = 'Kubernetes 1.26.14',
      uri = 'https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/v1.26.14-standalone-strict/all.json',
    },
  },
  lspconfig = {
    on_attach = default_on_attach,
    capabilities = capabilities,
    cmd = { 'node', vim.fn.expand '~/Repos/yaml-language-server/out/server/src/server.js', '--stdio' },
    settings = {
      yaml = {
        -- if these lines are commented out, there are no completions
        -- schemas = {
        --   kubernetes = '/*',
        -- },
      },
    },
  },
}
require('lspconfig')['yamlls'].setup(yaml_cfg)

Using minimal_init.lua and comparing the logs, the only difference I see is:

image

To reproduce:

save minimal_init.lua

minimal_init.lua ```lua local function join_paths(...) local path_sep = '/' local result = table.concat({ ... }, path_sep) return result end local temp_dir = vim.uv.os_getenv 'TEMP' or '/tmp' local package_root = join_paths(temp_dir, 'nvim', 'site', 'lazy') local lazypath = join_paths(temp_dir, 'nvim', 'site') .. '/lazy/lazy.nvim' if not vim.loop.fs_stat(lazypath) then vim .system({ 'git', 'clone', '--filter=blob:none', '--single-branch', 'https://github.com/folke/lazy.nvim.git', lazypath, }, { text = true }) :wait() end vim.opt.runtimepath:prepend(lazypath) _G.load_config = function() vim.lsp.set_log_level 'trace' require('vim.lsp.log').set_format_func(vim.inspect) ---@diagnostic disable-next-line: missing-fields require('nvim-treesitter.configs').setup { highlight = { enable = true }, } -- Set up completion local cmp = require 'cmp' cmp.setup { mapping = cmp.mapping.preset.insert { [''] = cmp.mapping.complete(), [''] = cmp.mapping.confirm { select = true }, }, sources = cmp.config.sources { { name = 'nvim_lsp' }, }, } -- Set up capabilities local capabilities = require('cmp_nvim_lsp').default_capabilities() local yaml_lspconfig = { on_attach = function() print 'YAML LSP attached' end, capabilities = capabilities, cmd = { 'node', vim.fn.expand '~/Repos/yaml-language-server/out/server/src/server.js', '--stdio' }, settings = { yaml = { schemas = { kubernetes = '/*', }, }, }, } -- Add the server that troubles you here local yaml_cfg = require('yaml-companion').setup { builtin_matchers = { -- Detects Kubernetes files based on content kubernetes = { enabled = true }, }, schemas = { { name = 'Kubernetes 1.27.12', uri = 'https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/v1.27.12-standalone-strict/all.json', }, { name = 'Kubernetes 1.26.14', uri = 'https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/v1.26.14-standalone-strict/all.json', }, }, lspconfig = yaml_lspconfig, } require('lspconfig')['yamlls'].setup(yaml_cfg) print [[You can find your log at $HOME/.cache/nvim/lsp.log. Please paste in a github issue under a details tag as described in the issue template.]] end require('lazy').setup({ 'habamax/vim-habamax', 'neovim/nvim-lspconfig', { 'hrsh7th/nvim-cmp', dependencies = { 'hrsh7th/cmp-nvim-lsp', }, }, 'nvim-treesitter/nvim-treesitter', 'someone-stole-my-name/yaml-companion.nvim', config = function() local configs = require 'nvim-treesitter.configs' ---@diagnostic disable-next-line: missing-fields configs.setup { ensure_installed = 'all', highlight = { enable = true, additional_vim_regex_highlighting = false, }, } end, }, { root = package_root, }) _G.load_config() vim.cmd [[colorscheme habamax]] ```

save a kubernetes resource yaml:

pod.yaml ```yaml --- apiVersion: v1 kind: Pod metadata: name: init-demo spec: containers: - name: nginx image: nginx ports: - containerPort: 80 volumeMounts: - name: workdir mountPath: /usr/share/nginx/html resources: limits: memory: '1Gi' cpu: '800m' requests: memory: '700Mi' cpu: '400m' # These containers are run during pod initialization initContainers: - name: install image: busybox command: - wget - '-O' - '/work-dir/index.html' - http://kubernetes.io volumeMounts: - name: workdir mountPath: '/work-dir' dnsPolicy: Default volumes: - name: workdir emptyDir: {} ```

open the pod.yaml using minimal_init.lua:

nvim -nu minimal_init.lua pod.yaml

make sure the lsp started and try to get completions in the metadata: block. no completions comment out kubernetes = '/*', on minimal_init.lua open the file again, now there are completions. now open a completely different yaml file, and there would be diagnostics for Kubernetes schema even though it's not a kubernetes file.

Desired behavior

When I open a kubernetes file, get kubernetes completion. when I open every other file, use schema store or set no schema at all.

If there's any more information I can add, please let me know. Thanks.

szechp commented 2 months ago

save yourself the hassle, this repo seems to be dead. this guy made a out of the box implementation of crd detection:

https://github.com/msvechla/yaml-companion.nvim/tree/kubernetes_crd_detection

mosheavni commented 2 months ago

save yourself the hassle, this repo seems to be dead. this guy made a out of the box implementation of crd detection:

https://github.com/msvechla/yaml-companion.nvim/tree/kubernetes_crd_detection

switched to using it, but still no completion if I don't specify kubernetes inside schemas = {}

agorgl commented 1 month ago

Bumped into this too, it worked before. I based my setup to this one: https://www.arthurkoziel.com/json-schemas-in-neovim/