Skip to content

Commit c0dba6c

Browse files
committed
feat: add robust path validation and error handling for file output
- Validate output directory exists and is writable before attempting manifest generation - Provide specific error messages for common failure cases (ENOENT, EACCES) - Check directory permissions early to fail fast with helpful guidance - Improve error messages to help users resolve issues quickly
1 parent 9d78c6d commit c0dba6c

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

src/bin/firebase-functions.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import * as http from "http";
2626
import * as express from "express";
2727
import * as fs from "fs/promises";
28+
import * as path from "path";
2829
import { loadStack } from "../runtime/loader";
2930
import { stackToWire } from "../runtime/manifest";
3031

@@ -57,20 +58,40 @@ function handleQuitquitquit(req: express.Request, res: express.Response, server:
5758

5859
if (process.env.FUNCTIONS_MANIFEST_OUTPUT_PATH) {
5960
void (async () => {
61+
const outputPath = process.env.FUNCTIONS_MANIFEST_OUTPUT_PATH;
6062
try {
63+
// Validate the output path
64+
const dir = path.dirname(outputPath);
65+
try {
66+
await fs.access(dir, fs.constants.W_OK);
67+
} catch (e) {
68+
console.error(
69+
`Error: Cannot write to directory '${dir}': ${e instanceof Error ? e.message : String(e)}`
70+
);
71+
console.error("Please ensure the directory exists and you have write permissions.");
72+
process.exit(1);
73+
}
74+
6175
const stack = await loadStack(functionsDir);
6276
const wireFormat = stackToWire(stack);
63-
await fs.writeFile(
64-
process.env.FUNCTIONS_MANIFEST_OUTPUT_PATH,
65-
JSON.stringify(wireFormat, null, 2)
66-
);
77+
await fs.writeFile(outputPath, JSON.stringify(wireFormat, null, 2));
6778
process.exit(0);
68-
} catch (e) {
69-
console.error(
70-
`Failed to generate manifest from function source: ${
71-
e instanceof Error ? e.message : String(e)
72-
}`
73-
);
79+
} catch (e: any) {
80+
if (e.code === "ENOENT") {
81+
console.error(`Error: Directory '${path.dirname(outputPath)}' does not exist.`);
82+
console.error("Please create the directory or specify a valid path.");
83+
} else if (e.code === "EACCES") {
84+
console.error(`Error: Permission denied writing to '${outputPath}'.`);
85+
console.error("Please check file permissions or choose a different location.");
86+
} else if (e.message?.includes("Failed to generate manifest")) {
87+
console.error(e.message);
88+
} else {
89+
console.error(
90+
`Failed to generate manifest from function source: ${
91+
e instanceof Error ? e.message : String(e)
92+
}`
93+
);
94+
}
7495
if (e instanceof Error && e.stack) {
7596
console.error(e.stack);
7697
}

0 commit comments

Comments
 (0)