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

--context option ignored when another option precedes it #593

Open
TheoD02 opened this issue Dec 17, 2024 · 1 comment · May be fixed by #594
Open

--context option ignored when another option precedes it #593

TheoD02 opened this issue Dec 17, 2024 · 1 comment · May be fixed by #594

Comments

@TheoD02
Copy link
Contributor

TheoD02 commented Dec 17, 2024

When attempting to use the --context option to select a specific context, it works as expected unless another option is declared before the --context option.

Example Setup

// Current declared contexts
#[AsContext(default: true)]
function root_context(): Context
{
    return new Context(workingDirectory: ROOT_DIR);
}

#[AsContext(name: 'symfony')]
function symfony_context(): Context
{
    return root_context()
        ->withName('symfony')
        ->withWorkingDirectory(ROOT_DIR . '/app');
}

Steps to Reproduce

  1. Run the following command:

    castor start --context=symfony

    Result: Works as expected. Running context()->name outputs symfony.

  2. Run the same command but with an additional option before --context:

    castor start --force --context=symfony

    Result: context()->name unexpectedly outputs default instead of symfony.

  3. Change the order of the options, placing --context before --force:

    castor start --context=symfony --force

    Result: Works as expected, and context()->name outputs symfony.

Expected Behavior

Regardless of the order of the options, specifying --context should correctly set the context.

Actual Behavior

If another option precedes --context, the context is ignored, and the default context is used.

Environment

  • Castor version: 0.21.0
  • PHP version: 8.4.0
@TheoD02
Copy link
Contributor Author

TheoD02 commented Dec 19, 2024

After some debug, it seem like we attempt to bind application definition, but when we are binding the application to input, application definition doesn't declare those from task, and this was silented by try/catch block in

castor/src/Kernel.php

Lines 206 to 224 in 902c05d

if ($contextNames) {
$defaultContext = PlatformHelper::getEnv('CASTOR_CONTEXT') ?: $this->contextRegistry->getDefaultName();
$applicationDefinition->addOption(new InputOption(
'context',
'_complete' === $input->getFirstArgument() || 'list' === $input->getFirstArgument() ? null : 'c',
InputOption::VALUE_REQUIRED,
\sprintf('The context to use (%s)', implode('|', $contextNames)),
$defaultContext,
$contextNames,
));
}
try {
$input->bind($applicationDefinition);
} catch (ExceptionInterface) {
// not an issue if parsing gone wrong, we'll just use the default
// context and it will fail later anyway
}

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 a pull request may close this issue.

1 participant