Skip to content

How to extend Package in v2? #1

@Frederick888

Description

@Frederick888

I'd like to always use the latest Git version of java-test and java-debug-adapter. So when I was on Mason v1, I

  1. Cloned those two Git repos
  2. Added a custom user.mason-repository in my config
  3. Wrote a LocalPackage that extended Package
    a. LocalPackage:get_installed_version returned the SHA of Git HEAD
    b. LocalPackage:check_new_version fetched Git remote and returned the SHA of Git origin/HEAD
  4. Put the two packages in lua/user/mason-registry
    a. They used LocalPackage.new()
    b. Their install ran git reset --hard origin/HEAD, built the package, and finally symlinked the files

Now I'm working on v2 migration in lazyvim15 branch, and I'm getting the error below:

.lua:28: attempt to index field 'instances' (a nil value)
stack traceback:
        .../nvim/lazy/mason.nvim/lua/mason-registry/sources/lua.lua:28: in function 'get_package'
        ...l/share/nvim/lazy/mason.nvim/lua/mason-registry/init.lua:26: in function <...l/share/nvim/lazy/mason.nvim/lua/mason-registry/init.lua:24>
        vim/shared.lua: in function 'get_all_packages'
        ...cal/share/nvim/lazy/mason.nvim/lua/mason/ui/instance.lua:727: in main chunk
        [C]: in function 'require'
        .../.local/share/nvim/lazy/mason.nvim/lua/mason/ui/init.lua:9: in function 'open'
        ...cal/share/nvim/lazy/mason.nvim/lua/mason/api/command.lua:5: in function <...cal/share/nvim/lazy/mason.nvim/lua/mason/api/command.lua:4>
        [C]: in function 'cmd'
        .../share/nvim/lazy/lazy.nvim/lua/lazy/core/handler/cmd.lua:48: in function <.../share/nvim/lazy/lazy.nvim/lua/lazy/core/handler/cmd.lua:16>
        [C]: in function 'nvim_exec2'
        vim/_editor.lua: in function 'action'
        ...cal/share/nvim/lazy/snacks.nvim/lua/snacks/dashboard.lua:693: in function <...cal/share/nvim/lazy/snacks.nvim/lua/snacks/dashboard.lua:692>

I'm mainly stuck on how I can pass the registry itself to the new Package:new(spec, reg) at the moment. Hopefully that's my last problem. Or maybe the whole thing is an overkill and there's a much simpler way to do Git + build + symlink?

I'd appreciate a little guidance. Thank you!


Update: Also tried getting rid of Package:new() let Mason hydrate the packages itself https://git.tsundere.moe/Frederick888/frederick-settings/-/commit/9b14146d0dbf6e87c60186295e8c03e0508d8d6b

local LocalPackage = require('user.mason-registry.local-package')

local pkg = setmetatable({
  name = 'java-test',
  description = [[
    The Test Runner for Java works with java-debug-adapter to provide the following features:
    - Run/Debug test cases
    - Customize test configurations
    - View test report
    - View tests in Test Explorer

    Enables support for the following test frameworks:

    - JUnit 4 (v4.8.0+)
    - JUnit 5 (v5.1.0+)
    - TestNG (v6.8.0+)
  ]],
  homepage = 'https://github.com/microsoft/vscode-java-test',
  licenses = { 'MIT' },
  languages = { 'Java' },
  categories = { 'DAP', 'User' },
  source = {
    id = 'pkg:mason/vscode-java-test',
    ---@param ctx InstallContext
    install = function(ctx)
      local package_spec
      for _, s in ipairs(ctx.package.registry:get_all_package_specs()) do
        if s.name == ctx.package.name then
          package_spec = s
          break
        end
      end
      local requested_version = ctx.requested_version or 'origin/HEAD'
      local source_path = package_spec:get_source_path()
      ctx.spawn.git({ '-C', source_path, 'reset', '--hard', requested_version })
      ctx.spawn.npm({ '-C', source_path, 'install' })
      ctx.spawn.npm({ '-C', source_path, 'run', 'build-plugin' })
      require('user.mason-registry.symlink-installer')('%.jar$', 'extension/server')(ctx)
    end,
  },
}, { __index = LocalPackage })

function pkg:get_source_path()
  return vim.fs.normalize('~/Programming/JavaScript/vscode-java-test')
end

function pkg:get_artefact_path()
  return vim.fs.normalize('~/Programming/JavaScript/vscode-java-test/server')
end

return pkg

Installation worked. However since all my LocalPackage methods, including get_latest_version() and get_installed_version(), are now only in the package specs rather than packages themselves, Mason cannot show / detect their versions correctly as it just uses the default ones from AbstractPackage.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions