hansec / fortran-language-server

Fortran Language Server for the Language Server Protocol
MIT License
296 stars 57 forks source link

Private variable declaration in module seen in other scopes #191

Closed stefabat closed 2 years ago

stefabat commented 3 years ago

Hi

I am using the fortran language server (FLS) in vscode, so I am not sure if it is a problem of the extension I am using, or the FLS itself. I suspect the second.

I have a private variable declared in a module with the same name as an intrinsic fortran keyword:

module foo
  implicit none
  private
  integer len
end module foo

Somewhere else in the code, I declare

character(len=*) :: bar = 'bar'

And vscode (through the FLS) is marking it as an error and saying 'object "len" not found in scope' and points me to the module above. Furthermore, if I start typing

use foo, only: l...

the auto completion suggests me the variable len even though this is private and should not be seen. Indeed, trying to use the variable len anywhere else in the code runs into a compilation error with gfortran.

Any idea on what is going on?

gnikit commented 2 years ago

So this is caused by fortls (sometimes?) not respecting the module scopes. I think there a few more issues that are caused from this, especially when it comes to autocompletion.

gnikit commented 2 years ago

The extend of this bug is generally related to visibility scopes and modules. If a variable is not defined as private or public in the same line e.g. integer, private :: len then there seems to be an issue distinguishing the public from private scopes.

If I remember correctly all modules are public by default, so I think this should be relatively easy to fix.

gnikit commented 2 years ago

@stefabat This has now been fixed in fortls v2.0.1

stefabat commented 2 years ago

Thanks, but lately I haven't been able to have fortls working in my vscode at all... I am bit confused on what should be installed: pip install fortran-language-server or pip install fortls? Reagrdless of which, I actually install it in a conda environment and then point vscode to the fortls executable within the conda folder, but it doesn't work.

gnikit commented 2 years ago

I think that might be a mistake on my end in the pre release of Modern Fortran. If you have turned on an option for disabling fortls it will skip registering the server all together see https://github.com/krvajal/vscode-fortran-support/issues/365 . I'll fix it over the weekend and in another prerelease

stefabat commented 2 years ago

Ok, I had time to fix my setup and everything works. I can confirm that this fixes the issue and I don't see anymore the error.

Just for completeness and future reference, this is the current setup with which I managed to have it working: I don't want to mess up my distribution python packages with pip, so what I have is a miniconda installation that I activate with the modulefiles system (like in HPC clusters). With conda active, I created a new environment where I installed fortls with pip. Then, in VSCode I only use the Modern Fortran extension in the pre-release version (hence no FORTRAN Intellisense and vscode-modern-fortran-formatter) and I explicitly set the path to the fortls within the miniconda installation. Everything works!