Extra_scripts seems to be executed when I change GitHub branches in VSCode

In my platformio.ini I set up some environments, one common part and some release environments for different ESP32 boards.

Here’s the file:

; PlatformIO Project Configuration File
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

platform = espressif32
board = az-delivery-devkit-v4
framework = arduino
monitor_speed = 115200
monitor_port = COM7
;build_type = debug
;check_skip_packages = yes
build_flags = 
	;!powershell -NonInteractive -NoLogo -NoProfile -File .\GetVersion.ps1 -ProjectDirectory . -OutputFile .\include\Version.h

upload_port =
upload_protocol = espota
extra_scripts = my_extra_script.py
build_flags = 
; lib_deps = chuckbell/MySQL Connector Arduino@^1.2.0

upload_port =
upload_protocol = espota
build_flags = 

upload_port = COM7
upload_protocol = esptool
extra_scripts = my_extra_script.py
build_flags = 
; lib_deps = chuckbell/MySQL Connector Arduino@^1.2.0

In the my_extra_script.py a Version.h file will be generated and some data will be written into a MySQL database. This works. My problem is, that with every change of a branch or with the startup of VSCode the extra_scripts seems to be executed. But what I try to achieve is, that this script will be executed during the build process only. What I’m doing wrong ? Need help.
Thx in advance

Yes. Your extra_script could be adding compile / linker settings, so PlatformIO of course executes it when the project is loaded or the platformio.ini itself is changed.

You can determine whether you are in an active build or not as shown in e.g.

Thx for the advise. I’ll give it a try. First of all I have to understand what’s going on.

So, I tested different approaches. But with no success.
The extra_script file is as follows:


import os
import git
import time
import subprocess
import shutil
import json

    import mysql.connector
except ImportError:
    env.Execute("$PYTHONEXE -m pip install mysql-connector-python")
    import mysql.connector
    import git
except ImportError:
    env.Execute("$PYTHONEXE -m pip install GitPython")
    import git

BUILDNR = int(round(time.time()))
VERSION_FILE = "version.h"
CONFIG_FILE = "config.h"
BUILD_DIR = "build/"
BOARD = env['BOARD']
API_URL = ""
cwd = os.path.abspath(os.getcwd())
r = git.repo.Repo(cwd)
# GITVERSION = r.git.describe("--tag")
#if is_pio_build:
#    GITVERSION = os.system("powershell -NonInteractive -NoLogo -NoProfile -File .\GetVersion.ps1 -ProjectDirectory . -OutputFile .\include\Version.h")
def is_pio_build():
    from SCons.Script import DefaultEnvironment
    env = DefaultEnvironment()
    if "IsCleanTarget" in dir(env) and env.IsCleanTarget(): return False
    return not env.IsIntegrationDump()

#def prepareVersion(source, target, env):
if is_pio_build:
    os.system("powershell -NonInteractive -NoLogo -NoProfile -File .\GetVersion.ps1 -ProjectDirectory . -OutputFile .\include\Version.h")
    mydb = mysql.connector.connect(

    mycursor = mydb.cursor()

    sql = "INSERT INTO boards (IP_Address) VALUES (%s)"
    val = ("",)
    mycursor.execute(sql, val)


    print(mycursor.rowcount, "record inserted.")

#env.AddPreAction("$PROGPATH", before_build)
#env.AddPreAction("$BUILD_DIR/src/main.o", prepareVersion)
#env.AddPreAction("buildprog", before_build)

The behaviour is still the same. The script will be executed during build AND when I modify the platformio.ini. I couldn’t find anything about env.IsCleanTarget() and env.IsIntegrationDump(). Therefore the question for more help. Any further ideas ?

No. You’re not executing your function. You have to write if is_pio_build():

1 Like

You’re right. This was the mistake. That’s the difference between expert and Newbie. :grinning:
Thx a lot for your support.