Using SCons Command and Builder from platformio extra_script?

I’m trying to use SCons Command from within an extra_script.

However it appears to have no effort whatsoever on the build process - the build_func is never executed, and the files are not in the tree (as seen by setting SCONSFLAGS=--tree=status).

I also tried changing projenv to env, and switch form post to pre setting to no effect.

Any ideas what am I doing wrong and how to utilize those “advanced” SCons features from extra_scripts?

extra_script.py:

def build_func(target, source, env):
    print(f">>>> build_func T:{target} S:{source}")

print(">Running extra_script.py")
Import("projenv")
c = projenv.Command('msgs.c', 'msgs.txt', action=build_func)
print(f">Command={c}")

To answer my own question, the problem was that the file node created was not set as a dependency of the main target.

Here’s a working example:
extra_script.py:

Import("env")
c = env.Command('msgs.c', 'msgs.txt', action="cp $SOURCE $TARGET")
env.Depends("$BUILD_DIR/$PROGNAME$PROGSUFFIX", c)