Skip to content

fix: restore os.Args after plugin completion and fix error return#6817

Open
luojiyin1987 wants to merge 1 commit intodocker:masterfrom
luojiyin1987:fix-plugin-cobra
Open

fix: restore os.Args after plugin completion and fix error return#6817
luojiyin1987 wants to merge 1 commit intodocker:masterfrom
luojiyin1987:fix-plugin-cobra

Conversation

@luojiyin1987
Copy link
Contributor

- What I did

Fix two issues in the plugin manager:

  1. Fix error return value: The plugin stub was returning the wrong error variable when flag parsing failed
  2. Restore os.Args after plugin completion: The global os.Args was being modified during plugin completion but not restored, which could cause side effects in subsequent code

- How I did it

  1. Changed the error return from err to perr when flag parsing fails
  2. Added code to save the original os.Args before modification and use defer to restore it
  3. Added comprehensive tests to verify both fixes

- Files changed:

File Change
cli-plugins/manager/cobra.go Fix error return + restore os.Args
cli-plugins/manager/cobra_test.go Add regression tests

- How to verify it

The new tests verify both scenarios.

- Description for the changelog

bug fix: restore os.Args after plugin completion and fix error return

Fixes #6816

Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops! I started reviewing, but didn't submit 🙈

Comment on lines 48 to +50
perr := flags.Parse(args)
if perr != nil {
return err
return perr
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch; while updating; perhaps just inline it;

if err := flags.Parse(args); err != nil {
	return err

assert.DeepEqual(t, os.Args, originalArgs)
}

func preparePluginStubCommand(t *testing.T, metadata string) *cobra.Command {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like my IDE (and possibly linters) has trouble detecting that the returned command will never be nil (due to the asserts); it's probably OK to just return the errors instead

Comment on lines +55 to +58
dir := fs.NewDir(t, t.Name(),
fs.WithFile("docker-testplugin", "#!/bin/sh\nprintf '%s' '"+metadata+"'\n", fs.WithMode(0o777)),
)
t.Cleanup(func() { dir.Remove() })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we use the fs package still in some other places in this package, but I'm .. slightly trying to get rid of it (in most cases it's overkill); this one can probably be simplified using stdlib;

Also looks like we don't use the metadata anywhere for assertions, so we could just make this all "static" (until there's a specific reason to customise it);

	tmpDir := t.TempDir()
	const cliPlugin = `
#!/bin/sh
printf '%s' '{"SchemaVersion":"0.1.0","Vendor":"e2e-testing"}'
`
	err := os.WriteFile(filepath.Join(tmpDir, "docker-testplugin"), []byte(cliPlugin), 0o777)

Comment on lines +61 to +63
cli.SetConfigFile(&configfile.ConfigFile{
CLIPluginsExtraDirs: []string{dir.Path()},
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably fine here to simplify and just patch the config;

cli.ConfigFile().CLIPluginsExtraDirs = []string{tmpDir}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: restore os.Args after plugin completion and fix error return

2 participants