igankevich / mesonic

Mesonic: A Vim plugin for Meson build system
45 stars 7 forks source link

Mesonic shouldn't call ninja directly #21

Open vid512 opened 3 months ago

vid512 commented 3 months ago

When building with mesonic :make command, it tries to execute ninja executable directly (from current PATH on Windows), regardless of what ninja executable the meson build was configured to use.

Particulary on Windows, where meson can autodetect compiler, it is possible that there is no ninja present in path. Or one can have different build directories, each configured to use different ninja executable, from different Visual Studio installation, etc.

IMO mesonic shouldn't ignore this part of Meson setup and shouldn't invoke ninja directly. The correct approach should be to use meson compile command, and let it do whatever it is configured to do. Or at least support :MesonCompile Vim command, which uses meson compile, for cases where the difference matters.

Set up build and compile from command line:

Microsoft Windows [Version 10.0.19045.4291]
(c) Microsoft Corporation. All rights reserved.

d:\dev\_testbed\mesonic>ninja
'ninja' is not recognized as an internal or external command,
operable program or batch file.

d:\dev\_testbed\mesonic>meson setup build
The Meson build system
Version: 1.4.0
Source dir: D:\dev\_testbed\mesonic
Build dir: D:\dev\_testbed\mesonic\build
Build type: native build
Project name: foo
Project version: undefined
Activating VS 17.10.0 Preview 2.0
C++ compiler for the host machine: cl (msvc 19.40.33617.1 "Microsoft (R) C/C++ Optimizing Compiler Version 19.40.33617.1 for x64")
C++ linker for the host machine: link link 14.40.33617.1
Host machine cpu family: x86_64
Host machine cpu: x86_64
Build targets in project: 1

Found ninja-1.11.0 at "C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja\ninja.EXE"

d:\dev\_testbed\mesonic>cd build

d:\dev\_testbed\mesonic\build>meson compile
Activating VS 17.10.0 Preview 2.0
INFO: automatically activated MSVC compiler environment
INFO: autodetecting backend as ninja
INFO: calculating backend command to run: "C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja\ninja.EXE"
[2/2] Linking target foo.exe

When I open foo.cpp in Vim and use :make, it tries to execute a :!ninja ... command. But because ninja.exe is not in path, I get shell returned 255, and E40: Can't open errorfile .... (sorry for not giving full log, I don't know how to copy command output from Vim)

igankevich commented 3 months ago

Thanks for the heads-up @vid512.

I will try to update the plugin when I have time to use meson compile. At the time of writing this plugin there were no such command :)

Currently you can try the following workaround. Just add the following line to your .vimrc.

let g:meson_ninja_command = 'meson compile'
igankevich commented 3 months ago

It was easier than I thought.

@vid512, please try the latest version. I only tested this on Linux.

vid512 commented 3 months ago

Nope. Now :make vim command tries to execute :!make 2>&1 | tee .... Even if I define g:meson_ninja_command to meson compile, same thing happens.

Mesonic seems to be loaded correctly. The g:meson_loaded is true, commands like :MesonTest work fine.

(BTW, do you know how I can copy output of Vim command to clipboard, so I can paste it here? )

vid512 commented 3 months ago

Also providing :MesonCompile, just like with other meson commands, would maybe be nice. It was my first intuition to try, after learning about other :MesonXxx commands.

vid512 commented 3 months ago

More info: This problem seenm only to happen if the source file being edited is inside subdirectory. When the .cpp file is in same directory as meson.build, :make works correctly and executes meson compile

igankevich commented 3 months ago

When the .cpp file is in same directory as meson.build, :make works correctly and executes meson compile

@vid512 You are supposed to open all files from the root of your project 😀 This is how most Vim plugins work. There is simply no way of automatically determining where is the root of the project because meson.build can be nested.

Nope. Now :make vim command tries to execute :!make 2>&1 | tee .... Even if I define g:meson_ninja_command to meson compile, same thing happens.

This means mesonic has not found meson.build file in the current directory. Is there such a file?

Also providing :MesonCompile, just like with other meson commands, would maybe be nice. It was my first intuition to try, after learning about other :MesonXxx commands.

I can do that for the sake of less confusion, but it will not solve your problem I guess. Also there is no :Ninja command because it is run automatically much like meson compile.

(BTW, do you know how I can copy output of Vim command to clipboard, so I can paste it here? )

Depends on the terminal you use. I do not work with Windows much. In Alacritty this should be ctrlshiftc.

vid512 commented 3 months ago

You are supposed to open all files from the root of your project 😀 This is how most Vim plugins work.

I was under impression that mesonic scans parent directories looking for meson.build, to allow running in subdirectories. I'm not sure anymore, how I got this idea. Just starting experimenting with higher-level IDE-like Vim plugins, so this wasn't obvious to me.

Is this assumption required only during mesonic setup, or does it expect that current working directory == project root during later actions as well? I am thinking of writing some kind of wrapper over plugin(s) initialization, that would use the correct root directory. At least to get some experience with lua vim scripting.

There is simply no way of automatically determining where is the root of the project because meson.build can be nested.

What could go wrong if topmost meson.build was assumed to be project root?

Depends on the terminal you use. I do not work with Windows much.

I meant a way in Vim itself giving you access to command output (eg. if it is longer than terminal can display at once).

igankevich commented 3 months ago

I was under impression that mesonic scans parent directories looking for meson.build, to allow running in subdirectories.

Yes that is correct. It was so long ago I don't remember :) However, you still need meson.build in the current directory to activate the plugin.

What could go wrong if topmost meson.build was assumed to be project root?

After checking the code I can confirm it works like this. Although this does not cover 100% cases.

I meant a way in Vim itself giving you access to command output (eg. if it is longer than terminal can display at once).

If you're talking about :make output :copen should open the window with the output. For :!cmd the best way is to redirect to a file via shell.

vid512 commented 3 months ago

What are your thoughts about always running this lookup, even if meson.build is not in current dir?

If this is a problem speed-wise (especially when opening files outside meson project), then maybe there could be a user-configurable maximum "depth" for this initial lookup. This could be defaulted to small-enough value (like 5 directories up). Or it could even be defaulted to 0, and only enabled by users who desire this functionality.