Skip to content

Commit 75cf106

Browse files
bryankenotedlyongemallo
authored andcommitted
fix(path): only replace $ with env var if value defined (sindrets#557)
Only expand environment variables in paths when the variable is actually defined. Previously, undefined variables would be replaced with their literal name (e.g., $FOO -> FOO), which could cause unexpected behaviour with paths containing dollar signs. Fixes sindrets#556
1 parent 3990835 commit 75cf106

2 files changed

Lines changed: 3 additions & 3 deletions

File tree

lua/diffview/path.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,8 @@ function PathLib:expand(path)
295295

296296
for i = idx, #segments do
297297
local env_var = segments[i]:match("^%$(%S+)$")
298-
if env_var then
299-
segments[i] = uv.os_getenv(env_var) or env_var
298+
if env_var and uv.os_getenv(env_var) ~= nil then
299+
segments[i] = uv.os_getenv(env_var)
300300
end
301301
end
302302

lua/diffview/tests/functional/pathlib_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ describe("diffview.path", function()
248248
local pl = PathLib({ os = "unix" })
249249

250250
eq("/lorem/ipsum/dolor/foo", pl:expand("~/foo"))
251-
eq("foo/EXPANDED_FOO/EXPANDED_BAR/baz", pl:expand("foo/$VAR_FOO/$VAR_BAR/baz"))
251+
eq("foo/EXPANDED_FOO/EXPANDED_BAR/$baz", pl:expand("foo/$VAR_FOO/$VAR_BAR/$baz"))
252252
end)
253253
end)
254254

0 commit comments

Comments
 (0)