Skip to content

Commit dd77a91

Browse files
9Y5justinmk
andauthored
[#104] Set --embed flag by default to avoid hanging on RPC calls (#161)
* [#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 <[email protected]>
1 parent b82f9d5 commit dd77a91

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

.github/workflows/labeler.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: Pull Request Labeler
22

33
on:
4-
- pull_request
4+
- pull_request_target
55

66
jobs:
77
triage:

nvim/nvim.go

+33-7
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,14 @@ type ChildProcessOption struct {
121121
}
122122

123123
type childProcessOptions struct {
124-
ctx context.Context
125-
logf func(string, ...interface{})
126-
command string
127-
dir string
128-
args []string
129-
env []string
130-
serve bool
124+
ctx context.Context
125+
logf func(string, ...interface{})
126+
command string
127+
dir string
128+
args []string
129+
env []string
130+
serve bool
131+
disableEmbed bool
131132
}
132133

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

191+
// ChildProcessDisableEmbed disables the --embed flag of nvim.
192+
// See: https://neovim.io/doc/user/starting.html#--embed for details.
193+
func ChildProcessDisableEmbed() ChildProcessOption {
194+
return ChildProcessOption{func(cpos *childProcessOptions) {
195+
cpos.disableEmbed = true
196+
}}
197+
}
198+
199+
// appendEmbedFlagIfNeeded appends the --embed flag, if it is not yet added.
200+
// This behavior can be overriden by setting the ChildProcessDisableEmbed() process option.
201+
func appendEmbedFlagIfNeeded(cpos *childProcessOptions) {
202+
for _, arg := range cpos.args {
203+
if arg == "--embed" {
204+
return
205+
}
206+
}
207+
if !cpos.disableEmbed {
208+
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")
209+
cpos.args = append(cpos.args, "--embed")
210+
return
211+
}
212+
}
213+
190214
// NewChildProcess returns a client connected to stdin and stdout of a new
191215
// child process.
192216
func NewChildProcess(options ...ChildProcessOption) (*Nvim, error) {
@@ -200,6 +224,8 @@ func NewChildProcess(options ...ChildProcessOption) (*Nvim, error) {
200224
cpo.f(cpos)
201225
}
202226

227+
appendEmbedFlagIfNeeded(cpos)
228+
203229
cmd := exec.CommandContext(cpos.ctx, cpos.command, cpos.args...)
204230
cmd.Env = cpos.env
205231
cmd.Dir = cpos.dir

nvim/nvim_test.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ func newChildProcess(tb testing.TB, opts ...ChildProcessOption) (v *Nvim) {
3030
"-u", "NONE",
3131
"-n",
3232
"-i", "NONE",
33-
"--embed",
3433
"--headless",
3534
),
3635
ChildProcessContext(ctx),
@@ -182,6 +181,18 @@ func TestCallWithNoArgs(t *testing.T) {
182181
}
183182
}
184183

184+
func TestCallWithNoArgsWithDisabledEmbed(t *testing.T) {
185+
t.Parallel()
186+
187+
v := newChildProcess(t, ChildProcessArgs("--embed"), ChildProcessDisableEmbed())
188+
189+
var wd string
190+
err := v.Call("getcwd", &wd)
191+
if err != nil {
192+
t.Fatal(err)
193+
}
194+
}
195+
185196
func TestStructValue(t *testing.T) {
186197
t.Parallel()
187198

0 commit comments

Comments
 (0)