Modern Fortran on VSCode fails to recognize external includes

Hi there! I’m on Windows 10, using Visual Studio Code Version: 1.77.3 (system setup) with Modern-Fortran extension v3.2.0 installed. However, the extension does not seem to recognize the external includes during linting (I get squiggles in the use ifwinty line) stating “Module ifwinty not found in project”. I have added the include folder manually to the linter’s include paths.

"fortran.linter.includePaths": [
        "C:\\Program Files (x86)\\Intel\\oneAPI\\compiler\\latest\\windows\\compiler\\include\\"
    ]

And the file ifwinty.f90 is present in the aforementioned directory.
The main issue with this is that I do not get hover assistance and autocomplete from the extension, which would be really helpful. I’d appreciate any workarounds. Thank you.

Includes in Fortran at least for modules and submodules look for the .(s)mod files not the source files.

That you can accomplish without getting the linter to work. For that you need to point fortls to the location of the external sources. This is the VS Code setting that you need to set

Maybe something like this will work

"fortran.fortls.directories": [
        ".\\**",     // Not sure about the Windows notation, but this is meant to discover all directories in your current project root
        "C:\\Program Files (x86)\\Intel\\oneAPI\\compiler\\latest\\windows\\compiler\\include\\"
    ]

Link to fortls config options.

Hope this helps.

1 Like

Thank you. I wasn’t aware that Fortran include paths meant the directory for .mod files. I thought Its similar to C’s headers. But Intel did include a directory with .mod files for all the modules, once I appended that directory to linter’s include paths, (added the fortls directories too) the squiggles are gone I got the hover assistance and autocompletion.

"fortran.linter.includePaths": [
        "C:\\Program Files (x86)\\Intel\\oneAPI\\compiler\\latest\\windows\\compiler\\include\\",  // .f90 sources
        "C:\\Program Files (x86)\\Intel\\oneAPI\\compiler\\latest\\windows\\compiler\\include\\intel64\\"  // their cognate 64 bit .mod files
    ]

Appreciate the help.

Hi there,
I’m having the same problem on Debian Linux 12 with VS Code v-1.85.1, Modern Fortran Pre-Release v3.4.2024010301. fortls is installed through pipx and available in my $PATH. The linter is disabled to try to isolate the issue below.

I tried specifying .fortls as the local settings file in my *.code-workspace file:

{
	"folders": [
		{
			"path": "."
		}
	],
	"settings": {
		"cmake.automaticReconfigure": false,
		"cmake.parallelJobs": 4,
		"workbench.colorTheme": "Visual Studio Dark - C++",
		"workbench.preferredDarkColorTheme": "Abyss",
		"workbench.iconTheme": "vs-minimal",
		"workbench.sideBar.location": "right",
		"workbench.editor.closeOnFileDelete": false,
		"window.density.editorTabHeight": "compact",
		"files.autoSave": "onFocusChange",
		"editor.wordWrap": "on",
		"fortran.linter.compiler": "Disabled",
		"fortran.linter.compilerPath": "",
		"fortran.logging.level": "Debug",
		"fortran.fortls.configure": "/home/ziad/CoursesTutorials/Fortran/NetCDF/.fortls"
	}
}

The workspace local .fortls contains the following:

{
  "lowercase_intrinsics": false,
  "source_dirs": ["./**", "/usr/include"]
} 

The system modules in /usr/include are not found and I get the following error in the Fortran file Module "netcdf" not found in project.
Here is the output of fortls in VS Code:

[DEBUG - 17:32:45] [lsp.client] Language Server version: 2.13.0
[INFO - 17:32:45] [lsp.client] Initialising Language Server for workspace: file:///home/ziad/CoursesTutorials/Fortran/NetCDF
[INFO - 17:32:45] [lsp.client] Language Server arguments: --enable_code_actions --hover_signature --use_signature_help -c /home/ziad/CoursesTutorials/Fortran/NetCDF/.fortls --nthreads=4 --notify_init --incremental_sync --sort_keywords --disable_autoupdate
[DEBUG - 17:32:45] [lsp.client] Language Server arguments:
[
  "--enable_code_actions",
  "--hover_signature",
  "--use_signature_help",
  "-c",
  "/home/ziad/CoursesTutorials/Fortran/NetCDF/.fortls",
  "--nthreads=4",
  "--notify_init",
  "--incremental_sync",
  "--sort_keywords",
  "--disable_autoupdate"
]
[DEBUG - 17:32:45] [lsp.client] Language Server version: 2.13.0
[INFO - 17:32:45] fortls - Fortran Language Server 2.13.0 Initialized
[INFO - 17:32:45] fortls initialization complete

I also tried the other approach of specifying the include files in $workplace/.vscode/settings.json:

{
  "fortran.preferredCase": "uppercase",
  "fortran.fortls.directories": ["./**","/usr/include"]
}

The module not found in project error in the Fortran file persists, and this is the fortls output:

[INFO - 19:16:11] Extension Name: Modern Fortran
[INFO - 19:16:11] Extension Version: 3.4.0
[INFO - 19:16:11] Linter set to: "Disabled"
[INFO - 19:16:11] Formatter set to: "findent"
[INFO - 19:16:11] Autocomplete set to: "fortls"
[INFO - 19:16:11] Hover set to: "fortls"
[INFO - 19:16:11] Symbols set to: "fortls"
[DEBUG - 19:16:11] [lsp.client] Fortran Language Server -- constructor
[DEBUG - 19:16:11] [lsp.client] Language Server arguments:
[
  "--enable_code_actions",
  "--hover_signature",
  "--use_signature_help",
  "-c",
  "/home/ziad/CoursesTutorials/Fortran/NetCDF/.fortls",
  "--nthreads=4",
  "--notify_init",
  "--incremental_sync",
  "--sort_keywords",
  "--disable_autoupdate"
]
[DEBUG - 19:16:11] [lsp.client] Language Server version: 2.13.0
[INFO - 19:16:11] [lsp.client] Initialising Language Server for workspace: file:///home/ziad/CoursesTutorials/Fortran/NetCDF
[INFO - 19:16:11] [lsp.client] Language Server arguments: --enable_code_actions --hover_signature --use_signature_help -c /home/ziad/CoursesTutorials/Fortran/NetCDF/.fortls --nthreads=4 --notify_init --incremental_sync --sort_keywords --disable_autoupdate
[INFO - 19:16:11] fortls - Fortran Language Server 2.13.0 Initialized
[INFO - 19:16:11] fortls initialization complete
[DEBUG - 19:16:11] [lsp.client] Language Server arguments:
[
  "--enable_code_actions",
  "--hover_signature",
  "--use_signature_help",
  "-c",
  "/home/ziad/CoursesTutorials/Fortran/NetCDF/.fortls",
  "--nthreads=4",
  "--notify_init",
  "--incremental_sync",
  "--sort_keywords",
  "--disable_autoupdate"
]
[DEBUG - 19:16:11] [lsp.client] Language Server version: 2.13.0

I even tried adding the "source_dirs" directly by adding them as entries in the Fortran > Fortls: Directories option in the Modern Fortran extension settings. Same module error!

At my wits end here, and wondering if anyone knows how to fix this. I get the feeling I’m just one simple setting away…

Thanks

fortls cannot read .mod and .smod files since they are compiler vendor and compiler version dependant. It can only parse Fortran sources, consequently if no sources for module are found in your source tree then it will raise an Info message saying that they are missing.

I’m already aware of this aspect of fortls, and did manage to make it work for module files generated in another project.

The modules I am trying to use now are system module files located in /usr/include (Debian repo libnetcdff). I added this path to source_dirs, both through Modern Fortran and .fortlsrc. These additions showed up in the DEBUG output for Modern Fortran (but not for .fortlsrc), but still the modules cannot be found.

What is the proper procedure to do this?

Here is a screenshot of the same project opened in Kate, the KDE editor, with fortls on stock settings. The netcdf module is found, and I get proper autocompletion for its functions. For what it’s worth, it doesn’t work anymore if I load the local .fortlsrc configuration.