Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using "Launch target" doesn't allow reading files from the project root due to weird cwd #4103

Open
nonk123 opened this issue Sep 30, 2024 · 2 comments
Labels
more info needed More info is needed from the community for us to properly triage and investigate.

Comments

@nonk123
Copy link

nonk123 commented Sep 30, 2024

Brief Issue Summary

Clicking "Launch target" runs the resulting executable with cwd = build/Debug. This means it cannot access e.g. the assets folder in the project root, which is a major inconvenience when multiple people are working on the same project and have to be instructed on copying that directory manually.

I haven't been able to find a cross-platform way to automate this besides using a file(COPY ...) hack or modifying the extension config, but it just feels wrong. Is there a different solution that works with the stock settings.json so anybody could just jump right in?

CMake Tools Diagnostics

No response

Debug Log

No response

Additional Information

No response

@Yingzi1234 Yingzi1234 added more info needed More info is needed from the community for us to properly triage and investigate. and removed triage labels Oct 8, 2024
@Yingzi1234
Copy link
Collaborator

Hi @nonk123 Thank you for your GitHub issue! To solve your issue, could you try the following four methods? Please let us know if you have any concerns! Thank you!

  1. Modify the Executable's Working Directory
    Instead of relying on the default behavior, you can configure your launch settings to set the working directory explicitly. This can usually be done in your settings.json or launch configuration file.

For example, in a typical launch configuration for Visual Studio Code, you might add:

"cwd": "${workspaceFolder}",
This will set the current working directory to the project root, allowing access to the assets folder without additional work.

  1. Use a Script to Set Up the Environment
    You could create a simple shell or batch script that handles building and then running the executable with the correct working directory. For example, a run.sh or run.bat script could do something like:
#!/bin/bash
# Build the project
cmake --build build/Debug
# Run the executable with the project root as the cwd
cd build/Debug
./your_executable

This way, team members just need to run the script, and it handles everything for them.

  1. Use Relative Paths in Your Code
    If your code can be modified, consider using relative paths to access the assets. For example, if your project structure is:
    bash
/project_root
    /assets
    /build
        /Debug
            your_executable

You can adjust the asset loading code to look for assets relative to the executable's location:
cpp
std::string assetPath = "../assets/your_asset.file";
4. Add Asset Copying to the Build Process
If you prefer not to change launch settings or rely on scripts, you can automate the copying of the assets directory during the build process using CMake. You can add a command in your CMakeLists.txt like this:

cmake

add_custom_command(TARGET your_target POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
    "${CMAKE_SOURCE_DIR}/assets" "${CMAKE_BINARY_DIR}/assets"
)

This way, every time you build, the assets folder will be copied to the build directory.

@nonk123
Copy link
Author

nonk123 commented Oct 8, 2024

Hi and thanks for your response!

Just to clarify: the project in question is developed on multiple OSes and with different code editors. The issue at hand is only applicable to development with VS Code and this extension.

I wouldn't have any issues building and running my project straight after cloning the repository using the command line:

cd MyProject
cmake -S . -B build
cmake --build build
./build/MyProject

But if I decide to build and run the project from VS Code, I'll need to either:

  1. Manually modify my local configuration (solution 1), which adds an extra step to getting the project to build and run on the dev's side after cloning
  2. Or add a custom copy target (solution 4) that would be redundant for those who don't work on the project in VS Code.

Solutions 2 and 3 don't quite apply here. Solution 4 is probably the best case scenario, but I'd still prefer to avoid an unnecessary copy for command line builds.

But I guess there isn't a "perfect" way to do this if you have to resort to hacks in all cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
more info needed More info is needed from the community for us to properly triage and investigate.
Projects
Status: Blocked
Development

No branches or pull requests

2 participants