Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: findVisualStudio with pwsh constrained lang. mode #2449

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions lib/find-visualstudio.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,40 @@ VisualStudioFinder.prototype = {
// Invoke the PowerShell script to get information about Visual Studio 2017
// or newer installations
findVisualStudio2017OrNewer: function findVisualStudio2017OrNewer (cb) {
var ps = path.join(process.env.SystemRoot, 'System32',
'WindowsPowerShell', 'v1.0', 'powershell.exe')
var csFile = path.join(__dirname, 'Find-VisualStudio.cs')
var psArgs = [
'-ExecutionPolicy',
'Unrestricted',
'-NoProfile',
'-Command',
'&{Add-Type -Path \'' + csFile + '\';' + '[VisualStudioConfiguration.Main]::PrintJson()}'
]
const ps = path.join(process.env.SystemRoot, 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe')
const csFile = path.join(__dirname, 'Find-VisualStudio.cs')
const basePsArgs = ['-ExecutionPolicy', 'Unrestricted', '-NoProfile', '-Command']
const findLanguageModePsCmd =
'&{ try { $ExecutionContext.SessionState.LanguageMode } catch { try { (Get-PSSessionConfiguration -Name Test).LanguageMode } catch { \'error\' } } }'

const psArgs = [...basePsArgs, findLanguageModePsCmd]
this.log.silly('Running', ps, psArgs)
var child = execFile(ps, psArgs, { encoding: 'utf8' },
const childFindPsLanguageMode = execFile(
ps,
psArgs,
{ encoding: 'utf8' },
(err, stdout, stderr) => {
this.parseData(err, stdout, stderr, cb)
this.log.silly('PS stderr = %j', stderr)
if (err) {
this.log.silly('PS err = %j', err && (err.stack || err))
this.addLog(
'could not determine PowerShell LanguageMode, try re-running with \'--loglevel silly\' for more details')
return cb(null)
}
const findVscDataPsCmd = `&{Add-Type -Path '${csFile}';[VisualStudioConfiguration.Main]::PrintJson()}`
const psArgs = [...basePsArgs, findVscDataPsCmd]
// the Add-Type command requires powershell to run with languageMode == FullLanguage
// Because languageMode feature is not introduced yet in powershell v2, we can try to use it.
// If powershell v2 is not installed (a problem only if languageMode is not FullLanguage), then it is not
// possible to use feature Add-Type.
if (stdout.toString('utf8').trim().toLowerCase() !== 'fulllanguage') {
psArgs.unshift('-Version', '2')
}
this.log.silly('Running', ps, psArgs)
const child = execFile(ps, psArgs, { encoding: 'utf8' }, (err, stdout, stderr) => this.parseData(err, stdout, stderr, cb))
child.stdin.end()
})
child.stdin.end()
childFindPsLanguageMode.stdin.end()
},

// Parse the output of the PowerShell script and look for an installation
Expand Down