Error: Unrecognized option when trying to debug

I have custom setup for setting building application project using ARMCC toolchain. Additionally, I provide compiler, linker, and assembler flags that differ from the default GCC ones. After some extensive troubleshooting, described here, I want to debug my application. However there is an “Fatal error: C3900U: Unrecognized option ‘-2’.” error comming up each time.

Now, the problem is that when I got these “Unrecognized option …” errors before, usually first character after hyphen is removed from this logged error. Even though, I was trying to locate any in-line option that has character “2” inside, but I find none. So, my guess would be this error is a consequence of some other issue, however I can’t determine it.

This is only in case of trying to debug application code. If I build, everything is okay. Where should I be looking at then? What different inline options are usually used, when trying to debug vs. when trying to build project code?

This is the whole script I use to re-direct toolchain paths and filter specific GCC non-compatible flags and add new ARMCC-specific flags.

# Run "pio run -t envdump" to display whoe "env" object with all its contents
# to verify whether data written here is also written in there 

import os
from SCons.Script import Import

platform = env.PioPlatform()

# Filter out excluded build options from flag variable
def FilterFlags(flagVar, filteredOptions):
    filteredFlags = [flag for flag in flagVar if flag not in filteredOptions]
    return filteredFlags

# Define list of flag variables for option exclude

# Define default flags to exclude from all flag variables
filteredOptions = [
    # Add more flags to exclude as needed

# Modify flags globally (as if through "build_flags")
for e in [env, projenv, DefaultEnvironment()]:

    # ARMCC toolchain executables
        AR = "armar",
        AS = "armasm",
        CC = "armcc",
        CXX = "armcc",
        LD = "armlink",
        LINK= "armlink",
        GDB = "arm-none-eabi-gdb",  # to retain compatibility
        OBJCOPY = "fromelf --bin --output $TARGET $SOURCE && echo",
        # For displaying ROM/RAM usage

    # Preprocessor flags
    #e.Replace(CPPFLAGS = [])
    e.Append(CPPFLAGS = [
        # Add more flags to exclude as needed

    # Compiler flags
    #e.Replace(CCFLAGS = [])
    e.Append(CCFLAGS = [
        # Add more flags to exclude as needed

    # Assembler flags
    #e.Replace(ASFLAGS = [])
    e.Append(ASFLAGS = [
        # Add more flags to exclude as needed

    # Linker flags
    #e.Replace(LINKFLAGS = [])
    e.Append(LINKFLAGS = [
        #"--list=$BUILD_DIR/$"  # causes errors for some reason
        # Add more flags to exclude as needed

    # Replace "-T" with "--scatter" for linker (scatter) scirpt
    old_flags = e["LINKFLAGS"].copy()
        i = old_flags.index("-T")
        old_flags[i] = "--scatter"

    # Remove "-Wl,..." which are GCC specific
    e.Replace(_LIBFLAGS = [])

    # Prevent searching for default linker file
    e.Replace(LIBPATH = [])

    # Filter out excluded flags from all flag variables
    for flag in filteredBuildFlags:
        existingFlags = e.get(flag, [])
        filteredFlags = FilterFlags(existingFlags, filteredOptions)
        e.Replace(**{flag: filteredFlags})

    # Add new toolchain to path
    pkg = platform.get_package("toolchain-armcc")
        os.path.join(pkg.path, "bin")
        if os.path.isdir(os.path.join(pkg.path, "bin"))
        else pkg.path,