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

fix: context must be reset, or nested sub-commands fail on second invocation #678

Merged
merged 2 commits into from Oct 19, 2016

Conversation

bcoe
Copy link
Member

@bcoe bcoe commented Oct 18, 2016

This addresses an issue @ceejbot ran into using headless .parse() in conjunction with nested sub-commands.

CC: @nexdrew

@nexdrew
Copy link
Member

nexdrew commented Oct 18, 2016

Hmm, the whole point of the generic context is to keep track of things that should not be reset. I honestly don't know how subcommands would work at all if we're resetting this, since it's used to determine which level of command is currently being executed and which files (for .commandDir()) have been visited (to account for potential cyclic directory refs).

I really need to test this thoroughly and would argue for a different solution.

@bcoe
Copy link
Member Author

bcoe commented Oct 18, 2016

@nexdrew keep in mind that the only time we reset the context object is in-between distinct runs of

.parse(rawArgs, context, parseFn)

The nested command handlers do invoke .parse() but they invoke it without parseFn; so unfreeze will not be executed until the entire stack has popped.

@nexdrew
Copy link
Member

nexdrew commented Oct 18, 2016

Ok, resetting the "context" makes more sense in the unfreeze() function (as opposed to the reset() method), but, at the risk of sounding silly, I must admit this has me baffled; I don't see the connection between the parseContext var (set from the 2nd arg to .parse() and applied to argv for the command handler) and the context var (private state used for subcommand scoping, never applied to argv) at all.

@bcoe
Copy link
Member Author

bcoe commented Oct 18, 2016

@nexdrew did a bit of digging, this is the underlying issue here:

      if (visited) {
        // check for cyclic reference
        // each command file path should only be seen once per execution
        if (~context.files.indexOf(joined)) return visited
        // keep track of visited files in context.files
        context.files.push(joined)
        self.addHandler(visited)
      }

Because files is left in the context object, on the next run of the parser we don't re-add handlers.

@nexdrew
Copy link
Member

nexdrew commented Oct 18, 2016

Ok, wow, good find. I didn't realize the problem is specific to a command that uses .commandDir() in its builder function. This makes sense now.

(Also, I apologize for my presumptive knee-jerk reaction! 😔 I should have taken more time to understand/diagnose the problem before responding. Thanks for keeping a level head and finding the root cause for us, which you have now had to fix twice! 😳)

Let me do a bit of experimenting to see if I can make the context management more robust. Otherwise, we could get away with simply resetting the context.files array.

@nexdrew
Copy link
Member

nexdrew commented Oct 18, 2016

@bcoe See #680 (which is a PR against the nested-command-fix branch) for my preferred solution to this problem. If you're ok with it, we can merge 680 first and then merge 678.

@bcoe bcoe closed this Oct 18, 2016
@nexdrew nexdrew reopened this Oct 18, 2016
so context is maintained for multiple command executions
Copy link
Member

@nexdrew nexdrew left a comment

Choose a reason for hiding this comment

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

LGTM! I vote merge once we confirm the new test passes.

@nexdrew nexdrew merged commit 6b85cc6 into master Oct 19, 2016
@nexdrew nexdrew deleted the nested-command-fix branch October 19, 2016 01:37
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.

None yet

2 participants