Skip to content

:PhpactorImportClass not finding class , though autocompletion shows the FQDN #1128

Open

Description

@allan-simon

I'm using the very latest version of phpactor (branch develop) , i'm using it with neovim and coc.vim for the LSP client

phpactor is working fine (i have autocompletion, go to definition etc. working) however PhpactorImportClass is not

given the following code (<> being my cursor position

  declare(strict_types=1);
  
  namespace App\Controller;
                                                                                          
 class StripeWebhookController extends Abstract<>Controller {                                                                                                                                            
                 

}

I then hit :PhpactorImportClass and it tells me

No classes found with name "AbstractController"

However when typing , it does correctly show me the autocompletion

image

Activity

  1. camilledejoye commented on Nov 15, 2020

    @camilledejoye
    Contributor

    Hi again,

    So the first thing to understand is that there is two different protocols: LSP and RPC
    LSP is used by CoC: CoC is the client and phpactor is the server
    RPC is used by our custom vim plugin

    When you call the command PhpactorImportClass you're using RPC, when you're completing you're using LSP.
    Since they are different and use different configuration it's possible than one work but not the other.

    One temporary workaround, until we can understand your problem and fix it, is for you to use CoC to import the class when you complete it.
    In order to import the class with auto completion you need to validate your choice with <C-y>, which is the vim notation for Ctrl + y.
    You can find an example of mapping to be able to complete "correctly" with enter here: https://phpactor.readthedocs.io/en/develop/lsp/vim.html
    You can also find more information about that in CoC's wiki: https://github.com/neoclide/coc.nvim/wiki/Completion-with-sources#limitation-of-cocnvims-completion

    Given your previous issue I think you have still have some trouble with the project root.
    If I remember correctly you have defined it in your vimrc with let g:PhpactorRootDirectoryStrategy = {-> '/vagrant'}
    Which means that the full path to access your project is /vagrant, is this path correct ?

    In order to have a better understanding of what is going on can you:

    • tell us if you have a phpactor config file ? If yes, is it a global one or do you have one in your project directory ?
    • enable the replay then:
      • Go to your StripeWebhookController class
      • Put the cursor on AbstractController and execute the PhpactorImportClass command
      • Without doing anything else inside vim, go to your terminal and run phpactor rpc --replay --verbose
      • Copy/paste the output here
  2. allan-simon commented on Nov 15, 2020

    @allan-simon
    Author

    i'm going to check your workaround, and yes it's /vagrant :)

  3. allan-simon commented on Nov 15, 2020

    @allan-simon
    Author

    tell us if you have a phpactor config file ? If yes, is it a global one or do you have one in your project directory ?

    no config file at all

  4. camilledejoye commented on Nov 15, 2020

    @camilledejoye
    Contributor

    i'm going to check your workaround, and yes it's /vagrant :)

    Seems a bit odd, just to be sure: the expected path should correspond to where the sources are on your local computer.
    If you deploy on a docker container and /vagrant is the path of your project inside the container it will not work since we don't even know about it :)

  5. allan-simon commented on Nov 15, 2020

    @allan-simon
    Author

    actually my vim is in a "docker as a vm" (i.e a docker in which i ssh in it ) https://github.com/allan-simon/docker-devbox-php
    so that each of my project are in a dockerized environment ( i.e my python project are in docker with a neovim with all the plugin needed to make python development and all the tools etc. )

    so when I develop i'm inside the docker in which the code is mounted in /vagrant

  6. dantleech commented on Nov 15, 2020

    @dantleech
    Collaborator

    What's the output of :PhpactorStatus (or $ phpactor status from your project root? Your other issue indicated that it couldn't find composer.json and so wasn't being run in the project root which can affect your ability to import classes over RPC etc (and possibly also the range of the indexing for the Language Server)

  7. allan-simon commented on Nov 15, 2020

    @allan-simon
    Author

    Here's the replay

    [DEBU][474580.751837] Resolved path "%cache%/replay.json" to "/home/vagrant/.cache/phpactor/replay.json"[]
    [DEBU][474580.752039] REQUEST{
        "action": "import_class",
        "parameters": {
            "source": "<?php\ndeclare(strict_types=1);\n\nnamespace App\\Controller;\n\nclass StripeWebhookController extends AbstractController {\n}",
            "offset": 102,
            "path": "\/vagrant\/src\/Controller\/StripeWebhookController.php"
        }
    }
    [DEBU][474580.753286] Resolved path "%project_config%/templates" to "/vagrant/.phpactor/templates"[]
    [DEBU][474580.753314] Resolved path "%config%/templates" to "/home/vagrant/.config/phpactor/templates"[]
    [DEBU][474580.753335] Resolved path "%application_root%/vendor/phpactor/code-builder/templates" to "/home/vagrant/.local/share/nvim/plugged/phpactor/vendor/phpactor/code-builder/templates"[]
    [DEBU][474580.753354] Resolved path "%project_root%/composer.json" to "/vagrant/composer.json"[]
    [DEBU][474580.761488] Resolved path "%project_root%" to "/vagrant"[]
    [DEBU][474580.761624] Resolved path "%project_root%" to "/vagrant"[]
    [DEBU][474580.761652] Resolved path "%project_root%" to "/vagrant"[]
    [DEBU][474580.761681] Resolved path "%project_root%/vendor/autoload.php" to "/vagrant/vendor/autoload.php"[]
    [DEBU][474580.767367] Resolved path "%application_root%/vendor/jetbrains/phpstorm-stubs" to "/home/vagrant/.local/share/nvim/plugged/phpactor/vendor/jetbrains/phpstorm-stubs"[]
    [DEBU][474580.767401] Resolved path "%cache%/worse-reflection" to "/home/vagrant/.cache/phpactor/worse-reflection"[]
    [DEBU][474580.767824] Resolved path "%cache%/index/%project_id%" to "/home/vagrant/.cache/phpactor/index/vagrant-6ad5fd"[]
    [DEBU][474580.768031] Resolved path "%project_root%" to "/vagrant"[]
    [DEBU][474580.768117] Resolved path "%project_root%" to "/vagrant"[]
    [DEBU][474580.934532] RESPONSE{
        "action": "collection",
        "parameters": {
            "actions": [
                {
                    "name": "update_file_source",
                    "parameters": {
                        "path": "\/vagrant\/src\/Controller\/StripeWebhookController.php",
                        "source": "<?php\ndeclare(strict_types=1);\n\nnamespace App\\Controller;\n\nuse Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController;\n\nclass StripeWebhookController extends AbstractController {\n}",
                        "edits": [
                            {
                                "start": {
                                    "line": 5,
                                    "character": 0
                                },
                                "end": {
                                    "line": 5,
                                    "character": 0
                                },
                                "text": "use Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController;\n"
                            },
                            {
                                "start": {
                                    "line": 6,
                                    "character": 0
                                },
                                "end": {
                                    "line": 6,
                                    "character": 0
                                },
                                "text": "\n"
                            }
                        ]
                    }
                },
                {
                    "name": "echo",
                    "parameters": {
                        "message": "Imported class \"Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController\""
                    }
                }
            ]
        }
    }
    {"version":"1.0.0","action":"collection","parameters":{"actions":[{"name":"update_file_source","parameters":{"path":"\/vagrant\/src\/Controller\/StripeWebhookController.php","source":"<?php\ndeclare(strict_types=1);\n\nnamespace App\\Controller;\n\nuse Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController;\n\nclass StripeWebhookController extends AbstractController {\n}","edits":[{"start":{"line":5,"character":0},"end":{"line":5,"character":0},"text":"use Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController;\n"},{"start":{"line":6,"character":0},"end":{"line":6,"character":0},"text":"\n"}]}},{"name":"echo","parameters":{"message":"Imported class \"Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController\""}}]}}%
    
  8. allan-simon commented on Nov 15, 2020

    @allan-simon
    Author

    In order to import the class with auto completion you need to validate your choice with , which is the vim notation for Ctrl + y.

    indeed C-y works :)

  9. dantleech commented on Nov 15, 2020

    @dantleech
    Collaborator

    ☝️ The above is a successful import response, indicating a problem with the CWD in VIM I think ... 🤔

  10. allan-simon commented on Nov 15, 2020

    @allan-simon
    Author

    point_up The above is a successful import response, indicating a problem with the CWD in VIM I think ... thinking

    indeed , removing the set autochdir and opening vim from /vagrant , and the PhpactorImportClass works correctly

  11. camilledejoye commented on Nov 15, 2020

    @camilledejoye
    Contributor

    indeed , removing the set autochdir and opening vim from /vagrant , and the PhpactorImportClass works correctly

    That was expected 😄
    The problem was with the autochdir or opening from a sub-directory, the error message is generated from the handler:

    So I think your first idea about composer not being found might be right.
    Unfortunately the status won't help us yet because it's not fixed, see the PR: #1127
    @allan-simon if you are in the mood for playing I guess you could try from the branch: https://github.com/elythyr/phpactor/tree/fix/%231085-status-does-not-use-correct-working-directory

    I think the best way for us to debug this is to reproduce it locally by starting with directly from a sub-directory of a project root.
    I'm not sure I will have the time soon though...

  12. allan-simon commented on Nov 15, 2020

    @allan-simon
    Author

    I will check your branch , thanks for the help ! at least as I have the c-y now it's less of a problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions