Skip to content
Merged
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ In some scenarios we make use of this cache purging mechanism to make sure users
of external tools that GM-CLI depends on. For instance, if we require a new version of Igor for GM-CLI to function, we may bump the
major version.

### Which Igor gets used

The above applies to the standalone Igor that GM-CLI downloads from `gms.yoyogames.com` into `.gmcache/igor`. That copy
is only used to bootstrap: fetching a licence, and listing and installing runtimes. It is versioned independently of the
runtimes and can lag a long way behind them.

Building, running and packaging a project instead use the Igor that ships inside the installed runtime, at
`bin/igor/<platform>/<arch>`, falling back to the downloaded one when the runtime has none (see `findRuntimeIgor`).
Igor and the runner are built and released together, and only that pairing is guaranteed to agree on where a build's
output belongs on disk — mixing them silently produces packages the runner cannot load. This means a fix on the Igor
side reaches users as soon as they install the runtime carrying it, with no `MAJOR` bump needed to purge the cache.

## Publish

```sh
Expand Down
80 changes: 79 additions & 1 deletion src/gms2/install-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type { Log } from "~/log";
import { KnownError } from "~/error";
import type { Target } from "~/target";
import {
IGOR_PLATFORM_DIRS,
installRuntime,
listRuntimes,
ModuleSchema,
Expand Down Expand Up @@ -304,7 +305,10 @@ export async function installRuntimeIfNeeded(
}
log.success("Runtime installed");

runtimeLocation = await findRuntimeLocation(ctx, runtimeDir);
// Keep honouring the requested version: without it we'd hand back whatever
// runtime in the cache happens to be newest, which is rarely the one we just
// installed.
runtimeLocation = await findRuntimeLocation(ctx, runtimeDir, version);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops! Good catch 👍

if (!runtimeLocation) {
throw new Error("Invariant broken: no runtime found after installation");
}
Expand All @@ -325,3 +329,77 @@ export async function getInstalledRuntimeModules(
.filter((result) => result.success)
.map((result) => result.data);
}

const MIN_RUNTIME_WITH_USABLE_IGOR: Gms2Version = [2026, 0, 0, 0];

/**
* Every runtime ships the Igor that was built alongside it, at
* `bin/igor/<platform>/<arch>/Igor`. That copy is the one that agrees with the
* runner about where files belong on disk, so it's what we build with.
*
* The standalone Igor we download to bootstrap (fetch a license, list and
* install runtimes) is versioned separately from the runtimes and lags behind
* them, which silently breaks targets where Igor does the packaging. For
* example on OperaGX, external assets such as `.yytex` texture pages moved into
* an `assets` subfolder of the package, and a runner from a runtime newer than
* the bootstrap Igor 404s on every one of them.
* See https://github.com/YoYoGames/GameMaker-Bugs/issues/15930
*
* Returns undefined when the runtime has no usable Igor, in which case the
* caller should stick with the bootstrap one.
*/
export async function findRuntimeIgor(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit sad that we need to do this, but makes a lot of sense! 👍

ctx: Context,
runtimeLocation: string,
): Promise<string | undefined> {
const runtimeVersion = parseRuntimeVersionFromDirName(
ctx.path.basename(runtimeLocation),
);
// Older runtimes predate the Igor flags we build with (-projectool, -prefabs,
// -jsonErrors), so for those the bootstrap Igor really is the better choice.
if (
runtimeVersion === undefined ||
gms2VersionCompare(runtimeVersion, MIN_RUNTIME_WITH_USABLE_IGOR) < 0
) {
return undefined;
}

const platform = ctx.process.platform;
const platformDir = IGOR_PLATFORM_DIRS[platform] ?? platform;
// We use x64 builds on ARM Windows, and fall back to the x64 build under
// Rosetta on Apple Silicon, matching downloadIgor().
const archDirs =
platform === "win32"
? ["x64"]
: platform === "darwin"
? [ctx.process.arch, "x64"]
: [ctx.process.arch];

for (const archDir of archDirs) {
const igorPath = ctx.path.join(
runtimeLocation,
"bin",
"igor",
platformDir,
archDir,
platform === "win32" ? "Igor.exe" : "Igor",
);
if (!(await exists(ctx, igorPath))) {
continue;
}
if (platform !== "win32") {
// installationFixup() already does this for runtimes we installed
// ourselves, but be safe about runtimes left behind by other tools.
try {
await ctx.fs.chmod(igorPath, 0o755);
} catch {
// Read-only cache, or someone else owns the file: if it's already
// executable the spawn below still works, and if it isn't we'd rather
// report the spawn failure than this one.
}
}
return igorPath;
}

return undefined;
}
12 changes: 8 additions & 4 deletions src/gms2/use-gms2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { stopProcesses } from "~/kill-process";
import { getProjectName, type ProjectPath } from "~/project";
import type { Target } from "~/target";
import type { Gms2VersionPartial } from "~/toolchain";
import { installRuntimeIfNeeded } from "./install-runtime";
import { findRuntimeIgor, installRuntimeIfNeeded } from "./install-runtime";
import {
defaultGms2ToolchainOptions,
type Gms2ToolchainOptions,
Expand Down Expand Up @@ -93,6 +93,12 @@ export async function useGms2(
target: options.target,
});

// Prefer the Igor that shipped with this runtime over the standalone one we
// downloaded to bootstrap: only that pairing is guaranteed to agree on where
// the build output belongs. See findRuntimeIgor().
const igorPath =
(await findRuntimeIgor(ctx, runtimeLocation)) ?? tools.igorPath;

const buildCacheDir = await cache.getSubDirPath(
ctx,
`build-gms2-${options.target}-${runtime}`,
Expand Down Expand Up @@ -149,12 +155,11 @@ export async function useGms2(

try {
await spawnIgor(ctx, actionLog, {
igorPath: tools.igorPath,
igorPath,
verbose: options.verbose,
args: constructIgorBuildArgs(
ctx,
{
igorPath: tools.igorPath,
licenseFile: options.licenseFile,
prefabsDir: options.prefabsDir,
runtimeDir: runtimeLocation,
Expand Down Expand Up @@ -369,7 +374,6 @@ function encodeFeatureFlags(flags: string[]): string {
}

export interface CommonIgorBuildArgs {
igorPath: string;
licenseFile: string;
prefabsDir: string;
runtimeDir: string;
Expand Down
2 changes: 1 addition & 1 deletion src/igor/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const IGOR_ZIPS: Record<string, Record<string, string>> = {
},
};

const IGOR_PLATFORM_DIRS: Record<string, string> = {
export const IGOR_PLATFORM_DIRS: Record<string, string> = {
win32: "windows",
linux: "linux",
darwin: "osx",
Expand Down
2 changes: 2 additions & 0 deletions src/parse-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface ParsedEnv {
NO_COLOR: boolean | undefined;
CI: boolean | undefined;
LOCALAPPDATA: string | undefined;
PROGRAMDATA: string | undefined;
XDG_CACHE_HOME: string | undefined;
}

Expand Down Expand Up @@ -75,6 +76,7 @@ function parseEnv(rawEnv: NodeJS.ProcessEnv): ParsedEnv {
NO_COLOR: parse("NO_COLOR", booleanEnvVar),
CI: parse("CI", booleanEnvVar),
LOCALAPPDATA: parse("LOCALAPPDATA", z.string()),
PROGRAMDATA: parse("PROGRAMDATA", z.string()),
XDG_CACHE_HOME: parse("XDG_CACHE_HOME", z.string()),
};

Expand Down
145 changes: 144 additions & 1 deletion src/restore-prefabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import type { Cache } from "./cache";
import type { Context } from "./context";
import { exists, type Context } from "./context";
import type { Log } from "./log";
import type { ProjectPath } from "./project";
import { spawnProcess } from "./spawn";
Expand All @@ -39,6 +39,7 @@ export async function restorePrefabs(
},
): Promise<string> {
const prefabsDir = await cache.getSubDirPath(ctx, "prefabs");
await seedPrefabsFromIdeStores(ctx, log, { projectPath, prefabsDir });
// TODO LATER: add options to pick registry etc.
const args = [
"PREFABS",
Expand All @@ -57,3 +58,145 @@ export async function restorePrefabs(
});
return prefabsDir;
}

/**
* Directories the GameMaker IDE unpacks prefab packages into, one
* `<name>-<version>` folder per package. The IDE keeps this store per machine
* rather than per project, so a project built in the IDE looks, to us, like it
* has no prefabs at all.
*
* Each entry is a directory that may hold several `GameMakerStudio2*` installs
* (stable, Beta, Dev, LTS2026, ...), each with its own `Prefabs` folder.
*/
function idePrefabStoreRoots(ctx: Context): string[] {
switch (ctx.process.platform) {
case "win32":
return [ctx.env.PROGRAMDATA ?? "C:\\ProgramData"];
case "darwin":
return ["/Users/Shared"];
default:
return [ctx.path.join(ctx.os.homedir(), ".config")];
}
}

/**
* Copy any prefab package the project references out of the IDE's store and
* into the folder we hand to ProjectTool, so it doesn't have to fetch them.
*
* This is a workaround: fetching is PackageTool's job, but the newest published
* package-tool binds to gmpm by reflection and no longer matches the gmpm we
* install alongside it, so every fetch dies with "Parameter count mismatch".
* Seeding from the IDE store sidesteps the fetch entirely on machines that have
* the IDE. Once package-tool is republished against the current gmpm this only
Comment on lines +86 to +90

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to avoid coupling the CLI to the IDE. (Especially since the CLI expects to run in CI environments where there is no pre-existing IDE installation.)

But I think it's up to you and @stuckie to decide if this workaround is needed for now, or if an upstream changed can be made in either gmpm or package-tool instead.

* saves a download and can go away.
*
* Best effort throughout: anything we can't work out is left to ProjectTool,
* which reports missing prefabs far better than we could here.
*/
async function seedPrefabsFromIdeStores(
ctx: Context,
log: Log,
{ projectPath, prefabsDir }: { projectPath: ProjectPath; prefabsDir: string },
): Promise<void> {
try {
const referenced = await readPrefabReferences(ctx, projectPath);
if (referenced.length === 0) {
return;
}

// ProjectTool looks next to the project as well as in the folder we pass,
// so a package in either place is already accounted for.
const projectPrefabsDir = ctx.path.join(
ctx.path.dirname(projectPath),
"prefabs",
);
const missing: string[] = [];
for (const name of referenced) {
if (
!(await exists(ctx, ctx.path.join(prefabsDir, name))) &&
!(await exists(ctx, ctx.path.join(projectPrefabsDir, name)))
) {
missing.push(name);
}
}
if (missing.length === 0) {
return;
}

const stores = await findIdePrefabStores(ctx);
if (stores.length === 0) {
return;
}

let copied = 0;
for (const name of missing) {
for (const store of stores) {
const src = ctx.path.join(store, name);
if (!(await exists(ctx, src))) {
continue;
}
await ctx.fs.cp(src, ctx.path.join(prefabsDir, name), {
recursive: true,
});
log.message(`Copied prefab ${name} from ${store}`);
copied++;
break;
}
}
if (copied > 0) {
log.message(
`Seeded ${String(copied)} of ${String(missing.length)} missing prefabs from the GameMaker IDE`,
);
}
} catch (e) {
log.message(`Could not read prefabs from the GameMaker IDE: ${String(e)}`);
}
}

/** Every `GameMakerStudio2*` install's `Prefabs` folder that exists on this machine. */
async function findIdePrefabStores(ctx: Context): Promise<string[]> {
const stores: string[] = [];
for (const root of idePrefabStoreRoots(ctx)) {
let entries: string[];
try {
entries = await ctx.fs.readdir(root);
} catch {
// Root doesn't exist (no IDE installed, or not where we look on this OS).
continue;
}
for (const entry of entries.sort()) {
if (!entry.startsWith("GameMakerStudio2")) {
continue;
}
const store = ctx.path.join(root, entry, "Prefabs");
if (await exists(ctx, store)) {
stores.push(store);
}
}
}
return stores;
}

/**
* Names of the prefab packages a project pulls in, read out of the .yyp's
* `ForcedPrefabProjectReferences`. The .yyp is JSON with trailing commas, so we
* pick the names out with a regex rather than teaching the CLI to parse it.
*/
async function readPrefabReferences(
ctx: Context,
projectPath: ProjectPath,
): Promise<string[]> {
const yyp = await ctx.fs.readFile(projectPath, "utf-8");
const block = /"ForcedPrefabProjectReferences"\s*:\s*\[([\s\S]*?)\]/.exec(
yyp,
)?.[1];
if (!block) {
return [];
}
const names = new Set<string>();
for (const match of block.matchAll(/"name"\s*:\s*"([^"\\/]+)"/g)) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
names.add(match[1]!);
}
return [...names];
}
Loading