fortran-lang / vscode-fortran-support

Fortran language support for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=fortran-lang.linter-gfortran
MIT License
214 stars 30 forks source link

"No symbols found in document" error in VS Code v1.67 with Modern Fortran v3.0.2022042917 #460

Closed rangsimanketkaew closed 2 years ago

rangsimanketkaew commented 2 years ago

Hi,

I had been using VS Code with Modern Fortran & fortls without any issues till today right before I upgraded the VS Code to the latest stable release v1.67. The issue I ran into is that the VS Code could not show the outline of the codes in the outline panel (see a snapshot below). --> "No symbols found in document"

Screenshot from 2022-05-06 14-16-10

I have tried disabling all extensions, reloading the windows, restarting the program, and even downgrading to VS Code 1.66 and v1.65v, but they didn't help. I also found this #154 and it seems that the issue I have is also from the VS Code, not Fortran extensions?

Below are the details of my environment setup:

Code snippets in my setting.json file:

  // Fortran
  "fortran.fortls.path": "fortls",
  "fortran.linter.extraArgs": ["-ffree-form"],
  "files.associations": {
    "*.F": "FortranFreeForm",
    "*.f": "FortranFreeForm"
  },

Any ideas to fix this issue?

gnikit commented 2 years ago

Can you post the output of

fortls --debug_filepath your_Fortran_file --debug_rootpath . --debug_symbols
gnikit commented 2 years ago

Also, please use the pre-Release version! We will be releasing version 3.0 soon, the old versions are quite outdated and problematic in all sorts of ways.

rangsimanketkaew commented 2 years ago

Can you post the output of

fortls --debug_filepath your_Fortran_file --debug_rootpath . --debug_symbols

Thanks for a prompt reply! Here is the output I tested your command with this source file:

Testing "initialize" request:
  Root = "."
[INFO - 14:58:07] fortls - Fortran Language Server 2.3.1 Initialized
  Successful!

  Source directories:
    /home/rketka/gitlab/cp2k/src/tmc
    /home/rketka/gitlab/cp2k/src/pw/fft
    /home/rketka/gitlab/cp2k/src/eri_mme
    /home/rketka/gitlab/cp2k/src/mpiwrap
    /home/rketka/gitlab/cp2k/src/fm
    /home/rketka/gitlab/cp2k/src/common
    /home/rketka/gitlab/cp2k/src/arnoldi
    /home/rketka/gitlab/cp2k/src/start
    /home/rketka/gitlab/cp2k/src/motion/mc
    /home/rketka/gitlab/cp2k/src/offload
    /home/rketka/gitlab/cp2k/src/subsys
    /home/rketka/gitlab/cp2k/src/emd
    /home/rketka/gitlab/cp2k/src/aobasis
    /home/rketka/gitlab/cp2k/src/motion/thermostat
    /home/rketka/gitlab/cp2k/src/hfxbase
    /home/rketka/gitlab/cp2k/src/metadyn_tools
    /home/rketka/gitlab/cp2k/src/minimax
    /home/rketka/gitlab/cp2k/src/pw_env
    /home/rketka/gitlab/cp2k/src
    /home/rketka/gitlab/cp2k/src/motion
    /home/rketka/gitlab/cp2k/src/dbcsrx
    /home/rketka/gitlab/cp2k/src/shg_int
    /home/rketka/gitlab/cp2k/src/xc
    /home/rketka/gitlab/cp2k/src/pw
    /home/rketka/gitlab/cp2k/src/input
    /home/rketka/gitlab/cp2k/src/dbm
    /home/rketka/gitlab/cp2k/src/swarm
    /home/rketka/gitlab/cp2k/src/grid
    /home/rketka/gitlab/cp2k/src/dbt/tas
    /home/rketka/gitlab/cp2k/src/base
    /home/rketka/gitlab/cp2k/src/dbt

Testing "textDocument/documentSymbol" request:
  File = "metadynamics.F"
  line    13  symbol ->   2:metadynamics                   parent = null
  line   127  symbol ->  12:metadyn_initialise_plumed      parent = metadynamics
  line   198  symbol ->  12:metadyn_finalise_plumed        parent = metadynamics
  line   225  symbol ->  12:metadyn_integrator             parent = metadynamics
  line   367  symbol ->  12:metadyn_forces                 parent = metadynamics
  line   629  symbol ->  12:metadyn_velocities_colvar      parent = metadynamics
  line   686  symbol ->  12:metadyn_position_colvar        parent = metadynamics
  line   728  symbol ->  12:metadyn_write_colvar           parent = metadynamics
  line   826  symbol ->  12:hills                          parent = metadynamics

Best, Rangsiman

gnikit commented 2 years ago

Well this is strange because I am unable to replicate your issue see image

Let's try the following:

  1. Uninstall the extension
  2. Delete the cached version of the extension (if any) in Linux it is under ~/.vscode/extensions/ You are looking to delete *.linter-gfortran*
  3. Reinstall Modern Fortran with the latest pre-release.
rangsimanketkaew commented 2 years ago

I followed your suggestion but, unfortunately, the issue still persists.

Best, Rangsiman

gnikit commented 2 years ago

Did you restart VSCode after uninstalling Modern Fortran? If so could you try again with VSCode closed.

Also, try setting in you settings, see if something changes if not, try the other option "Both"

  "fortran.provide.symbols": "fortls"
rangsimanketkaew commented 2 years ago

It works, thanks for the spell!

I uninstalled all extensions and installed Modern Fortran pre-release and added "fortran.provide.symbols": "Both" to setting file.

FYI: setting the key "fortran.provide.symbols" to fortls could not solve the issue.

Best, Rangsiman

gnikit commented 2 years ago

That is strange, it appears that VS Code does not recognise the symbols from the Language Server. the "Both" option turns on the fallback symbols that Modern Fortran generates.

Do any of the other features of fortls work? like hover? or GoTo variable definitions?

rangsimanketkaew commented 2 years ago

Just checked, unfortunately, hover does not work, nor GoTo, nor Peek.

gnikit commented 2 years ago

Just checked, unfortunately, hover does not work, nor GoTo, nor Peek.

That is indicative that your fortls installation is not being picked up by Modern Fortran. Could you point VS Code to the full path and see if that fixes anything (i.e. output of which fortls) by default it should be in ~/.local/bin/fortls

gnikit commented 2 years ago

Please also set "fortran.fortls.notifyInit": true

rangsimanketkaew commented 2 years ago

I already used the full path to fortls binary but it does not work.

I tried installing fortls via pip under both conda environment (/myhome/miniconda3/bin/fortls) and virtualenv (/myhome/fortls/bin/fortls) but still could not get those features (mouse hover, peek, and goto).

"fortran.fortls.notifyInit": true didn't help.

gnikit commented 2 years ago

Do you have conflicting settings for Modern Fortran i.e. have settings set on both the global settings.json file and your workspace settings.json file?

The problem is that fortls is not being picked up. Try and remove fortran.fortls.path from your settings. Try and play around with that until you get a popup notification that says fortls initialization complete (that is what "fortran.fortls.notifyInit": true does). We know that fortls works because of the output I made you generate before so it's just a matter of figuring out why your VS Code is struggling to find your fortls installation.

Also, please post the whole output of the log channel from Modern Fortran after having restarted VS Code see attached image about how to find the log channel image

rangsimanketkaew commented 2 years ago

I tested on Ubuntu, Windows, and macOS, only Windows and macOS that Modern Fortran & fortls function ok without issues. Outline can clearly distinguish between e.g. TYPE, SUBROUTINE, and FUNCTION, and I have hover, goto and peek,

Screenshot 2022-05-07 115204 Screenshot 2022-05-07 115229 Screenshot 2022-05-07 120020

but, I'm still not able to make it work on Ubuntu. I used the same settings.json for all operating systems. Not sure if there is something wrong in Python codes of fortls that is incompatible with Ubuntu (Linux) or it is just incompatibility between fortls & VS Code that occurs only on Ubuntu?

gnikit commented 2 years ago

Okay that is good progress. fortls is tested on both Linux(Ubuntu) and Windows see & Modern Fortran on Linux but also I exclusively work and develop on Ubuntu so I don't think that is the problem.

That to me sounds like your Ubuntu machine is configured a bit differently, so let's try and get to the bottom of this. Can you provide the following.

  1. From a new terminal the output of which fortls
  2. Your $PATH variable output
  3. I suspect you haven't changed your workspace settings from the original post, but IF you have, also include those
  4. Add this to your settings.json "fortran.fortls.extraArgs": ["--debug_log"] and post the output of the Modern Fortran Log channel (see my post above about that)
rangsimanketkaew commented 2 years ago
  1. /home/rketka/miniconda3/bin/fortls
  2. $PATH
    /home/rketka/miniconda3/bin:/home/rketka/gitlab/metad/scripts:/home/rketka/plumed-2.8.0/bin:/home/rketka/miniconda3/condabin:/usr/local/Modules/bin:/home/rketka/miniconda3/bin:/usr/local/bin:/home/rketka/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/home/modules/vmd/1.9.3/bin:/home/modules/vmd/1.9.3:/home/modules/gaussview/6.0.16/bin:/home/modules/gaussview/6.0.16
  3. My current setting is
    "fortran.fortls.path": "fortls",
    "fortran.linter.extraArgs": ["-ffree-form"],
    "fortran.provide.symbols": "fortls",
    "fortran.fortls.notifyInit": true,
    "fortran.fortls.extraArgs": ["--debug_log"],
    "files.associations": {
    "*.F": "FortranFreeForm",
    "*.f": "FortranFreeForm"
    },
  4. Log
    ["INFO" - 12:38:38 PM] Extension Name: Modern Fortran
    ["INFO" - 12:38:38 PM] Extension Version: 3.0.2022042917
    ["INFO" - 12:38:38 PM] Linter set to: gfortran
    ["INFO" - 12:38:38 PM] Formatter set to: findent
    ["INFO" - 12:38:38 PM] Autocomplete set to: fortls
    ["INFO" - 12:38:38 PM] Hover set to: fortls
    ["INFO" - 12:38:38 PM] Symbols set to: fortls
    ["INFO" - 12:38:38 PM] using linter: gfortran located in: gfortran
    ["INFO" - 12:38:38 PM] Linter.arguments:
    -ffree-form
    -ffree-line-length-none
    -ffixed-line-length-none
    ["INFO" - 12:38:38 PM] Linter.include:

It seems that the --debug-log didn't produce output because VS Code couldn't load fortls.

rangsimanketkaew commented 2 years ago

It seems that I have found the cause of the issue (?). I've just tested with a new folder containing a few Fortran source codes and now it works!! I guess that VS Code may need to take so long to initiate fortls on a large project (like cp2k, the one that I'm working on now). However, just curious, if it's the case, why don't I encounter the same issue on Windows and macOS as I also work on cp2k project folder? To reproduce, you may try cloning the whole cp2k project (https://github.com/cp2k/cp2k) and open the folder with your VS Code on your Ubuntu.

The following is the output of Modern Fortran with --debug_log set in my setting.json on my Ubuntu on a newly created project folder, which now it works for me (I now have hover, etc. for this small project folder):

["INFO" - 12:46:06 PM] Extension Name: Modern Fortran
["INFO" - 12:46:06 PM] Extension Version: 3.0.2022042917
["INFO" - 12:46:06 PM] Linter set to: gfortran
["INFO" - 12:46:06 PM] Formatter set to: findent
["INFO" - 12:46:06 PM] Autocomplete set to: fortls
["INFO" - 12:46:06 PM] Hover set to: fortls
["INFO" - 12:46:06 PM] Symbols set to: fortls
["INFO" - 12:46:06 PM] using linter: gfortran located in: gfortran
["INFO" - 12:46:06 PM] Linter.arguments:
-ffree-form
-ffree-line-length-none
-ffixed-line-length-none
["INFO" - 12:46:06 PM] Linter.include:

["INFO" - 12:46:06 PM] Fortran Language Server
["INFO" - 12:46:06 PM] Initialising Language Server for workspace: /home/rketka/github/fortran_for_fun
REQUEST 0 initialize
=== No PreProc ===

=== No PreProc ===

=== No PreProc ===

=== No PreProc ===

PROGRAM MAIN !!! PROGRAM - Ln:1
PROGRAM CALC !!! PROGRAM - Ln:1
program hello !!! PROGRAM - Ln:1
program solve_q !!! PROGRAM - Ln:1
end program hello !!! END PROGRAM Scope - Ln:3
INTEGER N, X !!! VARIABLE - Ln:2
REAL :: money, rate, howlong, results !!! VARIABLE - Ln:4
EXTERNAL SUB1 !!! VARIABLE - Ln:3
      real:: a, b,c !!! VARIABLE - Ln:2
=== No PreProc ===

END !!! END  Scope - Ln:9
      real::discriminant !!! VARIABLE - Ln:3
module module1 !!! MODULE - Ln:1
END !!! END  Scope - Ln:19
SUBROUTINE SUB1(X,DUMSUB) !!! SUBROUTINE - Ln:11
      real::x1, x2 !!! VARIABLE - Ln:4
integer:: n !!! VARIABLE - Ln:2
INTEGER N, X !!! VARIABLE - Ln:12
contains !!! CONTAINS - Ln:3
EXTERNAL DUMSUB !!! VARIABLE - Ln:13
recursive subroutine sub1(x) !!! SUBROUTINE - Ln:4
=== No PreProc ===

IF(X .LT. N)THEN !!! IF - Ln:15
      if (discriminant>0 ) then !!! IF - Ln:9
integer,intent(inout):: x !!! VARIABLE - Ln:5
END IF !!! END IF Scope - Ln:19
END !!! END  Scope - Ln:20
if (x < n) then !!! IF - Ln:6
PROGRAM COOR_EXTRACT !!! PROGRAM - Ln:5
IMPLICIT NONE !!! IMPLICIT - Ln:7
end if !!! END IF Scope - Ln:10
INTEGER :: i !!! VARIABLE - Ln:8
end subroutine sub1 !!! END SUBROUTINE Scope - Ln:11
end module module1 !!! END MODULE Scope - Ln:12
PROGRAM MAIN !!! PROGRAM - Ln:14
REAL, ALLOCATABLE :: coor(:,:) !!! VARIABLE - Ln:9
use module1 !!! USE - Ln:15
      end if !!! END IF Scope - Ln:21
REAL :: coor_trans !!! VARIABLE - Ln:10
end program solve_q !!! END PROGRAM Scope - Ln:22
integer:: x = 0 !!! VARIABLE - Ln:16
INTEGER, PARAMETER :: in1=1, out1=2, N=3 !!! VARIABLE - Ln:11
END PROGRAM MAIN !!! END PROGRAM Scope - Ln:20
DO i=1,N !!! DO - Ln:20
        END DO !!! END DO Scope - Ln:22
DO i=1,N !!! DO - Ln:25
        END DO !!! END DO Scope - Ln:27
END PROGRAM COOR_EXTRACT !!! END PROGRAM Scope - Ln:32
fortls - Fortran Language Server 2.3.1 Initialized
SEND {"jsonrpc":"2.0","method":"window/showMessage","params":{"type":3,"message":"fortls initialization complete"}}
fortls initialization complete
SEND {"jsonrpc":"2.0","id":0,"result":{"capabilities":{"completionProvider":{"resolveProvider":false,"triggerCharacters":["%"]},"definitionProvider":true,"documentSymbolProvider":true,"referencesProvider":true,"hoverProvider":true,"implementationProvider":true,"renameProvider":true,"workspaceSymbolProvider":true,"textDocumentSync":2,"signatureHelpProvider":{"triggerCharacters":["(",","]},"codeActionProvider":true}}}
SEND {"jsonrpc":"2.0","method":"window/showMessage","params":{"type":3,"message":"fortls debugging enabled"}}
fortls debugging enabled
RECV {"jsonrpc":"2.0","method":"initialized","params":{}}
REQUEST None initialized
RECV {"jsonrpc":"2.0","method":"workspace/didChangeConfiguration","params":{"settings":{"fortran":{"provide":{"hover":"fortls","autocomplete":"fortls","symbols":"fortls"},"linter":{"compiler":"gfortran","includePaths":[],"compilerPath":"","extraArgs":["-ffree-form"],"modOutput":""},"formatting":{"formatter":"findent","findentArgs":[],"fprettifyArgs":[],"path":""},"fortls":{"path":"fortls","configure":"","sortKeywords":false,"disableAutoupdate":false,"disableDiagnostics":false,"incrementalSync":true,"symbolTypes":true,"notifyInit":true,"maxLineLength":-1,"maxCommentLineLength":-1,"extraArgs":["--debug_log"],"disabled":false,"preserveKeywordOrder":null},"preferredCase":"lowercase","includePaths":null,"gfortranExecutable":null,"linterEnabled":null,"linterExtraArgs":null,"linterModOutput":null,"provideSymbols":null,"symbols":null,"provideHover":null,"provideCompletion":null}}}}
REQUEST None workspace/didChangeConfiguration
error handling request: {'jsonrpc': '2.0', 'method': 'workspace/didChangeConfiguration', 'params': {'settings': {'fortran': {'provide': {'hover': 'fortls', 'autocomplete': 'fortls', 'symbols': 'fortls'}, 'linter': {'compiler': 'gfortran', 'includePaths': [], 'compilerPath': '', 'extraArgs': ['-ffree-form'], 'modOutput': ''}, 'formatting': {'formatter': 'findent', 'findentArgs': [], 'fprettifyArgs': [], 'path': ''}, 'fortls': {'path': 'fortls', 'configure': '', 'sortKeywords': False, 'disableAutoupdate': False, 'disableDiagnostics': False, 'incrementalSync': True, 'symbolTypes': True, 'notifyInit': True, 'maxLineLength': -1, 'maxCommentLineLength': -1, 'extraArgs': ['--debug_log'], 'disabled': False, 'preserveKeywordOrder': None}, 'preferredCase': 'lowercase', 'includePaths': None, 'gfortranExecutable': None, 'linterEnabled': None, 'linterExtraArgs': None, 'linterModOutput': None, 'provideSymbols': None, 'symbols': None, 'provideHover': None, 'provideCompletion': None}}}}
Traceback (most recent call last):
  File "/home/rketka/miniconda3/lib/python3.9/site-packages/fortls/langserver.py", line 159, in handle
    handler(request)
  File "/home/rketka/miniconda3/lib/python3.9/site-packages/fortls/langserver.py", line 1485, in serve_default
    raise JSONRPC2Error(
fortls.langserver.JSONRPC2Error
RECV {"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"file:///home/rketka/github/fortran_for_fun/helloworld.f90","languageId":"FortranFreeForm","version":1,"text":"program hello\nprint *,\"Hello World!\"\nend program hello\n"}}}
REQUEST None textDocument/didOpen
SEND {"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{"uri":"file:///home/rketka/github/fortran_for_fun/helloworld.f90","diagnostics":[]}}
RECV {"jsonrpc":"2.0","id":1,"method":"textDocument/documentSymbol","params":{"textDocument":{"uri":"file:///home/rketka/github/fortran_for_fun/helloworld.f90"}}}
REQUEST 1 textDocument/documentSymbol
SEND {"jsonrpc":"2.0","id":1,"result":[{"name":"hello","kind":2,"location":{"uri":"file:///home/rketka/github/fortran_for_fun/helloworld.f90","range":{"start":{"line":0,"character":0},"end":{"line":2,"character":0}}}}]}
rangsimanketkaew commented 2 years ago

Latest update: it seems that the problem comes from whether a folder that is opened with VS Code contains Fortran source code or not.

Here is the structure of cp2k top directory which has no any *.F source files at this level. When I open this folder with VS Code, the Modern Fortran never works.

cp2k
├── arch
├── benchmarks
├── data
├── exe
├── exts
├── INSTALL.md
├── lib
├── LICENSE
├── Makefile
├── obj
├── README.md
├── regtesting
├── src
├── tests
└── tools

In contrast, when I reopen with src subfolder which contains Fortran source codes, it works.

gnikit commented 2 years ago

So VS Code extensions activate themselves on certain "events", like opening a file, folder, saving etc. At least that is the responsible way of doing this so as to not unnecessarily load a user's computer and pollute their log channels, etc. Modern Fortran adheres to that so it will only activate itself if it encounters any of the following file extensions or if the user sets the language as fortran see https://github.com/fortran-lang/vscode-fortran-support/blob/c2febbfcb321268e84c4360d11ed5ea97096c905/package.json#L46-L99

When you open a Fortran file, then Modern Fortran will activate and register all the necessary functionality including the Language Server. Language Servers in general take some time to initialize because they have to parse your entire source code into something called Abstract Syntax Trees (compilers do that in the backend too), therefore you should allow a few seconds for fortls to initialize. This process in fortls is actually multithreaded, by default it uses 4 cores, you can change this setting by the -n or --nthreads argument in fortls

I hope this clears things up a bit. It would be awesome if we could make Language Servers provide results for large projects instantaneously but even the most performant Language Servers or compiler parsers need a lot of time to create AST for complex projects.

rangsimanketkaew commented 2 years ago

I see. Thanks again for clarifying things.

gnikit commented 2 years ago

So everything is wroking okay? Can I close this issue now?

rangsimanketkaew commented 2 years ago

Yes. Have a nice weekend!