Civitasv/cmake-tools.nvim

Build Directory and Build Targets Not Always Found

Opened this issue · 11 comments

It seems that this plugin is unable to set the correct build directory in certain cases. (See below) This causes neovim to look in the wrong folder for the CMake cache, which results in cmake-tools failing to build the code and clean the cache when needed.

Steps to reproduce

1: In the config function, set cmake_build_directory to nil or just delete that line. [ I did this because I wanted CMake to set the build directory. ]

2: Enter command :CMakeSelectConfigurePreset and select the preset you want.

3: Enter command :CMakeGenerate. Everything is working fine up to this point.

4: Enter command :CMakeBuild. The command regenerates the cache... then regenerates it again... then again... hmm.

5: After quitting and reopening neovim, enter command :CMakeSelectBuildTarget. Nothing happens.

6: Enter command :CMakeClean, "unable to load cache".

7: Enter command :CMakeSelectBuildDir, build directory is incorrect.

Sorry for the incredibly long reproduce steps, but I wanted to make sure nothing got missed.

Probable cause

I was able to get the build command to work and the build targets were all found. The problem turned out to be that the preset (set in CMakeUserPresets.json) had to explicitly specify the binaryDir variable. Prior to this I had the binaryDir variable set in a preset that mine "inherits" from. The base preset was in the CMakePresets.json file not the user one.

Basically, selecting a child preset does not allow the build directory to be retained by cmake-tools if the binaryDir is set in a parent preset.

Hi!

Same bug. The plugin doesn't change the cwd (even though its changed in neovim and the pwd command is correct) unless i open neovim at the root folder in the terminal.

Even if the cwd is correct, building cmake will build the previous project (the one stored in the cache i guess) and not the one i'm currently in. To make it work i have to manually change the build_dir in the cmake-tools base settings.

Any solution to this?

Hello,

The workaround I use is to specify the 'binaryDir' variable in the CMake preset that I use. If you are not using presets, then you'll have to set the CMAKE_BINARY_DIR variable some other way. Letting CMake set the build directory should help you avoid needing to change it in the plugin config file.

As for building the project you want, cmake-tools keeps a cache of what configurations and build targets you are currently working with. If you wish to use a different project or configuration, you have to change it using one of the :CMakeSelect* commands.

I hope this helps.

Hi,
Thanks for the comment and tips, will look into setting up presets.
Played around with it a little (set the CMAKE_BINARY_DIR in CMakeLists.txt and building etc..) and no matter what i do i get the error: Cannot find CMakeLists.txt at cwd (/home/user/) - unless i open nvim from the project root in the terminal. Which is strange because neovims output of pwd is the root of the project.

I guess setting up presets is the way to go...

Hi, Thanks for the comment and tips, will look into setting up presets. Played around with it a little (set the CMAKE_BINARY_DIR in CMakeLists.txt and building etc..) and no matter what i do i get the error: Cannot find CMakeLists.txt at cwd (/home/user/) - unless i open nvim from the project root in the terminal. Which is strange because neovims output of pwd is the root of the project.

I guess setting up presets is the way to go...

You can use CMakeSelectCwd to change curent working dir.

Thats the only way i can make it work yes, but its a hassle.

Is there a way for it to make it respect the cwd changed by project.nvim or even running cd in neovim?

As it is now, i have to run CMakeSelectCwd to make it recognize my cwd's CMakeLists.txt, then edit the settings to change the build_dir because it defaults to /home/user/

Sorry M00se-3 for hijacking the thread, should i make a seperate issue?

Config from LazyVim solved it with the cwd checking...

{
  "Civitasv/cmake-tools.nvim",
  lazy = true,
  init = function()
    local loaded = false
    local function check()
      local cwd = vim.uv.cwd()
      if vim.fn.filereadable(cwd .. "/CMakeLists.txt") == 1 then
        require("lazy").load({ plugins = { "cmake-tools.nvim" } })
        loaded = true
      end
    end
    check()
    vim.api.nvim_create_autocmd("DirChanged", {
      callback = function()
        if not loaded then
          check()
        end
      end,
    })
  end,
  opts = {},
}

The workaround I use is to specify the 'binaryDir' variable in the CMake preset that I use.

Yeah, you should specify the binaryDir variable, because that's how this plugin retrieve build directory info from presets.

Thats the only way i can make it work yes, but its a hassle.

Is there a way for it to make it respect the cwd changed by project.nvim or even running cd in neovim?

As it is now, i have to run CMakeSelectCwd to make it recognize my cwd's CMakeLists.txt, then edit the settings to change the build_dir because it defaults to /home/user/

Sorry M00se-3 for hijacking the thread, should i make a seperate issue?

You can make an event listener, which invoke CMakeSelectCwd when cwd changes. But at default, this plugin only initialize cwd once when you firstly enter the project. Then a session file will be created to store info of this project. Then if you enter this project again, we just read this session file and get cwd. Is that clear?

No worries, @RBoroot.

I might actually close this issue. The reason for opening it was because the plugin was not able to infer the binaryDir through preset inheritance, though setting it explicitly in the selected preset works just fine.

Thinking about it now, such an improvement might be beyond the scope of a lua plugin. What do you think, @Civitasv?

Can you share your presets file? Actually, we've already supported inheritance.

@Civitasv Does it look in presets that have the 'hidden' flag set to true?