Skip to content

Commit eabdd30

Browse files
anton-107Isaac
andcommitted
aitools: accept a Windows shim probe that printed help but exited non-zero
On Windows, the `codex.cmd` and `copilot.bat` shims can print the full `plugin --help` and then exit 1. Because of that, `databricks aitools install` reported a working CLI as `CLI_NOT_ON_PATH`. The probe now accepts that one case: ```diff probePluginCLI(agent) bin = lookPath(agent CLI) err = run `<bin> plugin --help` (20s timeout) - if err: block CLI_NOT_ON_PATH + if err and not windowsShimPrintedHelp(bin, err): block CLI_NOT_ON_PATH +windowsShimPrintedHelp(bin, err) + GOOS is windows + and bin ends in .cmd or .bat + and err is not a timeout + and stdout+stderr has a `Usage: <bin> plugin` line + and indented `install`/`add` and `marketplace` command lines ``` Changelog fragment: `.nextchanges/cli/aitools-windows-shim-probe.md`. Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 2408150 commit eabdd30

3 files changed

Lines changed: 114 additions & 1 deletion

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* `databricks aitools install` on Windows accepts a coding agent CLI shim (`.cmd` or `.bat`) that prints its plugin help and then exits with an error, instead of reporting the CLI as not on PATH. ([#7476](https://github.com/databricks/cli/pull/7476))

‎libs/aitools/installer/plugin.go‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import (
66
"fmt"
77
"maps"
88
"os/exec"
9+
"path/filepath"
10+
"regexp"
11+
"runtime"
912
"strings"
1013
"time"
1114

@@ -265,12 +268,48 @@ func probePluginCLI(ctx context.Context, agent *agents.Agent) (string, error) {
265268
if err != nil {
266269
return "", &BlockedError{Agent: agent.Name, Reason: ReasonCLINotOnPath, Detail: err.Error()}
267270
}
268-
if _, err := runAgentCmd(ctx, pluginProbeTimeout, []string{bin, "plugin", "--help"}); err != nil {
271+
if _, err := runAgentCmd(ctx, pluginProbeTimeout, []string{bin, "plugin", "--help"}); err != nil && !windowsShimPrintedHelp(bin, err) {
269272
return "", newBlockedError(agent, ReasonCLINotOnPath, err)
270273
}
271274
return bin, nil
272275
}
273276

277+
// probeGOOS is runtime.GOOS; tests replace it to exercise the Windows path.
278+
var probeGOOS = runtime.GOOS
279+
280+
// pluginHelpUsage and pluginHelpCommand match the help that `<agent> plugin
281+
// --help` prints: a "Usage: <bin> plugin" line and an indented command list
282+
// that includes install and marketplace. An error message that merely
283+
// mentions those words has neither shape.
284+
var (
285+
pluginHelpUsage = regexp.MustCompile(`(?im)^usage:[ \t]+\S+[ \t]+plugin\b`)
286+
pluginHelpInstall = regexp.MustCompile(`(?m)^[ \t]+(install|add)\b`)
287+
pluginHelpMarket = regexp.MustCompile(`(?m)^[ \t]+marketplace\b`)
288+
)
289+
290+
// windowsShimPrintedHelp reports whether a failed probe is the Windows shim
291+
// quirk: codex.cmd and copilot.bat can print the full `plugin --help` and then
292+
// exit 1, which marked a working CLI as missing. It is deliberately narrow: Windows only, a
293+
// .cmd/.bat shim only, never a timeout, and only when the output is the help
294+
// itself, so a real failure on any platform still blocks the install.
295+
func windowsShimPrintedHelp(bin string, err error) bool {
296+
if probeGOOS != "windows" {
297+
return false
298+
}
299+
if ext := strings.ToLower(filepath.Ext(bin)); ext != ".cmd" && ext != ".bat" {
300+
return false
301+
}
302+
if _, ok := errors.AsType[*agentCmdTimeoutError](err); ok {
303+
return false
304+
}
305+
perr, ok := errors.AsType[*process.ProcessError](err)
306+
if !ok {
307+
return false
308+
}
309+
out := perr.Stdout + "\n" + perr.Stderr
310+
return pluginHelpUsage.MatchString(out) && pluginHelpInstall.MatchString(out) && pluginHelpMarket.MatchString(out)
311+
}
312+
274313
// InstallPluginForAgent registers the databricks marketplace and installs the
275314
// plugin through the agent's own CLI, returning the record to persist in state.
276315
// It never falls back to skills: a blocked install returns a *BlockedError.

‎libs/aitools/installer/plugin_test.go‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,3 +702,76 @@ func TestRunAgentCmdReportsTimeout(t *testing.T) {
702702
assert.Equal(t, ReasonCLINotOnPath, blocked.Reason)
703703
assert.Equal(t, "timed out after 1ms", blocked.Detail)
704704
}
705+
706+
// copilotPluginHelp is the shape of `copilot plugin --help`.
707+
const copilotPluginHelp = "Usage: copilot plugin [options] [command]\n\nManage plugins\n\nCommands:\n install <plugin> Install a plugin\n uninstall <plugin> Uninstall a plugin\n marketplace Manage marketplaces\n"
708+
709+
func TestProbeWindowsShimPrintedHelp(t *testing.T) {
710+
tests := []struct {
711+
name string
712+
goos string
713+
bin string
714+
stdout string
715+
accept bool
716+
}{
717+
{name: "windows .bat shim printed help and exited 1", goos: "windows", bin: `C:\npm\copilot.bat`, stdout: copilotPluginHelp, accept: true},
718+
{name: "windows .cmd shim printed help and exited 1", goos: "windows", bin: `C:\npm\codex.cmd`, stdout: copilotPluginHelp, accept: true},
719+
{name: "same output off windows is a real failure", goos: "linux", bin: "/usr/bin/copilot", stdout: copilotPluginHelp},
720+
{name: "windows .exe is not a shim", goos: "windows", bin: `C:\bin\copilot.exe`, stdout: copilotPluginHelp},
721+
{name: "error that mentions both words", goos: "windows", bin: `C:\npm\copilot.bat`, stdout: "Error: cannot install plugins: marketplace disabled by policy\n"},
722+
{name: "usage line without a command list", goos: "windows", bin: `C:\npm\copilot.bat`, stdout: "Usage: copilot plugin\nerror: install and marketplace are not available\n"},
723+
{name: "unindented words after blank lines", goos: "windows", bin: `C:\npm\copilot.bat`, stdout: "Usage: copilot plugin [options]\n\nmarketplace: disabled by policy\n\ninstall is not permitted\n"},
724+
}
725+
for _, tc := range tests {
726+
t.Run(tc.name, func(t *testing.T) {
727+
orig := probeGOOS
728+
t.Cleanup(func() { probeGOOS = orig })
729+
probeGOOS = tc.goos
730+
origLook := lookPath
731+
t.Cleanup(func() { lookPath = origLook })
732+
lookPath = func(string) (string, error) { return tc.bin, nil }
733+
734+
ctx, stub := process.WithStub(t.Context())
735+
stub.WithCallback(func(*exec.Cmd) error { return nil })
736+
stub.WithStdoutFor("plugin --help", tc.stdout)
737+
stub.WithFailureFor("plugin --help", errors.New("exit status 1"))
738+
739+
_, err := InstallPluginForAgent(ctx, pluginAgent(agents.NameCopilot, "GitHub Copilot", "copilot"), "", "v0.2.6")
740+
if tc.accept {
741+
require.NoError(t, err)
742+
return
743+
}
744+
var be *BlockedError
745+
require.ErrorAs(t, err, &be)
746+
assert.Equal(t, ReasonCLINotOnPath, be.Reason)
747+
assert.Len(t, stub.Commands(), 1, "nothing runs after a failed probe")
748+
})
749+
}
750+
}
751+
752+
func TestWindowsShimPrintedHelpRejectsTimeout(t *testing.T) {
753+
orig := probeGOOS
754+
t.Cleanup(func() { probeGOOS = orig })
755+
probeGOOS = "windows"
756+
757+
// A shim that printed the help and then hung until its deadline is not a
758+
// working CLI, even though the same output without the timeout is accepted.
759+
perr := &process.ProcessError{Command: "copilot.bat plugin --help", Err: errors.New("exit status 1"), Stdout: copilotPluginHelp}
760+
bin := `C:\npm\copilot.bat`
761+
assert.True(t, windowsShimPrintedHelp(bin, perr))
762+
assert.False(t, windowsShimPrintedHelp(bin, &agentCmdTimeoutError{timeout: time.Second, err: perr}))
763+
}
764+
765+
func TestProbeRejectsCLIWithoutPluginCommand(t *testing.T) {
766+
stubAgentLookPath(t, true)
767+
ctx, stub := process.WithStub(t.Context())
768+
stub.WithCallback(func(*exec.Cmd) error { return nil })
769+
stub.WithStdoutFor("plugin --help", "error: unknown command 'plugin'\n")
770+
stub.WithFailureFor("plugin --help", errors.New("exit status 1"))
771+
772+
_, err := InstallPluginForAgent(ctx, codexAgent(), "", "v0.2.6")
773+
var be *BlockedError
774+
require.ErrorAs(t, err, &be)
775+
assert.Equal(t, ReasonCLINotOnPath, be.Reason)
776+
assert.Len(t, stub.Commands(), 1, "nothing runs after a failed probe")
777+
}

0 commit comments

Comments
 (0)