vscode-cpptools icon indicating copy to clipboard operation
vscode-cpptools copied to clipboard

Add IntelliSense for C++20 modules importing

Open sean-mcmanus opened this issue 4 years ago • 83 comments

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

sean-mcmanus avatar Oct 13 '20 17:10 sean-mcmanus

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

cjwijtmans avatar Oct 16 '21 14:10 cjwijtmans

@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.

sean-mcmanus avatar Oct 19 '21 22:10 sean-mcmanus

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

Bigous avatar Mar 08 '22 14:03 Bigous

@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?

sean-mcmanus avatar Mar 08 '22 21:03 sean-mcmanus

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

Bigous avatar Mar 09 '22 10:03 Bigous

  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 avatar Mar 09 '22 22:03 sean-mcmanus

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

sean-mcmanus avatar Mar 09 '22 22:03 sean-mcmanus

@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 avatar Mar 09 '22 22:03 sean-mcmanus

@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).

sean-mcmanus avatar Mar 09 '22 23:03 sean-mcmanus

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

Bigous avatar Mar 10 '22 12:03 Bigous

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.

str0yd avatar Mar 16 '22 17:03 str0yd

@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".

sean-mcmanus avatar Mar 16 '22 18:03 sean-mcmanus

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.

fatboyzz avatar Jul 17 '22 15:07 fatboyzz

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

waruqi avatar Aug 13 '22 14:08 waruqi

@waruqi Not yet.

sean-mcmanus avatar Aug 15 '22 18:08 sean-mcmanus

image Ifc file version 0.42 is not supported

RimuruChan avatar Sep 17 '22 01:09 RimuruChan

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

sean-mcmanus avatar Sep 17 '22 02:09 sean-mcmanus

@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

sean-mcmanus avatar Sep 28 '22 05:09 sean-mcmanus

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

Bigous avatar Sep 28 '22 11:09 Bigous

@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.

bobbrow avatar Sep 28 '22 16:09 bobbrow

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

matheus-kirchesch-old avatar Oct 04 '22 00:10 matheus-kirchesch-old

@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.

sean-mcmanus avatar Oct 04 '22 13:10 sean-mcmanus

@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?

Bigous avatar Oct 04 '22 22:10 Bigous

@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 )

matheus-kirchesch-old avatar Oct 05 '22 03:10 matheus-kirchesch-old

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.

cjwijtmans avatar Oct 05 '22 07:10 cjwijtmans

@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.

sean-mcmanus avatar Oct 05 '22 15:10 sean-mcmanus

@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.

cjwijtmans avatar Oct 05 '22 18:10 cjwijtmans

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 avatar Oct 05 '22 22:10 Bigous

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.

Bigous avatar Oct 06 '22 01:10 Bigous

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

sean-mcmanus avatar Oct 06 '22 14:10 sean-mcmanus