Skip to content

Commit 6f1f4f8

Browse files
committed
Fix vswhere for Build Tools
1 parent 74a5e71 commit 6f1f4f8

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

build.py

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,39 @@ def Exit( self ):
114114
'or use your system Clangd. '
115115
'See the YCM docs for details on how to use a custom Clangd.' )
116116

117+
ACCEPTABLE_MSVC_VERSIONS = [ 17, 16, 15 ]
117118

118-
def FindLatestMSVC( quiet, preview=False ):
119-
ACCEPTABLE_VERSIONS = [ 17, 16, 15 ]
120119

120+
def UseVsWhere( quiet, vswhere_args ):
121+
if not quiet:
122+
print( "Calling", *vswhere_args )
123+
latest_full_v = subprocess.check_output( vswhere_args ).strip().decode()
124+
if '.' in latest_full_v:
125+
try:
126+
latest_v = int( latest_full_v.split( '.' )[ 0 ] )
127+
except ValueError:
128+
raise ValueError( f"{ latest_full_v } is not a version number." )
129+
130+
if not quiet:
131+
print( f'vswhere -latest returned version { latest_full_v }' )
132+
133+
if latest_v not in ACCEPTABLE_MSVC_VERSIONS:
134+
if latest_v > 17:
135+
if not quiet:
136+
print( f'MSVC Version { latest_full_v } is newer than expected.' )
137+
else:
138+
raise ValueError(
139+
f'vswhere returned { latest_full_v } which is unexpected.'
140+
'Pass --msvc <version> argument.' )
141+
return latest_v
142+
else:
143+
if not quiet:
144+
print( f'vswhere returned nothing usable: "{ latest_full_v }"' )
145+
146+
return None
147+
148+
149+
def FindLatestMSVC( quiet, preview=False ):
121150
VSWHERE_EXE = os.path.join( os.environ[ 'ProgramFiles(x86)' ],
122151
'Microsoft Visual Studio',
123152
'Installer', 'vswhere.exe' )
@@ -127,30 +156,16 @@ def FindLatestMSVC( quiet, preview=False ):
127156
'-latest', '-property', 'installationVersion' ]
128157
if preview:
129158
vswhere_args.append( '-prerelease' )
159+
160+
if msvc := UseVsWhere( quiet, vswhere_args ):
161+
return msvc
162+
130163
if not quiet:
131-
print( "Calling", *vswhere_args )
132-
latest_full_v = subprocess.check_output( vswhere_args ).strip().decode()
133-
if '.' in latest_full_v:
134-
try:
135-
latest_v = int( latest_full_v.split( '.' )[ 0 ] )
136-
except ValueError:
137-
raise ValueError( f"{ latest_full_v } is not a version number." )
164+
print( 'Retrying vswhere for Build Tools' )
138165

139-
if not quiet:
140-
print( f'vswhere -latest returned version { latest_full_v }' )
141-
142-
if latest_v not in ACCEPTABLE_VERSIONS:
143-
if latest_v > 17:
144-
if not quiet:
145-
print( f'MSVC Version { latest_full_v } is newer than expected.' )
146-
else:
147-
raise ValueError(
148-
f'vswhere returned { latest_full_v } which is unexpected.'
149-
'Pass --msvc <version> argument.' )
150-
return latest_v
151-
else:
152-
if not quiet:
153-
print( f'vswhere returned nothing usable, { latest_full_v }' )
166+
vswhere_args += [ '-products', 'Microsoft.VisualStudio.Product.BuildTools' ]
167+
if msvc := UseVsWhere( quiet, vswhere_args ):
168+
return msvc
154169

155170
# Fall back to registry parsing, which works at least until MSVC 2019 (16)
156171
# but is likely failing on MSVC 2022 (17)
@@ -160,7 +175,7 @@ def FindLatestMSVC( quiet, preview=False ):
160175
import winreg
161176
handle = winreg.ConnectRegistry( None, winreg.HKEY_LOCAL_MACHINE )
162177
msvc = None
163-
for i in ACCEPTABLE_VERSIONS:
178+
for i in ACCEPTABLE_MSVC_VERSIONS:
164179
if not quiet:
165180
print( 'Trying to find '
166181
rf'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\{ i }.0' )

0 commit comments

Comments
 (0)