Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
- Implemented a check in the Next.js build function to verify if
`.env.<PROJECT-ID>` file exists and make its variables available for the build
process.
- Fix esbuild path used to bundle next.config.js on Windows (#7555)
17 changes: 15 additions & 2 deletions src/frameworks/next/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { NextConfig } from "next";
import type { PrerenderManifest } from "next/dist/build";
import type { DomainLocale } from "next/dist/server/config";
import type { PagesManifest } from "next/dist/build/webpack/plugins/pages-manifest-plugin";
import { copy, mkdirp, pathExists, pathExistsSync } from "fs-extra";
import { copy, mkdirp, pathExists, pathExistsSync, readFile } from "fs-extra";
import { pathToFileURL, parse } from "url";
import { gte } from "semver";
import { IncomingMessage, ServerResponse } from "http";
Expand Down Expand Up @@ -82,6 +82,7 @@ import {
} from "./constants";
import { getAllSiteDomains, getDeploymentDomain } from "../../hosting/api";
import { logger } from "../../logger";
import { parseStrict } from "../../functions/env";

const DEFAULT_BUILD_SCRIPT = ["next build"];
const PUBLIC_DIR = "public";
Expand Down Expand Up @@ -126,7 +127,19 @@ export async function build(
process.env.__NEXT_REACT_ROOT = "true";
}

const env = { ...process.env };
let env = { ...process.env };

// Check if the .env.<PROJECT-ID> file exists and make it available for the build process
if (context?.projectId) {
const projectEnvPath = join(dir, `.env.${context.projectId}`);

if (await pathExists(projectEnvPath)) {
const projectEnvVars = parseStrict((await readFile(projectEnvPath)).toString());

// Merge the parsed variables with the existing environment variables
env = { ...projectEnvVars, ...env };
}
}

if (context?.projectId && context?.site) {
const deploymentDomain = await getDeploymentDomain(
Expand Down
47 changes: 47 additions & 0 deletions src/functions/env.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -696,4 +696,51 @@ FOO=foo
}).to.throw("Failed to load");
});
});

describe("parseStrict", () => {
it("should parse valid environment variables", () => {
const input = `
FOO=foo
BAR="bar"
BAZ='baz'
`;
const expected = { FOO: "foo", BAR: "bar", BAZ: "baz" };
expect(env.parseStrict(input)).to.deep.equal(expected);
});

it("should throw an error for invalid lines", () => {
const input = `
FOO=foo
INVALID LINE
BAR=bar
`;
expect(() => env.parseStrict(input)).to.throw(FirebaseError, /Invalid dotenv file/);
});

it("should throw an error for reserved keys", () => {
const input = `
FIREBASE_CONFIG=config
FOO=foo
`;
expect(() => env.parseStrict(input)).to.throw(FirebaseError, /Validation failed/);
});

it("should throw an error for invalid key formats", () => {
const input = `
foo=bar
BAR=baz
`;
expect(() => env.parseStrict(input)).to.throw(FirebaseError, /Validation failed/);
});

it("should handle escape sequences correctly", () => {
const input = `
FOO="foo\\nbar"
BAR="bar\\tfoo"
BAZ="baz\\rfoo"
`;
const expected = { FOO: "foo\nbar", BAR: "bar\tfoo", BAZ: "baz\rfoo" };
expect(env.parseStrict(input)).to.deep.equal(expected);
});
});
});