Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use templating to allow for dead-code elimination #228

Merged

Conversation

marckhouzam
Copy link
Contributor

@marckhouzam marckhouzam commented Feb 18, 2025

What this PR does / why we need it

This change has no visible effect to the behaviour of plugins, except for allowing them to possibly reduce their binary size.

When templating is used, the linker cannot know which functions are called and which ones are not; this is because templating uses calls to MethodByName() which can be used to call any function (similar to reflection).

With the release of Cobra 1.9.1, templates are no longer used by default in Cobra. By also not using templates in the tanzu-plugin-runtime it now gives an opportunity for plugins to try to avoid templates to allow dead-code elimination to work, if they so choose.

This PR therefore removes the use of templates in tanzu-plugin-runtime.
Note that we had technically already moved to using go code instead of templates but we had just kept the template engine calling the go code.

Note that the new TestDeadcodeElimination test is meant to notify us if templates are added. However, if any new dependency used by tanzu-plugin-runtime uses templates or reflection, it would also turn off dead-code elimination and fail the test. If there is a justification for that to happen, we will just need to remove the test.

Ref: spf13/cobra#1956

Which issue(s) this PR fixes

Fixes # N/A

Describe testing done for PR

I built a plugin using this new PR and verified that both its usage and help printouts were not changed:

$ tz project list -h
List the projects in the current workspace.

Project list configuration options include:
- Output the project formatted

Usage:
  tanzu project list [flags]

Aliases:
  list, ls

Examples:
  tanzu project list
  tanzu project list --wide

Flags:
  -h, --help   help for list
  -w, --wide   output the project list with additional information
$ tz project list --invalid
Usage:
  tanzu project list [flags]

Aliases:
  list, ls

Examples:
  tanzu project list
  tanzu project list --wide

Flags:
  -h, --help   help for list
  -w, --wide   output the project list with additional information

Error: unknown flag: --invalid

Release note

Do not use templates so that dead-code elimination is not turned off.

Additional information

Special notes for your reviewer

@marckhouzam marckhouzam requested a review from a team as a code owner February 18, 2025 15:44
@marckhouzam marckhouzam force-pushed the marck/allowDeadCodeElimination branch 2 times, most recently from d241e6b to 1a6dfb6 Compare February 19, 2025 16:08
@marckhouzam marckhouzam added this to the v1.4.8 milestone Feb 19, 2025
@marckhouzam marckhouzam force-pushed the marck/allowDeadCodeElimination branch from 1a6dfb6 to 72c4312 Compare February 19, 2025 17:48
When templating is used, the linker cannot know which functions are
called and which one are not; this is because templating use calls to
"MethodByName()" which can be used to call any function (similar to
reflection).

With the release of Cobra 1.9.1, templates are no longer used by default
in Cobra. By also not using templates in the tanzu-plugin-runtime it now
gives an opportunity for plugins to try to avoid templates to allow
dead-code elimination to work, if they so choose.

Ref: spf13/cobra#1956

Signed-off-by: Marc Khouzam <[email protected]>
@marckhouzam marckhouzam force-pushed the marck/allowDeadCodeElimination branch from 72c4312 to 7e3ff24 Compare February 19, 2025 18:34
Copy link
Contributor

@prkalle prkalle left a comment

Choose a reason for hiding this comment

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

Nice changes!!

LGTM. Thanks!

@marckhouzam marckhouzam merged commit 2075d4a into vmware-tanzu:main Feb 19, 2025
4 checks passed
@marckhouzam marckhouzam deleted the marck/allowDeadCodeElimination branch February 19, 2025 23:00
ajitgunturi pushed a commit to ajitgunturi/tanzu-plugin-runtime that referenced this pull request Feb 25, 2025
…#228)

When templating is used, the linker cannot know which functions are
called and which ones are not; this is because templating uses calls to
"MethodByName()" which can be used to call any function (similar to
reflection).

With the release of Cobra 1.9.1, templates are no longer used by default
in Cobra. By also not using templates in the tanzu-plugin-runtime it now
gives an opportunity for plugins to try to avoid templates to allow
dead-code elimination to work, if they so choose.

Ref: spf13/cobra#1956

Signed-off-by: Marc Khouzam <[email protected]>
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.

2 participants