BUG?: BASH_ENV as a target-specific variable is inherited by the target's prerequisites even if marked "private"

"Cook, Malcolm" <[email protected]> Mon, 4 Dec 2023 16:50:57 +0000
Newsgroups gmane.comp.gnu.make.general
Message-ID <DS7PR20MB467117D5B31F3CA7A5B6D19DBE86A@DS7PR20MB4671.namprd20.prod.outlook.com>
By my reading of the documentation, when SHELL is /bin/bash, defining a make target having BASH_ENV exported as a target-specific variable should cause that target's recipe to be executed in an shell where the file BASH_ENV points has been sourced.

And indeed this works in my hands.

However, by my sights, marking the target-specific exported BASH_ENV to be private  should cause it to not be inherited by any prerequisites, and thus recipes of any prerequisites should NOT source this file.

Running the following t.mk demonstrates that this is not the case.  

File: t.mk
--------------------
.SILENT:
.DEFAULT_GOAL:=test
.ONESHELL:
SHELL := /bin/bash

.PHONY test:
test:
	@echo Running $@ of $(MAKEFILE_LIST) in a clean environment with $$($(MAKE) --version | head -n 1)
	env -i $(MAKE) -f $(MAKEFILE_LIST) -B myTarget

.PHONY: myTarget
myTarget: private export BASH_ENV:=desiredEnvironment.sh
# I would expect that using the "private modifier" here would causes
# BASH_ENV to "not be inherited by prerequisites".
myTarget: itsPrerequisite desiredEnvironment.sh
	echo Running recipe for: $@
	echo BASH_ENV=$${BASH_ENV}
	echo myVariable=$${myVariable}

.PHONY: itsPrerequisite
itsPrerequisite: 
	echo Running recipe for: $@
	echo BASH_ENV=$${BASH_ENV}
	echo myVariable=$${myVariable}

desiredEnvironment.sh:
	echo Running recipe for: $@
	printf 'echo sourcing $@\n' > $@
--------------------

My expectation is that running it should have myTarget run in a shell with the desiredEnvironment.sh having been sourced, but itsPrerequiste would run in a shell without such. 

However, the following transcript demonstrates the surprising (to me) fact that the shell in which itsPrerequisite is running does in fact see, and source, BASH_ENV.

make -f t.mk
Running test of t.mk in a clean environment with GNU Make 4.4
sourcing desiredEnvironment.sh
Running recipe for: itsPrerequisite
BASH_ENV=desiredEnvironment.sh
myVariable=myValue
sourcing desiredEnvironment.sh
Running recipe for: desiredEnvironment.sh
sourcing desiredEnvironment.sh
Running recipe for: myTarget
BASH_ENV=desiredEnvironment.sh
myVariable=myValue

Thanks for any insight as to whether this is a bug in the Make, its documentation, or my understanding of the same.

~ Malcolm Cook

PS: discussion on this issue began here: https://stackoverflow.com/a/64991443/415228