vscode-cmake-tools icon indicating copy to clipboard operation
vscode-cmake-tools copied to clipboard

Propagates selected target from one VSCode instance into another VSCode instance even if there is no such target there

Open MartinsSmirnovs opened this issue 2 years ago • 9 comments

Brief Issue Summary

If you have two different projects opened in two different VSCode instances, using dev containers, and both of these projects have same exact absolute path, then selected CMake target in first VSCode instance will be propagated into second VSCode instance, usually breaking next build, as there is no such target in the second instance.

Prerequisites

ms-vscode-remote.remote-containers extension installed in VSCode.

Steps to reproduce

I have created repository to reproduce this bug.

  1. $ git clone https://github.com/MartinsSmirnovs/cmake_tools_same_path_bug.git
  2. $ code dir1 && code dir2
  3. In both VSCode instances execute >Dev Containers: Rebuild and Reopen in Container to open the projects in Docker containers.
  4. In both VSCode instances execute >CMake: Configure and select Debug configuration if such question pops up.
  5. In one of VSCode instances select available example* CMake target.
  6. Now go to the second VSCode instance and press Build.
  7. Observe the error that CMake cannot build the target you have selected in another VSCode instance, e.g.:
[main] Building folder: code 
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build /code/out/build/Debug --target example_2
[build] gmake: *** No rule to make target 'example_2'.  Stop.
[proc] The command: /usr/bin/cmake --build /code/out/build/Debug --target example_2 exited with code: 2
[driver] Build completed: 00:00:00.015
[build] Build finished with exit code 2

despite the fact that there is no such target in the project you were trying to build. You can see it in respective CMakeLists.txt file.

What is known

I believe that the bug comes from the fact that in .devcontainer/devcontainer.json configuration both projects use same absolute path /code as workspace directory.

CMake Tools Diagnostics

From example_2 instance:

{
  "os": "linux",
  "vscodeVersion": "1.84.2",
  "cmtVersion": "1.15.31",
  "configurations": [
    {
      "folder": "/code",
      "cmakeVersion": "3.22.1",
      "configured": true,
      "generator": "Unix Makefiles",
      "usesPresets": true,
      "compilers": {
        "C": "/usr/bin/cc",
        "CXX": "/usr/bin/c++"
      }
    }
  ],
  "cpptoolsIntegration": {
    "isReady": true,
    "hasCodeModel": true,
    "activeBuildType": "Debug",
    "buildTypesSeen": [
      "Debug"
    ],
    "requests": [],
    "responses": [],
    "partialMatches": [],
    "targetCount": 1,
    "executablesCount": 1,
    "librariesCount": 0,
    "targets": [
      {
        "name": "example_2",
        "type": "EXECUTABLE"
      }
    ]
  },
  "settings": [
    {
      "communicationMode": "automatic",
      "useCMakePresets": "auto",
      "configureOnOpen": null
    }
  ]
}

From example instance:

{
  "os": "linux",
  "vscodeVersion": "1.84.2",
  "cmtVersion": "1.15.31",
  "configurations": [
    {
      "folder": "/code",
      "cmakeVersion": "3.22.1",
      "configured": true,
      "generator": "Unix Makefiles",
      "usesPresets": true,
      "compilers": {
        "C": "/usr/bin/cc",
        "CXX": "/usr/bin/c++"
      }
    }
  ],
  "cpptoolsIntegration": {
    "isReady": true,
    "hasCodeModel": true,
    "activeBuildType": "Debug",
    "buildTypesSeen": [
      "Debug"
    ],
    "requests": [],
    "responses": [],
    "partialMatches": [],
    "targetCount": 1,
    "executablesCount": 1,
    "librariesCount": 0,
    "targets": [
      {
        "name": "example",
        "type": "EXECUTABLE"
      }
    ]
  },
  "settings": [
    {
      "communicationMode": "automatic",
      "useCMakePresets": "auto",
      "configureOnOpen": null
    }
  ]
}

Debug Log

From example_2 instance:

[main] Building folder: code 
[main] Saving open files before configure/build
[build] Starting build
[driver] Start build example_2
[proc] Executing command: /usr/bin/cmake --build /code/out/build/Debug --target example_2
[proc]   with environment: {"HOSTNAME":"65cf6799b24a","HOME":"/home/developer","PATH":"/vscode/vscode-server/bin/linux-x64/1a5daa3a0231a0fbba4f14db7ec463cf99d7768e/bin/remote-cli:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin","VSCODE_AGENT_FOLDER":"/home/developer/.vscode-server","SHELL":"/bin/bash","PWD":"/vscode/vscode-server/bin/linux-x64/1a5daa3a0231a0fbba4f14db7ec463cf99d7768e","VSCODE_HANDLES_SIGPIPE":"true","VSCODE_AMD_ENTRYPOINT":"vs/workbench/api/node/extensionHostProcess","VSCODE_HANDLES_UNCAUGHT_ERRORS":"true","VSCODE_NLS_CONFIG":"{\"locale\":\"en\",\"osLocale\":\"en\",\"availableLanguages\":{}}","REMOTE_CONTAINERS_IPC":"/tmp/vscode-remote-containers-ipc-6338bcd1-0095-4b40-9d2a-e8e22de28dc5.sock","LS_COLORS":"","REMOTE_CONTAINERS_SOCKETS":"[\"/tmp/vscode-ssh-auth-6338bcd1-0095-4b40-9d2a-e8e22de28dc5.sock\",\"/tmp/.X11-unix/X0\"]","SHLVL":"1","_":"/usr/bin/cat","SSH_AUTH_SOCK":"/tmp/vscode-ssh-auth-6338bcd1-0095-4b40-9d2a-e8e22de28dc5.sock","DISPLAY":":0","REMOTE_CONTAINERS_DISPLAY_SOCK":"/tmp/.X11-unix/X0","XDG_RUNTIME_DIR":"/tmp/user/1000","WAYLAND_DISPLAY":"vscode-wayland-4fad9280-21dc-4896-a6ef-d30dd666b31d.sock","REMOTE_CONTAINERS":"true","BROWSER":"/vscode/vscode-server/bin/linux-x64/1a5daa3a0231a0fbba4f14db7ec463cf99d7768e/bin/helpers/browser.sh","VSCODE_CWD":"/vscode/vscode-server/bin/linux-x64/1a5daa3a0231a0fbba4f14db7ec463cf99d7768e","ELECTRON_RUN_AS_NODE":"1","VSCODE_IPC_HOOK_CLI":"/tmp/user/1000/vscode-ipc-16c7cb9d-19bb-4a5c-9e13-85e64347bf66.sock","APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL":"1","VSCODE_L10N_BUNDLE_LOCATION":""}
[build] [100%] Built target example_2
[driver] Build completed: 00:00:00.039
[cmakefileapi-parser] Read reply folder: /code/out/build/Debug/.cmake/api/v1/reply
[cmakefileapi-parser] Found index files: ["cache-v2-148bf883f59bf292b4ab.json","cmakeFiles-v1-f4d8f676b5e1a771884a.json","codemodel-v2-eebfe514908abb96c35f.json","directory-.-Debug-f5ebdc15457944623624.json","index-2023-11-13T10-53-52-0697.json","target-example_2-Debug-35324a15e8a1a7dade9f.json","toolchains-v1-99265b04de7ada3aac9e.json"]
[build] Build finished with exit code 0
[cache] Reading CMake cache file /code/out/build/Debug/CMakeCache.txt
[cache] Parsing CMake cache string
[extension] [7514] cmake.build finished (returned 0)
[cache] Reading CMake cache file /code/out/build/Debug/CMakeCache.txt
[cache] Parsing CMake cache string

From example instance:

[main] Building folder: code 
[main] Saving open files before configure/build
[build] Starting build
[driver] Start build example_2
[proc] Executing command: /usr/bin/cmake --build /code/out/build/Debug --target example_2
[proc]   with environment: {"HOSTNAME":"2ce4bf2a90bb","HOME":"/home/developer","PATH":"/vscode/vscode-server/bin/linux-x64/1a5daa3a0231a0fbba4f14db7ec463cf99d7768e/bin/remote-cli:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin","VSCODE_AGENT_FOLDER":"/home/developer/.vscode-server","SHELL":"/bin/bash","PWD":"/vscode/vscode-server/bin/linux-x64/1a5daa3a0231a0fbba4f14db7ec463cf99d7768e","VSCODE_HANDLES_SIGPIPE":"true","VSCODE_AMD_ENTRYPOINT":"vs/workbench/api/node/extensionHostProcess","VSCODE_HANDLES_UNCAUGHT_ERRORS":"true","VSCODE_NLS_CONFIG":"{\"locale\":\"en\",\"osLocale\":\"en\",\"availableLanguages\":{}}","REMOTE_CONTAINERS_IPC":"/tmp/vscode-remote-containers-ipc-db906aae-8766-4bba-82c2-c11cb2afc739.sock","LS_COLORS":"","REMOTE_CONTAINERS_SOCKETS":"[\"/tmp/vscode-ssh-auth-db906aae-8766-4bba-82c2-c11cb2afc739.sock\",\"/tmp/.X11-unix/X0\"]","SHLVL":"1","_":"/usr/bin/cat","SSH_AUTH_SOCK":"/tmp/vscode-ssh-auth-db906aae-8766-4bba-82c2-c11cb2afc739.sock","DISPLAY":":0","REMOTE_CONTAINERS_DISPLAY_SOCK":"/tmp/.X11-unix/X0","XDG_RUNTIME_DIR":"/tmp/user/1000","WAYLAND_DISPLAY":"vscode-wayland-07505f61-3439-4acf-ae26-32ffa9db704b.sock","REMOTE_CONTAINERS":"true","BROWSER":"/vscode/vscode-server/bin/linux-x64/1a5daa3a0231a0fbba4f14db7ec463cf99d7768e/bin/helpers/browser.sh","VSCODE_CWD":"/vscode/vscode-server/bin/linux-x64/1a5daa3a0231a0fbba4f14db7ec463cf99d7768e","ELECTRON_RUN_AS_NODE":"1","VSCODE_IPC_HOOK_CLI":"/tmp/user/1000/vscode-ipc-94349287-f52b-45dd-a994-1867b90215e7.sock","APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL":"1","VSCODE_L10N_BUNDLE_LOCATION":""}
[build] gmake: *** No rule to make target 'example_2'.  Stop.
[proc] The command: /usr/bin/cmake --build /code/out/build/Debug --target example_2 exited with code: 2
[driver] Build completed: 00:00:00.011
[cmakefileapi-parser] Read reply folder: /code/out/build/Debug/.cmake/api/v1/reply
[cmakefileapi-parser] Found index files: ["cache-v2-755b24404b0b30eae0db.json","cmakeFiles-v1-e7d7c80ca50f5bae85ea.json","codemodel-v2-94beba8399fdbdab1378.json","directory-.-Debug-f5ebdc15457944623624.json","index-2023-11-13T10-54-14-0789.json","target-example-Debug-8a40020c94575c3e626b.json","toolchains-v1-99265b04de7ada3aac9e.json"]
[build] Build finished with exit code 2
[cache] Reading CMake cache file /code/out/build/Debug/CMakeCache.txt
[cache] Parsing CMake cache string
[extension] [4721] cmake.build finished (returned 2)
[cache] Reading CMake cache file /code/out/build/Debug/CMakeCache.txt
[cache] Parsing CMake cache string

Additional Information

If you close the second instance and then reopen it again, and press Build, the build will fail, and you will see that both instances will have the same target displayed on taskbars, despite such target not existing in the second instance.

image

MartinsSmirnovs avatar Nov 13 '23 11:11 MartinsSmirnovs

Hi @MartinsSmirnovs thank you for your detailed post! We'll add this to our backlog and look into it as soon as we have the capacity.

snehara99 avatar Nov 14 '23 16:11 snehara99

Any chance of this issue getting looked at? My team's workflow often involves running multiple instances of VS Code attached to different Docker containers based on the same Docker image. All of the projects use the same working directory inside the containers, although the build targets set are usually different, and this bug is causing major headaches.

AnElectricalEngineer avatar Jul 22 '24 07:07 AnElectricalEngineer

@v-frankwang Could you try to reproduce this issue? I wasn't able to reproduce following the steps that the user provided.

gcampbell-msft avatar Jul 22 '24 19:07 gcampbell-msft

I created a simple project that can be used as a POC to reproduce this bug. To reproduce it, please follow the following steps:

Setup:

  1. Clone my POC project. This project contains a simple CMake project with 2 different build targets: Main1 and Main2:
cd ~
git clone https://github.com/AnElectricalEngineer/cmake-tools-bug-poc
  1. Build the simple, Ubuntu 20.04-based Docker image:
cd cmake-tools-bug-poc/Docker
docker build -f ./Dockerfile -t poc-image .
  1. Copy the downloaded repo to another folder, since this bug requires two separate projects using same source code to be reproduced. Then, create two Docker containers that mount the source code folders via bind mount. Note that the target path of the bind mount inside the container is the same for both containers, but the source paths are different:
cp -r ~/cmake-tools-bug-poc ~/cmake-tools-bug-poc-2
docker run -itd --name container1 --mount type=bind,source=$HOME/cmake-tools-bug-poc,target=/home/user/workdir --workdir /home/user/workdir poc-image
docker run -itd --name container2 --mount type=bind,source=$HOME/cmake-tools-bug-poc-2,target=/home/user/workdir --workdir /home/user/workdir poc-image
  1. 'Attach' VS Code via Docker extension to container1, and inside, open the folder: /home/user/workdir in VS Code
  2. 'Attach' VS Code via Docker extension to container2, and inside, open the folder: /home/user/workdir in VS Code
  3. In both containers, create .vscode folder in project root, and create settings.json file. In this file, in both containers, add:
{
		"cmake.sourceDirectory": "${workspaceFolder}/src",
		"cmake.buildDirectory": "${workspaceFolder}/build"
}

To reproduce the bug:

  1. container1: from the Command Palette, do: Cmake: Configure and choose the GCC kit
  2. container2: from the Command Palette, do: Cmake: Configure and choose the GCC kit
  3. container1: from the Command Palette, do: Cmake: Set Build Target and choose Main1
  4. container2: from the Command Palette, do: Cmake: Set Build Target and choose Main2
  5. container1: from the Command Palette, do: Cmake: Build. This is the bug: Main2 is built instead of Main1!

AnElectricalEngineer avatar Jul 23 '24 12:07 AnElectricalEngineer

@AnElectricalEngineer Thanks for the repro! I was able to reproduce with this, so I will put it on deck and try to investigate it. Thanks.

gcampbell-msft avatar Jul 23 '24 14:07 gcampbell-msft

I may have at least a small fix, could you try out this vsix and see whether it fixes your issues? You'll need to download the zip file, change the extension from .zip to .vsix, and then install manually in your VS Code. If you want to install it in your dev containers, you likely will need to copy them into your docker container.

@AnElectricalEngineer @MartinsSmirnovs cmake-tools.zip

gcampbell-msft avatar Jul 23 '24 15:07 gcampbell-msft

I may have at least a small fix, could you try out this vsix and see whether it fixes your issues? You'll need to download the zip file, change the extension from .zip to .vsix, and then install manually in your VS Code. If you want to install it in your dev containers, you likely will need to copy them into your docker container.

@AnElectricalEngineer @MartinsSmirnovs cmake-tools.zip

Thanks for the quick response. Your fix seems to have indeed solved the issue with build targets propagating to other containers.

I did notice something odd though while testing your vsix on my production docker containers - perhaps it is another similar issue that was not seen until now.

I am using slightly different cmake-variants.json files in the two containers. When I choose the 'arm8-64bit' variant for the first container, and then the arm8-64bit + Debug variant for the second container, and then go back to the first container and try to configure CMake, I get an error:

[variant] Last variant selection is incompatible with present variant definition.
[variant] >> Missing setting "buildType" in variant definition.
[variant] Using default variant choices from variant definition.

If at this point, I again go through the process of selecting a variant for the first container, and then try to configure, it succeeds without errors.

The variants file used in container 1 was (note that buildType is part of the 'architecture' option):

{
  "architecture": {
    "default": "arm",
    "description": "Architecture to Compile For",
    "choices": {
      "arm": {
        "short": "arm8-64bit",
        "long": "Compile for arm8-64bit architecture",
        "buildType": "Release",
        "settings": {
          "TARGET_ARCH": "arm8-64bit",
          "DEBUG_SYMBOLS": "YES"
        }
      },
      "sim": {
        "short": "x86-64bit",
        "long": "Compile for x86-64bit architecture",
        "buildType": "Release",
        "settings": {
          "TARGET_ARCH": "x86-64bit",
          "DEBUG_SYMBOLS": "YES"
        }
      }
    }
  }
}

While the variants file used in container 2 is: (note that buildType is now a separate option)

{
  "architecture": {
    "default": "arm",
    "description": "Architecture to Compile For",
    "choices": {
      "arm": {
        "short": "arm8-64bit",
        "long": "Compile for arm8-64bit architecture",
        "settings": {
          "TARGET_ARCH": "arm8-64bit",
          "DEBUG_SYMBOLS": "YES",
        }
      },
      "sim": {
        "short": "x86-64bit",
        "long": "Compile for x86-64bit architecture",
        "settings": {
          "TARGET_ARCH": "x86-64bit",
          "DEBUG_SYMBOLS": "YES",
        }
      }
    }
  },
  "buildType": {
    "default": "release",
    "description": "CMake build type",
    "choices": {
      "debug": {
        "short": "Debug",
        "long": "Build with Debug build type",
        "buildType": "Debug"
      },
      "release": {
        "short": "Release",
        "long": "Build with Release build type",
        "buildType": "Release"
      }
    }
  }
}

Any idea why this might be the case? I don't think I have tested this exact case before (selecting variant in container 1, then selecting variant in container 2, and then going back to container 1 and configuring), but it seems like a similar issue - last set variant (container 2) propagating to container 1.

Let me know if you'd rather I open a separate bug about this issue.

AnElectricalEngineer avatar Jul 23 '24 16:07 AnElectricalEngineer

@gcampbell-msft I reproduced the problem using a user supplied project:

ENV: CMake Tools :v1.18.43 Dev Container:v0.375.1 C/C++:v1.21.2 vscode: v1.91.1

Actual result: dir1: image dir2: image

v-frankwang avatar Jul 26 '24 07:07 v-frankwang

@MartinsSmirnovs Following up on your last comment, it seems like there is some of our design that works based on workspace state, which is often mapped by the path, so there may be more work to do in order to fix this across the board and not just for target selection. Therefore, I've placed this in the "On Deck" category and we will aim to work on it in upcoming releases. Thanks!

gcampbell-msft avatar Jul 30 '24 15:07 gcampbell-msft