Home > Net >  What am I doing wrong with my .ycm_extra_conf.py?
What am I doing wrong with my .ycm_extra_conf.py?

Time:06-10

I have successfully installed the YouCompleteMe plugin for vim and it is already working, however, I cannot get it to work on an actual project. For some reason I cannot create a .ycm_extra_conf.py that actually works. To test it out I tried making a minimal project, i.e. I created ~/project with the following structure:

~/project
~/project/.ycm_extra_conf.py
~/project/code
~/project/code/example.cpp
~/project/include
~/project/include/example.hpp

Nothing fancy. When I edit example.cpp I try to #include "example.hpp" but it already fails there because ycm says it cannot find the file ('example.hpp' file not found [pp_file_not_found]). vim does find the .ycm_extra_conf.py file and it tries to load it, however, I am not sure whether it's working correctly.

The output from :YcmDebugInfo is:

Printing YouCompleteMe debug information...
-- Resolve completions: Up front
-- Client logfile: /tmp/ycm_bgv1ytg4.log
-- Server Python interpreter: /usr/bin/python3
-- Server Python version: 3.10.4
-- Server has Clang support compiled in: True
-- Clang version: clang version 13.0.0 (https://github.com/ycm-core/llvm 02d1fa3185f1beeebb182b8911bfe09d3429707f)
-- Extra configuration file found and loaded
-- Extra configuration path: ~/project/.ycm_extra_conf.py
-- C-family completer debug information:
--   Clangd running
--   Clangd process ID: 58005
--   Clangd executable: ['~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clangd/output/bin/clangd', '-header-insertion-decorators=0', '-resource-dir=~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_part
y/clang/lib/clang/13.0.0', '-limit-results=500']
--   Clangd logfiles:
--     /tmp/clangd_stderr2sp81d3p.log
--   Clangd Server State: Initialized
--   Clangd Project Directory: ~/project
--   Clangd Settings: {}
--   Clangd Compilation Command: False
-- Server running at: http://127.0.0.1:55029
-- Server process ID: 57976
-- Server logfiles:
--   /tmp/ycmd_55029_stdout_lm0galgv.log
--   /tmp/ycmd_55029_stderr_myqhxgqt.log

My .ycm_extra_config.py looks like this:

import os
import ycm_core

flags = [
  '-Wall',
  '-O3',
  '-I../include',
  '-I./include',
  ]

def FlagsForFile(filename, ** kwargs):
  return
  {
    'flags': flags,
    'do_cache': True
  }

The output from the temporary log files is as follows. For /tmp/clangd_stderr2sp81d3p.log:

I[16:52:02.713] clangd version 13.0.0 (https://github.com/ycm-core/llvm 02d1fa3185f1beeebb182b8911bfe09d3429707f)
I[16:52:02.713] Features: linux
I[16:52:02.713] PID: 58005
I[16:52:02.713] Working directory: ~/project
I[16:52:02.713] argv[0]: ~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clangd/output/bin/clangd
I[16:52:02.713] argv[1]: -header-insertion-decorators=0
I[16:52:02.713] argv[2]: -resource-dir=~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clang/lib/clang/13.0.0
I[16:52:02.713] argv[3]: -limit-results=500
I[16:52:02.713] Starting LSP over stdin/stdout
I[16:52:02.714] <-- initialize(1)
I[16:52:02.715] --> reply:initialize(1) 1 ms
I[16:52:02.716] <-- initialized
I[16:52:02.717] <-- workspace/didChangeConfiguration
I[16:52:02.717] <-- textDocument/didOpen
I[16:52:02.718] Failed to find compilation database for ~/project/code/example.cpp
I[16:52:02.718] ASTWorker building file ~/project/code/example.cpp version 1 with command clangd fallback
[~/project/code]
/usr/bin/clang -resource-dir=~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clang/lib/clang/13.0.0 -- ~/project/code/example.cpp
I[16:52:03.018] --> textDocument/publishDiagnostics

The other one is:

2022-06-08 16:52:01,284 - INFO - Completion config: 50, detailing -1 candiates
2022-06-08 16:52:01,284 - INFO - Completion config: 50, detailing -1 candiates
2022-06-08 16:52:01,285 - INFO - Completion config: 50, detailing -1 candiates
2022-06-08 16:52:01,286 - INFO - Completion config: 50, detailing -1 candiates
127.0.0.1 - - [08/Jun/2022 16:52:01] "GET /ready HTTP/1.1" 200 4

2022-06-08 16:52:01,382 - INFO - Clangd executable found at ~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clangd/output/bin and up to date
2022-06-08 16:52:01,383 - INFO - Using Clangd from ~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clangd/output/bin/clangd
2022-06-08 16:52:01,383 - INFO - Computed Clangd command: ['~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clangd/output/bin/clangd', '-header-insertion-decorators=0', '-resource-dir=~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clang/lib/clang/13.0.0', '-limit-results=500']
2022-06-08 16:52:01,383 - INFO - Completion config: 50, detailing -1 candiates
2022-06-08 16:52:01,383 - INFO - Returning cached Clangd command: ['~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clangd/output/bin/clangd', '-header-insertion-decorators=0', '-resource-dir=~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clang/lib/clang/13.0.0', '-limit-results=500']
127.0.0.1 - - [08/Jun/2022 16:52:01] "GET /signature_help_available?subserver=cpp HTTP/1.1" 200 23
127.0.0.1 - - [08/Jun/2022 16:52:01] "POST /event_notification HTTP/1.1" 200 2
Traceback (most recent call last):
  File "~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/bottle/bottle.py", line 868, in _handle
    return route.call(**args)
  File "~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/bottle/bottle.py", line 1748, in wrapper
    rv = callback(*a, **ka)
  File "~/.vim/bundle/YouCompleteMe/third_party/ycmd/ycmd/watchdog_plugin.py", line 97, in wrapper
    return callback( *args, **kwargs )
  File "~/.vim/bundle/YouCompleteMe/third_party/ycmd/ycmd/hmac_plugin.py", line 62, in wrapper
    body = callback( *args, **kwargs )
  File "~/.vim/bundle/YouCompleteMe/third_party/ycmd/ycmd/handlers.py", line 63, in EventNotification
    response_data = getattr( _server_state.GetFiletypeCompleter( filetypes ),
  File "~/.vim/bundle/YouCompleteMe/third_party/ycmd/ycmd/completers/language_server/language_server_completer.py", line 1787, in OnFileReadyToParse
    self._StartAndInitializeServer( request_data )
  File "~/.vim/bundle/YouCompleteMe/third_party/ycmd/ycmd/completers/language_server/language_server_completer.py", line 1773, in _StartAndInitializeServer
    self._extra_conf_dir = self._GetSettingsFromExtraConf( request_data )
  File "~/.vim/bundle/YouCompleteMe/third_party/ycmd/ycmd/completers/language_server/language_server_completer.py", line 1741, in _GetSettingsFromExtraConf
    module = extra_conf_store.ModuleForSourceFile( request_data[ 'filepath' ] )
  File "~/.vim/bundle/YouCompleteMe/third_party/ycmd/ycmd/extra_conf_store.py", line 55, in ModuleForSourceFile
    return Load( ModuleFileForSourceFile( filename ) )
  File "~/.vim/bundle/YouCompleteMe/third_party/ycmd/ycmd/extra_conf_store.py", line 66, in ModuleFileForSourceFile
    if Load( module_file ):
  File "~/.vim/bundle/YouCompleteMe/third_party/ycmd/ycmd/extra_conf_store.py", line 151, in Load
    if not force and not _ShouldLoad( module_file, is_global ):
  File "~/.vim/bundle/YouCompleteMe/third_party/ycmd/ycmd/extra_conf_store.py", line 134, in _ShouldLoad
    raise UnknownExtraConf( module_file )


(Question can be turned off with options, see YCM docs)
127.0.0.1 - - [08/Jun/2022 16:52:01] "POST /event_notification HTTP/1.1" 500 2671
127.0.0.1 - - [08/Jun/2022 16:52:01] "POST /semantic_completion_available HTTP/1.1" 200 4
127.0.0.1 - - [08/Jun/2022 16:52:02] "POST /load_extra_conf_file HTTP/1.1" 200 4


2022-06-08 16:52:02,704 - INFO - Starting Clangd: ['~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clangd/output/bin/clangd', '-header-insertion-decorators=0', '-resource-dir=~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clang/lib/clang/13.0.0', '-limit-results=500']
2022-06-08 16:52:02,706 - INFO - Clangd started with PID 58005
127.0.0.1 - - [08/Jun/2022 16:52:02] "POST /event_notification HTTP/1.1" 200 2
2022-06-08 16:52:02,716 - INFO - cfamily: Language server does not require resolve request
2022-06-08 16:52:02,716 - INFO - cfamily: Language server requires sync type of Incremental
2022-06-08 16:52:02,716 - INFO - cfamily: Using characters for signature triggers: (,,
127.0.0.1 - - [08/Jun/2022 16:52:02] "POST /receive_messages HTTP/1.1" 200 4
127.0.0.1 - - [08/Jun/2022 16:52:03] "POST /receive_messages HTTP/1.1" 200 1472
127.0.0.1 - - [08/Jun/2022 16:52:05] "POST /debug_info HTTP/1.1" 200 960
127.0.0.1 - - [08/Jun/2022 16:52:13] "POST /receive_messages HTTP/1.1" 200 4

Any help would be appreciated!

Cheers

CodePudding user response:

I found the answer. Basically my .ycm_extra_conf.py was borked. This one should work:

import os

flags = [
  '-Wall',
  '-O3',
  '-I../include',
  '-I./include',
  ]


def DirectoryOfThisScript():
  return os.path.dirname(os.path.abspath(__file__))


def Settings(** kwargs):
  return {
    'flags': flags,
    'include_paths_relative_to_dir': DirectoryOfThisScript()
  }

Basically the culprit was

return
{

instead of

return {

The latter one works, the former doesn't.

CodePudding user response:

In most cases, for most projects, we recommend:

  1. Using clangd
  2. Creating a compilation database
  3. NOT using a .ycm_extra_conf.py file at all.

This gives the best overall experience, including full-project analysis from clangd.

https://github.com/ycm-core/YouCompleteMe#compile-flags

  • Related