Cannot find module error in vscode with Modern Fortran, fpm and fortls

Hi, new to Fortran and first post here.

I’m trying to get used to fpm. I have this warning where it says

Cannot open module file 'solvers.mod' for reading at (1): No such file or directory

However, if I copy the .mod file in the “src” directory, the error vanishes. I’m not sure if it’s a vscode issue and json config or rather a fortls/fpm issue.

Any help will be appreciated. Thanks.

You shouldn’t need to do that. How did you build the solvers.f90 file? Did you invoke the compiler directly locally?

With fpm you should invoke fpm build or fpm run from the project root (where the fpm.toml file is located). It will build the static library within the build folder and link to your executable corresponding to the main.f90 automatically.

I did try both (fpm build or fpm run since the later does the first anyway).

Here is the .toml file if that can help.

name = "solvers"
version = "0.1.0"
license = "license"
author = "Jane Doe"
maintainer = "jane.doe@example.com"
copyright = "Copyright 2025, Jane Doe"
[build]
auto-executables = true
auto-tests = true
auto-examples = true
module-naming = false
[install]
library = false
[fortran]
implicit-typing = false
implicit-external = false
source-form = "free"

and here is the output from fortls:

["INFO" - 7:58:30 AM] Fortran Language Server
["INFO" - 7:58:30 AM] Initialising Language Server for workspace: /home/bill/projects/solvers/app/main.f90 with command-line options: --enable_code_actions, --hover_signature, --use_signature_help, --lowercase_intrinsics, --nthreads=8, --incremental_sync, --incl_suffixes, .cuf
[INFO - 07:58:36] fortls - Fortran Language Server 3.2.1 Initialized
["INFO" - 7:59:20 AM] using linter: gfortran located in: /usr/bin/gfortran
["INFO" - 7:59:20 AM] Linter.arguments:
-Wall
-ffree-line-length-none
-ffixed-line-length-none

I did not mention but it compiles and run, I only get the warning when I hover over use solver

Welcome to the forum!

So, After some investigation I found that the linter you are using was not sure where to find the solvers.mod file that it generates.

All you need to do is change the fortran.linter.includePaths variable to add the glob pattern ${workspaceFolder}/**/src/ to it since this is where the linter creates the .mod files.
Or alternatively you could play around with the fortran.linter.modOutput variable too to set the directory appropriately.

You can change these in Settings > Extensions > Modern Fortran > Linting

Let me know if this fixes the issue.

It worked, thank you.

I changed fprettify to findent and the same thing happen. Is it normal behavior or is it supposed to understand that files in /src may produce .mod files that are usefull in /app

I think there is some confusion here, fprettify and findent are code formatters and are not responsible for this. linters used by Modern Fortran extension are gfortran, ifort, ifx etc they are not supposed to understand such stuff and can be used however you want. While fpm is supposed to understand such stuff because it creates the directory structure we usually use (fpm new)

Strictly speaking gfortran is a compiler but compilers usually can provide basic linting. Not sure about the other linters