Skip to content

Fix .env.local additions in .gitignore #21

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
18 changes: 15 additions & 3 deletions src/cli/lib/deployment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
Expand Down
25 changes: 17 additions & 8 deletions src/cli/lib/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down