microsoft / vscode-cpptools

Official repository for the Microsoft C/C++ extension for VS Code.
Other
5.41k stars 1.52k forks source link

Add IntelliSense for C++20 modules importing #6302

Open sean-mcmanus opened 3 years ago

sean-mcmanus commented 3 years ago

See the previous Windows/cl.exe-only issue at https://github.com/microsoft/vscode-cpptools/issues/6290 (UPDATE: the issue was deleted, see https://github.com/microsoft/vscode-cpptools/issues/8256#issuecomment-941809096 ).

NOTE: C++20 modules importing works for cl.exe if you set /ifcSearchDir (and possibly other modules args if necessary); however the IntelliSense support is still in active development so there are lots of known issues/bugs still.

This is dependent on IntelliSense parser changes from VS (i.e. open file Linux solutions in VS don't have module importing support for IntelliSense either).

EDIT: example here: https://github.com/microsoft/vscode-cpptools/issues/6302#issuecomment-1930791901

cjwijtmans commented 2 years ago

Is there any way to get it somewhat working or does it not parse .ifc files at all?

sean-mcmanus commented 2 years ago

@cjwijtmans It may work with msvc mode with .ifc files for cl.exe, but it's not officialy supported yet. We process /headerUnit, /ifcSearchDir, /reference, and /stdIfcDir in compilerArgs.

Bigous commented 2 years ago

I don't know if it's only for me, but #6290 just goes to 404 error... The intellisense is not working for imports at all.

The definition: image

And whe we use it: image

But the compiler has no complains....

image

sean-mcmanus commented 2 years ago

@Bigous It's not officially supported, but we do have code that processes various modules command lines. Can you provide the command line args you're using in compilerArgs, particularly those that are modules-related?

Bigous commented 2 years ago

Hi @sean-mcmanus , actually I'm using the CMake to build... but essentially it's /std:c++latest /experimental:module /EHsc /MD.

It's a project to learn what C++20 brings and I really liked the module feature. I think it will save tons of time in compile.

I just uploaded the project to github so you can see all easily

sean-mcmanus commented 2 years ago
  1. You currently need to change it to something like target_compile_options(cpp20 PRIVATE "/experimental:module" "/ifcSearchDir\"C:\\pathTo\\build\""), i.e. see https://devblogs.microsoft.com/cppblog/standard-c20-modules-support-with-msvc-in-visual-studio-2019-version-16-8/ .
  2. But I'm seeing error "IFC file \"C:\pathTo\build\Utils.Numbers.ifc\" has unsupported version 0.41", so I'm looking into that...
sean-mcmanus commented 2 years ago

@Bigous Good news -- it works correctly with 1.9.2 (Pre-Release), i.e. the 0.41 version error goes away.

sean-mcmanus commented 2 years ago

@Bigous However, I'm seeing what appears to be an incorrect IntelliSense error: "expression preceding parentheses of apparent call must have (pointer-to-) function type" -- I'm looking into filing a bug on that against our shared VS parser...

sean-mcmanus commented 2 years ago

@Bigous Our VS parser team is still actively developing the IntelliSense feature for modules, so incorrect IntelliSense errors like that are "expected" at this point (and possibly other IntelliSense issues).

Bigous commented 2 years ago

Hi @sean-mcmanus , thanks for the effort! If I can help with anything, you can count on me.

str0yd commented 2 years ago

Do you guys have some time frame when we can expect it working? I tried it a few days ago but it was incredibly slow, or not working at all. It felt like it was kinda random.

Same in Visual Studio 2022.

I think the modules are one of the most exiting changes in C++20 because having to maintain both source and header file is at least for me effort that could be avoided.

sean-mcmanus commented 2 years ago

@str0yd It's being actively developed. I don't know of any set timeframes, i.e. it could be a year or so. We get updates periodically. I'm guessing the VS C++ team might release a blog or something when they get IntelliSense modules support "finished".

fatboyzz commented 2 years ago

I set the /ifcSearchDir for .ifc files. For my module "Math" the line: import Math; Got error "Can't find module file ***/Math.edgm" No idea what is .edgm file. And it is not generated in my project directory. But "Go To Definition" works for names exported from module.

waruqi commented 1 year ago

Is there now support for c++20 modules for gcc/clang in addition to ifc?

sean-mcmanus commented 1 year ago

@waruqi Not yet.

RimuruChan commented 1 year ago

image Ifc file version 0.42 is not supported

sean-mcmanus commented 1 year ago

@RimuruChan It's fixed in our pending 1.13.1 update -- maybe for Tuesday next week.

sean-mcmanus commented 1 year ago

@RimuruChan It's available now in 1.13.1 (the IFC file version 0.42): https://github.com/microsoft/vscode-cpptools/releases/tag/v1.13.1

Bigous commented 1 year ago

Hi @sean-mcmanus , I switched to the pré-release version, but it still complains when importing a mudule, but now about a edgm file:

image

bobbrow commented 1 year ago

@Bigous, there is something suspicious about that error message. How are you specifying the path to the IFC files? It looks like there is an extra set of quotes in the path.

matheus-kirchesch-old commented 1 year ago

Is there no way to write modules in a .cpp file? why should cpp code be written in a file named ixx wtf does that even mean

Right now if I do the hacks needed to compile modules in .cpp files I still can't use intellisense

sean-mcmanus commented 1 year ago

@Kirchesch I don't think it needs to be named with the .ixx extension, see https://learn.microsoft.com/en-us/cpp/cpp/tutorial-named-modules-cpp?view=msvc-170 : "you can treat a source file with any extension as a module interface file. Set the Compile As property in the Advanced tab for the source file's properties page to Compile As Module (/interface).". Also, "Module unit implementation files don't end with an .ixx extension. They're normal .cpp files.".

We haven't validated if C++20 modules is usable in VS Code yet -- it's still being developed on the VS side and there might be more work we need to do on our side.

Bigous commented 1 year ago

@Bigous, there is something suspicious about that error message. How are you specifying the path to the IFC files? It looks like there is an extra set of quotes in the path.

Hy @bob, it's in my CMakeLists.txt file:

target_compile_options(cpp20
    PRIVATE
    /experimental:module
    /ifcSearchDir"R:\\Richard\\src\\playground\\c++\\C++20\\build\\cpp20.dir\\Debug"
)

And it's correctly finding the .ifc files. There is no .edgm file beeing generated in this directory and it compiles and runs normally. The error message is only in VSCode.

image

@sean-mcmanus Tks sean. Can I help with something?

matheus-kirchesch-old commented 1 year ago

@Kirchesch I don't think it needs to be named with the .ixx extension, see https://learn.microsoft.com/en-us/cpp/cpp/tutorial-named-modules-cpp?view=msvc-170 : "you can treat a source file with any extension as a module interface file. Set the Compile As property in the Advanced tab for the source file's properties page to Compile As Module (/interface).". Also, "Module unit implementation files don't end with an .ixx extension. They're normal .cpp files.".

We haven't validated if C++20 modules is usable in VS Code yet -- it's still being developed on the VS side and there might be more work we need to do on our side.

Yes you are right, but I can't declare a module in a .cpp file in visual studio the compiler doesn't allow, unless I specifically tell the compiler that the specific file should be compiled as a module (msvc ofc), and when I do that neither Visual Studio nor vs code can give me intellisense, I was worried Microsoft was trying to force standardization of .ixx as module files..

Btw I get perfectly fine intellisense from vs and vs code when using ixx files, I don't even need to specify any "experimental" feature on cmake or anything, I just treat as a normal library and it works fine. ( I have to delete the output with certain changes but mostly fine )

cjwijtmans commented 1 year ago

The problem is i cant specify ifc dir in cmake. This is non standard and changes between compilers generators os etc. Ninja generator should take care of this not cmake.

sean-mcmanus commented 1 year ago

@Bigous If there is specific modules issue that works correctly in VS 2022 that we're not handling correctly we can look into fixing that.

@Kirchesch @cjwijtmans Can you file an issue on VS (https://developercommunity.visualstudio.com) ? I didn't see an existing issue.

cjwijtmans commented 1 year ago

@Kirchesch @cjwijtmans Can you file an issue on VS (https://developercommunity.visualstudio.com) ? I didn't see an existing issue.

There really isnt an issue here because ifc search dir already seems to be correctly defined by the generator and i dont need to do anything, just got confused because someone in the thread said to defined it manually.

In my c++ modules in vscode test project i have a different issue though. When i add a subproject as INTERFACE or OBJECT library that is a dependency the compiler gets confused. When its an INTERFACE project it cant find the module, when its an OBJECT library the module interface seems to exist twice. In both methods there is a compile error. I just wanted to avoid compiling a static library its not neccesary and not desired.

Bigous commented 1 year ago

Hi @sean-mcmanus , Visual Studio 2022 Community edition also has the same problem:

image

And compiles and link and execute and debug the software with no problems... just like vscode.

image

Bigous commented 1 year ago

Ok, some progress:

I've created a new simpler project with one module but this time, I've created from Visual Studio 2022 Community CMake project wizard.

This time Visual Studio has no problem in finding the module (you must compile it twice for it to work properly - because the first time it generates the ifc files):

image

VSCode still can't see it:

image

The CMake project uses presets when created with VS wizards.

In both I can compile, run, and debug normally.

And if I force the /ifcSearchDir into CMakeLists.txt VSCode gives me the edgm error (VS does see the modules and finds the ifc files without setting this manually in the CMakeLists.txt). I noticed that .command files were generated in the build destination, and they have all the compilers parameters that are necessary to compile each file. My guess is that VS is using it to correctly generate the Intellisense and find everything.

sean-mcmanus commented 1 year ago

@Bigous I've moved your issue to https://github.com/microsoft/vscode-cpptools/issues/9965 .

matheus-kirchesch-old commented 1 year ago

@sean-mcmanus @cjwijtmans VS 2022 with cmake 3.24 with this exact configuration

cmake_minimum_required(VERSION 3.24)

project(testing-modules-project VERSION 0.1.0)

####

# Ack use of experimental features
set(CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API "3c375311-a3c9-4396-a187-3227ef642046")

# Experimental Module support
set(CMAKE_EXPERIMENTAL_CXX_MODULE_DYNDEP 1)

# This setup is MSVC-only for now
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
    string(CONCAT CMAKE_EXPERIMENTAL_CXX_SCANDEP_SOURCE
        "<CMAKE_CXX_COMPILER> <DEFINES> <INCLUDES> <FLAGS> <SOURCE> /nologo /TP"
        " /showIncludes /scanDependencies <DYNDEP_FILE>"
        " /Fo<OBJECT>")

    set(CMAKE_EXPERIMENTAL_CXX_SCANDEP_DEPFILE_FORMAT "msvc")
    set(CMAKE_EXPERIMENTAL_CXX_MODULE_MAP_FORMAT "msvc")
    set(CMAKE_EXPERIMENTAL_CXX_MODULE_MAP_FLAG "@<MODULE_MAP_FILE>")
endif()

####

add_library(modules-library STATIC)
    target_compile_features(modules-library PUBLIC cxx_std_20)
    target_sources(
      modules-library
      PUBLIC
        FILE_SET CXX_MODULES FILES
        src/test-module.cpp
 "src/test-ixx-module.ixx")

add_executable(testing-modules-project "src/main.cpp")
target_link_libraries(testing-modules-project PUBLIC modules-library)

image

VS Code with the latest release of cmake in the same project image

What I can see is that only ixx files generate ifc's. Another thing is that vscode can't read those ifc's maybe it doesn't know where it is or idk.

mnmkay commented 1 year ago

@sean-mcmanus @cjwijtmans VS 2022 with cmake 3.24 with this exact configuration

I've discovered a potential solution from my own attempts in another situation. You may try this: copy test-ixx-module.ixx.ifc to test-ixx-module.ifc. Then vscode may be able to read it (restart intellisense for the file or restart vscode to make sure). We could at least benefit for now (while we wait for a real fix) if cpptools can incorpoate some kind of hack-fix base on this, in the event that this "solution" works reliably. Does it work for you?

By the way I'm sure you do also have to add /ifcSearchDir to cmake comple options and this is EXTREMELY FINICKY, example:

target_compile_options(<TARGET> PUBLIC /ifcSearchDir<PATH-TO-IFC>)

Don't put a space between /ifcSearchDir and . Or compilation fails. Don't surround in quotes, or vscode falsely complains it can't open something about .edgem file in intellisense.

Perhaps someone can provide a different solution to automatically have our ixx's named so vscode can read them (for now while we wait). By the way I've noticed sometimes vscode reads an IFC for intellisense purposes and refuses to let go. So when I rebuild the IFC, it fails to write the file because write-access is locked by the operating system (Windows 10). I must kill the cpptools-srv process. Something to look out for as well...

lunfardo commented 1 year ago

any idea how to fix "edgm file not found" error on vscode? I dont know how those files are generated.

sean-mcmanus commented 1 year ago

@lunfardo Are you using msvc or gcc/clang IntelliSense mode? Oh, it looks like you may be hitting bug https://github.com/microsoft/vscode-cpptools/issues/9965

masx200 commented 1 year ago

At present, the format of the module IFC has not been standardized, why not let me manually specify the source files or set which directories to search for the source files? And other compilers other than mavc do not support IFC, do other compilers generate module binaries in different formats?

bjornr commented 1 year ago

For those of you trying experimental cmake support for c++ modules, check out

https://www.kitware.com/import-cmake-c20-modules/

These instructions seems more updated than some of the guidelines above. Works for me with named modules on macosx with cmake@3.26, clang++@16 and recent ninja. Sadly module import in IntelliSense does not work though, but compile, link, run works.

cjwijtmans commented 1 year ago

Does clang provide introspection with modules?

ytimenkov commented 1 year ago

Yes, clangd extension can read modules built by clang compiler. I played over the Christmas holidays with waf as a build system (since it's the easiest one to extend). At the time neither clang nor gcc could build standard headers all the way and the server mode for module resolver didn't pay well with compile_commands.json which were used to communicate between the build system and the language servers, so I lost interest. But the integration itself between compiler and LS worked surprisingly well.

Gwani commented 1 year ago

@sean-mcmanus @cjwijtmans VS 2022 with cmake 3.24 with this exact configuration By the way I've noticed sometimes vscode reads an IFC for intellisense purposes and refuses to let go. So when I rebuild the IFC, it fails to write the file because write-access is locked by the operating system (Windows 10). I must kill the cpptools-srv process. Something to look out for as well...

I just wanted to confirm this observation, which happens pretty reliably for my setup (VSCode 1.78.2, vscode-cpptools 1.15.4, CMake Tools 1.14.31, CMake 3.26.1 using MSVC Toolset 14.32.31326 due to #10843). Project is generated with CMake using the experimental C++ Module features. Got Intellisense to work as excpected with my own imported Modules by adding the ifcSearchDir compiler argument to c_cpp_properties.json using an absolute path to where CMake places the .ifc files:

"compilerArgs": [ "-ifcSearchDirD:/Development/matrix/.build/windows-x64-msvc/CMakeFiles/matrix.dir/debug" ]

Changing any code necessitating rebuilding the .ifc results in: [build] D:\Development\matrix\interface\engine\core\math\matrix.ixx(119): error C3474: could not open output file 'CMakeFiles\matrix.dir\debug\engine.core.math-matrix.ifc'

"Clean Reconfigure" in CMake Tools results in: [rollbar] Unhandled exception: Unhandled Promise rejection: cleanConfigureAll Error: EBUSY: resource busy or locked, unlink 'D:\Development\matrix\.build\windows-x64-msvc\CMakeFiles\matrix.dir\debug\engine.core.math-matrix.ifc' {}

"Clean all projects" in CMake Tools results in: ninja: error: remove(CMakeFiles/matrix.dir/debug/engine.core.math-matrix.ifc): The process cannot access the file because it is being used by another process.

.ifc files seem to be in this locked state almost immediately whenever a relevant source file is opened. Restarting VSCode only helps when those source files are closed beforehand (so theyre not automatically opened again).

Growlith1223 commented 1 year ago

Im not sure if this is the right area to be asking this but has anyone managed to get intellisense working with modules in GCC/Clang? compilation works just fine, it's just the intellisense that is borked.

H-G-Hristov commented 10 months ago

@bobbrow I was going to ask if there are any updates on modules but then I remembered to check the EDG site: [REDACTED] If I understand correctly, it's the base for all IntelliSense features.

In that context I have another question: In the case of modules, how are going Documentation comments to be supported? Where should they be placed? Any ideas? I assume we mean Doxygen and the current support in VSCode.

I would really like to be able to continue seeing the help hover dialogs, with documentation strings.

bobbrow commented 10 months ago

@H-G-Hristov I would expect documentation comments to be picked up for module exports the same way they are for regular types/functions/etc. Does that not happen with the unofficial support today?

JG-Adams commented 10 months ago

I use VScode, Cmake, Mingw and I get this false positive on import. It's currently the only thing that's keeping me from using module. When will they fix it?

bobbrow commented 10 months ago

Hi @JG-Adams, unfortunately, we don't have a good estimate as to when we will be able to support modules across all the different compilers. When our language server gets the update we will add this issue to a Milestone and enable the support in VS Code.

dortamiguel commented 9 months ago

Growlith1223

did you found out how to do this?

ntswamp commented 6 months ago

any workaround to enjoy modules in vscode?

wusikijeronii commented 6 months ago

any workaround to enjoy modules in vscode?

I use cpptools as the debugger frontend and clangd for IntelliSense (via compile_commands) until the issue and a few others that are important to me are closed. Just know, clangd use caching for Intellisense, like Intelli Idea. It means you can get suggestions from old or irrelevant files. And this doesn't support doxygen-like commands. You just have to choose what is more important to you.

glcraft commented 6 months ago

any workaround to enjoy modules in vscode?

I'm using the extension clangd and the program xmake to generate the compile_commands.json (and compile modules).

g199209 commented 6 months ago

clangd can work, but there's two small problems.

  1. Is there any way to highligh import keyword?
  2. I can not jump to module file from import statement.
someview commented 6 months ago

It's surprising that the 'import' feature hasn't been supported yet. What factors need to be considered?"