cbuchart
cbuchart

Reputation: 11555

Using local Clang-format when developing in a remote Docker container

I'm using VS Code connected to a Docker container: project files and compilation tools are located there. Host system is macOS Big Sur.

I'm using the Clang-Format extension, but the clang-format available in the Docker image is very outdated and cannot be upgraded (for external reasons), so I cannot use the project's .clang-format file (which contains newer directives).

I have the newest version of clang-format installed locally in the host computer. Is there any way to configure VS Code to use that version of clang-format instead of the one in the Docker image?

Upvotes: 0

Views: 2408

Answers (1)

cbuchart
cbuchart

Reputation: 11555

The local copy cannot be used because VS Code will run it inside the Docker instead of in the host, and the binaries are not compatible.

The best way I've found to do this is to download clang-format for Linux in the host, share it with the container and tell VS Code to use it:

  1. Download (or compile) clang-format compatible with the Linux version of the container. In my case the container is based on Ubuntu 20.04, so there is already a precompiled version of clang+LLVM available in the official project repo.

  2. Uncompress it into a directory that can be bound to the container (you can do it in Docker Desktop, under Settings > Resources > File sharing). In my case I have one prepared for this: ~/shared, so I uncompressed it under ~/shared/clang+llvm-13.0.0.

  3. In the docker-compose.yml file mount the local directory where clang-format is located (use the remote directory that best works for you):

services:
    volumes:
      - ~/shared/clang+llvm-13.0.0:/home/user/clang+llvm-13.0.0:ro
  1. Finally, set the path to the executable in the user's settings.json:
{
  "clang-format.executable": "/home/user/clang+llvm-13.0.0/bin/clang-format"
}

The only drawback of this approach is that the clang-format.executable setting is user-wide, there is no workspace-level setting, so the change apply universally. In my case it is not a big issue because everything is done in containers, but I know it may not be the best option for mixed setups.

Upvotes: 0

Related Questions