PlatformIO IDE 'platformio init' runs build scripts

I noticed that whenever VSCode starts up or reloads it executes the build scripts for the default_env as part of running platformio -c vscode init -t vscode. This results in a long error message appearing in the Javascript console, because one of Marlin’s scripts validates the build against files in the project, returns a list of problems, and cancels the build.

So, is there any way to detect from within a pre: script that platformio init is being executed, as opposed to a build or upload? Or, is there any way to configure platformio.ini so that it doesn’t invoke a “build” during the init phase?

I’m pretty sure this is by design because extra scripts are able to modify the e.g. include path, and that has to be reflected in the project configuration files for VSCode (c_cpp_properties.json) that platformio -c vscode init -t vscode is supposed to generate correctly.

Maybe @ivankravets knows more about this.

1 Like

Just add to your extra Python scripts:

from SCons.Script import COMMAND_LINE_TARGETS

Import("env")

if set(["_idedata", "idedata"]) & set(COMMAND_LINE_TARGETS):
    env.Exit(0)

Edited: We also added this use case to our docs Redirecting...

1 Like

Thanks Ivan, that check works! It turns out that it shouldn’t use env.Exit or there’s still a message, but if the script just skips ‘preflight checks’ then no more messages come out in the console.

This breaks debugging inside VSCode for me. I’m using PIO 5.1.1.

And I don’t have the same issue as @thinkyhead .

Before the (corrected) patch, if MOTHERBOARD was not compatible with default_env, the preflight checks would fail and print an error message in the VSCode Developer Tools Javascript console. One would only see the error message if all of these conditions were true:

  • Marlin Firmware is open in the workspace.
  • The MOTHERBOARD setting is incompatible with default_env.
  • The VSCode Developer Tools panel is open.
  • The window is actively loading / reloading.

Could you provide more details?

Specifically, with the recommended prefix (including env.Exit(0)) in five of the Marlin pre: build scripts it prevented debugging in some way.

So the next question is, in what manner did that affect debugging? It has something to do with the “idedata” being present.

Could you run pio upgrade --dev. Does debugging work now?

It looks like this method (looking for “idedata” inside of COMMAND_LINE_TARGETS doesn’t work in the latest version of the IDE (PIO 5.2.1 / Home 3.3.4). Is there some better (i.e., formally supported) method now for checking whether platformio init is currently executing so that scripts can bail in that case?

Ah, I see that idedata is now called _idedata. So, I can just add code to look for that item in addition to the older one.

from SCons.Script import COMMAND_LINE_TARGETS
if "idedata" in COMMAND_LINE_TARGETS or "_idedata" in COMMAND_LINE_TARGETS:
	env.Exit(0)

Apparently the env.Exit(0) technique to break out of the script is incompatible with IntelliSense in some regard (according to https://github.com/MarlinFirmware/Marlin/pull/23058). So instead of using env.Exit(0) I’m just checking for VSCode initialization as above and then doing nothing in that case.

I don’t think IntelliSense cares about SCONS or PlatformIO, but just parses the source code in its own way, so this shouldn’t have any effect on how IntelliSense interprets things, regardless of whether additional build flags would be added by PlatformIO for the actual build.

I’ve just updated the original comment at PlatformIO IDE 'platformio init' runs build scripts - #3 by ivankravets

See code at

Sorry, could you explain problem in detail? Do you still have any issues?

The basic issue was… A user reported that env.Exit(0) was interfering with IntelliSense, but I don’t know exactly in what ways, as it seemed to behave well enough for me. But it cleared things up for the complainant once I removed env.Exit(0) and just let the scripts do nothing when vscode init is detected. In the VSCode Developer Tools Console the only clear effect I could see is that env.Exit(0) invokes an onexit handler and dumps a big yellow-box message into the VSCode Developer Tools console. Once I stopped calling env.Exit(0) of course that message no longer appears, as all the script code is simply bypassed.

Just use this code:

if set(["_idedata", "idedata"]) & set(COMMAND_LINE_TARGETS):
  env.Exit(0)

This code helps you to skip extra scripting when user manually calls pio run -t idedata or when VSCode or other our extensions regenerate C/C++ index via pio project init --vscode (in this case we call internally _idedata).