How to generate all .mod files for linting automatically?

Hi, everyone!

I’m new to VS code and Modern Fortran plugin. I found that when I first click and open Fortran source file, the plugin will generate a .mod file (or .smod) in the same directory. However, it often tells me that some modules cannot be found, because I haven’t clicked those source file and create the related .mod files. It seems that .mod files are required for correct linting.

So, my question is: Is there any way to generate all the .mod files for linting automatically? I just find it’s very silly to click and open each source file one by one… Can anyone give a wise, elegant solution to it?

Many thanks!

Welcome to the Discourse @BrianXia.
If you build your project (i.e. compile it) and point the linter include paths to the locations of the .mod/.smod files the linter should stop complaining.

In the pre-release, we have added some very basic initialisation functionality but it is highly experimental so a lot of times it might not suffice.

1 Like

Normally one specifies the compilation order of the files within the build system. This can be done with a Makefile or with cmake or fpm or other ways. For fortran codes, that compilation order depends on the module dependencies.

There are two settings that I think may be relevant. One is
Linter: Include Paths
The other is:
Linter: Mod Output
I tried both and found that by setting the Mod Output to ${workspaceFolder}/include (where .mod files locate), I can solve my issue. Thanks!

By the way, I just wonder:

  • What is the function of Include Paths?
  • Will linter automatically overwrite the existing .mod file in the include directory after I open or modify the source file?

You would want to set your include Paths option to ${workspaceFolder}/include. This will search for .mod/.smod files under that directory.

The mod Output option is the directory where you want the linter to place the .mod/.smod files it sometimes generates. By default it places them in the same directory as the file you are trying to lint.
You don’t want to mix the .mod/.smod files created by the linter and the ones created by the build tool (cmake, make, autoconf, etc.) for compilation. So I would suggest you create a temporary folder under ${workspaceFolder}/.vscode/linter and use that in mod Output.

1 Like

I tried as your said and it works fine. Many thanks! @gnikit

It seems that the linter will always create new .mod file for the current source file, even if I have set the Include Path where it can find the other mods… Whatever, by setting the Mod Output path to some irrelevant folder, I think I can now comfortably edit Fortran code!

This shouldn’t be happening, we have tests in place that make sure the extension doesn’t do that so you might want to double check that your mod output is set correctly and that you’ve deleted the previously in-source generated mod files. FYI, Intel compilers have a bug where their preprocessor files not being discarded during a lint, they should hopefully fix it with the next release.

I’m glad that you were able to get your set-up up and running

Pardon for late response.

I guess I didn’t make it clear. What I mean is that when I click and open the source file, the extension always generates new .mod file to the Mod Output, rather than to the same directory of source file. Is that the expected behaviour?

No worries, that is the expected behaviour. You linter generates .mod files, because it queries the compiler for the diagnostic messages. The linter would not work robustly if .mod files were not generated.