Skip to content

Commit 05ae867

Browse files
Enable just build in any cmd or powershell simply (not necessary in Developer Command Prompt for VS 2022) (#41475)
This pull request refactors and modernizes the PowerToys build scripts to provide a more robust, platform-aware, and user-friendly local build experience. The changes introduce shared PowerShell helpers, add clear documentation, improve Visual Studio environment detection, and unify build logic across scripts. The new approach enables easier builds from any folder, better error reporting, and automatic platform detection for x64/arm64. **Build system modernization and shared helpers:** * Added new shared helper script `build-common.ps1` containing reusable functions for MSBuild invocation, Visual Studio environment initialization, project discovery, and platform auto-detection. This script is now dot-sourced by all build scripts for consistent behavior. * Refactored `build-essentials.ps1` and `build-installer.ps1` to use the shared helpers, enabling automatic Visual Studio dev environment setup and platform detection. Both scripts now work from any folder inside the repo and provide improved logging and error handling. [[1]](diffhunk://#diff-946ed85e16779fdbcfeb7de80f631eae2da0f7bd478e27e22621121b409dde88L1-R73) [[2]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L55-R77) **Improved build workflow and error reporting:** * All build scripts now write detailed logs (full, errors, warnings, binlog) next to the solution/project being built, with troubleshooting guidance documented in the new guidelines. [[1]](diffhunk://#diff-43764921d6c830dbb3a15fe875aebfbc46966ae5ff62f3179adb3ff046b47b9dR1-R284) [[2]](diffhunk://#diff-283bc775aac55085b6a0a47e40b3cf619fff47e20a2f5537fd6dd342d19d2afdR1-R48) * Command-line wrappers (`build.cmd`, `build-essentials.cmd`) added for easy invocation from `cmd.exe`, forwarding all arguments to the PowerShell scripts. [[1]](diffhunk://#diff-4bf353f2a88f1378983e4e2f3a5555e69b6a6ccfbe004001c1ebfe99ca57903dR1-R5) [[2]](diffhunk://#diff-48b3da077cd89d8ed6befe57a781bea813e6f9594bfcefbc320b20dea589c5abR1-R6) **Documentation and usage guidance:** * Introduced `BUILD-GUIDELINES.md` with clear instructions, usage examples, and troubleshooting tips for all build scripts, including platform overrides and log locations. **Installer pipeline improvements:** * The installer build pipeline (`build-installer.ps1`) now uses shared helpers for platform detection and Visual Studio initialization, and passes unified build arguments to all MSBuild invocations, improving consistency and maintainability. [[1]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L83-L126) [[2]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L137-R113) [[3]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L151-R128) [[4]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L162-R142) --- **References:** [[1]](diffhunk://#diff-43764921d6c830dbb3a15fe875aebfbc46966ae5ff62f3179adb3ff046b47b9dR1-R284) [[2]](diffhunk://#diff-946ed85e16779fdbcfeb7de80f631eae2da0f7bd478e27e22621121b409dde88L1-R73) [[3]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L55-R77) [[4]](diffhunk://#diff-283bc775aac55085b6a0a47e40b3cf619fff47e20a2f5537fd6dd342d19d2afdR1-R48) [[5]](diffhunk://#diff-4bf353f2a88f1378983e4e2f3a5555e69b6a6ccfbe004001c1ebfe99ca57903dR1-R5) [[6]](diffhunk://#diff-48b3da077cd89d8ed6befe57a781bea813e6f9594bfcefbc320b20dea589c5abR1-R6) [[7]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L83-L126) [[8]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L137-R113) [[9]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L151-R128) [[10]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L162-R142)
1 parent 377d134 commit 05ae867

File tree

5 files changed

+160
-17
lines changed

5 files changed

+160
-17
lines changed

tools/build/BUILD-GUIDELINES.md

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,48 @@
1-
# Build scripts - quick guideline
1+
# Build scripts quick guideline
22

3-
As the result of our recent changes, use the following guidance when working in the PowerToys repo:
3+
Use these scripts to build PowerToys locally. They auto-detect your platform (x64/arm64), initialize the Visual Studio developer environment, and write helpful logs on failure.
44

5-
1. Use `build-essentials.ps1` before any development in general
6-
- Purpose: restore NuGet packages for the full solution and build a small set of essential native projects (runner, settings). This is a fast way to ensure native artifacts required for local development are available.
5+
## Quick start (from cmd.exe)
6+
- Fast essentials (runner + settings) and NuGet restore first:
7+
- `tools\build\build-essentials.cmd`
8+
- Build projects in the current folder:
9+
- `tools\build\build.cmd`
710

8-
2. Use `build.ps1` from any folder
9-
- Purpose: lightweight local builder. It auto-discovers the target platform (x64/arm64/x86) and builds projects it finds in the current directory.
10-
- Notes: you can pass additional MSBuild arguments (e.g. `./tools/build/build.ps1 '/p:CIBuild=true'`) — the script will forward them to MSBuild.
11-
- Use `-RestoreOnly` to only restore packages for local projects.
11+
Tip: Add `D:\PowerToys\tools\build` to your PATH to use the wrappers anywhere.
1212

13-
3. Use `build-installer.ps1` to create a local installer (use with caution)
14-
- Purpose: runs the full pipeline that restores, builds the full solution, signs packages, and builds the installer (MSI/bootstrapper).
15-
- Caution: this script performs cleaning (git clean) and installer packaging steps that may remove untracked files under `installer/`.
13+
## When to use which
14+
1) `build-essentials.ps1`
15+
- Restores NuGet for `PowerToys.sln` and builds essentials (runner, settings).
16+
- Auto-detects Platform; initializes VS Dev environment automatically.
17+
- Example (PowerShell):
18+
- `./tools/build/build-essentials.ps1`
19+
- `./tools/build/build-essentials.ps1 -Platform arm64 -Configuration Release`
1620

17-
Additional notes
18-
- Shared helpers live in `build-common.ps1` and are used by the other scripts (`RunMSBuild`, `RestoreThenBuild`, `BuildProjectsInDirectory`, platform auto-detection).
19-
- If you want a different default platform selection, set the `-Platform` parameter explicitly when invoking the scripts.
21+
2) `build.ps1` (from any folder)
22+
- Builds any `.sln/.csproj/.vcxproj` in the current directory.
23+
- Auto-detects Platform; initializes VS Dev environment automatically.
24+
- Accepts extra MSBuild args (forwarded to msbuild):
25+
- `./tools/build/build.ps1 '/p:CIBuild=true' '/p:SomeProp=Value'`
26+
- Restore only:
27+
- `./tools/build/build.ps1 -RestoreOnly`
2028

21-
If you want, I can add this guidance to the repository README instead or add a short one-liner comment to the top of `build-common.ps1` so tools can discover it automatically.
29+
3) `build-installer.ps1` (use with caution)
30+
- Full local packaging pipeline (restore, build, sign MSIX, WiX v5 MSI/bootstrapper).
31+
- Auto-inits VS Dev environment. Cleans some output (keeps *.exe) under `installer/`.
32+
- Key options: `-PerUser true|false`, `-InstallerSuffix wix5|vnext`.
33+
- Example:
34+
- `./tools/build/build-installer.ps1 -Platform x64 -Configuration Release -PerUser true -InstallerSuffix wix5`
35+
36+
## Logs and troubleshooting
37+
- On failure, see logs next to the solution/project being built:
38+
- `build.<configuration>.<platform>.all.log` — full text log
39+
- `build.<configuration>.<platform>.errors.log` — errors only
40+
- `build.<configuration>.<platform>.warnings.log` — warnings only
41+
- `build.<configuration>.<platform>.trace.binlog` — open with MSBuild Structured Log Viewer
42+
- VS environment init:
43+
- Scripts try DevShell first (`Microsoft.VisualStudio.DevShell.dll` / `Enter-VsDevShell`), then fall back to `VsDevCmd.bat`.
44+
- If VS isn’t found, run from “Developer PowerShell for VS 2022”, or ensure `vswhere.exe` exists under `Program Files (x86)\Microsoft Visual Studio\Installer`.
45+
46+
## Notes
47+
- Override platform explicitly with `-Platform x64|arm64` if needed.
48+
- CMD wrappers: `build.cmd`, `build-essentials.cmd` forward all arguments to the PowerShell scripts.

tools/build/build-common.ps1

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,22 @@ This file provides reusable helper functions used by the build scripts:
88
- RunMSBuild: wrapper around msbuild.exe (accepts optional Platform/Configuration)
99
- RestoreThenBuild: performs restore and optionally builds the solution/project
1010
- BuildProjectsInDirectory: discovers and builds local .sln/.csproj/.vcxproj files
11+
- Ensure-VsDevEnvironment: initializes the Visual Studio developer environment when possible.
12+
It prefers the DevShell PowerShell module (Microsoft.VisualStudio.DevShell.dll / Enter-VsDevShell),
13+
falls back to running VsDevCmd.bat and importing its environment into the current PowerShell session,
14+
and restores the caller's working directory after initialization.
1115
1216
USAGE
1317
Dot-source this file from a script to load helpers:
1418
. "$PSScriptRoot\build-common.ps1"
1519
20+
ERROR DETAILS
21+
When a build fails, check the logs written next to the solution/project folder:
22+
- build.<configuration>.<platform>.all.log — full MSBuild text log
23+
- build.<configuration>.<platform>.errors.log — extracted errors only
24+
- build.<configuration>.<platform>.warnings.log — extracted warnings only
25+
- build.<configuration>.<platform>.trace.binlog — binary log (open with the MSBuild Structured Log Viewer)
26+
1627
.NOTES
1728
Do not execute this file directly; dot-source it from `build.ps1` or `build-installer.ps1` so helpers are available in your script scope.
1829
#>
@@ -59,7 +70,7 @@ function RunMSBuild {
5970
try {
6071
& msbuild.exe @cmd
6172
if ($LASTEXITCODE -ne 0) {
62-
Write-Error (("Build failed: {0} {1}" -f $Solution, $ExtraArgs))
73+
Write-Error (("Build failed: {0} {1}`nSee logs:`n All: {2}`n Errors: {3}`n Binlog: {4}" -f $Solution, $ExtraArgs, $allLog, $errorsLog, $binLog))
6374
exit $LASTEXITCODE
6475
}
6576
} finally {
@@ -164,3 +175,98 @@ function Get-DefaultPlatform {
164175

165176
return 'x64'
166177
}
178+
179+
function Ensure-VsDevEnvironment {
180+
$OriginalLocationForVsInit = Get-Location
181+
try {
182+
183+
if ($env:VSINSTALLDIR -or $env:VCINSTALLDIR -or $env:DevEnvDir -or $env:VCToolsInstallDir) {
184+
Write-Host "[VS] VS developer environment already present"
185+
return $true
186+
}
187+
188+
# Locate vswhere if available
189+
$vswhereCandidates = @(
190+
"$env:ProgramFiles (x86)\Microsoft Visual Studio\Installer\vswhere.exe",
191+
"$env:ProgramFiles\Microsoft Visual Studio\Installer\vswhere.exe"
192+
)
193+
$vswhere = $vswhereCandidates | Where-Object { Test-Path $_ } | Select-Object -First 1
194+
if ($vswhere) { Write-Host "[VS] vswhere found: $vswhere" } else { Write-Host "[VS] vswhere not found" }
195+
196+
$instPaths = @()
197+
if ($vswhere) {
198+
# First try with the VC tools requirement (preferred)
199+
try { $p = & $vswhere -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath 2>$null; if ($p) { $instPaths += $p } } catch {}
200+
# Fallback: try without -requires to find any VS installations
201+
if (-not $instPaths) {
202+
try { $p2 = & $vswhere -latest -products * -property installationPath 2>$null; if ($p2) { $instPaths += $p2 } } catch {}
203+
}
204+
}
205+
206+
# Add explicit common year-based candidates as a last resort
207+
if (-not $instPaths) {
208+
$explicit = @(
209+
"$env:ProgramFiles (x86)\Microsoft Visual Studio\2022\Community",
210+
"$env:ProgramFiles (x86)\Microsoft Visual Studio\2022\Professional",
211+
"$env:ProgramFiles (x86)\Microsoft Visual Studio\2022\Enterprise",
212+
"$env:ProgramFiles\Microsoft Visual Studio\2022\Community",
213+
"$env:ProgramFiles\Microsoft Visual Studio\2022\Professional",
214+
"$env:ProgramFiles\Microsoft Visual Studio\2022\Enterprise"
215+
)
216+
foreach ($c in $explicit) { if (Test-Path $c) { $instPaths += $c } }
217+
}
218+
219+
if (-not $instPaths -or $instPaths.Count -eq 0) {
220+
Write-Warning "[VS] Could not locate Visual Studio installation (no candidates found)"
221+
return $false
222+
}
223+
224+
# Try each candidate installation path until one works
225+
foreach ($inst in $instPaths) {
226+
if (-not $inst) { continue }
227+
Write-Host "[VS] Checking candidate: $inst"
228+
229+
$devDll = Join-Path $inst 'Common7\Tools\Microsoft.VisualStudio.DevShell.dll'
230+
if (Test-Path $devDll) {
231+
try {
232+
Import-Module $devDll -DisableNameChecking -ErrorAction Stop
233+
234+
# Call Enter-VsDevShell using only the install path to avoid parameter name differences
235+
try {
236+
Enter-VsDevShell -VsInstallPath $inst -ErrorAction Stop
237+
Write-Host "[VS] Entered Visual Studio DevShell at $inst"
238+
return $true
239+
} catch {
240+
Write-Warning ("[VS] DevShell import/Enter-VsDevShell failed: {0}" -f $_)
241+
}
242+
} catch {
243+
Write-Warning ("[VS] DevShell import failed: {0}" -f $_)
244+
}
245+
}
246+
247+
$vsDevCmd = Join-Path $inst 'Common7\Tools\VsDevCmd.bat'
248+
if (Test-Path $vsDevCmd) {
249+
Write-Host "[VS] Running VsDevCmd.bat and importing environment from $vsDevCmd"
250+
try {
251+
$cmdOut = cmd.exe /c "`"$vsDevCmd`" && set"
252+
foreach ($line in $cmdOut) {
253+
$parts = $line -split('=',2)
254+
if ($parts.Length -eq 2) {
255+
try { [Environment]::SetEnvironmentVariable($parts[0], $parts[1], 'Process') } catch {}
256+
}
257+
}
258+
Write-Host "[VS] Imported environment from VsDevCmd.bat at $inst"
259+
return $true
260+
} catch {
261+
Write-Warning ("[VS] Failed to run/import VsDevCmd.bat at {0}: {1}" -f $inst, $_)
262+
}
263+
}
264+
}
265+
266+
Write-Warning "[VS] Neither DevShell module nor VsDevCmd.bat found in any candidate paths"
267+
return $false
268+
269+
} finally {
270+
try { Set-Location $OriginalLocationForVsInit } catch {}
271+
}
272+
}

tools/build/build-essentials.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ Set-Variable -Name RepoRoot -Value $repoRoot -Scope Script -Force
4848
# Load shared helpers
4949
. "$PSScriptRoot\build-common.ps1"
5050

51+
# Initialize Visual Studio dev environment
52+
if (-not (Ensure-VsDevEnvironment)) { exit 1 }
53+
5154
# If platform not provided, auto-detect from host
5255
if (-not $Platform -or $Platform -eq '') {
5356
try {

tools/build/build-installer.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ param (
6161
# Ensure helpers are available
6262
. "$PSScriptRoot\build-common.ps1"
6363

64+
# Initialize Visual Studio dev environment
65+
if (-not (Ensure-VsDevEnvironment)) { exit 1 }
66+
6467
# Auto-detect platform when not provided
6568
if (-not $Platform -or $Platform -eq '') {
6669
try {

tools/build/build.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ param (
4848

4949
. "$PSScriptRoot\build-common.ps1"
5050

51+
# Initialize Visual Studio dev environment
52+
if (-not (Ensure-VsDevEnvironment)) { exit 1 }
53+
5154
# If user passed MSBuild-style args (e.g. './build.ps1 /p:CIBuild=true'),
5255
# those will bind to $Platform/$Configuration; detect those and move them to ExtraArgs.
5356
$positionalExtra = @()
@@ -79,7 +82,8 @@ $cwd = (Get-Location).ProviderPath
7982
$extraArgsString = $null
8083
if ($ExtraArgs -and $ExtraArgs.Count -gt 0) { $extraArgsString = ($ExtraArgs -join ' ') }
8184

82-
if (BuildProjectsInDirectory -DirectoryPath $cwd -ExtraArgs $extraArgsString -Platform $Platform -Configuration $Configuration -RestoreOnly:$RestoreOnly) {
85+
$built = BuildProjectsInDirectory -DirectoryPath $cwd -ExtraArgs $extraArgsString -Platform $Platform -Configuration $Configuration -RestoreOnly:$RestoreOnly
86+
if ($built) {
8387
Write-Host "[BUILD] Local projects built; exiting."
8488
exit 0
8589
} else {

0 commit comments

Comments
 (0)