Skip to content
79 changes: 48 additions & 31 deletions SCons/Tool/MSCommon/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,18 @@ def normalize_env(env, keys, force: bool=False):
if sys32_wbem_dir not in normenv['PATH']:
normenv['PATH'] = normenv['PATH'] + os.pathsep + sys32_wbem_dir

# ProgramFiles for PowerShell 7 Path and PSModulePath
progfiles_dir = os.environ.get("ProgramFiles")
if not progfiles_dir:
Comment thread
bdbaddog marked this conversation as resolved.
sysroot_drive, _ = os.path.splitdrive(sys32_dir)
sysroot_path = sysroot_drive + os.sep
progfiles_dir = os.path.join(sysroot_path, "Program Files")

# Powershell 7
progfiles_ps_dir = os.path.join(progfiles_dir, "PowerShell", "7")
if progfiles_ps_dir not in normenv["PATH"]:
normenv["PATH"] = normenv["PATH"] + os.pathsep + progfiles_ps_dir

# Without Powershell in PATH, an internal call to a telemetry
# function (starting with a VS2019 update) can fail
# Note can also set VSCMD_SKIP_SENDTELEMETRY to avoid this.
Expand All @@ -353,48 +365,53 @@ def normalize_env(env, keys, force: bool=False):
return normenv


# TODO: Hard-coded list of the variables that (may) need to be
# imported from os.environ[] for the chain of development batch
# files to execute correctly. One call to vcvars*.bat may
# end up running a dozen or more scripts, changes not only with
# each release but with what is installed at the time. We think
# in modern installations most are set along the way and don't
# need to be picked from the env, but include these for safety's sake.
# Any VSCMD variables definitely are picked from the env and
# control execution in interesting ways.
# Note these really should be unified - either controlled by vs.py,
# or synced with the the common_tools_var # settings in vs.py.
_VS_VC_VARS = [
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can remove the _ prefix here. There's no major reason to sugguest accessing this from other code should be avoided ?

Copy link
Copy Markdown
Contributor Author

@jcbrill jcbrill May 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is an indicator that it shouldn't necessarily be modified. I don't feel strongly either way.

We should probably add a debug logging statement for each of the keys retrieved from the list in normalize_env since the list can now be modified. The mscommon log file is the easiest way to determine if a user modified the list.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed change removing the _ prefix and added debug logging in the loop that evaluates the keys.

'COMSPEC', # path to "shell"
'OS', # name of OS family: Windows_NT or undefined (95/98/ME)
'VS170COMNTOOLS', # path to common tools for given version
'VS160COMNTOOLS',
'VS150COMNTOOLS',
'VS140COMNTOOLS',
'VS120COMNTOOLS',
'VS110COMNTOOLS',
'VS100COMNTOOLS',
'VS90COMNTOOLS',
'VS80COMNTOOLS',
'VS71COMNTOOLS',
'VSCOMNTOOLS',
'MSDevDir',
'VSCMD_DEBUG', # enable logging and other debug aids
'VSCMD_SKIP_SENDTELEMETRY',
'windir', # windows directory (SystemRoot not available in 95/98/ME)
'VCPKG_DISABLE_METRICS',
'VCPKG_ROOT',
]

def get_output(vcbat, args=None, env=None, skip_sendtelemetry=False):
"""Parse the output of given bat file, with given args."""

if env is None:
# Create a blank environment, for use in launching the tools
env = SCons.Environment.Environment(tools=[])

# TODO: Hard-coded list of the variables that (may) need to be
# imported from os.environ[] for the chain of development batch
# files to execute correctly. One call to vcvars*.bat may
# end up running a dozen or more scripts, changes not only with
# each release but with what is installed at the time. We think
# in modern installations most are set along the way and don't
# need to be picked from the env, but include these for safety's sake.
# Any VSCMD variables definitely are picked from the env and
# control execution in interesting ways.
# Note these really should be unified - either controlled by vs.py,
# or synced with the the common_tools_var # settings in vs.py.
vs_vc_vars = [
'COMSPEC', # path to "shell"
'OS', # name of OS family: Windows_NT or undefined (95/98/ME)
'VS170COMNTOOLS', # path to common tools for given version
'VS160COMNTOOLS',
'VS150COMNTOOLS',
'VS140COMNTOOLS',
'VS120COMNTOOLS',
'VS110COMNTOOLS',
'VS100COMNTOOLS',
'VS90COMNTOOLS',
'VS80COMNTOOLS',
'VS71COMNTOOLS',
'VSCOMNTOOLS',
'MSDevDir',
'VSCMD_DEBUG', # enable logging and other debug aids
'VSCMD_SKIP_SENDTELEMETRY',
'windir', # windows directory (SystemRoot not available in 95/98/ME)
]
env['ENV'] = normalize_env(env['ENV'], vs_vc_vars, force=False)
env['ENV'] = normalize_env(env['ENV'], _VS_VC_VARS, force=False)

if skip_sendtelemetry:
_force_vscmd_skip_sendtelemetry(env)

# debug("ENV=%r", env['ENV'])

if args:
debug("Calling '%s %s'", vcbat, args)
cmd_str = '"%s" %s & set' % (vcbat, args)
Expand Down
Loading