Skip to content

feat: add .env.j2 template for scaffolded projects#395

Merged
davidpoblador merged 3 commits into
mainfrom
fix/scaffold-env-file
Nov 6, 2025
Merged

feat: add .env.j2 template for scaffolded projects#395
davidpoblador merged 3 commits into
mainfrom
fix/scaffold-env-file

Conversation

@davidpoblador

@davidpoblador davidpoblador commented Nov 6, 2025

Copy link
Copy Markdown
Member

Summary

  • Add .env.j2 template that generates proper .env file during scaffolding
  • Preserve _skip_if_exists for .env to avoid overwriting custom environment files
  • Include database URLs, project info, and development settings
  • Generate unique secret key based on project slug

Fixes the issue where scaffolded projects were missing .env file required by compose.dev.yml, preventing just dev from working properly.

Behavior

  • New projects: .env file is generated automatically since it doesn't exist
  • Existing projects: Custom .env files are preserved during updates
  • Template variables: Includes MongoDB/Redis URLs, project metadata, and development settings

Testing

  • Verified new projects get .env generated with correct variables
  • Confirmed _skip_if_exists preserves custom .env files during updates
  • Tested just dev can now find and load the generated .env file

- Add .env.j2 template that generates proper .env file during scaffolding
- Remove .env from _skip_if_exists to allow generation
- Include database URLs, project info, and development settings
- Generate unique secret key based on project slug
@claude

claude Bot commented Nov 6, 2025

Copy link
Copy Markdown

Claude finished @davidpoblador's task —— View job


Code Review Complete ✅

Summary

This PR successfully addresses the missing .env file issue in scaffolded projects. The implementation is well-structured and follows good practices for template generation.

Strengths

Problem Solving:

  • Correctly identifies and fixes the core issue: compose.dev.yml requires .env file (line 25) but scaffolding was skipping it
  • Clean solution: Remove .env from _skip_if_exists and provide a comprehensive template

Template Quality (copier-template/.env.j2):

  • Well-organized with clear section headers and comments
  • Comprehensive coverage of all necessary environment variables
  • Proper conditional logic for optional features (Redis when enable_job_queue is enabled)
  • Good use of Jinja2 templating with appropriate variable substitution

Development Workflow Integration:

  • Correctly integrates with existing just dev command (copier-template/justfile:14-19)
  • Maintains consistency with project's Docker Compose setup
  • Follows the project's environment variable naming conventions

⚠️ Security Concerns

Secret Key Generation (Line 14):

SECRET_KEY=ct-!secret-must-change-me-{{ project_slug | replace('-', '') }}-dev

Issue: The secret key generation is predictable and not cryptographically secure:

  • Uses a static prefix ct-!secret-must-change-me-
  • Only appends project slug (user-controlled input)
  • No random component

Recommendation: Use a more secure approach:

SECRET_KEY={{ ('ct-' + range(1, 99999999) | random | string + '-' + project_slug | replace('-', '') + '-dev-' + range(1, 99999999) | random | string) }}

Or better yet, integrate with Python's secrets module during scaffolding.

Database URL (Line 6):

DATABASE_URL={{ mongodb_url }}{{ project_slug }}

Consider validating that mongodb_url ends with / to avoid malformed URLs.

📋 Minor Improvements

Documentation:

  • Consider adding a comment explaining that SECRET_KEY should be changed in production
  • The comment "only if job queue is enabled" could be more descriptive

Consistency:

  • .gitignore addition of test_projects/ is good for keeping test artifacts clean

🧪 Test Coverage Assessment

Current Testing:

  • Manual testing mentioned in PR description ✅
  • Test scaffold command exists (just test-scaffold in justfile:50-56) ✅

Recommendations:

  • Add integration test that verifies .env file is generated during scaffolding
  • Test that just dev command succeeds with generated .env file
  • Validate all template variables are properly substituted

🏆 Code Quality & Best Practices

  • ✅ Follows conventional commit format in PR title
  • ✅ Clean, readable Jinja2 template structure
  • ✅ Proper separation of concerns (template vs configuration)
  • ✅ Good use of conditional logic for optional features
  • ✅ Maintains existing project patterns and conventions

📊 Performance Considerations

No performance concerns - template generation is a one-time operation during scaffolding.

🎯 Overall Assessment

Approve with minor security enhancement recommended. This PR effectively solves the stated problem and integrates well with the existing codebase. The main concern is the predictable secret key generation, which should be addressed before production use.

Files reviewed:

  • copier-template/.env.j2: Well-structured template with security consideration needed
  • copier.yml: Clean removal of skip condition
  • .gitignore: Appropriate addition for test artifacts

- Add back _skip_if_exists for .env to avoid overwriting custom environment files
- New projects still get .env generated since it doesn't exist initially
- Existing projects with custom .env files are preserved during updates
@claude

claude Bot commented Nov 6, 2025

Copy link
Copy Markdown

Claude finished @davidpoblador's task —— View job


Code Review Complete ✅

Reviewing PR #395: feat: add .env.j2 template for scaffolded projects

Todo List:

  • Read repository guidelines (CLAUDE.md)
  • Analyze .env.j2 template file
  • Review .gitignore changes
  • Check for security concerns
  • Assess code quality and best practices
  • Evaluate potential bugs or issues
  • Consider performance implications
  • Assess test coverage needs
  • Provide comprehensive feedback

Overall Assessment

This PR successfully addresses the scaffolding issue where projects were missing the required .env file for just dev to work. The implementation is clean and follows good practices.

📋 Detailed Review

1. Code Quality & Best Practices

  • .env.j2 template (copier-template/.env.j2:1-36): Well-structured with clear sections and helpful comments
  • Jinja2 syntax: Properly uses conditional blocks for optional features (Redis when enable_job_queue is enabled)
  • Variable naming: Consistent with existing copier template variables
  • Template organization: Logical grouping of related environment variables

2. Security Analysis ⚠️ Minor Concern

  • Secret key generation (copier-template/.env.j2:14):
    SECRET_KEY=ct-!secret-must-change-me-{{ project_slug | replace('-', '') }}-dev
    
    • ✅ Good: Uses project slug for uniqueness
    • ⚠️ Concern: Predictable pattern could be a security risk in production
    • 💡 Recommendation: Consider adding a random component or clearer warning that this must be changed for production

3. Technical Implementation

  • Database URL construction (copier-template/.env.j2:6): Correctly appends project slug to MongoDB URL
  • Conditional logic (copier-template/.env.j2:9-11, 34-36): Properly handles optional features
  • Development settings (copier-template/.env.j2:30-31): Appropriate defaults for development environment

4. Potential Issues ⚠️ Important

  • copier.yml configuration: Based on git history, there seems to be confusion about _skip_if_exists for .env
    • The PR description says ".env from _skip_if_exists to allow generation"
    • But current copier.yml:3 still has .env in _skip_if_exists
    • This would prevent the template from generating .env files
    • 🔍 Need clarification: Should .env be removed from _skip_if_exists or is this intentional?

5. Integration Analysis

  • compose.dev.yml dependency (copier-template/compose.dev.yml:25): Correctly references the .env file
  • Variable coverage: Template includes all necessary variables for the development environment
  • Scaffolding workflow: Should resolve the "just dev" execution issues mentioned in the PR

6. Performance Considerations

  • Minimal performance impact (static template file)
  • Template generation is fast and efficient

7. Test Coverage ℹ️ Suggestion

  • Consider adding integration tests to verify:
    • .env file is generated correctly during scaffolding
    • just dev works with generated environment file
    • All template variables are properly substituted

🎯 Recommendations

  1. High Priority: Clarify the _skip_if_exists configuration for .env
  2. Medium Priority: Enhance secret key generation security
  3. Low Priority: Add integration tests for scaffolding workflow

💡 Additional Notes

  • PR title follows conventional commit format correctly: feat:
  • .gitignore changes are minimal and appropriate ✅
  • Template follows existing project patterns ✅

@davidpoblador davidpoblador merged commit da335b5 into main Nov 6, 2025
2 checks passed
@davidpoblador davidpoblador deleted the fix/scaffold-env-file branch November 6, 2025 12:33
davidpoblador pushed a commit that referenced this pull request Nov 8, 2025
🤖 I have created a release *beep* *boop*
---


##
[2.16.0](v2.15.2...v2.16.0)
(2025-11-08)


### Features

* add .env.j2 template for scaffolded projects
([#395](#395))
([da335b5](da335b5))
* add mongo and redis services to compose.dev.yml
([#397](#397))
([9c299f9](9c299f9))
* integrate docs publishing into unified publish workflow
([#401](#401))
([e615b43](e615b43))


### Code Refactoring

* simplify template structure and improve organization
([#398](#398))
([16f1b27](16f1b27))


### Miscellaneous Chores

* **deps:** update mongo docker tag to v8.2
([#400](#400))
([f3c9f46](f3c9f46))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant