Files
PowerToys/tools/build/build-common.ps1

273 lines
10 KiB
PowerShell
Raw Normal View History

Improve how to build the project in PowerToys easier (#41459) This pull request refactors and standardizes the PowerToys build scripts to improve maintainability, reusability, and platform detection. The main changes are the introduction of a shared helper script (`build-common.ps1`), migration of build logic in individual scripts to use these helpers, and enhanced platform auto-detection. This makes the build pipeline more robust and easier to use from any directory within the repository. As the result of our recent changes, use the following guidance when working in the PowerToys repo: 1. Use `build-essentials.ps1` before any development in general - 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. 2. Use `build.ps1` from any folder - Purpose: lightweight local builder. It auto-discovers the target platform (x64/arm64/x86) and builds projects it finds in the current directory. - Notes: you can pass additional MSBuild arguments positionally (e.g. `./tools/build/build.ps1 '/p:CIBuild=true'`) — the script will forward them to MSBuild. - Use `-RestoreOnly` to only restore packages for local projects. 3. Use `build-installer.ps1` to create a local installer (use with caution) - Purpose: runs the full pipeline that restores, builds the full solution, signs packages, and builds the installer (MSI/bootstrapper). - Caution: this script performs cleaning (git clean) and installer packaging steps that may remove untracked files under `installer/`. Additional notes - Shared helpers live in `build-common.ps1` and are used by the other scripts (`RunMSBuild`, `RestoreThenBuild`, `BuildProjectsInDirectory`, platform auto-detection). **Shared build logic and helper functions:** * Added new `tools/build/build-common.ps1` file containing reusable PowerShell functions for MSBuild invocation, solution/project restore and build, platform detection, and project discovery. All build scripts now dot-source this file for shared functionality. **Refactoring of build scripts to use shared helpers:** * Updated `tools/build/build-essentials.ps1` to use `build-common.ps1` helpers, including auto-detection of repository root and platform, and simplified project build logic. * Created new `tools/build/build.ps1` for quick local builds, using shared helpers and supporting extra MSBuild arguments and platform auto-detection. * Refactored `tools/build/build-installer.ps1` to remove duplicate build logic, use shared helpers, and support platform auto-detection and argument forwarding. [[1]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L55-R74) [[2]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L83-L126) **Improved platform detection and argument handling:** * All scripts now auto-detect the target platform if not specified, using the new `Get-DefaultPlatform` helper. This supports x64, arm64, and x86 hosts. [[1]](diffhunk://#diff-43764921d6c830dbb3a15fe875aebfbc46966ae5ff62f3179adb3ff046b47b9dR1-R166) [[2]](diffhunk://#diff-946ed85e16779fdbcfeb7de80f631eae2da0f7bd478e27e22621121b409dde88L1-R70) [[3]](diffhunk://#diff-7a444242b2a6d9c642341bd2ef45f51ba5698ad7827e5136e85eb483863967a7R1-R88) [[4]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L55-R74) **Consistent MSBuild invocation and logging:** * MSBuild calls now consistently use shared helpers, centralized logging, and support passing extra arguments such as `/p:CIBuild=true` and custom solution/project paths. [[1]](diffhunk://#diff-43764921d6c830dbb3a15fe875aebfbc46966ae5ff62f3179adb3ff046b47b9dR1-R166) [[2]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L137-R110) [[3]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L151-R125) [[4]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L162-R139) **Project and solution build improvements:** * Build scripts now discover and build projects in preferred order (.sln, .csproj, .vcxproj), and support restoring packages only if requested. [[1]](diffhunk://#diff-43764921d6c830dbb3a15fe875aebfbc46966ae5ff62f3179adb3ff046b47b9dR1-R166) [[2]](diffhunk://#diff-7a444242b2a6d9c642341bd2ef45f51ba5698ad7827e5136e85eb483863967a7R1-R88) Let me know if you need a walkthrough of the new helper functions or how to use the updated build scripts!
2025-08-29 01:23:52 -07:00
<#
.SYNOPSIS
Shared build helper functions for PowerToys build scripts.
.DESCRIPTION
This file provides reusable helper functions used by the build scripts:
- Get-BuildPaths: returns ScriptDir, OriginalCwd, RepoRoot (repo root detection)
- RunMSBuild: wrapper around msbuild.exe (accepts optional Platform/Configuration)
- RestoreThenBuild: performs restore and optionally builds the solution/project
- BuildProjectsInDirectory: discovers and builds local .sln/.csproj/.vcxproj 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)
2025-08-31 19:23:27 -07:00
- Ensure-VsDevEnvironment: initializes the Visual Studio developer environment when possible.
It prefers the DevShell PowerShell module (Microsoft.VisualStudio.DevShell.dll / Enter-VsDevShell),
falls back to running VsDevCmd.bat and importing its environment into the current PowerShell session,
and restores the caller's working directory after initialization.
Improve how to build the project in PowerToys easier (#41459) This pull request refactors and standardizes the PowerToys build scripts to improve maintainability, reusability, and platform detection. The main changes are the introduction of a shared helper script (`build-common.ps1`), migration of build logic in individual scripts to use these helpers, and enhanced platform auto-detection. This makes the build pipeline more robust and easier to use from any directory within the repository. As the result of our recent changes, use the following guidance when working in the PowerToys repo: 1. Use `build-essentials.ps1` before any development in general - 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. 2. Use `build.ps1` from any folder - Purpose: lightweight local builder. It auto-discovers the target platform (x64/arm64/x86) and builds projects it finds in the current directory. - Notes: you can pass additional MSBuild arguments positionally (e.g. `./tools/build/build.ps1 '/p:CIBuild=true'`) — the script will forward them to MSBuild. - Use `-RestoreOnly` to only restore packages for local projects. 3. Use `build-installer.ps1` to create a local installer (use with caution) - Purpose: runs the full pipeline that restores, builds the full solution, signs packages, and builds the installer (MSI/bootstrapper). - Caution: this script performs cleaning (git clean) and installer packaging steps that may remove untracked files under `installer/`. Additional notes - Shared helpers live in `build-common.ps1` and are used by the other scripts (`RunMSBuild`, `RestoreThenBuild`, `BuildProjectsInDirectory`, platform auto-detection). **Shared build logic and helper functions:** * Added new `tools/build/build-common.ps1` file containing reusable PowerShell functions for MSBuild invocation, solution/project restore and build, platform detection, and project discovery. All build scripts now dot-source this file for shared functionality. **Refactoring of build scripts to use shared helpers:** * Updated `tools/build/build-essentials.ps1` to use `build-common.ps1` helpers, including auto-detection of repository root and platform, and simplified project build logic. * Created new `tools/build/build.ps1` for quick local builds, using shared helpers and supporting extra MSBuild arguments and platform auto-detection. * Refactored `tools/build/build-installer.ps1` to remove duplicate build logic, use shared helpers, and support platform auto-detection and argument forwarding. [[1]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L55-R74) [[2]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L83-L126) **Improved platform detection and argument handling:** * All scripts now auto-detect the target platform if not specified, using the new `Get-DefaultPlatform` helper. This supports x64, arm64, and x86 hosts. [[1]](diffhunk://#diff-43764921d6c830dbb3a15fe875aebfbc46966ae5ff62f3179adb3ff046b47b9dR1-R166) [[2]](diffhunk://#diff-946ed85e16779fdbcfeb7de80f631eae2da0f7bd478e27e22621121b409dde88L1-R70) [[3]](diffhunk://#diff-7a444242b2a6d9c642341bd2ef45f51ba5698ad7827e5136e85eb483863967a7R1-R88) [[4]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L55-R74) **Consistent MSBuild invocation and logging:** * MSBuild calls now consistently use shared helpers, centralized logging, and support passing extra arguments such as `/p:CIBuild=true` and custom solution/project paths. [[1]](diffhunk://#diff-43764921d6c830dbb3a15fe875aebfbc46966ae5ff62f3179adb3ff046b47b9dR1-R166) [[2]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L137-R110) [[3]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L151-R125) [[4]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L162-R139) **Project and solution build improvements:** * Build scripts now discover and build projects in preferred order (.sln, .csproj, .vcxproj), and support restoring packages only if requested. [[1]](diffhunk://#diff-43764921d6c830dbb3a15fe875aebfbc46966ae5ff62f3179adb3ff046b47b9dR1-R166) [[2]](diffhunk://#diff-7a444242b2a6d9c642341bd2ef45f51ba5698ad7827e5136e85eb483863967a7R1-R88) Let me know if you need a walkthrough of the new helper functions or how to use the updated build scripts!
2025-08-29 01:23:52 -07:00
USAGE
Dot-source this file from a script to load helpers:
. "$PSScriptRoot\build-common.ps1"
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)
2025-08-31 19:23:27 -07:00
ERROR DETAILS
When a build fails, check the logs written next to the solution/project folder:
- build.<configuration>.<platform>.all.log full MSBuild text log
- build.<configuration>.<platform>.errors.log extracted errors only
- build.<configuration>.<platform>.warnings.log extracted warnings only
- build.<configuration>.<platform>.trace.binlog binary log (open with the MSBuild Structured Log Viewer)
Improve how to build the project in PowerToys easier (#41459) This pull request refactors and standardizes the PowerToys build scripts to improve maintainability, reusability, and platform detection. The main changes are the introduction of a shared helper script (`build-common.ps1`), migration of build logic in individual scripts to use these helpers, and enhanced platform auto-detection. This makes the build pipeline more robust and easier to use from any directory within the repository. As the result of our recent changes, use the following guidance when working in the PowerToys repo: 1. Use `build-essentials.ps1` before any development in general - 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. 2. Use `build.ps1` from any folder - Purpose: lightweight local builder. It auto-discovers the target platform (x64/arm64/x86) and builds projects it finds in the current directory. - Notes: you can pass additional MSBuild arguments positionally (e.g. `./tools/build/build.ps1 '/p:CIBuild=true'`) — the script will forward them to MSBuild. - Use `-RestoreOnly` to only restore packages for local projects. 3. Use `build-installer.ps1` to create a local installer (use with caution) - Purpose: runs the full pipeline that restores, builds the full solution, signs packages, and builds the installer (MSI/bootstrapper). - Caution: this script performs cleaning (git clean) and installer packaging steps that may remove untracked files under `installer/`. Additional notes - Shared helpers live in `build-common.ps1` and are used by the other scripts (`RunMSBuild`, `RestoreThenBuild`, `BuildProjectsInDirectory`, platform auto-detection). **Shared build logic and helper functions:** * Added new `tools/build/build-common.ps1` file containing reusable PowerShell functions for MSBuild invocation, solution/project restore and build, platform detection, and project discovery. All build scripts now dot-source this file for shared functionality. **Refactoring of build scripts to use shared helpers:** * Updated `tools/build/build-essentials.ps1` to use `build-common.ps1` helpers, including auto-detection of repository root and platform, and simplified project build logic. * Created new `tools/build/build.ps1` for quick local builds, using shared helpers and supporting extra MSBuild arguments and platform auto-detection. * Refactored `tools/build/build-installer.ps1` to remove duplicate build logic, use shared helpers, and support platform auto-detection and argument forwarding. [[1]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L55-R74) [[2]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L83-L126) **Improved platform detection and argument handling:** * All scripts now auto-detect the target platform if not specified, using the new `Get-DefaultPlatform` helper. This supports x64, arm64, and x86 hosts. [[1]](diffhunk://#diff-43764921d6c830dbb3a15fe875aebfbc46966ae5ff62f3179adb3ff046b47b9dR1-R166) [[2]](diffhunk://#diff-946ed85e16779fdbcfeb7de80f631eae2da0f7bd478e27e22621121b409dde88L1-R70) [[3]](diffhunk://#diff-7a444242b2a6d9c642341bd2ef45f51ba5698ad7827e5136e85eb483863967a7R1-R88) [[4]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L55-R74) **Consistent MSBuild invocation and logging:** * MSBuild calls now consistently use shared helpers, centralized logging, and support passing extra arguments such as `/p:CIBuild=true` and custom solution/project paths. [[1]](diffhunk://#diff-43764921d6c830dbb3a15fe875aebfbc46966ae5ff62f3179adb3ff046b47b9dR1-R166) [[2]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L137-R110) [[3]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L151-R125) [[4]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L162-R139) **Project and solution build improvements:** * Build scripts now discover and build projects in preferred order (.sln, .csproj, .vcxproj), and support restoring packages only if requested. [[1]](diffhunk://#diff-43764921d6c830dbb3a15fe875aebfbc46966ae5ff62f3179adb3ff046b47b9dR1-R166) [[2]](diffhunk://#diff-7a444242b2a6d9c642341bd2ef45f51ba5698ad7827e5136e85eb483863967a7R1-R88) Let me know if you need a walkthrough of the new helper functions or how to use the updated build scripts!
2025-08-29 01:23:52 -07:00
.NOTES
Do not execute this file directly; dot-source it from `build.ps1` or `build-installer.ps1` so helpers are available in your script scope.
#>
function RunMSBuild {
param (
[string]$Solution,
[string]$ExtraArgs,
[string]$Platform,
[string]$Configuration
)
# Prefer the solution's folder for logs; fall back to current directory
$logRoot = Split-Path -Path $Solution
if (-not $logRoot) { $logRoot = '.' }
$cfg = $null
if ($Configuration) { $cfg = $Configuration.ToLower() } else { $cfg = 'unknown' }
$plat = $null
if ($Platform) { $plat = $Platform.ToLower() } else { $plat = 'unknown' }
$allLog = Join-Path $logRoot ("build.{0}.{1}.all.log" -f $cfg, $plat)
$warningLog = Join-Path $logRoot ("build.{0}.{1}.warnings.log" -f $cfg, $plat)
$errorsLog = Join-Path $logRoot ("build.{0}.{1}.errors.log" -f $cfg, $plat)
$binLog = Join-Path $logRoot ("build.{0}.{1}.trace.binlog" -f $cfg, $plat)
$base = @(
$Solution
"/p:Platform=$Platform"
"/p:Configuration=$Configuration"
"/verbosity:normal"
'/clp:Summary;PerformanceSummary;ErrorsOnly;WarningsOnly'
"/fileLoggerParameters:LogFile=$allLog;Verbosity=detailed"
"/fileLoggerParameters1:LogFile=$warningLog;WarningsOnly"
"/fileLoggerParameters2:LogFile=$errorsLog;ErrorsOnly"
"/bl:$binLog"
'/nologo'
)
$cmd = $base + ($ExtraArgs -split ' ')
Write-Host (("[MSBUILD] {0}" -f ($cmd -join ' ')))
Push-Location $script:RepoRoot
try {
& msbuild.exe @cmd
if ($LASTEXITCODE -ne 0) {
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)
2025-08-31 19:23:27 -07:00
Write-Error (("Build failed: {0} {1}`nSee logs:`n All: {2}`n Errors: {3}`n Binlog: {4}" -f $Solution, $ExtraArgs, $allLog, $errorsLog, $binLog))
Improve how to build the project in PowerToys easier (#41459) This pull request refactors and standardizes the PowerToys build scripts to improve maintainability, reusability, and platform detection. The main changes are the introduction of a shared helper script (`build-common.ps1`), migration of build logic in individual scripts to use these helpers, and enhanced platform auto-detection. This makes the build pipeline more robust and easier to use from any directory within the repository. As the result of our recent changes, use the following guidance when working in the PowerToys repo: 1. Use `build-essentials.ps1` before any development in general - 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. 2. Use `build.ps1` from any folder - Purpose: lightweight local builder. It auto-discovers the target platform (x64/arm64/x86) and builds projects it finds in the current directory. - Notes: you can pass additional MSBuild arguments positionally (e.g. `./tools/build/build.ps1 '/p:CIBuild=true'`) — the script will forward them to MSBuild. - Use `-RestoreOnly` to only restore packages for local projects. 3. Use `build-installer.ps1` to create a local installer (use with caution) - Purpose: runs the full pipeline that restores, builds the full solution, signs packages, and builds the installer (MSI/bootstrapper). - Caution: this script performs cleaning (git clean) and installer packaging steps that may remove untracked files under `installer/`. Additional notes - Shared helpers live in `build-common.ps1` and are used by the other scripts (`RunMSBuild`, `RestoreThenBuild`, `BuildProjectsInDirectory`, platform auto-detection). **Shared build logic and helper functions:** * Added new `tools/build/build-common.ps1` file containing reusable PowerShell functions for MSBuild invocation, solution/project restore and build, platform detection, and project discovery. All build scripts now dot-source this file for shared functionality. **Refactoring of build scripts to use shared helpers:** * Updated `tools/build/build-essentials.ps1` to use `build-common.ps1` helpers, including auto-detection of repository root and platform, and simplified project build logic. * Created new `tools/build/build.ps1` for quick local builds, using shared helpers and supporting extra MSBuild arguments and platform auto-detection. * Refactored `tools/build/build-installer.ps1` to remove duplicate build logic, use shared helpers, and support platform auto-detection and argument forwarding. [[1]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L55-R74) [[2]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L83-L126) **Improved platform detection and argument handling:** * All scripts now auto-detect the target platform if not specified, using the new `Get-DefaultPlatform` helper. This supports x64, arm64, and x86 hosts. [[1]](diffhunk://#diff-43764921d6c830dbb3a15fe875aebfbc46966ae5ff62f3179adb3ff046b47b9dR1-R166) [[2]](diffhunk://#diff-946ed85e16779fdbcfeb7de80f631eae2da0f7bd478e27e22621121b409dde88L1-R70) [[3]](diffhunk://#diff-7a444242b2a6d9c642341bd2ef45f51ba5698ad7827e5136e85eb483863967a7R1-R88) [[4]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L55-R74) **Consistent MSBuild invocation and logging:** * MSBuild calls now consistently use shared helpers, centralized logging, and support passing extra arguments such as `/p:CIBuild=true` and custom solution/project paths. [[1]](diffhunk://#diff-43764921d6c830dbb3a15fe875aebfbc46966ae5ff62f3179adb3ff046b47b9dR1-R166) [[2]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L137-R110) [[3]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L151-R125) [[4]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L162-R139) **Project and solution build improvements:** * Build scripts now discover and build projects in preferred order (.sln, .csproj, .vcxproj), and support restoring packages only if requested. [[1]](diffhunk://#diff-43764921d6c830dbb3a15fe875aebfbc46966ae5ff62f3179adb3ff046b47b9dR1-R166) [[2]](diffhunk://#diff-7a444242b2a6d9c642341bd2ef45f51ba5698ad7827e5136e85eb483863967a7R1-R88) Let me know if you need a walkthrough of the new helper functions or how to use the updated build scripts!
2025-08-29 01:23:52 -07:00
exit $LASTEXITCODE
}
} finally {
Pop-Location
}
}
function RestoreThenBuild {
param (
[string]$Solution,
[string]$ExtraArgs,
[string]$Platform,
[string]$Configuration,
[bool]$RestoreOnly=$false
)
$restoreArgs = '/t:restore /p:RestorePackagesConfig=true'
if ($ExtraArgs) { $restoreArgs = "$restoreArgs $ExtraArgs" }
RunMSBuild $Solution $restoreArgs $Platform $Configuration
if (-not $RestoreOnly) {
$buildArgs = '/m'
if ($ExtraArgs) { $buildArgs = "$buildArgs $ExtraArgs" }
RunMSBuild $Solution $buildArgs $Platform $Configuration
}
}
function BuildProjectsInDirectory {
param(
[string]$DirectoryPath,
[string]$ExtraArgs,
[string]$Platform,
[string]$Configuration,
[switch]$RestoreOnly
)
if (-not (Test-Path $DirectoryPath)) {
return $false
}
$files = @()
try {
$files = Get-ChildItem -Path (Join-Path $DirectoryPath '*') -Include *.sln,*.slnx,*.csproj,*.vcxproj -File -ErrorAction SilentlyContinue
Improve how to build the project in PowerToys easier (#41459) This pull request refactors and standardizes the PowerToys build scripts to improve maintainability, reusability, and platform detection. The main changes are the introduction of a shared helper script (`build-common.ps1`), migration of build logic in individual scripts to use these helpers, and enhanced platform auto-detection. This makes the build pipeline more robust and easier to use from any directory within the repository. As the result of our recent changes, use the following guidance when working in the PowerToys repo: 1. Use `build-essentials.ps1` before any development in general - 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. 2. Use `build.ps1` from any folder - Purpose: lightweight local builder. It auto-discovers the target platform (x64/arm64/x86) and builds projects it finds in the current directory. - Notes: you can pass additional MSBuild arguments positionally (e.g. `./tools/build/build.ps1 '/p:CIBuild=true'`) — the script will forward them to MSBuild. - Use `-RestoreOnly` to only restore packages for local projects. 3. Use `build-installer.ps1` to create a local installer (use with caution) - Purpose: runs the full pipeline that restores, builds the full solution, signs packages, and builds the installer (MSI/bootstrapper). - Caution: this script performs cleaning (git clean) and installer packaging steps that may remove untracked files under `installer/`. Additional notes - Shared helpers live in `build-common.ps1` and are used by the other scripts (`RunMSBuild`, `RestoreThenBuild`, `BuildProjectsInDirectory`, platform auto-detection). **Shared build logic and helper functions:** * Added new `tools/build/build-common.ps1` file containing reusable PowerShell functions for MSBuild invocation, solution/project restore and build, platform detection, and project discovery. All build scripts now dot-source this file for shared functionality. **Refactoring of build scripts to use shared helpers:** * Updated `tools/build/build-essentials.ps1` to use `build-common.ps1` helpers, including auto-detection of repository root and platform, and simplified project build logic. * Created new `tools/build/build.ps1` for quick local builds, using shared helpers and supporting extra MSBuild arguments and platform auto-detection. * Refactored `tools/build/build-installer.ps1` to remove duplicate build logic, use shared helpers, and support platform auto-detection and argument forwarding. [[1]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L55-R74) [[2]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L83-L126) **Improved platform detection and argument handling:** * All scripts now auto-detect the target platform if not specified, using the new `Get-DefaultPlatform` helper. This supports x64, arm64, and x86 hosts. [[1]](diffhunk://#diff-43764921d6c830dbb3a15fe875aebfbc46966ae5ff62f3179adb3ff046b47b9dR1-R166) [[2]](diffhunk://#diff-946ed85e16779fdbcfeb7de80f631eae2da0f7bd478e27e22621121b409dde88L1-R70) [[3]](diffhunk://#diff-7a444242b2a6d9c642341bd2ef45f51ba5698ad7827e5136e85eb483863967a7R1-R88) [[4]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L55-R74) **Consistent MSBuild invocation and logging:** * MSBuild calls now consistently use shared helpers, centralized logging, and support passing extra arguments such as `/p:CIBuild=true` and custom solution/project paths. [[1]](diffhunk://#diff-43764921d6c830dbb3a15fe875aebfbc46966ae5ff62f3179adb3ff046b47b9dR1-R166) [[2]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L137-R110) [[3]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L151-R125) [[4]](diffhunk://#diff-21888769485d767c43c0895fe315e6f6d7384da62f60ef917d8a61a610da10b9L162-R139) **Project and solution build improvements:** * Build scripts now discover and build projects in preferred order (.sln, .csproj, .vcxproj), and support restoring packages only if requested. [[1]](diffhunk://#diff-43764921d6c830dbb3a15fe875aebfbc46966ae5ff62f3179adb3ff046b47b9dR1-R166) [[2]](diffhunk://#diff-7a444242b2a6d9c642341bd2ef45f51ba5698ad7827e5136e85eb483863967a7R1-R88) Let me know if you need a walkthrough of the new helper functions or how to use the updated build scripts!
2025-08-29 01:23:52 -07:00
} catch {
$files = @()
}
if (-not $files -or $files.Count -eq 0) {
return $false
}
$names = ($files | ForEach-Object { $_.Name }) -join ', '
Write-Host ("[LOCAL BUILD] Found {0} project(s) in {1}: {2}" -f $files.Count, $DirectoryPath, $names)
$preferredOrder = @('.sln', '.csproj', '.vcxproj')
$files = $files | Sort-Object @{Expression = { [array]::IndexOf($preferredOrder, $_.Extension.ToLower()) }}
foreach ($f in $files) {
Write-Host ("[LOCAL BUILD] Building {0}" -f $f.FullName)
if ($f.Extension -eq '.sln') {
RestoreThenBuild $f.FullName $ExtraArgs $Platform $Configuration $RestoreOnly
} else {
$buildArgs = '/m'
if ($ExtraArgs) { $buildArgs = "$buildArgs $ExtraArgs" }
RunMSBuild $f.FullName $buildArgs $Platform $Configuration
}
}
return $true
}
function Get-DefaultPlatform {
<#
Returns a default target platform string based on the host machine (x64, arm64, x86).
#>
try {
$envArch = $env:PROCESSOR_ARCHITECTURE
if ($envArch) { $envArch = $envArch.ToLower() }
if ($envArch -eq 'amd64' -or $envArch -eq 'x86_64') { return 'x64' }
if ($envArch -match 'arm64') { return 'arm64' }
if ($envArch -eq 'x86') { return 'x86' }
if ($env:PROCESSOR_ARCHITEW6432) {
$envArch2 = $env:PROCESSOR_ARCHITEW6432.ToLower()
if ($envArch2 -eq 'amd64') { return 'x64' }
if ($envArch2 -match 'arm64') { return 'arm64' }
}
try {
$osArch = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture
switch ($osArch.ToString().ToLower()) {
'x64' { return 'x64' }
'arm64' { return 'arm64' }
'x86' { return 'x86' }
}
} catch {
# ignore - RuntimeInformation may not be available
}
} catch {
# ignore any errors and fall back
}
return 'x64'
}
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)
2025-08-31 19:23:27 -07:00
function Ensure-VsDevEnvironment {
$OriginalLocationForVsInit = Get-Location
try {
if ($env:VSINSTALLDIR -or $env:VCINSTALLDIR -or $env:DevEnvDir -or $env:VCToolsInstallDir) {
Write-Host "[VS] VS developer environment already present"
return $true
}
# Locate vswhere if available
$vswhereCandidates = @(
"$env:ProgramFiles (x86)\Microsoft Visual Studio\Installer\vswhere.exe",
"$env:ProgramFiles\Microsoft Visual Studio\Installer\vswhere.exe"
)
$vswhere = $vswhereCandidates | Where-Object { Test-Path $_ } | Select-Object -First 1
if ($vswhere) { Write-Host "[VS] vswhere found: $vswhere" } else { Write-Host "[VS] vswhere not found" }
$instPaths = @()
if ($vswhere) {
# First try with the VC tools requirement (preferred)
try { $p = & $vswhere -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath 2>$null; if ($p) { $instPaths += $p } } catch {}
# Fallback: try without -requires to find any VS installations
if (-not $instPaths) {
try { $p2 = & $vswhere -latest -products * -property installationPath 2>$null; if ($p2) { $instPaths += $p2 } } catch {}
}
}
# Add explicit common year-based candidates as a last resort
if (-not $instPaths) {
$explicit = @(
"$env:ProgramFiles (x86)\Microsoft Visual Studio\2022\Community",
"$env:ProgramFiles (x86)\Microsoft Visual Studio\2022\Professional",
"$env:ProgramFiles (x86)\Microsoft Visual Studio\2022\Enterprise",
"$env:ProgramFiles\Microsoft Visual Studio\2022\Community",
"$env:ProgramFiles\Microsoft Visual Studio\2022\Professional",
"$env:ProgramFiles\Microsoft Visual Studio\2022\Enterprise"
)
foreach ($c in $explicit) { if (Test-Path $c) { $instPaths += $c } }
}
if (-not $instPaths -or $instPaths.Count -eq 0) {
Write-Warning "[VS] Could not locate Visual Studio installation (no candidates found)"
return $false
}
# Try each candidate installation path until one works
foreach ($inst in $instPaths) {
if (-not $inst) { continue }
Write-Host "[VS] Checking candidate: $inst"
$devDll = Join-Path $inst 'Common7\Tools\Microsoft.VisualStudio.DevShell.dll'
if (Test-Path $devDll) {
try {
Import-Module $devDll -DisableNameChecking -ErrorAction Stop
# Call Enter-VsDevShell using only the install path to avoid parameter name differences
try {
Enter-VsDevShell -VsInstallPath $inst -ErrorAction Stop
Write-Host "[VS] Entered Visual Studio DevShell at $inst"
return $true
} catch {
Write-Warning ("[VS] DevShell import/Enter-VsDevShell failed: {0}" -f $_)
}
} catch {
Write-Warning ("[VS] DevShell import failed: {0}" -f $_)
}
}
$vsDevCmd = Join-Path $inst 'Common7\Tools\VsDevCmd.bat'
if (Test-Path $vsDevCmd) {
Write-Host "[VS] Running VsDevCmd.bat and importing environment from $vsDevCmd"
try {
$cmdOut = cmd.exe /c "`"$vsDevCmd`" && set"
foreach ($line in $cmdOut) {
$parts = $line -split('=',2)
if ($parts.Length -eq 2) {
try { [Environment]::SetEnvironmentVariable($parts[0], $parts[1], 'Process') } catch {}
}
}
Write-Host "[VS] Imported environment from VsDevCmd.bat at $inst"
return $true
} catch {
Write-Warning ("[VS] Failed to run/import VsDevCmd.bat at {0}: {1}" -f $inst, $_)
}
}
}
Write-Warning "[VS] Neither DevShell module nor VsDevCmd.bat found in any candidate paths"
return $false
} finally {
try { Set-Location $OriginalLocationForVsInit } catch {}
}
}