diff options
| author | William Boman <william@redwill.se> | 2021-12-07 17:36:40 +0100 |
|---|---|---|
| committer | William Boman <william@redwill.se> | 2021-12-07 17:36:40 +0100 |
| commit | 18fa978dde6277617c269b726532eada9119988a (patch) | |
| tree | 4b280855e7425dd9aeed8454cb011cdad650947d /lua/nvim-lsp-installer/installers | |
| parent | fix(quick_lint_js): use latest dist instead of collecting it via github API (... (diff) | |
| download | mason-18fa978dde6277617c269b726532eada9119988a.tar mason-18fa978dde6277617c269b726532eada9119988a.tar.gz mason-18fa978dde6277617c269b726532eada9119988a.tar.bz2 mason-18fa978dde6277617c269b726532eada9119988a.tar.lz mason-18fa978dde6277617c269b726532eada9119988a.tar.xz mason-18fa978dde6277617c269b726532eada9119988a.tar.zst mason-18fa978dde6277617c269b726532eada9119988a.zip | |
fix(installers/npm): dont apply global-style for standalone npm install
Diffstat (limited to 'lua/nvim-lsp-installer/installers')
| -rw-r--r-- | lua/nvim-lsp-installer/installers/npm.lua | 60 | ||||
| -rw-r--r-- | lua/nvim-lsp-installer/installers/pip3.lua | 12 |
2 files changed, 39 insertions, 33 deletions
diff --git a/lua/nvim-lsp-installer/installers/npm.lua b/lua/nvim-lsp-installer/installers/npm.lua index afd697ab..b695a9b2 100644 --- a/lua/nvim-lsp-installer/installers/npm.lua +++ b/lua/nvim-lsp-installer/installers/npm.lua @@ -6,6 +6,8 @@ local std = require "nvim-lsp-installer.installers.std" local platform = require "nvim-lsp-installer.platform" local process = require "nvim-lsp-installer.process" +local list_copy = Data.list_copy + local M = {} local npm = platform.is_win and "npm.cmd" or "npm" @@ -24,39 +26,42 @@ local function ensure_npm(installer) } end -local function create_installer(read_version_from_context) +---@param standalone boolean @If true, will run `npm install` as a standalone command, with no consideration to the surrounding installer context (i.e. the requested version in context is ignored, global-style is not applied). +local function create_installer(standalone) ---@param packages string[] return function(packages) return ensure_npm( ---@type ServerInstallerFunction - function(_, callback, context) - local pkgs = Data.list_copy(packages or {}) + function(_, callback, ctx) + local pkgs = list_copy(packages or {}) local c = process.chain { - cwd = context.install_dir, - stdio_sink = context.stdio_sink, + cwd = ctx.install_dir, + stdio_sink = ctx.stdio_sink, } - -- Use global-style. The reasons for this are: - -- a) To avoid polluting the executables (aka bin-links) that npm creates. - -- b) The installation is, after all, more similar to a "global" installation. We don't really gain - -- any of the benefits of not using global style (e.g., deduping the dependency tree). - -- - -- We write to .npmrc manually instead of going through npm because managing a local .npmrc file - -- is a bit unreliable across npm versions (especially <7), so we take extra measures to avoid - -- inadvertently polluting global npm config. - fs.append_file(path.concat { context.install_dir, ".npmrc" }, "global-style=true") + if not standalone then + -- Use global-style. The reasons for this are: + -- a) To avoid polluting the executables (aka bin-links) that npm creates. + -- b) The installation is, after all, more similar to a "global" installation. We don't really gain + -- any of the benefits of not using global style (e.g., deduping the dependency tree). + -- + -- We write to .npmrc manually instead of going through npm because managing a local .npmrc file + -- is a bit unreliable across npm versions (especially <7), so we take extra measures to avoid + -- inadvertently polluting global npm config. + fs.append_file(path.concat { ctx.install_dir, ".npmrc" }, "global-style=true") + end -- stylua: ignore start - if not (fs.dir_exists(path.concat { context.install_dir, "node_modules" }) or - fs.file_exists(path.concat { context.install_dir, "package.json" })) + if not (fs.dir_exists(path.concat { ctx.install_dir, "node_modules" }) or + fs.file_exists(path.concat { ctx.install_dir, "package.json" })) then -- Create a package.json to set a boundary for where npm installs packages. c.run(npm, { "init", "--yes", "--scope=lsp-installer" }) end - if read_version_from_context and context.requested_server_version and #pkgs > 0 then + if not standalone and ctx.requested_server_version and #pkgs > 0 then -- The "head" package is the recipient for the requested version. It's.. by design... don't ask. - pkgs[1] = ("%s@%s"):format(pkgs[1], context.requested_server_version) + pkgs[1] = ("%s@%s"):format(pkgs[1], ctx.requested_server_version) end -- stylua: ignore end @@ -68,21 +73,22 @@ local function create_installer(read_version_from_context) end ---Creates an installer that installs the provided packages. Will respect user's requested version. -M.packages = create_installer(true) +M.packages = create_installer(false) + ---Creates an installer that installs the provided packages. Will NOT respect user's requested version. ---This is useful in situation where there's a need to install an auxiliary npm package. -M.install = create_installer(false) +M.install = create_installer(true) ---Creates a server installer that executes the given executable. ---@param executable string ---@param args string[] function M.exec(executable, args) ---@type ServerInstallerFunction - return function(_, callback, context) - process.spawn(M.executable(context.install_dir, executable), { + return function(_, callback, ctx) + process.spawn(M.executable(ctx.install_dir, executable), { args = args, - cwd = context.install_dir, - stdio_sink = context.stdio_sink, + cwd = ctx.install_dir, + stdio_sink = ctx.stdio_sink, }, callback) end end @@ -92,11 +98,11 @@ end function M.run(script) return ensure_npm( ---@type ServerInstallerFunction - function(_, callback, context) + function(_, callback, ctx) process.spawn(npm, { args = { "run", script }, - cwd = context.install_dir, - stdio_sink = context.stdio_sink, + cwd = ctx.install_dir, + stdio_sink = ctx.stdio_sink, }, callback) end ) diff --git a/lua/nvim-lsp-installer/installers/pip3.lua b/lua/nvim-lsp-installer/installers/pip3.lua index b0eb4611..4282bcc8 100644 --- a/lua/nvim-lsp-installer/installers/pip3.lua +++ b/lua/nvim-lsp-installer/installers/pip3.lua @@ -23,22 +23,22 @@ local function create_installer(python_executable, packages) }, }, ---@type ServerInstallerFunction - function(_, callback, context) + function(_, callback, ctx) local pkgs = Data.list_copy(packages or {}) local c = process.chain { - cwd = context.install_dir, - stdio_sink = context.stdio_sink, + cwd = ctx.install_dir, + stdio_sink = ctx.stdio_sink, } c.run(python_executable, { "-m", "venv", REL_INSTALL_DIR }) - if context.requested_server_version then + if ctx.requested_server_version then -- The "head" package is the recipient for the requested version. It's.. by design... don't ask. - pkgs[1] = ("%s==%s"):format(pkgs[1], context.requested_server_version) + pkgs[1] = ("%s==%s"):format(pkgs[1], ctx.requested_server_version) end local install_command = { "-m", "pip", "install", "-U" } vim.list_extend(install_command, settings.current.pip.install_args) - c.run(M.executable(context.install_dir, "python"), vim.list_extend(install_command, pkgs)) + c.run(M.executable(ctx.install_dir, "python"), vim.list_extend(install_command, pkgs)) c.spawn(callback) end, |
