Improve the attempted dynamic environment-capture error message

This pull request fixes #7118.