conan icon indicating copy to clipboard operation
conan copied to clipboard

[feature] Provide `conanrun.env` file when using `VirtualRunEnv` generator.

Open hwhsu1231 opened this issue 3 years ago • 15 comments

I use VSCode with CMake-Tools plugin to write/debug the CMake project. After the executable is generated, it needs to find those DLL files on Windows when using xxx:shared=True option. If we don't want to use imports section to copy those DLL file to where executable locates, we can use VirtualRunEnv generator to use PATH to find them. According to the reply: https://github.com/conan-io/conan/issues/11040#issuecomment-1101357775

Yes, you could use the VirtualRunEnv generator, to generate scripts that will add those paths to the PATH and after sourcing those generated scripts the system will find the dll's for those libraries, please check the VirtualRunEnv documentation for more detailed information.

However, although VirtualRunEnv generators provide scripts(conanrun.(bat|sh)) to setup the environment variables, it seems that VSCode doesn't provide the features to preload those *.bat or *.sh file in launch.json, dynamically. The only way to do that is to use envFile option to load *.env file to setup environment.

If VirtualRunEnv generator can generate conanrun.env additionally, that would be helpful when using launch.json to debug the executable. For example:

image

hwhsu1231 avatar Apr 19 '22 01:04 hwhsu1231

I manually copy the PATH environment setup from conanrunenv-debug-x86_64.bat to conanrun.env, and it works "successfully" without using imports section to copy those DLL files.

image image image

hwhsu1231 avatar Apr 19 '22 01:04 hwhsu1231

I found this issue, which might be related to mine:

  • https://github.com/conan-io/conan/issues/8714

I'm confused that "Does Conan already have other features to generate .env file?"

hwhsu1231 avatar Apr 19 '22 05:04 hwhsu1231

Yes, we have been prioritizing so far the terminal usage and the core Conan integration, being correct over the environment activation. Integrations with the IDEs is still to be checked for the new environment, and it makes sense to have some feature that generates .env files (maybe besides the .sh/.bat ones). I have annotated it for discussion shortly after 2.0. In the meantime, I think it is relatively easy to generate the file directly with the generate() method, and instantiating the VirtualRunEnv(), then iterating its .vars to dump to a file.

memsharded avatar Apr 19 '22 07:04 memsharded

@memsharded May I ask, you add this issue to the 2.X milestone, will this feature be added in Conan 1.X, recently?

hwhsu1231 avatar Apr 19 '22 13:04 hwhsu1231

It's also something I've suggested here: https://github.com/conan-io/conan/issues/10075#issuecomment-978064194

Until conan can provide some IDE integration, I use this tasks.json: https://github.com/SpaceIm/vscode-cpp-cmake-conan-template/blob/master/.vscode/tasks.json

Basically, I use this task to run conan install with many generators, parse the conanrun script file generated by conan, generate a dotenv file, and give it to launch.json (do not run executables through CMake: Launch).

Full instructions: https://github.com/SpaceIm/vscode-cpp-cmake-conan-template#readme

SpaceIm avatar Sep 03 '22 17:09 SpaceIm

It would be great if the EnvVars contained a method like save_env similar to the existing ones like save_bat, save_ps1 and save_sh, which do the following:

result = []
for varname, varvalues in self._values.items():
    result.append("{}={}".format(varname, varvalues))

content = "\n".join(result)
open(file_location, "w", encoding="utf-8").write(content)

This will allow to simply make .env file in the generate() method like

re = VirtualRunEnv(self)
re.generate()
re.save_env(re.basename + ".env")

maximMalofeev avatar May 03 '24 19:05 maximMalofeev

Hi @maximMalofeev

This ticket is kind of an old one. Nowadays, the modern CMakeToolchain is able to generate CMakePresets.json files with env-vars defined, so that would be useful for a large majority of cases. What is exactly your use case? The same as suggested above by the OP?

memsharded avatar May 05 '24 16:05 memsharded

Yes, the case is the same as above. I need a way to provide environment variables for the vscode debug target in the launch.json.

maximMalofeev avatar May 06 '24 07:05 maximMalofeev

@memsharded Althought having CMakePresets with the correct environment set is a nice addition, there are still some cases where it is not enough. I am writing right now a pybind11 module and I need to create a vscode launch.json to launch /usr/bin/python which runs the script containing my C++ module. Since my module depends on other C++ libraries, I need some way to say where to find them.

jjcasmar avatar Oct 09 '24 09:10 jjcasmar