Skip to content

Commit 5d210a7

Browse files
authored
Merge pull request #371 from karthik2804/fix_build_caching
Fix build caching
2 parents e5fb3cc + d9f3522 commit 5d210a7

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

packages/build-tools/src/build.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { readFile } from 'node:fs/promises';
12
import {
23
calculateChecksum,
34
fileExists,
@@ -9,15 +10,16 @@ export function getBuildDataPath(src: string): string {
910
}
1011

1112
export async function ShouldComponentize(
12-
src: string, outputPath: string, componentizeVersion: string, runtimeArgs: string,
13+
src: string, outputPath: string, componentizeVersion: string, runtimeArgs: string, targetWitChecksum: string
1314
) {
14-
const sourceChecksum = await calculateChecksum(src);
15+
const sourceChecksum = await calculateChecksum(await readFile(src));
1516
const existingBuildData = await getExistingBuildData(getBuildDataPath(src));
1617

1718
if (
1819
existingBuildData?.version == componentizeVersion &&
1920
existingBuildData?.checksum === sourceChecksum &&
2021
existingBuildData?.runtimeArgs === runtimeArgs &&
22+
existingBuildData?.targetWitChecksum === targetWitChecksum &&
2123
(await fileExists(outputPath))
2224
) {
2325
return false;

packages/build-tools/src/index.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@ async function main() {
2323
let outputPath = CliArgs.output;
2424
let runtimeArgs = CliArgs.debug ? '--enable-script-debugging' : '';
2525

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-
3526
// generate wit world string
3627
let wasiDeps = getPackagesWithWasiDeps(process.cwd(), new Set(), true);
3728
let { witPaths, targetWorlds } = processWasiDeps(wasiDeps);
@@ -63,6 +54,16 @@ async function main() {
6354
'combined-wit:[email protected]',
6455
);
6556

57+
let inlineWitChecksum = await calculateChecksum(inlineWit);
58+
// Small optimization to skip componentization if the source file hasn't changed
59+
if (!(await ShouldComponentize(src, outputPath, componentizeVersion, runtimeArgs, inlineWitChecksum))) {
60+
console.log(
61+
'No changes detected in source file and target World. Skipping componentization.',
62+
);
63+
return;
64+
}
65+
console.log('Componentizing...');
66+
6667
const source = await readFile(src, 'utf8');
6768
const precompiledSource = precompile(source, src, true) as string;
6869

@@ -85,9 +86,10 @@ async function main() {
8586
// Save the checksum of the input file along with the componentize version
8687
await saveBuildData(
8788
getBuildDataPath(src),
88-
await calculateChecksum(src),
89+
await calculateChecksum(await readFile(src)),
8990
componentizeVersion,
9091
runtimeArgs,
92+
inlineWitChecksum,
9193
);
9294

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

packages/build-tools/src/utils.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ 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 calculateChecksum(content: string | Buffer) {
88
try {
9-
const fileBuffer = await readFile(filePath);
109
const hash = createHash('sha256');
11-
hash.update(fileBuffer);
10+
hash.update(content);
1211
return hash.digest('hex');
1312
} catch (error) {
14-
console.error(`Error calculating checksum for file ${filePath}:`, error);
13+
console.error(`Error calculating checksum:`, error);
1514
throw error;
1615
}
1716
}
1817

18+
1919
// Function to check if a file exists
2020
export async function fileExists(filePath: string) {
2121
try {
@@ -43,13 +43,14 @@ export async function getExistingBuildData(buildDataPath: string) {
4343
}
4444

4545
export async function saveBuildData(
46-
buildDataPath: string, checksum: string, version: string, runtimeArgs: string,
46+
buildDataPath: string, checksum: string, version: string, runtimeArgs: string, targetWitChecksum: string
4747
) {
4848
try {
4949
const checksumData = {
5050
version,
5151
checksum,
5252
runtimeArgs,
53+
targetWitChecksum,
5354
};
5455
await writeFile(buildDataPath, JSON.stringify(checksumData, null, 2));
5556
} catch (error) {

0 commit comments

Comments
 (0)