Skip to content

Commit 3ed91ff

Browse files
committed
make sure to account for target world for build cache
Signed-off-by: Karthik Ganeshram <[email protected]>
1 parent e5fb3cc commit 3ed91ff

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

packages/build-tools/src/build.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
calculateChecksum,
2+
calculateFileChecksum,
33
fileExists,
44
getExistingBuildData,
55
} from './utils.js';
@@ -9,15 +9,16 @@ export function getBuildDataPath(src: string): string {
99
}
1010

1111
export async function ShouldComponentize(
12-
src: string, outputPath: string, componentizeVersion: string, runtimeArgs: string,
12+
src: string, outputPath: string, componentizeVersion: string, runtimeArgs: string, targetWitChecksum: string
1313
) {
14-
const sourceChecksum = await calculateChecksum(src);
14+
const sourceChecksum = await calculateFileChecksum(src);
1515
const existingBuildData = await getExistingBuildData(getBuildDataPath(src));
1616

1717
if (
1818
existingBuildData?.version == componentizeVersion &&
1919
existingBuildData?.checksum === sourceChecksum &&
2020
existingBuildData?.runtimeArgs === runtimeArgs &&
21+
existingBuildData?.targetWitChecksum === targetWitChecksum &&
2122
(await fileExists(outputPath))
2223
) {
2324
return false;

packages/build-tools/src/index.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { componentize } from '@bytecodealliance/componentize-js';
44
import { version as componentizeVersion } from '@bytecodealliance/componentize-js';
55
import { getPackagesWithWasiDeps, processWasiDeps } from './wasiDepsParser.js';
66
import {
7-
calculateChecksum,
7+
calculateCheckSum,
8+
calculateFileChecksum,
89
saveBuildData,
910
} from './utils.js';
1011
import { getCliArgs } from './cli.js';
@@ -23,15 +24,6 @@ async function main() {
2324
let outputPath = CliArgs.output;
2425
let runtimeArgs = CliArgs.debug ? '--enable-script-debugging' : '';
2526

26-
// Small optimization to skip componentization if the source file hasn't changed
27-
if (!(await ShouldComponentize(src, outputPath, componentizeVersion, runtimeArgs))) {
28-
console.log(
29-
'No changes detected in source file. Skipping componentization.',
30-
);
31-
return;
32-
}
33-
console.log('Componentizing...');
34-
3527
// generate wit world string
3628
let wasiDeps = getPackagesWithWasiDeps(process.cwd(), new Set(), true);
3729
let { witPaths, targetWorlds } = processWasiDeps(wasiDeps);
@@ -63,6 +55,17 @@ async function main() {
6355
'combined-wit:[email protected]',
6456
);
6557

58+
// Calcuilate the checksum of the inline wit
59+
let inlineWitChecksum = await calculateCheckSum(inlineWit);
60+
// Small optimization to skip componentization if the source file hasn't changed
61+
if (!(await ShouldComponentize(src, outputPath, componentizeVersion, runtimeArgs, inlineWitChecksum))) {
62+
console.log(
63+
'No changes detected in source file and target World. Skipping componentization.',
64+
);
65+
return;
66+
}
67+
console.log('Componentizing...');
68+
6669
const source = await readFile(src, 'utf8');
6770
const precompiledSource = precompile(source, src, true) as string;
6871

@@ -85,9 +88,10 @@ async function main() {
8588
// Save the checksum of the input file along with the componentize version
8689
await saveBuildData(
8790
getBuildDataPath(src),
88-
await calculateChecksum(src),
91+
await calculateFileChecksum(src),
8992
componentizeVersion,
9093
runtimeArgs,
94+
inlineWitChecksum,
9195
);
9296

9397
console.log('Component successfully written.');

packages/build-tools/src/utils.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { access, writeFile } from 'node:fs/promises';
44

55

66
// Function to calculate file checksum
7-
export async function calculateChecksum(filePath: string) {
7+
export async function calculateFileChecksum(filePath: string) {
88
try {
99
const fileBuffer = await readFile(filePath);
1010
const hash = createHash('sha256');
@@ -16,6 +16,12 @@ export async function calculateChecksum(filePath: string) {
1616
}
1717
}
1818

19+
export function calculateCheckSum(data: string | Buffer): string {
20+
const hash = createHash('sha256');
21+
hash.update(data);
22+
return hash.digest('hex');
23+
}
24+
1925
// Function to check if a file exists
2026
export async function fileExists(filePath: string) {
2127
try {
@@ -43,13 +49,14 @@ export async function getExistingBuildData(buildDataPath: string) {
4349
}
4450

4551
export async function saveBuildData(
46-
buildDataPath: string, checksum: string, version: string, runtimeArgs: string,
52+
buildDataPath: string, checksum: string, version: string, runtimeArgs: string, targetWitChecksum: string
4753
) {
4854
try {
4955
const checksumData = {
5056
version,
5157
checksum,
5258
runtimeArgs,
59+
targetWitChecksum,
5360
};
5461
await writeFile(buildDataPath, JSON.stringify(checksumData, null, 2));
5562
} catch (error) {

0 commit comments

Comments
 (0)