Skip to content

Skip .env.local creation when environment variables exist in process.env #193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions npm-packages/convex/src/cli/lib/deployment.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { test, expect } from "vitest";
import { changesToEnvVarFile, changesToGitIgnore } from "./deployment.js";
import { test, expect, vi, beforeEach, afterEach } from "vitest";
import { changesToEnvVarFile, changesToGitIgnore, writeDeploymentEnvVar } from "./deployment.js";
import { Context } from "../../bundler/context.js";

vi.mock("./envvars.js", async (importOriginal) => {
const actual = (await importOriginal()) as any;
return {
...actual,
gitIgnoreEnvVarFile: vi.fn().mockResolvedValue(false),
};
});

const DEPLOYMENT = {
team: "snoops",
Expand Down Expand Up @@ -83,3 +92,40 @@ test("git ignore changes", () => {
"!.env.local\n.env.local\n",
);
});

const mockContext = {
fs: {
exists: vi.fn(),
readUtf8File: vi.fn(),
writeUtf8File: vi.fn(),
},
} as unknown as Context;

const originalProcessEnv = process.env;

beforeEach(() => {
vi.clearAllMocks();
process.env = { ...originalProcessEnv };
});

afterEach(() => {
process.env = originalProcessEnv;
});

test("writeDeploymentEnvVar skips file creation when CONVEX_DEPLOYMENT exists with correct value", async () => {
process.env.CONVEX_DEPLOYMENT = "prod:tall-bar";

const result = await writeDeploymentEnvVar(
mockContext,
"prod",
DEPLOYMENT,
null,
);

expect(result).toEqual({
wroteToGitIgnore: false,
changedDeploymentEnvVar: false,
});

expect(mockContext.fs.writeUtf8File).not.toHaveBeenCalled();
});
13 changes: 11 additions & 2 deletions npm-packages/convex/src/cli/lib/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ export async function writeDeploymentEnvVar(
},
existingValue: string | null,
): Promise<{ wroteToGitIgnore: boolean; changedDeploymentEnvVar: boolean }> {
const deploymentEnvVarValue =
deploymentType + ":" + deployment.deploymentName;

// Check if the correct value already exists in process.env
if (process.env[CONVEX_DEPLOYMENT_ENV_VAR_NAME] === deploymentEnvVarValue) {
return {
wroteToGitIgnore: false,
changedDeploymentEnvVar: false,
};
}

const existingFile = ctx.fs.exists(ENV_VAR_FILE_PATH)
? ctx.fs.readUtf8File(ENV_VAR_FILE_PATH)
: null;
Expand All @@ -57,8 +68,6 @@ export async function writeDeploymentEnvVar(
deploymentType,
deployment,
);
const deploymentEnvVarValue =
deploymentType + ":" + deployment.deploymentName;

if (changedFile !== null) {
ctx.fs.writeUtf8File(ENV_VAR_FILE_PATH, changedFile);
Expand Down
66 changes: 66 additions & 0 deletions npm-packages/convex/src/cli/lib/envvars.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {afterEach, beforeEach, expect, test, vi} from "vitest";
import {writeConvexUrlToEnvFile} from "./envvars.js";
import {Context} from "../../bundler/context.js";

vi.mock("./utils/utils.js", () => ({
loadPackageJson: vi.fn().mockResolvedValue({name: "test-project"}),
ENV_VAR_FILE_PATH: ".env.local",
}));

const mockContext = {
fs: {
exists: vi.fn() as any,
readUtf8File: vi.fn() as any,
writeUtf8File: vi.fn() as any,
},
crash: vi.fn() as any,
} as unknown as Context;

const originalProcessEnv = process.env;

beforeEach(() => {
vi.clearAllMocks();
process.env = {...originalProcessEnv};
});

afterEach(() => {
process.env = originalProcessEnv;
});

test("writeConvexUrlToEnvFile process.env behavior", async () => {
// Test core functionality: skip file creation when env var exists with correct value
process.env.CONVEX_URL = "https://test.convex.cloud";
(mockContext.fs.exists as any).mockReturnValue(false);

let result = await writeConvexUrlToEnvFile(mockContext, "https://test.convex.cloud");
expect(result).toBeNull(); // Should skip file creation
expect(mockContext.fs.writeUtf8File).not.toHaveBeenCalled();

// Test different value - should create file
vi.clearAllMocks();
process.env.CONVEX_URL = "https://different.convex.cloud";

result = await writeConvexUrlToEnvFile(mockContext, "https://test.convex.cloud");
expect(result).not.toBeNull(); // Should create file
expect(mockContext.fs.writeUtf8File).toHaveBeenCalled();

// Test missing env var - should create file
vi.clearAllMocks();
delete process.env.CONVEX_URL;

result = await writeConvexUrlToEnvFile(mockContext, "https://test.convex.cloud");
expect(result).not.toBeNull(); // Should create file
expect(mockContext.fs.writeUtf8File).toHaveBeenCalled();

// Empty string should trigger file creation
vi.clearAllMocks();
process.env.CONVEX_URL = "";
result = await writeConvexUrlToEnvFile(mockContext, "https://test.convex.cloud");
expect(result).not.toBeNull();

// Whitespace should trigger file creation
vi.clearAllMocks();
process.env.CONVEX_URL = " ";
result = await writeConvexUrlToEnvFile(mockContext, "https://test.convex.cloud");
expect(result).not.toBeNull();
});
10 changes: 10 additions & 0 deletions npm-packages/convex/src/cli/lib/envvars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ export async function writeConvexUrlToEnvFile(
ctx: Context,
value: string,
): Promise<ConvexUrlWriteConfig> {
// Check if any of the expected environment variables already has the correct value
const { envVar: suggestedEnvVar } = await suggestedEnvVarName(ctx);

// Check process.env for the suggested env var or any of the expected names
for (const envVarName of [suggestedEnvVar, ...EXPECTED_NAMES]) {
if (process.env[envVarName] === value) {
return null;
}
}

const writeConfig = await envVarWriteConfig(ctx, value);

if (writeConfig === null) {
Expand Down