Build.jl from command line vs. from Pkg.build()

In test/runtests.jl I have

using Test
using Logging
using MyPackage
#...

but in test/Project.toml, MyPackage is not a dependency. I understand that when I’m in the main project folder and Pkg.activate("."), then Pkg.test(), that MyPackage is available to test/runtests.jl without needing to list it in test/Project.toml, and so the tests proceed with no complaint about using MyPackage.

In deps/build.jl I have

using Pkg, PackageCompiler

pkg"activate .." #activate main project environment

# do stuff ...

# generate precompile statements from package tests
runcmd = `julia --banner=no --project=../test --startup-file=no --trace-compile=precompile_out.jl -e 'include("../test/runtests.jl")'`
run(runcmd)

# then make a custom sysimage ...

This works fine when using Pkg.build(). In fact I can comment out the line pkg"activate .." and it still works fine using Pkg.build().

However, running

julia --project=../test --startup-file=no -e 'include("../test/runtests.jl")'

from the shell prompt in the deps folder fails of course with a LoadError that MyPackage isn’t found on the current path, since it isn’t included in the test environment.

Running

julia build.jl

or

julia --project build.jl

from the shell prompt in the deps folder also gives the same error, despite the pkg"activate .." statement in build.jl.

So when running Pkg.build() there’s some extra magic going on to make the project’s main environment available in the build script, including to a julia process run via julia’s external command mechanism, that isn’t available when I run julia build.jl from the command line in the deps folder, despite activating the project’s main environment in build.jl.

Is this documented somewhere?

What’s the best way to run julia build.jl from a shell prompt so that I can generate the custom sysimage without needing to call Pkg.build()? Add MyPackage to test/Project.toml?

(I’m using Julia 1.4.2.)