From 80af32f0f3ddd5c05df094f7190727ac03142769 Mon Sep 17 00:00:00 2001 From: William Boman Date: Fri, 13 Oct 2023 04:25:27 +0200 Subject: [PATCH] feat(linker): use relative targets for symlinks Closes #1156. --- lua/mason-core/installer/linker.lua | 14 ++++++---- tests/mason-core/installer/linker_spec.lua | 32 +++++++++------------- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/lua/mason-core/installer/linker.lua b/lua/mason-core/installer/linker.lua index c4b325d46..3a4a0e334 100644 --- a/lua/mason-core/installer/linker.lua +++ b/lua/mason-core/installer/linker.lua @@ -73,7 +73,7 @@ end ---@async ---@param context InstallContext ---@param link_context LinkContext ----@param link_fn async fun(new_abs_path: string, target_abs_path: string): Result +---@param link_fn async fun(new_abs_path: string, target_abs_path: string, target_rel_path: string): Result local function link(context, link_context, link_fn) log.trace("Linking", context.package, link_context.type, context.links[link_context.type]) return Result.try(function(try) @@ -83,6 +83,7 @@ local function link(context, link_context, link_fn) end local new_abs_path = link_context.prefix(name, context.location) local target_abs_path = vim.fs.joinpath(context.package:get_install_path(), rel_path) + local target_rel_path = path.relative(new_abs_path, target_abs_path) do -- 1. Ensure destination directory exists @@ -109,7 +110,7 @@ local function link(context, link_context, link_fn) end -- 3. Execute link. - try(link_fn(new_abs_path, target_abs_path)) + try(link_fn(new_abs_path, target_abs_path, target_rel_path)) context.receipt:with_link(link_context.type, name, rel_path) end end) @@ -118,8 +119,8 @@ end ---@param context InstallContext ---@param link_context LinkContext local function symlink(context, link_context) - return link(context, link_context, function(new_abs_path, target_abs_path) - return Result.pcall(fs.async.symlink, target_abs_path, new_abs_path) + return link(context, link_context, function(new_abs_path, _, target_rel_path) + return Result.pcall(fs.async.symlink, target_rel_path, new_abs_path) end) end @@ -133,7 +134,8 @@ end ---@param context InstallContext local function win_bin_wrapper(context) - return link(context, LinkContext.BIN, function(new_abs_path, target_abs_path) + return link(context, LinkContext.BIN, function(new_abs_path, __, target_rel_path) + local windows_target_rel_path = target_rel_path:gsub("/", "\\") return Result.pcall( fs.async.write_file, new_abs_path, @@ -148,7 +150,7 @@ local function win_bin_wrapper(context) CALL :find_dp0 endLocal & goto #_undefined_# 2>NUL || title %%COMSPEC%% & "%s" %%* - ]]):format(target_abs_path)) + ]]):format(windows_target_rel_path)) ) end) end diff --git a/tests/mason-core/installer/linker_spec.lua b/tests/mason-core/installer/linker_spec.lua index 7ca1040bb..442c3e25f 100644 --- a/tests/mason-core/installer/linker_spec.lua +++ b/tests/mason-core/installer/linker_spec.lua @@ -1,6 +1,5 @@ local a = require "mason-core.async" local fs = require "mason-core.fs" -local path = require "mason-core.path" local registry = require "mason-registry" local stub = require "luassert.stub" local test_helpers = require "mason-test.helpers" @@ -63,13 +62,10 @@ describe("linker", function() assert.spy(fs.async.symlink).was_called(2) assert .spy(fs.async.symlink) - .was_called_with(vim.fs.joinpath(dummy:get_install_path(), "another-executable"), ctx.location:bin "another-executable") + .was_called_with("../packages/dummy/another-executable", ctx.location:bin "another-executable") assert .spy(fs.async.symlink) - .was_called_with( - vim.fs.joinpath(dummy:get_install_path(), "nested", "path", "my-executable"), - ctx.location:bin "my-executable" - ) + .was_called_with("../packages/dummy/nested/path/my-executable", ctx.location:bin "my-executable") end) it("should write executable wrapper on Windows", function() @@ -101,14 +97,15 @@ describe("linker", function() assert.spy(fs.async.symlink).was_called(0) assert.spy(fs.async.write_file).was_called(2) - assert.spy(fs.async.write_file).was_called_with( - ctx.location:bin "another-executable.cmd", - WIN_CMD_SCRIPT:format(vim.fs.joinpath(dummy:get_install_path(), "another-executable")) - ) - assert.spy(fs.async.write_file).was_called_with( - ctx.location:bin "my-executable.cmd", - WIN_CMD_SCRIPT:format(vim.fs.joinpath(dummy:get_install_path(), "nested", "path", "my-executable")) - ) + assert + .spy(fs.async.write_file) + .was_called_with(ctx.location:bin "another-executable.cmd", WIN_CMD_SCRIPT:format "..\\packages\\dummy\\another-executable") + assert + .spy(fs.async.write_file) + .was_called_with( + ctx.location:bin "my-executable.cmd", + WIN_CMD_SCRIPT:format "..\\packages\\dummy\\nested\\path\\my-executable" + ) end) it("should symlink share files", function() @@ -142,13 +139,10 @@ describe("linker", function() assert.spy(fs.async.write_file).was_called(0) assert.spy(fs.async.symlink).was_called(2) + assert.spy(fs.async.symlink).was_called_with("../packages/dummy/share-file", ctx.location:share "share-file") assert .spy(fs.async.symlink) - .was_called_with(vim.fs.joinpath(dummy:get_install_path(), "share-file"), ctx.location:share "share-file") - assert.spy(fs.async.symlink).was_called_with( - vim.fs.joinpath(dummy:get_install_path(), "nested", "path", "to", "share-file"), - ctx.location:share "nested/path/share-file" - ) + .was_called_with("../../../packages/dummy/nested/path/to/share-file", ctx.location:share "nested/path/share-file") assert.spy(fs.async.mkdirp).was_called(2) assert.spy(fs.async.mkdirp).was_called_with(ctx.location:share "nested/path")