You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
msvc initialization can blow up if nonexistant or not installed TARGET_ARCH requested #4312
The scenario is: on a lightly provisioned (but working) Windows instance with VS2022, test/MSVC/TARGET_ARCH.py blew up scons with an IndexError indexing into an empty list. In fact, the third and fourth tests in that file both do this, the specific lines are these:
It takes a little work to generate a backtrace (I ended up pulling it into a standalone SConstruct and running outside the test framework), but the output including trace looks like this, trimmed down. I'll attach the full trace also, but I don't think the rest of it is interesting:
@jcbrill I can make the test pass by just wrapping the block of code in lines 191-203 in a if _Data.default_tools_re_list: but wanted to know if there was "more to it than this" in your opinion....
At first glance, I'm a little concerned about why it reports that no versions of the MSVC compiler were found:
scons: warning: unsupported host, target combination ('amd64', 'arm64') for MSVC version 11.0
File "C:\Users\mats\github\scons\TRY.THIS.4", line 1, in <module>
scons: warning: MSVC version '11.0' was not found.
No versions of the MSVC compiler were found.
Visual Studio C/C++ compilers may not be set correctly
@mwichmann If you have it running "stand-alone", is there any chance you could enable the MSCommon debugging (i.e., set SCONS_MSCOMMON_DEBUG=msvciniterr-debug.txt) and post the log?
changed the title [-]msvc initialization can blow up if nonexistent target requested[/-][+]msvc initialization can blow up if nonexistant or not installed TARGET_ARCH requested[/+]on Mar 2, 2023
Meanwhile, it's certainly finding a compiler - 14.3 (I can add the cache file too if useful)... there was no arm or arm64 support in 11.0, but I assume we're not smart enough to know that much?
Does the toolset 14.35.32215 folder exist and is it populated? C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\bin\Hostx64\arm64\cl.exe
No... in the directory a bit up, Hostx64 and Hostx86 only have x64 and x86 subdirectories, not arm or amd64. Nor does my fully built-up system, although it does have arm64 dirs in the 2019 install...
@mwichmann The root problem is with the construction of the installed VCS cache list (i.e., __INSTALLED_VCS_RUN) in vc.py.
The installed VCS list is cached (i.e., computed once). However, there are indirect dependencies on values from the environment. Most notably, the TARGET_ARCH.
In some of the tests, when the installed VCS cache list is constructed, the TARGET_ARCH (e.g., arm and arm64) does not exist in VS2022 and therefore there are no detected VCS.
The quick fix below uses an undefined TARGET_ARCH during the initial construction and then restores the original value prior to exit. This should be enough to accurately detect all installations with at least one usable HOST/TARGET combination.
I'm not convinced that a user specified VSWHERE via the environment is guaranteed to be used in light of the installed VCS cache. The VSWHERE specification might only work during the initial construction. I could be really wrong though.
When testing on my development box which happens to have MSVC 11.0 Express installed, the arm batch file appears to succeed.
However, the debug log for the batch file invocation indicates usage errors but no exception is raised because cl.exe is found on the path. I believe it to be the correct cl.exe as well.
Unfortunately, when reviewing the relevant code, there is a bug involving the host platform and likely just express versions due to an error of omission of an additional check for existence. I need to test that an additional check does not break anything that is currently working as expected.
The implementation now passes the msvc/msvs tests (79 tests, 5 skipped) on my development box and in a VMWare virtual machine with a minimal VS2022 (x86 and amd64 for latest 14.3 toolset) as described above (79 tests, 15 skipped).
Changes:
The issue with the installed VCS cache list has been resolved and tweaked from the code listing above to handle the test suite when env=None is passed.
A test for the existence of cl.exe for the host/target combination was added to the batch file existence check for VS6 to VS2015 in find_batch_file. There may be issues with VS2017+. I know there is a looming problem with VS2022 on ARM64 when trying to configure the ARM64 native batch files.
The sdk_batch_file query was moved into its own function and the sdk script invocation(s) are no longer interleaved with the vc script invocations. The sdk script invocations are only evaluated if all vc script host/target combinations have been exhausted without finding a valid vc script. The existence of cl.exe and the batch file combined is a "stronger" guarantee than the batch file existence alone.
I don't know if it's relevant to the issue under investigation here (and sorry haven't caught up with the above few comments), but came across these two errors in a pylint report:
SCons/Tool/MSCommon/MSVC/SetupEnvDefault.py:87:19: E1135: Value '_Data.msvc_tools' doesn't support membership test (unsupported-membership-test)
SCons/Tool/MSCommon/MSVC/SetupEnvDefault.py:100:58: E1135: Value '_Data.msvc_tools' doesn't support membership test (unsupported-membership-test)
Not sure quite what the question is... there are a few tools that directly use both WhereIs functions, which does indeed seem odd, and then, as you note, there's find_program_path. Someone thought it was a good idea to fall back to looking in os.environ's path, and it's been that way for a long time, though that seems kind of pointless. @bdbaddog ?
Why does it matter if cl.exe was found on the system path when it is missing from the constructed environment path?
My guess is that should a user have a cl.exe on the system path and there is a problem with the constructed environment, the build could fail without the warning which in that case seems to be useful/important diagnostic information.
Why does it matter if cl.exe was found on the system path when it is missing from the constructed environment path?
My guess is that should a user have a cl.exe on the system path and there is a problem with the constructed environment, the build could fail without the warning which in that case seems to be useful/important diagnostic information.
assuming someone actually does something with that information - which I think, in general, we don't. find_program_path doesn't give us any way to say "hey, I found your tool, but not in a place that's going to be useful".
assuming someone actually does something with that information - which I think, in general, we don't. find_program_path doesn't give us any way to say "hey, I found your tool, but not in a place that's going to be useful".
There is no guarantee that the tool found in the system environment is the same tool that was requested in scons.
The environment may be configured for one version of msvc (either via command-line batch file or permanently registered environment variables) while the scons msvc version request is different.
There is no guarantee that the tool found in the system environment is the same tool that was requested in scons.
The environment may be configured for one version of msvc (either via command-line batch file or permanently registered environment variables) while the scons msvc version request is different.
Indeed, that's a good point. So to me it seems like the behavior of find_program_path is not what we really want.
Not sure quite what the question is... there are a few tools that directly use both WhereIs functions, which does indeed seem odd, and then, as you note, there's find_program_path. Someone thought it was a good idea to fall back to looking in os.environ's path, and it's been that way for a long time, though that seems kind of pointless. @bdbaddog ?
See #4312 (comment) above for original question and code fragments.
The final check to issue a warning if the compiler is not present in msvc_setup_env uses the function find_program_path to search for cl.exe. Function find_program_path will check if the program is found in the scons environment path, and if not found, whether the program is found on the os.environ path.
In this context, env.WhereIs seems more appropriate than find_program_path.
Why should the warning message should be suppressed if a cl.exe is not in the constructed environment but happens to found on the system path?
The cl.exe on the system path may have no relation to the msvc version requested.
See #4312 (comment) above for original question and code fragments.
The final check to issue a warning if the compiler is not present in msvc_setup_env uses the function find_program_path to search for cl.exe. Function find_program_path will check if the program is found in the scons environment path, and if not found, whether the program is found on the os.environ path.
In this context, env.WhereIs seems more appropriate than find_program_path.
Why should the warning message should be suppressed if a cl.exe is not in the constructed environment but happens to found on the system path?
The cl.exe on the system path may have no relation to the msvc version requested.
Am I missing something?
Gotcha.
It seems like (minimally) the WhereIs() call should not be done by default.. or at least a way to disable.
Also the error message doesn't let the user know where it did actually find cl.exe..
I assume there was a reason why this system evolved the way it did, but I don't know it, as a "newcomer". Even env.WhereIs calls SCons.Util.WhereIs evenutally, which has a go at os.environ['PATH'].
Even env.WhereIs calls SCons.Util.WhereIs evenutally, which has a go at os.environ['PATH']
SCons.Util.WhereIs will only use os.environ['PATH'] if the path argument is None.
env.WhereIs will pass path = self['ENV']['PATH'] to SCons.Util.WhereIs as the path argument as long as ['ENV']['PATH'] does not result in a KeyError. This path will suppress the use of os.environ['PATH'] in SCons.Util.WhereIs.
It seems like (minimally) the WhereIs() call should not be done by default.. or at least a way to disable.
I don't understand.
The scons environment path should be checked for cl.exe. The system path (os.environ) should not be checked for cl.exe.
Three code paths lead to the check: MSVC_USE_SCRIPT (user-defined script) , MSVC_USE_SETTINGS (user-defined dictionary), and MSVC_USE_SCRIPT (MSVC_VERSION request). The only case that does an early exit is when MSVC_USE_SCRIPT is set to False to bypass detection.
All three cases require a cl.exe instance on the environment path or a downstream compile/link command likely will likely fail. In particular, MSVC_USE_SETTINGS doesn't go through any other of the detection code so the check is relatively important.
Also the error message doesn't let the user know where it did actually find cl.exe..
In this context, scons did not find the tool requested.
I'm not sure finding a cl.exe on the system path is useful information as the check is only performed for the three cases listed above and not when a user is actually bypassing detection via MSVC_USE_SCRIPT.
For example, the system environment could be configured for the 2008 x86 cl.exe. The scons build request could be for 2022 arm64. I'm not sure reporting tool found on the system path is necessarily reliable and/or useful.
Even env.WhereIs calls SCons.Util.WhereIs evenutally, which has a go at os.environ['PATH']
SCons.Util.WhereIs will only use os.environ['PATH'] if the path argument is None.
env.WhereIs will pass path = self['ENV']['PATH'] to SCons.Util.WhereIs as the path argument as long as ['ENV']['PATH'] does not result in a KeyError. This path will suppress the use of os.environ['PATH'] in SCons.Util.WhereIs.
It seems like (minimally) the WhereIs() call should not be done by default.. or at least a way to disable.
I don't understand.
The scons environment path should be checked for cl.exe. The system path (os.environ) should not be checked for cl.exe.
Three code paths lead to the check: MSVC_USE_SCRIPT (user-defined script) , MSVC_USE_SETTINGS (user-defined dictionary), and MSVC_USE_SCRIPT (MSVC_VERSION request). The only case that does an early exit is when MSVC_USE_SCRIPT is set to False to bypass detection.
All three cases require a cl.exe instance on the environment path or a downstream compile/link command likely will likely fail. In particular, MSVC_USE_SETTINGS doesn't go through any other of the detection code so the check is relatively important.
Also the error message doesn't let the user know where it did actually find cl.exe..
In this context, scons did not find the tool requested.
I'm not sure finding a cl.exe on the system path is useful information as the check is only performed for the three cases listed above and not when a user is actually bypassing detection via MSVC_USE_SCRIPT.
For example, the system environment could be configured for the 2008 x86 cl.exe. The scons build request could be for 2022 arm64. I'm not sure reporting tool found on the system path is necessarily reliable and/or useful.
Other tools use these functions, and for those, configuration is pretty much as simple as finding the tool.
I'm sure that's what lead to much of that logic.
Well, one could argue either way: that a message noting that a desired executable was found, but only in a path that won't be used, has some benefit - or that it's completely useless. What's presumably not useful is to proceed as if all were well if it is found, but not in the execution environment, because then it won't work.
To wade even deeper into the weeds, there is yet another possible state to consider: the requested tool was not found but there is a tool already in the users env['ENV']['PATH'].
IMHO, there needs to be a warning if the requested tool was not found regardless of whether a tool is found in the env['ENV']['PATH'] (or os.environ['PATH']).
An additional warning should probably be issued if any other tool is found in the environment as there is no easy way to check if the found tool is "the same" as the requested tool.
Bug-riddled pseudo-code to illustrate the possible states:
# BEGIN NEW
def _find_cl_dict(d):
rval = None
path = d.get('PATH')
if path:
# note: no substitution performed on path
rval = SCons.Util.WhereIs('cl', path=path, pathext='.exe')
return rval
def _find_cl_env(env):
rval = None
path = env['ENV'].get('PATH')
if path:
# note: substitution performed on path
rval = env.WhereIs('cl', path=path, pathext='.exe')
return rval
def _cl_warning(d, env):
cl_request = _find_cl_dict(d)
if cl_request:
debug("%s found in d['PATH']", _CL_EXE_NAME)
return
# TODO: cache propose needs to be restored
debug("%s not found in d['PATH']", _CL_EXE_NAME)
warn_msg = "Could not find requested MSVC compiler 'cl'."
SCons.Warnings.warn(SCons.Warnings.VisualCMissingWarning, warn_msg)
cl_env = _find_cl_env(env)
if cl_env:
debug("%s found in env['ENV']['PATH']", _CL_EXE_NAME)
# cl.exe not found in d['PATH']
# cl.exe found in env['ENV']['PATH']
warn_msg = "MSVC compiler 'cl' found may not be the same as the requested 'cl'."
SCons.Warnings.warn(SCons.Warnings.VisualCMissingWarning, warn_msg)
return
debug("%s not found in env['ENV']['PATH']", _CL_EXE_NAME)
cl_environ = _find_cl_dict(os.environ)
if cl_environ:
debug("%s found in os.environ['PATH']", _CL_EXE_NAME)
# cl.exe not found in d['PATH']
# cl.exe not found in env['ENV']['PATH']
# cl.exe found in os.environ['PATH']
warn_msg = "MSVC compiler 'cl' found on the system path but will not be used."
SCons.Warnings.warn(SCons.Warnings.VisualCMissingWarning, warn_msg)
return
debug("%s not found in os.environ['PATH']", _CL_EXE_NAME)
# END NEW
def msvc_setup_env(env):
debug('called')
version = get_default_version(env)
if version is None:
if not msvc_setup_env_user(env):
MSVC.SetupEnvDefault.set_nodefault()
return None
# XXX: we set-up both MSVS version for backward
# compatibility with the msvs tool
env['MSVC_VERSION'] = version
env['MSVS_VERSION'] = version
env['MSVS'] = {}
use_script, use_settings = get_use_script_use_settings(env)
if SCons.Util.is_String(use_script):
use_script = use_script.strip()
if not os.path.exists(use_script):
raise MSVCScriptNotFound('Script specified by MSVC_USE_SCRIPT not found: "{}"'.format(use_script))
args = env.subst('$MSVC_USE_SCRIPT_ARGS')
debug('use_script 1 %s %s', repr(use_script), repr(args))
d = script_env(env, use_script, args)
elif use_script:
d = msvc_find_valid_batch_script(env,version)
debug('use_script 2 %s', d)
if not d:
return d
elif use_settings is not None:
if not SCons.Util.is_Dict(use_settings):
error_msg = 'MSVC_USE_SETTINGS type error: expected a dictionary, found {}'.format(type(use_settings).__name__)
raise MSVCUseSettingsError(error_msg)
d = use_settings
debug('use_settings %s', d)
else:
debug('MSVC_USE_SCRIPT set to False')
warn_msg = "MSVC_USE_SCRIPT set to False, assuming environment " \
"set correctly."
SCons.Warnings.warn(SCons.Warnings.VisualCMissingWarning, warn_msg)
return None
# !!! ISSUE WARNING HERE BEFORE PREPENDING !!!
# inspect d['PATH'], env['ENV']['PATH'], and os.environ['PATH'] if necessary
_cl_warning(d, env)
for k, v in d.items():
env.PrependENVPath(k, v, delete_existing=True)
debug("env['ENV']['%s'] = %s", k, env['ENV'][k])
# !!! EXISTING WARNING REMOVED FROM HERE !!!
Activity
mwichmann commentedon Mar 2, 2023
@jcbrill I can make the test pass by just wrapping the block of code in lines 191-203 in a
if _Data.default_tools_re_list:but wanted to know if there was "more to it than this" in your opinion....jcbrill commentedon Mar 2, 2023
@mwichmann I'll look into it.
At first glance, I'm a little concerned about why it reports that no versions of the MSVC compiler were found:
jcbrill commentedon Mar 2, 2023
@mwichmann If you have it running "stand-alone", is there any chance you could enable the MSCommon debugging (i.e.,
set SCONS_MSCOMMON_DEBUG=msvciniterr-debug.txt) and post the log?mwichmann commentedon Mar 2, 2023
Was just typing that I hadn't captured the debug.... will do that
[-]msvc initialization can blow up if nonexistent target requested[/-][+]msvc initialization can blow up if nonexistant or not installed TARGET_ARCH requested[/+]mwichmann commentedon Mar 2, 2023
debug log added
debug.txt
mwichmann commentedon Mar 2, 2023
Meanwhile, it's certainly finding a compiler - 14.3 (I can add the cache file too if useful)... there was no arm or arm64 support in 11.0, but I assume we're not smart enough to know that much?
jcbrill commentedon Mar 2, 2023
Does the toolset
14.35.32215folder exist and is it populated?C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\bin\Hostx64\arm64\cl.exemwichmann commentedon Mar 2, 2023
No... in the directory a bit up,
Hostx64andHostx86only havex64andx86subdirectories, notarmoramd64. Nor does my fully built-up system, although it does have arm64 dirs in the 2019 install...jcbrill commentedon Mar 2, 2023
Still not sure what is happening. The log for 14.3 reports:
It appears like it is only checking 14.3 with a given
TARGET_ARCH='arm64'.I'm a little rusty evidently...
EDIT: changed 14.2 to 14.3.
jcbrill commentedon Mar 3, 2023
@mwichmann The root problem is with the construction of the installed VCS cache list (i.e.,
__INSTALLED_VCS_RUN) in vc.py.The installed VCS list is cached (i.e., computed once). However, there are indirect dependencies on values from the environment. Most notably, the TARGET_ARCH.
In some of the tests, when the installed VCS cache list is constructed, the TARGET_ARCH (e.g.,
armandarm64) does not exist in VS2022 and therefore there are no detected VCS.The quick fix below uses an undefined TARGET_ARCH during the initial construction and then restores the original value prior to exit. This should be enough to accurately detect all installations with at least one usable HOST/TARGET combination.
I'm not convinced that a user specified VSWHERE via the environment is guaranteed to be used in light of the installed VCS cache. The VSWHERE specification might only work during the initial construction. I could be really wrong though.This is a quick fix for the immediate problem:
https://github.com/jcbrill/scons/tree/jbrill-msvc-vcscache
The
_UNDEFINEDobject was moved to the top of the file as it was now being used in code "above" the original declaration.Still not exactly sure how/why the original symptom is manifested yet. It seems to have disappeared with the quick fix.
jcbrill commentedon Mar 3, 2023
When testing on my development box which happens to have MSVC 11.0 Express installed, the arm batch file appears to succeed.
However, the debug log for the batch file invocation indicates usage errors but no exception is raised because cl.exe is found on the path. I believe it to be the correct cl.exe as well.
Unfortunately, when reviewing the relevant code, there is a bug involving the host platform and likely just express versions due to an error of omission of an additional check for existence. I need to test that an additional check does not break anything that is currently working as expected.
jcbrill commentedon Mar 4, 2023
@mwichmann updated branch available at:
https://github.com/jcbrill/scons/tree/jbrill-msvc-vcscache
The implementation now passes the msvc/msvs tests (79 tests, 5 skipped) on my development box and in a VMWare virtual machine with a minimal VS2022 (x86 and amd64 for latest 14.3 toolset) as described above (79 tests, 15 skipped).
Changes:
mwichmann commentedon Mar 4, 2023
I don't know if it's relevant to the issue under investigation here (and sorry haven't caught up with the above few comments), but came across these two errors in a pylint report:
9 remaining items
mwichmann commentedon Jun 24, 2023
Not sure quite what the question is... there are a few tools that directly use both
WhereIsfunctions, which does indeed seem odd, and then, as you note, there'sfind_program_path. Someone thought it was a good idea to fall back to looking inos.environ's path, and it's been that way for a long time, though that seems kind of pointless. @bdbaddog ?jcbrill commentedon Jun 24, 2023
Why does it matter if cl.exe was found on the system path when it is missing from the constructed environment path?
My guess is that should a user have a cl.exe on the system path and there is a problem with the constructed environment, the build could fail without the warning which in that case seems to be useful/important diagnostic information.
mwichmann commentedon Jun 24, 2023
assuming someone actually does something with that information - which I think, in general, we don't.
find_program_pathdoesn't give us any way to say "hey, I found your tool, but not in a place that's going to be useful".jcbrill commentedon Jun 24, 2023
There is no guarantee that the tool found in the system environment is the same tool that was requested in scons.
The environment may be configured for one version of msvc (either via command-line batch file or permanently registered environment variables) while the scons msvc version request is different.
mwichmann commentedon Jun 24, 2023
Indeed, that's a good point. So to me it seems like the behavior of
find_program_pathis not what we really want.bdbaddog commentedon Jun 25, 2023
Do you mean in find_program_path()?
jcbrill commentedon Jun 25, 2023
See #4312 (comment) above for original question and code fragments.
The final check to issue a warning if the compiler is not present in
msvc_setup_envuses the functionfind_program_pathto search for cl.exe. Functionfind_program_pathwill check if the program is found in the scons environment path, and if not found, whether the program is found on theos.environpath.In this context,
env.WhereIsseems more appropriate thanfind_program_path.Why should the warning message should be suppressed if a cl.exe is not in the constructed environment but happens to found on the system path?
The cl.exe on the system path may have no relation to the msvc version requested.
Am I missing something?
bdbaddog commentedon Jun 25, 2023
Gotcha.
It seems like (minimally) the
WhereIs()call should not be done by default.. or at least a way to disable.Also the error message doesn't let the user know where it did actually find
cl.exe..mwichmann commentedon Jun 25, 2023
I assume there was a reason why this system evolved the way it did, but I don't know it, as a "newcomer". Even
env.WhereIscallsSCons.Util.WhereIsevenutally, which has a go atos.environ['PATH'].jcbrill commentedon Jun 25, 2023
SCons.Util.WhereIs will only use os.environ['PATH'] if the path argument is None.
env.WhereIs will pass
path = self['ENV']['PATH']to SCons.Util.WhereIs as the path argument as long as ['ENV']['PATH'] does not result in a KeyError. This path will suppress the use of os.environ['PATH'] in SCons.Util.WhereIs.I don't understand.
The scons environment path should be checked for cl.exe. The system path (os.environ) should not be checked for cl.exe.
Three code paths lead to the check: MSVC_USE_SCRIPT (user-defined script) , MSVC_USE_SETTINGS (user-defined dictionary), and MSVC_USE_SCRIPT (MSVC_VERSION request). The only case that does an early exit is when MSVC_USE_SCRIPT is set to False to bypass detection.
All three cases require a cl.exe instance on the environment path or a downstream compile/link command likely will likely fail. In particular, MSVC_USE_SETTINGS doesn't go through any other of the detection code so the check is relatively important.
In this context, scons did not find the tool requested.
I'm not sure finding a cl.exe on the system path is useful information as the check is only performed for the three cases listed above and not when a user is actually bypassing detection via MSVC_USE_SCRIPT.
For example, the system environment could be configured for the 2008 x86 cl.exe. The scons build request could be for 2022 arm64. I'm not sure reporting tool found on the system path is necessarily reliable and/or useful.
bdbaddog commentedon Jun 26, 2023
Other tools use these functions, and for those, configuration is pretty much as simple as finding the tool.
I'm sure that's what lead to much of that logic.
mwichmann commentedon Jun 26, 2023
Well, one could argue either way: that a message noting that a desired executable was found, but only in a path that won't be used, has some benefit - or that it's completely useless. What's presumably not useful is to proceed as if all were well if it is found, but not in the execution environment, because then it won't work.
jcbrill commentedon Jun 26, 2023
+1
jcbrill commentedon Jun 26, 2023
Sigh. Apologies for hijacking.
To wade even deeper into the weeds, there is yet another possible state to consider: the requested tool was not found but there is a tool already in the users
env['ENV']['PATH'].IMHO, there needs to be a warning if the requested tool was not found regardless of whether a tool is found in the
env['ENV']['PATH'](oros.environ['PATH']).An additional warning should probably be issued if any other tool is found in the environment as there is no easy way to check if the found tool is "the same" as the requested tool.
Bug-riddled pseudo-code to illustrate the possible states: