# Feature Request: Run Fortran code with one click in VSCode

**URL:** <https://fortran-lang.discourse.group/t/feature-request-run-fortran-code-with-one-click-in-vscode/3976>\
**Category:** Uncategorized\
**Created:** [July 11, 2022, 6:22pm UTC](https://fortran-lang.discourse.group/t/feature-request-run-fortran-code-with-one-click-in-vscode/3976 "2022-07-11T18:22:26Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [July 11, 2022, 6:22pm UTC](https://fortran-lang.discourse.group/t/feature-request-run-fortran-code-with-one-click-in-vscode/3976/1 "2022-07-11T18:22:26Z")

</div>

Currently when I teach newcomers Fortran, I use VSCode, the Modern Fortran extension (@gnikit), then setup autosave (on focus change). We modify the code, then click on the built-in terminal, and compile by hand by executing commands, and run the generated binary. Then modify the code again and repeat.

It would be very nice if one could just click some kind of a button to compile and run the code. As well as clicking some key, such a F9, that would compile and run (probably executing `fpm run` underneath).

We also need some ways to pass in `--compiler=LFortran` into fpm, not sure if Modern Fortran VSCode extension should do it, or I should have some `fpm` config file for that.

My other big issue with explaining “implicit none” is now fixed by LFortran, that by default does not require it. So once we fix the issue above, then we have a solid environment to teach.

---

<div class="post-metadata">

**Author:** ![gnikit](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gnikit/32/1213_2.png) [@gnikit](https://fortran-lang.discourse.group/u/gnikit)\
**Post date:** [July 11, 2022, 6:47pm UTC](https://fortran-lang.discourse.group/t/feature-request-run-fortran-code-with-one-click-in-vscode/3976/2 "2022-07-11T18:47:14Z")

</div>

> [@certik](#):
>
> t would be very nice if one could just click some kind of a button to compile and run the code. As well as clicking some key, such a F9, that would compile and run (probably executing `fpm run` underneath).

This has been something that I have been meaning to add for a while now. The project as a whole is being tracked here: [FPM Integration VS Code · GitHub](https://github.com/fortran-lang/vscode-fortran-support/projects/1).

Having said that, this kind of build and debug automation is currently possible already through the use of [tasks](https://code.visualstudio.com/docs/editor/tasks) and [`launch.json`](https://code.visualstudio.com/docs/editor/debugging) scripts for debugging. You can bind these actions to keys, by default building is `Ctrl/Cmd + Shift + B` and debugging is `F5`. Other things that VS Code allows are: always building before launching the debugger and have multiple build tasks.

There was a post in FPM Discussions I think where I provided some scripts for VS Code and FPM integration. [This](https://github.com/fortran-lang/fpm/discussions/479#discussioncomment-2942179) is how currently one can debug in Modern Fortran using FPM. In that same thread there are more general purpose scripts for using FPM.

Techinically, we already have the ability to integrate with an FPM (or CMake or Autotools) it is just **very** user unfriendly.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [July 11, 2022, 6:51pm UTC](https://fortran-lang.discourse.group/t/feature-request-run-fortran-code-with-one-click-in-vscode/3976/3 "2022-07-11T18:51:57Z")

</div>

Yes, I know one can setup things already by hand, so I am hoping there is no technical obstacle.

But I would like this to be the default, so that just installing the extension would just make things work (it’s ok for now to install fpm and a Fortran compiler separately).

Can an extension create a button to click?

---

<div class="post-metadata">

**Author:** ![plevold](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/plevold/32/754_2.png) [@plevold](https://fortran-lang.discourse.group/u/plevold)\
**Post date:** [July 11, 2022, 7:21pm UTC](https://fortran-lang.discourse.group/t/feature-request-run-fortran-code-with-one-click-in-vscode/3976/4 "2022-07-11T19:21:19Z")

</div>

It’s not the same as full integration with Modern Fortran, but I use an extension that adds a keyboard shortcut to repeat the last command executed in the terminal. As long as you just use the terminal to run fpm it would just compile and run the code. I think it’s this one:[Re-run Last Command - Visual Studio Marketplace](https://marketplace.visualstudio.com/items?itemName=Cameron.rerun-last-command)

---

<div class="post-metadata">

**Author:** ![gnikit](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gnikit/32/1213_2.png) [@gnikit](https://fortran-lang.discourse.group/u/gnikit)\
**Post date:** [July 11, 2022, 7:25pm UTC](https://fortran-lang.discourse.group/t/feature-request-run-fortran-code-with-one-click-in-vscode/3976/5 "2022-07-11T19:25:46Z")

</div>

We can do all of that. Downloading `lfortran` and `fpm` is quite easy, in theory. The problem we have in practice is that we wouldn’t want people to have to setup Anaconda for that. So we might want to look for another build system.

> [@certik](#):
>
> Can an extension create a button to click?

Yes, absolutely, if you have a look at the C++ extension, they have added Debug/Run, Choose build configuration and Build

![image](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/4/44cc8a0f91be488401c7f7b5c38a968ec3bded17.png)

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [July 11, 2022, 7:53pm UTC](https://fortran-lang.discourse.group/t/feature-request-run-fortran-code-with-one-click-in-vscode/3976/6 "2022-07-11T19:53:30Z")

</div>

Perfect! Yes, we want to have a working debugger too, but one step at a time. 🙂 Just the “run” button (with a default simple key combination like F5 or F9) would be awesome.

Yes, so how to download fpm/lfortran?

My experience with Conda is that it’s best to leave it to the user to install. Installing it automatically does not seem to yield robust solid results.

We control how LFortran is distributed, so we could make a simple binary tarball for each platform. The extension could download, unpack. The same with fpm. I think that’s all that is needed.

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [July 11, 2022, 8:27pm UTC](https://fortran-lang.discourse.group/t/feature-request-run-fortran-code-with-one-click-in-vscode/3976/7 "2022-07-11T20:27:24Z")

</div>

> [@certik](#):
>
> My other big issue with explaining “implicit none” is now fixed by LFortran, that by default does not require it.

+1

Thank you, thank you, thank you! 🙂

One should never have to explain “`implicit none`” to newcomers!

And not have to waste any more precious time and energy and resources, however brief and small, with being forced to include the statement in one’s programs.

---

<div class="post-metadata">

**Author:** ![gnikit](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gnikit/32/1213_2.png) [@gnikit](https://fortran-lang.discourse.group/u/gnikit)\
**Post date:** [July 13, 2022, 1:48pm UTC](https://fortran-lang.discourse.group/t/feature-request-run-fortran-code-with-one-click-in-vscode/3976/8 "2022-07-13T13:48:55Z")

</div>

> [@certik](#):
>
> Yes, so how to download fpm/lfortran?

@awvwgk Do you have any ideas on this, I think my `PyPi` suggestion is likely to be more maintainance work than we want.

Whatever the solution ends up being I think we should keep a log of the download stats (somehow) and aggregate them with the Anaconda stats. If a lot of people are using the packages we want that to show.

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [July 13, 2022, 3:59pm UTC](https://fortran-lang.discourse.group/t/feature-request-run-fortran-code-with-one-click-in-vscode/3976/9 "2022-07-13T15:59:58Z")

</div>

> [@gnikit](#):
>
> @awvwgk Do you have any ideas on this, I think my `PyPi` suggestion is likely to be more maintainance work than we want.

If you have access to `curl` / `wget` / `Invoke-WebRequest` you could download one of the pre-built binaries from the GitHub release and add them to the system `PATH` at least for fpm. However, the distribution at the moment is limited to Linux/x86\_64, MacOS/x86\_64, and Windows/x86\_64, but could be extended depending on the required platforms.

For LFortran creating pre-built binaries should be possible as well, but due to the dependency on LLVM for any serious application, downloading binaries on-demand seems resource-demanding and prone to conflicts with system libraries.

Both fpm and LFortran use conda-forge as primary distribution channel and offer a good coverage of platforms such as Linux (x86\_64, aarch64, ppc64le), MacOS (x86\_64, arm64), and Windows (x86\_64). Installing compilers should preferably be done with a proper package manager, to avoid conflicts with the system libraries, which can lead to unnecessary user frustration.

---

<div class="post-metadata">

**Author:** ![gnikit](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gnikit/32/1213_2.png) [@gnikit](https://fortran-lang.discourse.group/u/gnikit)\
**Post date:** [July 13, 2022, 5:20pm UTC](https://fortran-lang.discourse.group/t/feature-request-run-fortran-code-with-one-click-in-vscode/3976/10 "2022-07-13T17:20:41Z")

</div>

> [@awvwgk](#):
>
> If you have access to `curl` / `wget` / `Invoke-WebRequest` you could download one of the pre-built binaries from the GitHub release

We absolutely could, we can download anything from anywhere we want. The other idea that I had for `fpm` was to download the repo and compile it since we always have a working compiler (gfortran, ifort, nagfor) for the linter. Both of these approaches would effectively store the binaries somewhere inside the extension installation, similar to the C++ extension storing all the language servers.

`lfortran` still puzzles me a bit, I need to get more familiar with the codebase to be able to make any meaningful comments about how to install it reliably.

Ultimately, it might be worth prompting the user to install miniconda if not detected in their system and then creating a venv for the VS Code extension and doing everything inside that.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [July 13, 2022, 5:47pm UTC](https://fortran-lang.discourse.group/t/feature-request-run-fortran-code-with-one-click-in-vscode/3976/11 "2022-07-13T17:47:42Z")

</div>

LLVM is linked statically, so that’s not a problem. On Linux at least, LFortran is a standalone statically linked binary. On macOS I think one has to always dynamically link to their “system” (libc), but otherwise everything else is also statically linked. On Windows I don’t have that much experience, but I assume it can also be done. You can have multiple versions of LFortran installed and they should not clash with each other.

---

<div class="post-metadata">

**Author:** ![gnikit](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gnikit/32/1213_2.png) [@gnikit](https://fortran-lang.discourse.group/u/gnikit)\
**Post date:** [October 7, 2022, 11:06am UTC](https://fortran-lang.discourse.group/t/feature-request-run-fortran-code-with-one-click-in-vscode/3976/12 "2022-10-07T11:06:45Z")

</div>

FYI @certik this should be possible now with the latest pre-release. It can run/debug standalone files and if the dependencies are pre-built and properly included in the linter it can build/debug those as well.

For anything more serious we would have to properly

1. Integrate with CMake. Mostly done already on the CMake extension, we just need to fetch some info from the extension through their API
2. Integrate with FPM. I have drafted a quick proposal on how I think this should look like to get the ball rolling [FPM Integration with Visual Studio Code - Discussion · Discussion #768 · fortran-lang/fpm · GitHub](https://github.com/fortran-lang/fpm/discussions/768)
3. Integrate with Makefiles and Autoconf. The Makefile Tools extension is still a Preview so we would have to chat with Microsoft to see how we would do this and the direction where their extension is heading

Any of these is a fair bit of work so we might want to list them in next year’s GSoC projects or seek some funding to hire devs/do it ourselves.

---

<div class="post-metadata">

**Author:** ![band-a-prend](https://avatars.discourse-cdn.com/v4/letter/b/7c8e57/32.png) [@band-a-prend](https://fortran-lang.discourse.group/u/band-a-prend)\
**Post date:** [October 10, 2022, 12:07am UTC](https://fortran-lang.discourse.group/t/feature-request-run-fortran-code-with-one-click-in-vscode/3976/13 "2022-10-10T00:07:07Z")

</div>

> [@certik](#):
>
> we want to have a working debugger

I early used with C++ extension (`cpptools`) an offered solution from [vscode issue](https://github.com/Microsoft/vscode-cpptools/issues/75) to allow debug breakpoints “everywhere” :  
“File → Preferences → Settings → Debug” and set parameter “debug.allowBreakpointsEverywhere” value “true”. When I used it several years ago VSCode could only output on debugging into xterm and gnome-terminal in Linux.

---

<div class="post-metadata">

**Author:** ![gnikit](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gnikit/32/1213_2.png) [@gnikit](https://fortran-lang.discourse.group/u/gnikit)\
**Post date:** [October 10, 2022, 7:52am UTC](https://fortran-lang.discourse.group/t/feature-request-run-fortran-code-with-one-click-in-vscode/3976/14 "2022-10-10T07:52:40Z")

</div>

You don’t have to do that anymore. I think since late 2019 Modern Fortran for VS Code supports debugging natively. It still leverages the C/C++ extension’s debug adapters (that’s the program sitting between VS Code and the debugger).

The linked issue you provided, in my opinion, is the wrong way to go about this. You wouldn’t want the C/C++ extension being the one registering their debuggers for usage in other languages. The extension that should be doing that is the one providing language support for language X. There is nothing stopping current extension Devs from using cpptools as a dependency to their extension and leveraging their debug technology.

What we really want from Microsoft is for them to package their debug functionality in a module and provide a Typescript API instead of relying to an entire vscode extension. This would allow everyone else to very easily add debugging in their extensions. Although, I doubt that this will ever happen since a lot of Microsoft’s tech for this is proprietary and the current code is deeply embedded in the cppttools extension.

---

<div class="post-metadata">

**Author:** ![band-a-prend](https://avatars.discourse-cdn.com/v4/letter/b/7c8e57/32.png) [@band-a-prend](https://fortran-lang.discourse.group/u/band-a-prend)\
**Post date:** [October 10, 2022, 4:28pm UTC](https://fortran-lang.discourse.group/t/feature-request-run-fortran-code-with-one-click-in-vscode/3976/15 "2022-10-10T16:28:46Z")

</div>

Yes, it was required for me in 2017. I should recheck all with new versions of vscode and fortran extension after cleanup vscode user configuration files. The quick test shows that my old `task.json` that called `make` (release or debug build) doesn’t work anymore with new versions maybe because of changing it’s format too and problems with paths.

---

<div class="post-metadata">

**Author:** ![gnikit](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gnikit/32/1213_2.png) [@gnikit](https://fortran-lang.discourse.group/u/gnikit)\
**Post date:** [October 10, 2022, 4:35pm UTC](https://fortran-lang.discourse.group/t/feature-request-run-fortran-code-with-one-click-in-vscode/3976/16 "2022-10-10T16:35:23Z")

</div>

Yeah things have changed a fair bit since then. Give me a ping if you need any help with your setup.

---

<div class="post-metadata">

**Author:** ![band-a-prend](https://avatars.discourse-cdn.com/v4/letter/b/7c8e57/32.png) [@band-a-prend](https://fortran-lang.discourse.group/u/band-a-prend)\
**Post date:** [October 10, 2022, 8:55pm UTC](https://fortran-lang.discourse.group/t/feature-request-run-fortran-code-with-one-click-in-vscode/3976/17 "2022-10-10T20:55:14Z")

</div>

Well, the path problem (linux) on build was related due to I opened separate file instread of folder with project. Maybe it requires additional changes in my tasks.json with env working directory variables. But after I opened folder instead of separate files the build tasks works ok.

The second problem was due to my too simple Makefile doesn’t manage separate debug and release directories. It uses targets “switching”. But if source files is unchanged then switch to another build task doesn’t rebuild executable. I.e. if I initially built release and rebuit as debug without changes then executable file will be the same without debug info. But it’s my fault of using such easy Makefile, I just need to improve it.

So debugging with breakpoints works for me if executable contains debug symbols. All works! 🙂
