[DO-NOT-MERGE] Fix over-broad regex causing spurious filtering in Habitat packages#73
Draft
[DO-NOT-MERGE] Fix over-broad regex causing spurious filtering in Habitat packages#73
Conversation
Plugins like knife-cloud define abstract base classes inside the
Chef::Knife namespace, e.g.:
Chef::Knife::Cloud::ServerCreateCommand
Chef::Knife::Cloud::ServerDeleteCommand
Chef::Knife::Cloud::ServerListCommand
Chef::Knife::Cloud::ServerShowCommand
These classes transitively inherit from Chef::Knife, so the inherited
callback in knife.rb was registering them as subcommands. Their
snake_case_name ('server_create_command' etc.) gave them the category
'server', causing ** SERVER COMMANDS ** to appear in 'knife --help'
with broken 'Usage: knife (options)' banners.
This affects any installation where knife-cloud (or similar plugins
that define abstract helper classes in chef/knife subdirectories) are
present, including chef-workstation.
Root cause: the inherited callback unconditionally registered any
named Chef::Knife subclass, including abstract base classes that are
loaded *transitively* as side effects when real command files are
required. GemGlobLoader discovers commands by globbing only at the
top level (chef/knife/*.rb), but loading those files pulls in
abstract base classes from subdirectories like
chef/knife/cloud/server/*.rb.
Fix: extract the caller path before registering the subclass and skip
registration if the defining file lives inside a subdirectory of
chef/knife/. Real subcommands always live directly at
chef/knife/<name>.rb (the same flat pattern GemGlobLoader uses).
Abstract base classes nested in subdirectories (chef/knife/cloud/,
chef/knife/cloud/server/, etc.) are now silently ignored.
Classes defined outside the chef/knife/ path entirely (test helpers,
eval'd code, etc.) are unaffected since the guard only rejects paths
that match /chef/knife/[^/]+/ (i.e. containing a subdirectory).
2232 examples, 0 failures, 7 pending.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Ashique Saidalavi <Ashique.saidalavi@progress.com>
The previous regex /chef/knife/[^/]+/ was intended to skip abstract base classes nested under lib/chef/knife/ subdirectories (e.g. knife-cloud's lib/chef/knife/cloud/server/create_command.rb). However, in Habitat packages the pkg origin and name appear in the package install path: /hab/pkgs/chef/knife/19.0.99/TIMESTAMP/... This means the path component /chef/knife/19.0.99/ also matched the regex, causing ALL installed knife commands to be filtered out and leaving only ~7 categories in 'knife --help'. Fix: anchor the regex to /lib/chef/knife/ so it only matches files that live in a subdirectory of the lib/chef/knife/ tree, not arbitrary occurrences of /chef/knife/X/ elsewhere in the filesystem path. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Ashique Saidalavi <Ashique.saidalavi@progress.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up fix for the
** SERVER COMMANDS **regression introduced in PR #71. After the initial fix was merged, building the knife Habitat package showed only ~7 categories instead of 30+.Root Cause
The previous regex
/chef/knife/[^/]+/was too broad. In Habitat packages the install path is structured as:With
origin=chefandpkg_name=knife, every installed knife command file contains/chef/knife/19.0.99/in its path, which matched the regex and caused all commands to be filtered out.Fix
Anchor the regex to
/lib/chef/knife/[^/]+/so it only matches files inside a subdirectory of thelib/chef/knife/tree (e.g.lib/chef/knife/cloud/server/create_command.rb), not arbitrary occurrences of/chef/knife/X/in filesystem paths.Verification