Skip to content

Commit

Permalink
[#104] Set --embed flag by default to avoid hanging on RPC calls (#161)
Browse files Browse the repository at this point in the history
* [#104] Add --embed flag by default. Added option to disable this default behavior.

* Fix labeler to run on forks.

* Update Comment

---------

Co-authored-by: Justin M. Keyes <justinkz@gmail.com>
  • Loading branch information
9Y5 and justinmk committed Jul 16, 2023
1 parent b82f9d5 commit dd77a91
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/labeler.yaml
@@ -1,7 +1,7 @@
name: Pull Request Labeler

on:
- pull_request
- pull_request_target

jobs:
triage:
Expand Down
40 changes: 33 additions & 7 deletions nvim/nvim.go
Expand Up @@ -121,13 +121,14 @@ type ChildProcessOption struct {
}

type childProcessOptions struct {
ctx context.Context
logf func(string, ...interface{})
command string
dir string
args []string
env []string
serve bool
ctx context.Context
logf func(string, ...interface{})
command string
dir string
args []string
env []string
serve bool
disableEmbed bool
}

// ChildProcessArgs specifies the command line arguments. The application must
Expand Down Expand Up @@ -187,6 +188,29 @@ func ChildProcessLogf(logf func(string, ...interface{})) ChildProcessOption {
}}
}

// ChildProcessDisableEmbed disables the --embed flag of nvim.
// See: https://neovim.io/doc/user/starting.html#--embed for details.
func ChildProcessDisableEmbed() ChildProcessOption {
return ChildProcessOption{func(cpos *childProcessOptions) {
cpos.disableEmbed = true
}}
}

// appendEmbedFlagIfNeeded appends the --embed flag, if it is not yet added.
// This behavior can be overriden by setting the ChildProcessDisableEmbed() process option.
func appendEmbedFlagIfNeeded(cpos *childProcessOptions) {
for _, arg := range cpos.args {
if arg == "--embed" {
return
}
}
if !cpos.disableEmbed {
cpos.logf("[go-client/nvim] Warning: '--embed' flag missing, appending by default. It enables RPC calls via stdin/stdout. To disable this behavior, add ChildProcessDisableEmbed(). More info: https://neovim.io/doc/user/starting.html#--embed")
cpos.args = append(cpos.args, "--embed")
return
}
}

// NewChildProcess returns a client connected to stdin and stdout of a new
// child process.
func NewChildProcess(options ...ChildProcessOption) (*Nvim, error) {
Expand All @@ -200,6 +224,8 @@ func NewChildProcess(options ...ChildProcessOption) (*Nvim, error) {
cpo.f(cpos)
}

appendEmbedFlagIfNeeded(cpos)

cmd := exec.CommandContext(cpos.ctx, cpos.command, cpos.args...)
cmd.Env = cpos.env
cmd.Dir = cpos.dir
Expand Down
13 changes: 12 additions & 1 deletion nvim/nvim_test.go
Expand Up @@ -30,7 +30,6 @@ func newChildProcess(tb testing.TB, opts ...ChildProcessOption) (v *Nvim) {
"-u", "NONE",
"-n",
"-i", "NONE",
"--embed",
"--headless",
),
ChildProcessContext(ctx),
Expand Down Expand Up @@ -182,6 +181,18 @@ func TestCallWithNoArgs(t *testing.T) {
}
}

func TestCallWithNoArgsWithDisabledEmbed(t *testing.T) {
t.Parallel()

v := newChildProcess(t, ChildProcessArgs("--embed"), ChildProcessDisableEmbed())

var wd string
err := v.Call("getcwd", &wd)
if err != nil {
t.Fatal(err)
}
}

func TestStructValue(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit dd77a91

Please sign in to comment.