Skip to content

Unable to spawn process #647

@JonathanFrias

Description

@JonathanFrias

I kept seeing an error coming from this library when using my nvim setup. The issue is that special buffers like nvim's terminal report a invalid cwd term:///path/to/file which causes the call to spawn to fail with some error. I have been able to trace down a solution that works for me in the following diff. I doubt this code is acceptable for this project, but I'm sharing since it works for me.

diff --git a/lua/plenary/job.lua b/lua/plenary/job.lua
index 7f54971..4c69e48 100644
--- a/lua/plenary/job.lua
+++ b/lua/plenary/job.lua
@@ -400,7 +400,23 @@ function Job:_execute()
     self:_user_on_start()
   end

-  self.handle, self.pid = uv.spawn(options.command, options, shutdown_factory(self, options))
+  local spawnOptions = {}
+
+  for k, v in pairs(options) do
+    spawnOptions[k] = v
+  end
+
+  if spawnOptions.cwd then
+    local stat = uv.fs_stat(spawnOptions.cwd)
+    if not stat or stat.type ~= "directory" then
+      -- Directory doesn't exist, remove cwd from options
+      spawnOptions.cwd = nil
+    end
+  end
+
+  self.handle, self.pid = uv.spawn(spawnOptions.command, spawnOptions, shutdown_factory(self, spawnOptions))

   if not self.handle then
     error(debug.traceback("Failed to spawn process: " .. vim.inspect(self)))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions