Skip to content

[DO-NOT-MERGE] Fix over-broad regex causing spurious filtering in Habitat packages#73

Draft
ashiqueps wants to merge 3 commits intomainfrom
ashiqueps/fix-knife-help
Draft

[DO-NOT-MERGE] Fix over-broad regex causing spurious filtering in Habitat packages#73
ashiqueps wants to merge 3 commits intomainfrom
ashiqueps/fix-knife-help

Conversation

@ashiqueps
Copy link
Copy Markdown
Contributor

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:

/hab/pkgs/<origin>/<pkg_name>/<version>/<timestamp>/...

With origin=chef and pkg_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 the lib/chef/knife/ tree (e.g. lib/chef/knife/cloud/server/create_command.rb), not arbitrary occurrences of /chef/knife/X/ in filesystem paths.

Verification

  • Regex correctly rejects real command paths (dev and hab package alike)
  • Regex correctly matches knife-cloud abstract base class paths
  • Full test suite: 2232 examples, 0 failures

ashiqueps and others added 2 commits April 4, 2026 21:38
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>
@ashiqueps ashiqueps added the ai-assisted Work completed with AI assistance following Progress AI policies label Apr 6, 2026
@ashiqueps ashiqueps marked this pull request as draft April 7, 2026 04:47
@ashiqueps ashiqueps changed the title Fix over-broad regex causing spurious filtering in Habitat packages {DO-NOT-MERGE] Fix over-broad regex causing spurious filtering in Habitat packages Apr 7, 2026
@ashiqueps ashiqueps changed the title {DO-NOT-MERGE] Fix over-broad regex causing spurious filtering in Habitat packages [DO-NOT-MERGE] Fix over-broad regex causing spurious filtering in Habitat packages Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-assisted Work completed with AI assistance following Progress AI policies

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant