Skip to content

Commit 6d455ad

Browse files
fix: Template resolve files fixes (#121)
* fix: Remove hashing of files. Not needed with template tags and aliases * fix: Show error when file write fails on template creation
1 parent 0119379 commit 6d455ad

File tree

2 files changed

+16
-37
lines changed

2 files changed

+16
-37
lines changed

src/bin/commands/build.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
handleResponse,
1919
} from "../../utils/api";
2020
import { getInferredApiKey } from "../../utils/constants";
21-
import { hashDirectory } from "../utils/hash";
21+
import { hashDirectory as getFilePaths } from "../utils/files";
2222
import { startVm } from "../../Sandboxes";
2323
import { mkdir, writeFile } from "fs/promises";
2424

@@ -121,8 +121,7 @@ export const buildCommand: yargs.CommandModule<
121121
alias = createAlias(path.resolve(argv.directory), argv.alias);
122122
}
123123

124-
const { hash, files: filePaths } = await hashDirectory(argv.directory);
125-
const tag = `sha:${hash.slice(0, 6)}`;
124+
const filePaths = await getFilePaths(argv.directory);
126125

127126
try {
128127
const templateData = handleResponse(
@@ -132,7 +131,7 @@ export const buildCommand: yargs.CommandModule<
132131
forkOf: argv.fromSandbox || getDefaultTemplateId(apiClient),
133132
title: argv.name,
134133
// We filter out sdk-templates on the dashboard
135-
tags: ["sdk-template", tag],
134+
tags: ["sdk-template"],
136135
},
137136
}),
138137
"Failed to create template"
@@ -209,10 +208,10 @@ export const buildCommand: yargs.CommandModule<
209208
updateSpinnerMessage(index, "Writing files to sandbox...")
210209
);
211210

212-
try {
213-
let i = 0;
214-
for (const filePath of filePaths) {
215-
i++;
211+
let i = 0;
212+
for (const filePath of filePaths) {
213+
i++;
214+
try {
216215
const fullPath = path.join(argv.directory, filePath);
217216
const content = await fs.readFile(fullPath);
218217
const dirname = path.dirname(filePath);
@@ -221,9 +220,11 @@ export const buildCommand: yargs.CommandModule<
221220
create: true,
222221
overwrite: true,
223222
});
223+
} catch (error) {
224+
throw new Error(
225+
`Failed to write "${filePath}" to sandbox: ${error}`
226+
);
224227
}
225-
} catch (error) {
226-
throw new Error(`Failed to write files to sandbox: ${error}`);
227228
}
228229

229230
spinner.start(updateSpinnerMessage(index, "Building sandbox..."));
Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
import { createHash } from "crypto";
21
import { existsSync, readFileSync } from "fs";
3-
import { readFile, stat, readdir } from "fs/promises";
2+
import { stat, readdir } from "fs/promises";
43
import { join, relative } from "path";
54

65
import ignore from "ignore";
76

8-
interface HashResult {
9-
hash: string;
10-
files: string[];
11-
}
12-
137
const MAX_FILES = 50_000;
148

15-
export async function hashDirectory(dirPath: string): Promise<HashResult> {
9+
export async function hashDirectory(dirPath: string): Promise<string[]> {
1610
// Initialize ignore rules from .gitignore, .dockerignore and .csbignore
1711
const ig = ignore();
1812
const ignoreFiles = [".gitignore", ".dockerignore", ".csbignore"];
@@ -24,11 +18,10 @@ export async function hashDirectory(dirPath: string): Promise<HashResult> {
2418
}
2519
});
2620

27-
// Always ignore .git folder
28-
ig.add(".git/**");
21+
// Always ignore root .git folder
22+
ig.add("/.git/");
2923

3024
const relevantFiles: string[] = [];
31-
const fileHashes: string[] = [];
3225

3326
async function processDirectory(currentPath: string) {
3427
const files = await readdir(currentPath);
@@ -51,11 +44,6 @@ export async function hashDirectory(dirPath: string): Promise<HashResult> {
5144
if (stats.isDirectory()) {
5245
await processDirectory(fullPath);
5346
} else if (stats.isFile()) {
54-
const fileContent = await readFile(fullPath);
55-
const fileHash = createHash("sha256")
56-
.update(fileContent)
57-
.digest("hex");
58-
fileHashes.push(fileHash);
5947
relevantFiles.push(relativePath);
6048
}
6149
})
@@ -64,17 +52,7 @@ export async function hashDirectory(dirPath: string): Promise<HashResult> {
6452

6553
await processDirectory(dirPath);
6654

67-
// Sort for consistent hashing
68-
fileHashes.sort();
6955
relevantFiles.sort();
7056

71-
// Create final hash from all file hashes
72-
const finalHash = createHash("sha256")
73-
.update(fileHashes.join(""))
74-
.digest("hex");
75-
76-
return {
77-
hash: finalHash,
78-
files: relevantFiles,
79-
};
57+
return relevantFiles;
8058
}

0 commit comments

Comments
 (0)