Vscode conda and modern fortran extension

Hi all,

First of all, than you to the community for all the efforts, it is greate to have common space where look at to start, really great.
I am new with fortran, I normally code in python in VScode in windows but I use linux miniconda environments host in the WSL [Ubuntu 20.04].

I am starting to learn Fortran and I think that will be great if I can create a conda environement with all the libraries required (I am not sure if this is possible or even adequate, please let me know)

So I start with this simple environment file, that conda diggest properly with conda env create --file environement.yml and successfully create an environment called fortran with all my requirements.

---- environment.yml ----

name: fortran
channels:
  - conda-forge
  - defaults
dependencies:
  - fpm
  - fortls
  - findent
  - gfortran
  - gdb

Now, I activate my new environement from the vscode terminal and I am able to use fpm to create a project, bluid and run from terminal.
I review and from terminal with the active environment I can run which gfortran and obtain the path of the environement /home/aragong/miniconda3/envs/fortran/bin/gfortran, same with gdb, fortls and fpm but Vscode extension is not finding these paths. Indeed a pop-up alerts me to install fortls any time I open de project.

Are there any way to configure automatically the paths of the active environment for the extension or Do I have to define these paths hardcoded for each environement in the configuration of the extension?

Also I am having troubles to create a launch.json to perform debug… I have no Idea how to configure these file. Any clue about that based on my setup in WSL and conda?

Thank you in advance!!

1 Like

Welcome to the discourse. The problem is that vscode knowns nothing about your conda environment and all the things that it adds to your PATH when it’s activated. I think the only way that you can get vscode to detect such programs is if you launch it from within a conda activated terminal window, and even then I am not sure what’s going to happen. That’s why Modern Fortran installs everything through pip. If you use cmake and install it in an virtual environment (e.g. anaconda) you will face exactly the same issues.

Also, if you are using WSL I don’t see a reason why you would want to have the above configuration over letting Modern Fortran configure itself.

As for the launch.json, I would start by copying the one provided in the extension description or googling debugging vscode c++. Unfortunately you have to learn the basics of how vscode works to use such features.

I hope this helps.

Thank you for your complete response,

The problem is that vscode knowns nothing about your conda environment and all the things that it adds to your PATH when it’s activated. I think the only way that you can get vscode to detect such programs is if you launch it from within a conda activated terminal window, and even then I am not sure what’s going to happen.

Understood! Will be great that the extension should integrate the management of this, this will allow to have isolated environments.

Also, if you are using WSL I don’t see a reason why you would want to have the above configuration over letting Modern Fortran configure itself.

I want to have isolated environments to work in them, This way is easy to me to clean any mess I make trying things… :slight_smile:
I will try to point the extension my conda fortran env.

As for the launch.json , I would start by copying the one provided in the extension description or googling debugging vscode c++. Unfortunately you have to learn the basics of how vscode works to use such features.

Ok, I will try, is there any easy tutorial arround or youtube video related to this? If any of you know please, let me know!

Thank you!

This is hard to do since we are a Fortran extension, not a Python one. Like every other vscode extension we only have access to what is available to us by the vscode API, which I’ll say, and leave it at that, it often falls short of what extension authors would wish to have.

In general, the virtual environment strategy (isolating project dependencies locally) does not work well with most extensions e.g. C/C++, CMake, etc.

Yes, It is quite triky…

I achieve to link all to my conda environment and now I am working on the launch.json, I achieve to debug using this:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
      {
        "name": "(gdb) Fortran",
        "type": "cppdbg",
        "request": "launch",
        "program": "${workspaceFolder}/build/gfortran_2A42023B310FA28D/${relativeFileDirname}/${fileBasenameNoExtension}",
        "args": [], // Possible input args for a.out
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}/build/gfortran_2A42023B310FA28D/${relativeFileDirname}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "setupCommands": [
          {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
          }
        ]
      }
    ]
  }

Now the point is that I don’t know how to automate the build foldername gfortran_2A42023B310FA28D. I would like to automate this but I run out of ideas. Any suggestion?

Other thing that I am not sure if I doing well is the program path I am passing to the debugger. I am passing the one without extension located in the path build/gfortran_*/app/filename-without-ext that is correct? I see that there are some equivalent files in the path build/gfort_*/"package-name"/app_name.f90.o. All this is created by fpm so I am not sure…

Thank you!

From the looks of it you are using fpm, which adds another degree of complexity. You might want to do what I posted in this GitHub Discussion Integrating fpm with text editors (Sublime, Atom, VS Code) · Discussion #479 · fortran-lang/fpm · GitHub. The current way of using fpm and vscode Modern Fortran is essentially installing the application you are trying to debug in an agreed upon location. We plan in having a better integration with fpm in the future see.

I hope this helps albeit with a minor delay on my end.

1 Like