Skip to content
TypeScript

fix(build): preserve directory desktop artifacts and framework symlinks (#24434)

zayd / cold771afdac8d5f
diff --git a/scripts/build-desktop-artifact.test.ts b/scripts/build-desktop-artifact.test.ts
index a4cef9ba03b..165ca80cb30 100644
--- a/scripts/build-desktop-artifact.test.ts
+++ b/scripts/build-desktop-artifact.test.ts
@@ -22,6 +22,7 @@ import { ChildProcess, ChildProcessSpawner } from "effect/process";
import {
buildDesktopBundles,
+ copyDesktopBuildArtifacts,
DesktopBuildManifestRestoreError,
BundleNotSelfContainedError,
BuildCommandFailedError,
@@ -1745,3 +1746,57 @@ it.effect.skipIf(!symlinksSupported)("rebases packaged links into the isolated t
);
}).pipe(Effect.provide(NodeServices.layer)),
);
+
+it.effect("preserves packaged app directories from the dir target", () =>
+ Effect.gen(function* () {
+ const fs = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-dir-artifact-" });
+ const stage = path.join(root, "stage");
+ const output = path.join(root, "output");
+ const app = path.join("mac-arm64", "T3 Code.app", "Contents");
+ yield* fs.makeDirectory(path.join(stage, app), { recursive: true });
+ yield* fs.writeFileString(path.join(stage, app, "Info.plist"), "built-app");
+ yield* fs.writeFileString(path.join(stage, "builder-debug.yml"), "debug");
+ const copied = yield* copyDesktopBuildArtifacts(stage, output, true);
+ assert.include(copied, path.join(output, "mac-arm64"));
+ assert.equal(yield* fs.readFileString(path.join(output, app, "Info.plist")), "built-app");
+ assert.equal(yield* fs.readFileString(path.join(stage, app, "Info.plist")), "built-app");
+ yield* fs.remove(path.join(stage, app, "Info.plist"));
+ yield* fs.writeFileString(path.join(stage, app, "New.plist"), "rebuilt-app");
+ yield* copyDesktopBuildArtifacts(stage, output, true);
+ assert.isFalse(yield* fs.exists(path.join(output, app, "Info.plist")));
+ assert.equal(yield* fs.readFileString(path.join(output, app, "New.plist")), "rebuilt-app");
+ const filesOnly = yield* copyDesktopBuildArtifacts(stage, path.join(root, "files-only"));
+ assert.deepEqual(filesOnly, [path.join(root, "files-only", "builder-debug.yml")]);
+ assert.isFalse(yield* fs.exists(path.join(root, "files-only", "mac-arm64")));
+ yield* fs.remove(path.join(stage, "mac-arm64"), { recursive: true });
+ yield* fs.writeFileString(path.join(stage, "mac-arm64"), "flattened");
+ yield* copyDesktopBuildArtifacts(stage, output, true);
+ assert.equal(yield* fs.readFileString(path.join(output, "mac-arm64")), "flattened");
+ }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
+);
+
+it.effect.skipIf(!symlinksSupported)("preserves relative links when replacing dir artifacts", () =>
+ Effect.gen(function* () {
+ const fs = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-dir-artifact-links-" });
+ const stage = path.join(root, "stage");
+ const output = path.join(root, "output");
+ const app = path.join("mac-arm64", "T3 Code.app", "Contents");
+ yield* fs.makeDirectory(path.join(stage, app), { recursive: true });
+ yield* fs.writeFileString(path.join(stage, app, "Info.plist"), "built-app");
+ yield* fs.symlink("Info.plist", path.join(stage, app, "Current"));
+ yield* copyDesktopBuildArtifacts(stage, output, true);
+ assert.equal(yield* fs.readLink(path.join(output, app, "Current")), "Info.plist");
+ assert.equal(yield* fs.readFileString(path.join(output, app, "Current")), "built-app");
+
+ yield* fs.remove(path.join(stage, app, "Current"));
+ yield* fs.writeFileString(path.join(stage, app, "New.plist"), "rebuilt-app");
+ yield* fs.symlink("New.plist", path.join(stage, app, "Current"));
+ yield* copyDesktopBuildArtifacts(stage, output, true);
+ assert.equal(yield* fs.readLink(path.join(output, app, "Current")), "New.plist");
+ assert.equal(yield* fs.readFileString(path.join(output, app, "Current")), "rebuilt-app");
+ }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
+);
diff --git a/scripts/build-desktop-artifact.ts b/scripts/build-desktop-artifact.ts
index 3a8686f248f..8ba7f1adcbb 100644
--- a/scripts/build-desktop-artifact.ts
+++ b/scripts/build-desktop-artifact.ts
@@ -4057,19 +4057,11 @@ const buildDesktopArtifactUnlocked = Effect.fn("buildDesktopArtifactUnlocked")(f
yield* validateMacPackagedPayload({ stageDistDir, verbose: options.verbose });
}
- const stageEntries = yield* fs.readDirectory(stageDistDir);
- yield* fs.makeDirectory(options.outputDir, { recursive: true });
-
- const copiedArtifacts: string[] = [];
- for (const entry of stageEntries) {
- const from = path.join(stageDistDir, entry);
- const stat = yield* fs.stat(from).pipe(Effect.orElseSucceed(() => null));
- if (!stat || stat.type !== "File") continue;
-
- const to = path.join(options.outputDir, entry);
- yield* fs.copyFile(from, to);
- copiedArtifacts.push(to);
- }
+ const copiedArtifacts = yield* copyDesktopBuildArtifacts(
+ stageDistDir,
+ options.outputDir,
+ options.target === "dir",
+ );
if (copiedArtifacts.length === 0) {
return yield* new DesktopBuildNoArtifactsProducedError({
@@ -4091,6 +4083,41 @@ const buildDesktopArtifact = Effect.fn("buildDesktopArtifact")(function* (
return yield* withDesktopBuildLock(repoRoot, buildDesktopArtifactUnlocked(options));
});
+export const copyDesktopBuildArtifacts = Effect.fn("copyDesktopBuildArtifacts")(function* (
+ stageDistDir: string,
+ outputDir: string,
+ includeDirectories = false,
+) {
+ const fs = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const stageEntries = yield* fs.readDirectory(stageDistDir);
+ yield* fs.makeDirectory(outputDir, { recursive: true });
+
+ const copiedArtifacts: string[] = [];
+ for (const entry of stageEntries) {
+ const from = path.join(stageDistDir, entry);
+ const stat = yield* fs.stat(from).pipe(Effect.orElseSucceed(() => null));
+ if (!stat || (stat.type !== "File" && !(includeDirectories && stat.type === "Directory")))
+ continue;
+
+ const to = path.join(outputDir, entry);
+ // Replace whatever a reused output dir already holds at this path. fs.cp refuses to
+ // overwrite a directory with a file or a file with a directory, and only merges into
+ // an existing directory, so a stale entry of either type must go first.
+ yield* fs.remove(to, { recursive: true, force: true });
+ yield* Effect.tryPromise(() =>
+ NodeFSP.cp(from, to, {
+ recursive: true,
+ force: true,
+ verbatimSymlinks: true,
+ }),
+ );
+ copiedArtifacts.push(to);
+ }
+
+ return copiedArtifacts;
+});
+
const buildDesktopArtifactCli = Command.make("build-desktop-artifact", {
platform: Flag.Literals("platform", BuildPlatform.literals).pipe(
Flag.withDescription("Build platform (env: T3CODE_DESKTOP_PLATFORM)."),