Unexpected behavior trying to set job.cwd to another value in the same function
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 32/100
Hướng nghiên cứu
Start with the reproduction in johb_test.lua and the plenary.job entry point created by j:new(), focusing on how sync() uses the mutable cwd field between runs. Run nvim --headless -c 'PlenaryBustedFile johb_test.lua' from the reproduced project tree. Done means the relevant tests pass when the same job changes cwd between buffer directories, or the expected limitation is documented.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Tree structure of the project:
johb
├── johb.lua
├ ── johb_1
│ └── johb_1.lua
└── johb_test.lua
This test tries to:
- Open two buffers
- Buffer 1 =
johb.lua - Buffer 2 =
johb_1.lua - Call
get_p:sync()withcwdset onvim.fn.expand('%:p:h') - Change focus from buffer 1 to buffer 2 via
vim.api.nvim_set_current_buf - Reset the local variable
cwdtovim.fn.expand('%:p:h') - Reset
get_p.cwdwith the local variablecwd - Call
get_p:sync()withget_p.cwdset as the local variablecwd
-- johb_test.lua
--
-- [[
-- function get_p
-- Call the command 'git rev-parse --show-toplevel'
--
-- function last_item
-- Return the last item of given path.
-- foo/bar/baz => baz
-- ]]
local j = require("plenary.job")
describe("plenary.job testing", function()
-- Set the path to reflect your local project path
local path_to_johb = '/Users/lorenzo/.config/nvim/scratch/johb/'
local path_to_johb_1 = '/Users/lorenzo/.config/nvim/scratch/johb/johb_1'
local buf_count = 0
local buf_list = {}
vim.cmd.edit('johb.lua')
vim.cmd.edit('johb_1/johb_1.lua')
local last_item = function(p)
local sp = vim.split(p, '/')
return sp[#sp]
end
buf_list = vim.api.nvim_list_bufs()
buf_count = #buf_list
local get_p = j:new({
'git',
'rev-parse',
'--show-toplevel',
})
before_each(function() vim.api.nvim_set_current_buf(1) end)
it("(pre) Should open two buffer", function()
assert.equals(2, buf_count)
end)
it("(pre) Buffer 1 should be 'johb.lua'", function()
local buf_01 = vim.api.nvim_buf_get_name(1)
assert.equals(last_item(buf_01), 'johb.lua')
end)
it("(pre) Buffer 2 should be 'johb_1.lua'", function()
local buf_02 = vim.api.nvim_buf_get_name(2)
local buf = vim.api.nvim_buf_get_name(0)
assert.equals(last_item(buf_02), 'johb_1.lua')
end)
it("(pre) Should move focus to Buffer 1", function()
local cur_buf = vim.api.nvim_buf_get_name(0)
assert.equals(last_item(cur_buf), 'johb.lua')
end)
it("1. Outside of job it returns the correct path", function()
local cwd = vim.fn.expand('%:p:h')
assert.equals(last_item(cwd), 'johb')
-- Move focus on johb_1.lua buffer
vim.api.nvim_set_current_buf(2)
-- recalculate path after moving focus to buffer 2
cwd = vim.fn.expand('%:p:h')
assert.equals(last_item(cwd), 'johb_1')
end)
it("2. Should return correct path if job.cwd is set manually", function()
get_p.cwd = path_to_johb
local ok, _ = get_p:sync()
assert.equals(last_item(ok[1]), 'johb')
-- Move focus on johb_1.lua buffer
vim.api.nvim_set_current_buf(2)
get_p.cwd = path_to_johb_1
ok, _ = get_p:sync()
assert.equals(last_item(ok[1]), 'johb_1', 'It fails when you try to set to a new cwd the job..')
end)
it("3. Should return correct path after re-set get_p.cwd", function()
local cwd = vim.fn.expand('%:p:h')[1]
get_p.cwd = cwd
local ok, _ = get_p:sync()
assert.equals(last_item(ok[1]), 'johb')
-- Move focus on johb_1.lua buffer
vim.api.nvim_set_current_buf(2)
-- fix: (maybe?)
-- to print cwd you should pass
-- cwd = vim.fn.expand('%:p:h')
-- to pass cwd as argument to job:new().cmd you should pass
-- cwd = vim.fn.expand('%:p:h')[1]
--
-- print(cwd) returns the correct path, but it pass the old one to get_p.cwd
cwd = vim.fn.expand('%:p:h')[1]
get_p.cwd = cwd
ok, _ = get_p:sync()
assert.equals(last_item(ok[1]), 'johb_1')
end)
it("4. Should return correct path after run a new job with new cwd", function()
local cwd = vim.fn.expand('%:p:h')[1]
get_p.cwd = cwd
local ok, _ = get_p:sync()
assert.equals(last_item(ok[1]), 'johb')
-- Move focus on johb_1.lua buffer
vim.api.nvim_set_current_buf(2)
-- print(cwd) returns the correct path, but it pass the old one to get_p.cwd
cwd = vim.fn.expand('%:p:h')[1]
local get_p2 = j:new({
'git',
'rev-parse',
'--show-toplevel',
cwd = cwd
})
ok, _ = get_p2:sync()
assert.equals(last_item(ok[1]), 'johb_1')
end)
end)
Take a look at test 1:
I've set local cwd = vim.fn.expand('%:p:h')
Move focus to buffer 2 and reset that with the same expand
The test passed.
In tests 2, 3 I've done the same thing and set the job.cwd property to the local cwd but the test failed.
This is something strange to me because in tests 2, 3, 4 only the second part of the test fails when I reset the cwd variable
To be sure to reproduce exactly how I do:
- Recreate the tree structure as mentioned in this issue. ( contents of
johb.luaandjohb_1.luait's not important, I've leaved them both empty ) - Replace
local path_to_johbandlocal path_to_johb_1with the correct path, based on your local machine - Run this command from the root folder:
nvim --headless -c 'PlenaryBustedFile johb_test.lua'
Running the command from the terminal returns this result:
❯ echo $PWD && git -C $PWD rev-parse --show-toplevel
/Users/lorenzo/.config/nvim/scratch/johb/johb_1 # this is the same path passed as path_to_johb
/Users/lorenzo/.config/nvim/scratch/johb/johb_1
❯ echo $PWD && git -C $PWD rev-parse --show-toplevel
/Users/lorenzo/.config/nvim/scratch/johb # this is the same path passed as path_to_johb_1
/Users/lorenzo/.config/nvim/scratch/johb
It's possible that I've missed or misunderstood something...
- Ngôn ngữ chính
- Lua
- Star
- 3.5k
- Fork
- 341
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của nvim-lua/plenary.nvim
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
nvim-lua/plenary.nvim#682 ·
-
uniform usage of `os.getenv`Có thể đã có người làm @rakotomandimby đã nhận 123 ngày trước. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 72/100
nvim-lua/plenary.nvim#680 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 45/100
nvim-lua/plenary.nvim#675 ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 28/100
nvim-lua/plenary.nvim#672 · 1 bình luận ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 25/100
nvim-lua/plenary.nvim#671 · 1 bình luận ·
Tất cả issue của nvim-lua/plenary.nvim
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
mogenson/PaperWM.spoon#178 ·
-
enhancement good first issue
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
-
ci: Verify install never runs examples_install or tests_install, so a clear they widen passesĐang mởseverity: low
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
luainkernel/lunatik#1876 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 91/100
-
bug ready ticket
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Domktorymysli/vclu#5 ·