diff --git a/src/cli/lib/deployment.test.ts b/src/cli/lib/deployment.test.ts index 1d6bd97..656d829 100644 --- a/src/cli/lib/deployment.test.ts +++ b/src/cli/lib/deployment.test.ts @@ -52,17 +52,29 @@ test("env var changes", () => { }); test("git ignore changes", () => { + // Handle additions expect(changesToGitIgnore(null)).toEqual(".env.local\n"); - expect(changesToGitIgnore("")).toEqual("\n.env.local\n"); - expect(changesToGitIgnore(".env")).toEqual(".env\n.env.local\n"); + expect(changesToGitIgnore("# .env.local")).toEqual( + "# .env.local\n.env.local\n", + ); + // Handle existing expect(changesToGitIgnore(".env.local")).toEqual(null); expect(changesToGitIgnore(".env.*")).toEqual(null); expect(changesToGitIgnore(".env*")).toEqual(null); + expect(changesToGitIgnore(".env*.local")).toEqual(null); + expect(changesToGitIgnore("*.local")).toEqual(null); + expect(changesToGitIgnore("# convex env\n.env.local")).toEqual(null); + + // Handle Windows + expect(changesToGitIgnore(".env.local\r")).toEqual(null); + + // Handle whitespace + expect(changesToGitIgnore(" .env.local ")).toEqual(null); - // This is wonky, but will guide the user to solve the problem + // Add .env.local (even if it's negated) to guide the user to solve the problem expect(changesToGitIgnore("!.env.local")).toEqual( "!.env.local\n.env.local\n", ); diff --git a/src/cli/lib/deployment.ts b/src/cli/lib/deployment.ts index 56cd68d..a36b226 100644 --- a/src/cli/lib/deployment.ts +++ b/src/cli/lib/deployment.ts @@ -146,14 +146,23 @@ export function changesToGitIgnore(existingFile: string | null): string | null { return `${ENV_VAR_FILE_PATH}\n`; } const gitIgnoreLines = existingFile.split("\n"); - const envVarFileIgnored = gitIgnoreLines.some( - (line) => - line === ".env.local" || - line === ".env.*" || - line === ".env*" || - line === "*.local" || - line === ".env*.local", - ); + const envVarFileIgnored = gitIgnoreLines.some((line) => { + // Remove extra whitespace + const trimmedLine = line.trim(); + + // Ignore negated patterns and comments + if (trimmedLine.startsWith("!") || trimmedLine.startsWith("#")) + return false; + + const envIgnorePatterns = [ + // .env.local, .env.*, .env* + /^\.env(\.local|\.\*|\*)$/, + // .env*.local, *.local + /^(\.env)?\*\.local$/, + ]; + + return envIgnorePatterns.some((pattern) => pattern.test(trimmedLine)); + }); if (!envVarFileIgnored) { return `${existingFile}\n${ENV_VAR_FILE_PATH}\n`; } else {