Skip to content

Commit d115cdc

Browse files
legendecaslukekarrys
authored andcommitted
feat: allow VCINSTALLDIR to specify a portable instance (#3036)
1 parent 5d7db74 commit d115cdc

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

lib/find-visualstudio.js

+45-1
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ class VisualStudioFinder {
5454
}
5555

5656
const checks = [
57+
() => this.findVisualStudio2019OrNewerFromSpecifiedLocation(),
5758
() => this.findVisualStudio2019OrNewerUsingSetupModule(),
5859
() => this.findVisualStudio2019OrNewer(),
60+
() => this.findVisualStudio2017FromSpecifiedLocation(),
5961
() => this.findVisualStudio2017UsingSetupModule(),
6062
() => this.findVisualStudio2017(),
6163
() => this.findVisualStudio2015(),
@@ -116,6 +118,48 @@ class VisualStudioFinder {
116118
throw new Error('Could not find any Visual Studio installation to use')
117119
}
118120

121+
async findVisualStudio2019OrNewerFromSpecifiedLocation () {
122+
return this.findVSFromSpecifiedLocation([2019, 2022])
123+
}
124+
125+
async findVisualStudio2017FromSpecifiedLocation () {
126+
if (this.nodeSemver.major >= 22) {
127+
this.addLog(
128+
'not looking for VS2017 as it is only supported up to Node.js 21')
129+
return null
130+
}
131+
return this.findVSFromSpecifiedLocation([2017])
132+
}
133+
134+
async findVSFromSpecifiedLocation (supportedYears) {
135+
if (!this.envVcInstallDir) {
136+
return null
137+
}
138+
const info = {
139+
path: path.resolve(this.envVcInstallDir),
140+
// Assume the version specified by the user is correct.
141+
// Since Visual Studio 2015, the Developer Command Prompt sets the
142+
// VSCMD_VER environment variable which contains the version information
143+
// for Visual Studio.
144+
// https://learn.microsoft.com/en-us/visualstudio/ide/reference/command-prompt-powershell?view=vs-2022
145+
version: process.env.VSCMD_VER,
146+
packages: [
147+
'Microsoft.VisualStudio.Component.VC.Tools.x86.x64',
148+
// Assume MSBuild exists. It will be checked in processing.
149+
'Microsoft.VisualStudio.VC.MSBuild.Base'
150+
]
151+
}
152+
153+
// Is there a better way to get SDK information?
154+
const envWindowsSDKVersion = process.env.WindowsSDKVersion
155+
const sdkVersionMatched = envWindowsSDKVersion?.match(/^(\d+)\.(\d+)\.(\d+)\..*/)
156+
if (sdkVersionMatched) {
157+
info.packages.push(`Microsoft.VisualStudio.Component.Windows10SDK.${sdkVersionMatched[3]}.Desktop`)
158+
}
159+
// pass for further processing
160+
return this.processData([info], supportedYears)
161+
}
162+
119163
async findVisualStudio2019OrNewerUsingSetupModule () {
120164
return this.findNewVSUsingSetupModule([2019, 2022])
121165
}
@@ -321,7 +365,7 @@ class VisualStudioFinder {
321365

322366
// Helper - process version information
323367
getVersionInfo (info) {
324-
const match = /^(\d+)\.(\d+)\..*/.exec(info.version)
368+
const match = /^(\d+)\.(\d+)(?:\..*)?/.exec(info.version)
325369
if (!match) {
326370
this.log.silly('- failed to parse version:', info.version)
327371
return {}

test/test-find-visualstudio.js

+25
Original file line numberDiff line numberDiff line change
@@ -782,4 +782,29 @@ describe('find-visualstudio', function () {
782782
assert.ok(/find .* Visual Studio/i.test(err), 'expect error')
783783
assert.ok(!info, 'no data')
784784
})
785+
786+
it('run on a portable VS Command Prompt with sufficient environs', async function () {
787+
process.env.VCINSTALLDIR = 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC'
788+
process.env.VSCMD_VER = '16.0'
789+
process.env.WindowsSDKVersion = '10.0.17763.0'
790+
791+
const finder = new TestVisualStudioFinder(semverV1, null)
792+
793+
allVsVersions(finder)
794+
const { err, info } = await finder.findVisualStudio()
795+
assert.strictEqual(err, null)
796+
assert.deepStrictEqual(info, {
797+
msBuild: 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\' +
798+
'Community\\MSBuild\\Current\\Bin\\MSBuild.exe',
799+
path:
800+
'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community',
801+
sdk: '10.0.17763.0',
802+
toolset: 'v142',
803+
// Assume version in the environ is correct.
804+
version: '16.0',
805+
versionMajor: 16,
806+
versionMinor: 0,
807+
versionYear: 2019
808+
})
809+
})
785810
})

0 commit comments

Comments
 (0)