Skip to content

Commit

Permalink
[eas-cli] fix expo-updates fingerprinting during update
Browse files Browse the repository at this point in the history
  • Loading branch information
wschurman committed Feb 16, 2024
1 parent efd9213 commit 8432cc4
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 43 deletions.
8 changes: 4 additions & 4 deletions packages/eas-cli/src/commands/update/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,12 @@ export default class UpdatePublish extends EasCommand {
throw e;
}

const runtimeVersions = await getRuntimeVersionObjectAsync(
const runtimeVersions = await getRuntimeVersionObjectAsync({
exp,
realizedPlatforms,
platforms: realizedPlatforms,
projectDir,
vcsClient
);
vcsClient,
});
const runtimeToPlatformMapping =
getRuntimeToPlatformMappingFromRuntimeVersions(runtimeVersions);

Expand Down
8 changes: 4 additions & 4 deletions packages/eas-cli/src/commands/update/roll-back-to-embedded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,12 @@ export default class UpdateRollBackToEmbedded extends EasCommand {
const gitCommitHash = await vcsClient.getCommitHashAsync();
const isGitWorkingTreeDirty = await vcsClient.hasUncommittedChangesAsync();

const runtimeVersions = await getRuntimeVersionObjectAsync(
const runtimeVersions = await getRuntimeVersionObjectAsync({
exp,
realizedPlatforms,
platforms: realizedPlatforms,
projectDir,
vcsClient
);
vcsClient,
});

let newUpdates: UpdatePublishMutation['updateBranch']['publishUpdateGroups'];
const publishSpinner = ora('Publishing...').start();
Expand Down
120 changes: 85 additions & 35 deletions packages/eas-cli/src/project/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
shouldUseVersionedExpoCLI,
shouldUseVersionedExpoCLIWithExplicitPlatforms,
} from '../utils/expoCli';
import { expoUpdatesCommandAsync } from '../utils/expoUpdatesCli';
import chunk from '../utils/expodash/chunk';
import { truthy } from '../utils/expodash/filter';
import uniqBy from '../utils/expodash/uniqBy';
Expand Down Expand Up @@ -675,49 +676,98 @@ export function getRequestedPlatform(
}

/** Get runtime versions grouped by platform. Runtime version is always `null` on web where the platform is always backwards compatible. */
export async function getRuntimeVersionObjectAsync(
exp: ExpoConfig,
platforms: Platform[],
projectDir: string,
vcsClient: Client
): Promise<{ platform: string; runtimeVersion: string }[]> {
for (const platform of platforms) {
if (platform === 'web') {
continue;
}
const isPolicy = typeof (exp[platform]?.runtimeVersion ?? exp.runtimeVersion) === 'object';
if (isPolicy) {
const isManaged =
(await resolveWorkflowAsync(projectDir, platform as EASBuildJobPlatform, vcsClient)) ===
Workflow.MANAGED;
if (!isManaged) {
throw new Error(
`You're currently using the bare workflow, where runtime version policies are not supported. You must set your runtime version manually. For example, define your runtime version as "1.0.0", not {"policy": "appVersion"} in your app config. ${learnMore(
'https://docs.expo.dev/eas-update/runtime-versions'
)}`
);
}
}
}

export async function getRuntimeVersionObjectAsync({
exp,
platforms,
projectDir,
vcsClient,
}: {
exp: ExpoConfig;
platforms: Platform[];
projectDir: string;
vcsClient: Client;
}): Promise<{ platform: string; runtimeVersion: string }[]> {
return await Promise.all(
[...new Set(platforms)].map(async platform => {
if (platform === 'web') {
return { platform: 'web', runtimeVersion: 'UNVERSIONED' };
}
platforms.map(async platform => {
return {
platform,
runtimeVersion: nullthrows(
await Updates.getRuntimeVersionAsync(projectDir, exp, platform),
`Unable to determine runtime version for ${
requestedPlatformDisplayNames[platform]
}. ${learnMore('https://docs.expo.dev/eas-update/runtime-versions/')}`
),
runtimeVersion: await getRuntimeVersionForPlatformAsync({
exp,
platform,
projectDir,
vcsClient,
}),
};
})
);
}

async function getRuntimeVersionForPlatformAsync({
exp,
platform,
projectDir,
vcsClient,
}: {
exp: ExpoConfig;
platform: Platform;
projectDir: string;
vcsClient: Client;
}): Promise<string> {
if (platform === 'web') {
return 'UNVERSIONED';
}

const runtimeVersion = exp[platform]?.runtimeVersion ?? exp.runtimeVersion;
if (typeof runtimeVersion === 'object') {
const policy = runtimeVersion.policy;

if (policy === 'fingerprintExperimental') {
// log to inform the user that the fingerprint has been calculated
Log.warn(
`Calculating native fingerprint for platform ${platform} using current state of the "${platform}" directory. ` +
`If the fingerprint differs from the build's fingerint, ensure the state of your project is consistent ` +
`(repository is clean, ios and android native directories are in the same state as the build if applicable).`
);

const fingerprintRawString = await expoUpdatesCommandAsync(projectDir, [
'fingerprint:generate',
'--platform',
platform,
]);
const fingerprintObject = JSON.parse(fingerprintRawString);
const hash = nullthrows(
fingerprintObject.hash,
'invalid response from expo-update CLI for fingerprint generation'
);
return hash;
}

const workflow = await resolveWorkflowAsync(
projectDir,
platform as EASBuildJobPlatform,
vcsClient
);
if (workflow !== Workflow.MANAGED) {
throw new Error(
`You're currently using the bare workflow, where runtime version policies are not supported. You must set your runtime version manually. For example, define your runtime version as "1.0.0", not {"policy": "appVersion"} in your app config. ${learnMore(
'https://docs.expo.dev/eas-update/runtime-versions'
)}`
);
}
}

const resolvedRuntimeVersion = await Updates.getRuntimeVersionAsync(projectDir, exp, platform);
if (!resolvedRuntimeVersion) {
throw new Error(
`Unable to determine runtime version for ${
requestedPlatformDisplayNames[platform]
}. ${learnMore('https://docs.expo.dev/eas-update/runtime-versions/')}`
);
}

return resolvedRuntimeVersion;
}

export function getRuntimeToPlatformMappingFromRuntimeVersions(
runtimeVersions: { platform: string; runtimeVersion: string }[]
): { runtimeVersion: string; platforms: string[] }[] {
Expand Down
40 changes: 40 additions & 0 deletions packages/eas-cli/src/utils/expoUpdatesCli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import spawnAsync from '@expo/spawn-async';
import chalk from 'chalk';
import resolveFrom, { silent as silentResolveFrom } from 'resolve-from';

import Log, { link } from '../log';

export async function expoUpdatesCommandAsync(projectDir: string, args: string[]): Promise<string> {
let expoUpdatesCli;
try {
expoUpdatesCli =
silentResolveFrom(projectDir, 'expo-updates/bin/cli') ??
resolveFrom(projectDir, 'expo-updates/bin/cli.js');
} catch (e: any) {
if (e.code === 'MODULE_NOT_FOUND') {
throw new Error(
`The \`expo-updates\` package was not found. Follow the installation directions at ${link(
'https://docs.expo.dev/bare/installing-expo-modules/'
)}`
);
}
throw e;
}

const spawnPromise = spawnAsync(expoUpdatesCli, args, {
stdio: ['inherit', 'pipe', 'pipe'], // inherit stdin so user can install a missing expo-cli from inside this command
});
const {
child: { stderr },
} = spawnPromise;
if (!stderr) {
throw new Error('Failed to spawn expo-updates cli');
}
stderr.on('data', data => {
for (const line of data.toString().trim().split('\n')) {
Log.warn(`${chalk.gray('[expo-cli]')} ${line}`);
}
});
const result = await spawnPromise;
return result.stdout;
}

0 comments on commit 8432cc4

Please sign in to comment.