stecman / symfony-console-completion

Automatic tab-key completion for Symfony console application options, arguments and parameters
MIT License
420 stars 26 forks source link

Failed to configure from environment; Environment var COMP_LINE not set. #7

Closed fatganz closed 9 years ago

fatganz commented 9 years ago

Brand new ubuntu installation. And there is no such env variables like COMP_LINE, COMP_POINT, COMP_WORDBREAKS Any suggestions what should i do?

stecman commented 9 years ago

Hi @fatganz, can you give a bit more context about when you're seeing that message?

In general there are a few cases where you might see that:

  1. Running yourprogram _completion with no options in a terminal.

    This use of the command is intended to be used from within the shell function that yourprogram _completion -g generates. It is not designed to be run manually in a terminal. If this is your situation, have a look at step 3 of the instructions in the readme.

    It wasn't a great decision on my part to have the completion program as the default action, and I intend to fix that at some point. Ideally running the command with no options/arguments should print some use instructions, and an option like --run-completion would be required to trigger the completion part of the program.

  2. The bash-completion system package needs to be installed

    I'm pretty sure this is installed by default on Ubuntu, but it's worth mentioning as BASH doesn't have any completion functionality without it.

  3. If everything else is set up correctly, there may be an issue with your shell/environment. In this case the output of echo $BASH_VERSION would be useful.

I just tried this on a completely fresh install of Ubuntu 14.10 and it worked for me using a known working implementation of this module (Beam):

# Install PHP
sudo apt-get install php5-cli

# Get Beam executable
curl https://beam.heyday.net.nz/beam.phar -O

# Use an alias since this isn't on PATH (as discussed in issue #4)
alias beam="php `pwd`/beam.phar"

# Set up completion
eval `beam _completion --generate-hook --program beam`

# Use completion
beam [tab] [tab]

# (Clean up)
unalias beam
complete -r beam
rm beam.phar
CaptainJojo commented 9 years ago

I have the same error, I install in my project Cilex

when I launch

php src/run.php _completion --generate-hook | source /dev/stdin

I have this error

[RuntimeException]
Failed to configure from environment; Environment var COMP_LINE not set.

stecman commented 9 years ago

What do you get if you use the eval method to set up the hook instead of source? I noticed the other day that using source wasn't working on some machines.

eval $(php src/run.php _completion --generate-hook)

As a side note @CaptainJojo, you might want to have a look at the issue 'How to call this command via "php www/index.php"'. Bash completion essentially has a map of strings to completion functions; it doesn't do anything clever to determine if ./src/run.php is in directory A or B, it just uses the completion registered against the string "./src/run.php". The easiest workaround for this is to use an alias with an absolute path to your program, then add the --program [alias] option of this module.

CaptainJojo commented 9 years ago

It's ok but I always Failed to configure from environment; Environment var COMP_LINE not set.

What is the value for COMP_LINE ?

CaptainJojo commented 9 years ago

I add in my bashrc

alias php-console='php /home/jonathan/Sites/php-scripts/src/run.php'
eval $(php-console _completion --generate-hook --program php-console)

But I have already the same error

[RuntimeException]                                                        
  Failed to configure from environment; Environment var COMP_LINE not set.

When I source .bashrc

stecman commented 9 years ago

COMP_LINE is an environment variable set by BASH's completion system when completion is triggered (ie. tab is pressed). It contains the current contents of the command line. The way the whole setup should work is like this:

  1. Hook code from this module (--generate-hook) is run, which creates a shell function and registers it as the completion handler for yourapp. The shell function doesn't run at this point, so you shouldn't get an error like this here.
  2. When you type yourapp into the command line and press tab twice, BASH sets environment variables with information about the current input, then runs the shell function created in 1.
  3. The shell function runs yourapp _completion, which uses the environment variables to figure out what should be offered as completions.
  4. The output of yourapp _completion is stored in an environment variable that BASH reads after the shell function has returned.

At what point exactly are you getting the error? It looks like it's one of when you run the setup command, or when you press tab, but I'm not quite sure from what you've said.

As a side note, I just tried running this after uninstalling the bash-completion system package and does it error, though I get a different error:

_get_comp_words_by_ref: command not found

Note that if you were running ZSH, you'd need to use --generate-hook zsh which emulates the environment variables from BASH's completion system, as ZSH has different variables with a similar purpose (COMP_LINE and friends don't exist under ZSH).

CaptainJojo commented 9 years ago

I understand, it's ok I don't have the error but when I tab

/home/jonathan/Sites/php-scripts/src/run.php: ligne 1: ?php: Aucun fichier ou dossier de ce type
/home/jonathan/Sites/php-scripts/src/run.php: ligne 3: Erreur de syntaxe près du symbole inattendu « { »
/home/jonathan/Sites/php-scripts/src/run.php: ligne 3: `if (!$loader = include __DIR__.'/../vendor/autoload.php') {
CaptainJojo commented 9 years ago

That is my generate

function _console_0e904345f971a5d3_complete {
    export COMP_LINE COMP_POINT COMP_WORDBREAKS;
    local RESULT STATUS;

    RESULT=`/home/jonathan/Sites/php-scripts/src/run.php _completion`;
    STATUS=$?;

    if [ $STATUS -ne 0 ]; then
        echo $RESULT;
        return $?;
    fi;

    local cur;
    _get_comp_words_by_ref -n : cur;

    COMPREPLY=(`compgen -W "$RESULT" -- \$cur`);

    __ltrim_colon_completions "$cur";
};

complete -F _console_0e904345f971a5d3_complete console;
CaptainJojo commented 9 years ago

It's ok I find I need too add 'php' in RESULT command.

Thx for your patience

stecman commented 9 years ago

That completion hook works fine for me using beam in place of your program. To rule out an environment or PHP config issue, you could try running the following to check if PHP is able to read environment variables:

function _test() {
    export COMP_LINE;
    echo; echo "shell: $COMP_LINE";
    php -r 'echo "php: ", getenv("COMP_LINE"), "\n";';
}

complete -F _test _test

Then typing _test [tab] [tab] in the command line should print:

shell: _test
php: _test

Apart from that, the other thing that this could be is a problem in your implementation of the completion command if you've done anything more than the zero-config use. If you can put together a minimal test case for this that would be really helpful, or let me know if the example in my first comment using Beam gets the same error.