Skip to content

The agent that grows with you

fix(desktop): build-bundle cleans under the lock, restores the shell, never signs

ethernetaac3440bac71
diff --git a/apps/desktop/BUILDING.md b/apps/desktop/BUILDING.md
index a3fa0658ff7..ed242a35d2b 100644
--- a/apps/desktop/BUILDING.md
+++ b/apps/desktop/BUILDING.md
@@ -81,16 +81,31 @@ scripts\build-bundle.ps1 # Windows sideload MSIX
scripts\build-bundle.ps1 -Store # Windows Store MSIX
```
-The checkout must be clean, because the build packages `HEAD`. Both scripts
-delete the previous build output first and write to `apps/desktop/release/`.
-The packages are unsigned.
+The checkout must be clean, because the build packages `HEAD`. Both scripts pass
+`--clean` to the driver. The driver removes the previous outputs only after it holds
+the checkout lock, so a second run cannot delete a build that is still going. Output
+goes to `apps/desktop/release/`.
+
+The scripts do not sign or notarize. They remove `AZURE_SIGN_*` (Windows) and the
+`CSC_*`/`APPLE_*` signing variables (macOS, Linux) from the build, and restore your
+Windows shell afterward. macOS can still sign nested binaries when it finds a
+Developer ID in your keychain.
`-Store` allocates a local claim tag `rc.<N>-vX.Y.Z` (N starts at 900) for the
next patch after the newest published stable tag, and deletes it when the
build ends. The tag is never pushed. Each architecture builds on its own host.
-To combine the x64 and arm64 Store packages, copy both `Store-*.msix` files into
-one `apps/desktop/release` and run `node scripts/bundle-store-msixbundle.mjs --tag vX.Y.Z`.
-On Windows, use a short checkout path such as `C:\hsb`.
+`-Store` prints the claim timestamp as `HERMES_RELEASE_EPOCH=<n>`. To combine the
+x64 and arm64 Store packages, copy both `Store-*.msix` files into one
+`apps/desktop/release`, then run this on that host:
+
+```powershell
+$env:HERMES_RELEASE_EPOCH = '<n from the build>'
+node scripts/bundle-store-msixbundle.mjs --tag vX.Y.Z
+```
+
+Use the epoch from ONE claim for both architectures. The tag is already deleted, so
+without the epoch the bundle step finds no release timestamp. On Windows, use a
+short checkout path such as `C:\hsb`.
## Complete native build
diff --git a/scripts/build-bundle.ps1 b/scripts/build-bundle.ps1
index 57d683f0a08..f0987dc6383 100644
--- a/scripts/build-bundle.ps1
+++ b/scripts/build-bundle.ps1
@@ -7,14 +7,19 @@
scripts\build-bundle.ps1 -Store Microsoft Store MSIX (Store identity).
The commit does not need to be pushed. The checkout must be clean: the build
- packages HEAD. Output goes to apps\desktop\release\. The packages are unsigned.
+ packages HEAD. Output goes to apps\desktop\release\. The script removes the
+ AZURE_SIGN_* variables from the build, so the packages are unsigned even when
+ your shell has release signing set.
-Store needs a stable release tag. This script makes a local claim tag
(rc.<N>-vX.Y.Z) for the next patch version, builds with it, and deletes it
when the build ends. It never pushes the tag. Each architecture builds on its
- own native host. To combine x64 and arm64 Store packages into one bundle, copy
- both Store-*.msix files into one apps\desktop\release and run
- scripts\bundle-store-msixbundle.mjs.
+ own native host. The script prints the claim tag's timestamp as
+ HERMES_RELEASE_EPOCH. To combine the x64 and arm64 Store packages, copy both
+ Store-*.msix files into one apps\desktop\release, set the same
+ HERMES_RELEASE_EPOCH on that host, and run
+ node scripts\bundle-store-msixbundle.mjs --tag vX.Y.Z.
+ Both hosts must use the epoch from ONE claim, or the package versions differ.
Use a short checkout path such as C:\hsb. On Windows ARM64, a long path makes
the cryptography build fail with LNK1104.
@@ -65,14 +70,6 @@ function Git-Lines {
return $lines | Where-Object { $_ }
}
-# Previous builds leave read-only payload files, which a plain delete refuses.
-function Remove-Output([string]$Path) {
- if (-not (Test-Path -LiteralPath $Path)) { return }
- Write-Host "build-bundle: removing previous output $Path"
- & attrib -r "$Path\*" /s /d | Out-Null
- Remove-Item -LiteralPath $Path -Recurse -Force -ErrorAction Stop
-}
-
# Next patch after the newest published stable tag (vX.Y.Z, not CalVer or canary).
function Get-ClaimVersion {
$latest = $null
@@ -111,11 +108,14 @@ if ($Repo.Length -gt 40) {
Write-Warning "checkout path is $($Repo.Length) characters. On Windows ARM64 a long path can fail the cryptography build (LNK1104). Prefer C:\hsb."
}
-# .cache stays: it holds downloaded tools and is safe to reuse.
-foreach ($stale in '.build\desktop-job', 'apps\desktop\build', 'apps\desktop\dist', 'apps\desktop\release') {
- Remove-Output (Join-Path $Repo $stale)
-}
-
+# Release settings and signing credentials exist only for this build. Save the caller's
+# values and put them back, so a later direct build command sees an untouched session.
+$buildVariables = @('PYTHONUTF8', 'RELEASE_CLAIM_TAG', 'RELEASE_CLAIM_OBJECT', 'HERMES_PAYLOAD_TAG',
+ 'HERMES_PAYLOAD_VERSION', 'HERMES_DESKTOP_VARIANT') +
+ @(Get-ChildItem Env: | Where-Object { $_.Name -like 'AZURE_*' } | ForEach-Object { $_.Name })
+$savedEnvironment = @{}
+foreach ($name in $buildVariables) { $savedEnvironment[$name] = [Environment]::GetEnvironmentVariable($name, 'Process') }
+foreach ($name in $buildVariables | Where-Object { $_ -like 'AZURE_*' }) { Remove-Item "Env:$name" }
$env:PYTHONUTF8 = '1'
$claim = $null
try {
@@ -127,6 +127,8 @@ try {
tag -a $claim -m 'local build claim' $sha
if ($LASTEXITCODE) { Fail "could not create claim tag $claim" }
$claimObject = @(Git-Lines rev-parse "refs/tags/$claim")[0]
+ # Every architecture's Store package derives its 4-part version from this one timestamp.
+ $claimEpoch = [int64]((@(Git-Lines cat-file -p $claimObject) | Where-Object { $_ -match '^tagger ' })[0] -replace '^tagger .* (\d+) [+-]\d{4}$', '$1')
$env:RELEASE_CLAIM_TAG = $claim
$env:RELEASE_CLAIM_OBJECT = $claimObject
$env:HERMES_PAYLOAD_TAG = "v$version"
@@ -136,16 +138,19 @@ try {
# Prepare as bundled, then package as store from that preparation, as CI does.
$work = Join-Path $Repo '.build\desktop-job'
& $py scripts/bundles/desktop.py --tag "v$version" --release-commit $sha --variant bundled --prepare-only `
- --work $work --cache (Join-Path $Repo '.cache\desktop-inputs')
+ --work $work --cache (Join-Path $Repo '.cache\desktop-inputs') --clean
if ($LASTEXITCODE) { Fail "preparation failed (exit $LASTEXITCODE)" }
& $py scripts/bundles/desktop.py --prepared (Join-Path $work 'prepared.json') --variant store
if ($LASTEXITCODE) { Fail "Store packaging failed (exit $LASTEXITCODE)" }
} else {
Write-Host "build-bundle: building $sha (sideload)"
- & $py scripts/bundles/desktop.py --commit $sha --variant bundled
+ & $py scripts/bundles/desktop.py --commit $sha --variant bundled --clean
if ($LASTEXITCODE) { Fail "build failed (exit $LASTEXITCODE)" }
}
} finally {
+ foreach ($name in $buildVariables) {
+ [Environment]::SetEnvironmentVariable($name, $savedEnvironment[$name], 'Process')
+ }
if ($claim) {
# Claim tags are local only. Delete this one even when the build fails.
& git tag -d $claim | Out-Null
@@ -153,5 +158,8 @@ try {
}
Write-Host "build-bundle: done. Unsigned packages in $Repo\apps\desktop\release:"
+if ($Store) {
+ Write-Host "build-bundle: claim timestamp for combining architectures: HERMES_RELEASE_EPOCH=$claimEpoch (version v$version)"
+}
Get-ChildItem (Join-Path $Repo 'apps\desktop\release') -Filter '*.msix*' |
ForEach-Object { ' {0} {1:N0} MB' -f $_.Name, ($_.Length / 1MB) }
diff --git a/scripts/build-bundle.sh b/scripts/build-bundle.sh
index 82edbb22606..6c8d471548a 100755
--- a/scripts/build-bundle.sh
+++ b/scripts/build-bundle.sh
@@ -4,7 +4,9 @@
# scripts/build-bundle.sh [--variant bundled|light] [-- <electron-builder args>]
#
# macOS gets a DMG and ZIP, Linux an AppImage, in apps/desktop/release/.
-# The commit does not need to be pushed. The build is unsigned.
+# The commit does not need to be pushed. The build is not notarized, and the
+# release signing variables in your shell are ignored. macOS can still sign
+# nested binaries with a Developer ID that it finds in your keychain.
# Windows has its own script: scripts/build-bundle.ps1.
set -euo pipefail
@@ -13,7 +15,7 @@ while [ $# -gt 0 ]; do
case "$1" in
--variant) variant="${2:?--variant needs bundled or light}"; shift 2 ;;
--) shift; break ;;
- -h|--help) sed -n '2,8p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
+ -h|--help) sed -n '2,10p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
*) echo "build-bundle: unknown argument $1 (see --help)" >&2; exit 2 ;;
esac
done
@@ -40,18 +42,17 @@ if [ -n "$(git status --porcelain --untracked-files=all)" ]; then
fi
commit="$(git rev-parse HEAD)"
-# A rebuild in the same checkout needs the previous work and outputs gone.
-# .cache stays: it holds downloaded tools and is safe to reuse.
-for stale in .build/desktop-job apps/desktop/build apps/desktop/dist apps/desktop/release; do
- if [ -e "$stale" ]; then
- echo "build-bundle: removing previous output $stale"
- chmod -R u+w "$stale" 2>/dev/null || true
- rm -rf "$stale"
- fi
-done
+# A local build never signs or notarizes with credentials from the caller's shell.
+# CSC_IDENTITY_AUTO_DISCOVERY=false is the same switch the desktop rebuild command uses.
+unset APPLE_API_KEY APPLE_API_KEY_ID APPLE_API_ISSUER APPLE_NOTARY_PROFILE APPLE_SIGNING_IDENTITY \
+ CSC_LINK CSC_KEY_PASSWORD CSC_NAME CSC_KEYCHAIN
+export CSC_IDENTITY_AUTO_DISCOVERY=false
+# --clean removes the previous outputs after the driver holds the checkout lock, so a
+# second invocation cannot delete the files of a build that is still running.
+# .cache stays: it holds downloaded tools and is safe to reuse.
echo "build-bundle: building $commit ($variant)"
-"$python" scripts/bundles/desktop.py --commit "$commit" --variant "$variant" ${1+-- "$@"}
+"$python" scripts/bundles/desktop.py --commit "$commit" --variant "$variant" --clean ${1+-- "$@"}
echo "build-bundle: done. Artifacts in $repo/apps/desktop/release:"
ls -lh apps/desktop/release | sed 's/^/ /'
diff --git a/scripts/bundles/desktop.py b/scripts/bundles/desktop.py
index 974e8ec8019..c71042167ff 100644
--- a/scripts/bundles/desktop.py
+++ b/scripts/bundles/desktop.py
@@ -152,6 +152,8 @@ def main() -> None:
parser.add_argument("--work", type=Path)
parser.add_argument("--cache", type=Path)
parser.add_argument("--prepare-only", action="store_true")
+ parser.add_argument("--clean", action="store_true",
+ help="Remove this checkout's previous build outputs, under the checkout lock, before preparing")
parser.add_argument("--prepared", type=Path)
parser.add_argument("builder_args", nargs=argparse.REMAINDER)
args = parser.parse_args()
@@ -159,7 +161,7 @@ def main() -> None:
try:
if args.prepared:
if (args.tag or args.commit_build or args.release_commit or args.channel_request
- or args.prepare_only or args.work or args.cache):
+ or args.prepare_only or args.work or args.cache or args.clean):
parser.error("--prepared supplies the complete build request")
build_prepared(args.prepared, builder_args, args.variant)
else:
@@ -175,7 +177,7 @@ def main() -> None:
release_commit=args.release_commit)
if args.prepare_only and builder_args:
parser.error("builder arguments belong to the build phase")
- result = prepare(request)
+ result = prepare(request, clean=args.clean)
if args.prepare_only:
print(result)
else:
diff --git a/scripts/bundles/desktop_prepare.py b/scripts/bundles/desktop_prepare.py
index 908513e268a..6ba8b465734 100644
--- a/scripts/bundles/desktop_prepare.py
+++ b/scripts/bundles/desktop_prepare.py
@@ -266,7 +266,26 @@ class PreparedDesktop:
raise ValueError(f"preparation input changed or is missing: {path}; prepare again")
-def prepare(request: BuildRequest) -> Path:
+def clean_outputs(request: BuildRequest) -> None:
+ """Remove the previous build's outputs. The caller must hold the checkout lock.
+
+ Do not call this before taking the lock: a second invocation would delete the
+ files of a build that is still running. A work directory this checkout does not
+ own stays in place, so ``_prepare`` can refuse it instead of losing user files.
+ """
+ from pm.filesystem import remove_tree
+
+ stale = [request.source / "apps/desktop" / name for name in ("build", "dist", "release")]
+ owner = request.work / ".desktop-preparation"
+ if owner.is_file() and owner.read_text(encoding="utf-8-sig") == str(request.source):
+ stale.append(request.work)
+ for path in stale:
+ if path.exists():
+ print(f"desktop build: removing previous output {path}", flush=True)
+ remove_tree(path)
+
+
+def prepare(request: BuildRequest, *, clean: bool = False) -> Path:
from scripts.bundles.desktop_inputs import build_lock
request.validate_channel()
@@ -274,6 +293,8 @@ def prepare(request: BuildRequest) -> Path:
raise ValueError("channel builds require a supported native macOS or Windows target")
require_source(request.source, request.commit)
with build_lock(request.source):
+ if clean:
+ clean_outputs(request)
return _prepare(request)
diff --git a/tests/scripts/test_desktop_preparation.py b/tests/scripts/test_desktop_preparation.py
index 07d3350d595..b2ccb74de0d 100644
--- a/tests/scripts/test_desktop_preparation.py
+++ b/tests/scripts/test_desktop_preparation.py
@@ -273,3 +273,27 @@ def test_flavored_icon_staging_hands_the_admitted_checkout_back(tmp_path):
with flavored_assets(rendered, assets):
raise RuntimeError("packaging failed")
require_source(source, commit)
+
+
+def test_clean_run_cannot_delete_the_outputs_of_a_build_that_holds_the_lock(tmp_path):
+ from scripts.bundles.desktop_inputs import build_lock
+ from scripts.bundles.desktop_prepare import BuildRequest, prepare
+
+ source, commit = _project(tmp_path)
+ request = BuildRequest.create(source, tag=None, commit=commit, variant="light",
+ work=tmp_path / "work", cache=tmp_path / "cache", bundle_env={})
+ running = source / "apps/desktop/release/running-build.msix"
+ running.parent.mkdir(parents=True)
+ running.write_text("output of the build that holds the lock", encoding="utf-8")
+ (source / ".gitignore").write_text(".build/\napps/desktop/release/\n", encoding="utf-8")
+ subprocess.run(["git", "add", ".gitignore"], cwd=source, check=True)
+ subprocess.run(["git", "-c", "user.name=Fixture", "-c", "[email protected]",
+ "commit", "-m", "ignore outputs"], cwd=source, check=True, capture_output=True)
+ request = BuildRequest.create(source, tag=None, commit=subprocess.check_output(
+ ["git", "rev-parse", "HEAD"], cwd=source, text=True).strip(), variant="light",
+ work=tmp_path / "work", cache=tmp_path / "cache", bundle_env={})
+
+ with build_lock(source), pytest.raises(ValueError, match="another desktop"):
+ prepare(request, clean=True)
+
+ assert running.read_text(encoding="utf-8") == "output of the build that holds the lock"