mojo-dotenv Project Plan

January 12, 2026 ยท View on GitHub

A modern .env file parser and loader for Mojo ๐Ÿ”ฅ, compatible with Mojo 2025/2026.

๐Ÿ“‹ Project Overview

Goal: Create a production-ready dotenv library for Mojo that loads environment variables from .env files, following the 12-factor app methodology.

Inspiration: This project is inspired by mojoenv by itsdevcoffee. However, mojoenv was built for Mojo in 2023 and is no longer compatible with modern Mojo. This is a complete rewrite using current Mojo patterns and stdlib.

๐ŸŽฏ Goals

  1. Compatibility: Work with latest Mojo (2025/2026)
  2. Correctness: Validate against python-dotenv behaviour
  3. Simplicity: Clean API, easy to use
  4. Educational: Well-documented, good learning resource
  5. Production-ready: Proper error handling, edge cases covered

๐Ÿ“ฆ Project Structure

mojo-dotenv/
โ”œโ”€โ”€ src/
โ”‚   โ””โ”€โ”€ dotenv/
โ”‚       โ”œโ”€โ”€ __init__.mojo          # Public API
โ”‚       โ”œโ”€โ”€ parser.mojo            # Core parsing logic
โ”‚       โ”œโ”€โ”€ loader.mojo            # Environment loading
โ”‚       โ””โ”€โ”€ errors.mojo            # Error types
โ”œโ”€โ”€ tests/
โ”‚   โ”œโ”€โ”€ test_basic.mojo            # Basic functionality tests
โ”‚   โ”œโ”€โ”€ test_quotes.mojo           # Quote handling tests
โ”‚   โ”œโ”€โ”€ test_comments.mojo         # Comment handling tests
โ”‚   โ”œโ”€โ”€ test_python_compat.mojo    # Validate vs python-dotenv
โ”‚   โ””โ”€โ”€ fixtures/
โ”‚       โ”œโ”€โ”€ basic.env
โ”‚       โ”œโ”€โ”€ quotes.env
โ”‚       โ”œโ”€โ”€ comments.env
โ”‚       โ”œโ”€โ”€ multiline.env
โ”‚       โ””โ”€โ”€ expansion.env
โ”œโ”€โ”€ examples/
โ”‚   โ”œโ”€โ”€ simple.mojo                # Basic usage
โ”‚   โ”œโ”€โ”€ custom_path.mojo           # Custom .env path
โ”‚   โ””โ”€โ”€ no_override.mojo           # Don't override existing vars
โ”œโ”€โ”€ docs/
โ”‚   โ”œโ”€โ”€ API.md                     # API documentation
โ”‚   โ”œโ”€โ”€ FEATURES.md                # Feature support matrix
โ”‚   โ””โ”€โ”€ COMPARISON.md              # vs python-dotenv, mojoenv
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ PLAN.md                        # This file
โ”œโ”€โ”€ LICENSE                        # Apache 2.0
โ”œโ”€โ”€ .gitignore
โ””โ”€โ”€ pixi.toml                      # Package management

๐Ÿš€ Development Phases

Phase 0: Research & Validation (CURRENT)

Tasks:

  • Research existing solutions (mojoenv)
  • Clone and test mojoenv compatibility
  • Document Mojo package management state
  • Review python-dotenv API for reference
  • Set up development environment

Deliverables:

  • This PLAN.md document
  • Validation results from mojoenv test
  • Package management documentation

Phase 1: MVP Implementation (v0.1.0)

Features:

  • Parse simple KEY=value pairs
  • Handle comments (lines starting with #)
  • Handle basic quotes ("value", 'value')
  • Strip leading/trailing whitespace
  • Load into os.environ
  • Return Dict[String, String]

API:

from dotenv import load_dotenv, dotenv_values

# Load into environment
load_dotenv()  # Load from .env
load_dotenv(".env.local")  # Custom path
load_dotenv(override=False)  # Don't override existing vars

# Get as dict without loading
var config = dotenv_values(".env")
var db_url = config["DATABASE_URL"]

Tasks:

  • Implement parser.mojo - basic parsing
  • Implement loader.mojo - environment loading
  • Implement __init__.mojo - public API
  • Create basic tests
  • Create examples
  • Write README

Success Criteria:

  • Parses simple .env files correctly
  • Loads into environment successfully
  • Tests pass
  • Examples run without errors

Phase 2: Python Compatibility Testing (v0.1.1)

Tasks:

  • Set up python-dotenv for comparison
  • Create comprehensive test fixtures
  • Implement compatibility tests
  • Document differences
  • Fix any discovered bugs

Deliverables:

  • test_python_compat.mojo
  • Test fixtures that work in both
  • COMPARISON.md documenting differences

Phase 3: Advanced Features (v0.2.0)

Features:

  • Variable expansion: PATH=${HOME}/bin
  • Multiline values (with \ continuation)
  • Basic escape sequences: \n, \t, \\, \"
  • Multiple .env files (.env, .env.local, etc.)
  • Export prefix support: export KEY=value

Tasks:

  • Implement variable expansion
  • Implement multiline support
  • Implement escape sequences
  • Add multi-file loading
  • Update tests
  • Update documentation

Phase 4: Production Hardening (v0.3.0)

Features:

  • Comprehensive error handling
  • Validation and warnings
  • Performance optimization
  • Memory safety audit
  • Security review

Tasks:

  • Add detailed error messages
  • Implement validation
  • Profile and optimize
  • Security audit
  • Edge case testing
  • Fuzzing tests

Phase 5: Distribution (v1.0.0)

Tasks:

  • Package as .mojopkg
  • Publish to Mojo package registry (when available)
  • Create release notes
  • Announce on Discord/forums
  • Write blog post/tutorial

๐Ÿ“Š Feature Support Matrix

Featurev0.1.0v0.2.0v0.3.0python-dotenvNotes
KEY=valueโœ…โœ…โœ…โœ…Basic parsing
Comments (#)โœ…โœ…โœ…โœ…Ignore comment lines
Quotes (", ')โœ…โœ…โœ…โœ…Strip quotes
Whitespace strippingโœ…โœ…โœ…โœ…Trim spaces
Empty linesโœ…โœ…โœ…โœ…Ignore
export KEY=valueโŒโœ…โœ…โœ…Optional prefix
Variable expansionโŒโœ…โœ…โŒ${VAR} syntax
Multiline valuesโŒโœ…โœ…โœ…With \ continuation
Escape sequencesโŒโœ…โœ…โœ…\n, \t, etc.
Inline commentsโŒโŒโœ…โœ…KEY=value # comment
Command substitutionโŒโŒโŒโŒUse dotenvx instead

๐Ÿงช Testing Strategy

Unit Tests

  • Parser tests: Each parsing rule independently
  • Loader tests: Environment manipulation
  • Error handling: Invalid input

Integration Tests

  • End-to-end: Load actual .env files
  • Python compatibility: Compare with python-dotenv
  • Edge cases: Malformed files, special characters

Test Fixtures

Create .env files covering:

  • Basic key=value pairs
  • All quote styles
  • Comments (inline and full-line)
  • Empty lines and whitespace
  • Special characters
  • Unicode
  • Very long lines
  • Malformed syntax

๐Ÿ“ API Design

Public API (__init__.mojo)

fn load_dotenv(
    dotenv_path: String = ".env",
    override: Bool = True,
) raises -> Dict[String, String]:
    """
    Load .env file into environment.
    
    Args:
        dotenv_path: Path to .env file (default: ".env")
        override: Override existing environment variables (default: True)
    
    Returns:
        Dictionary of loaded variables
    
    Raises:
        Error: If file cannot be read or parsed
    """

fn dotenv_values(
    dotenv_path: String = ".env",
) raises -> Dict[String, String]:
    """
    Parse .env file without loading into environment.
    
    Args:
        dotenv_path: Path to .env file
    
    Returns:
        Dictionary of parsed variables
    
    Raises:
        Error: If file cannot be read or parsed
    """

fn find_dotenv(
    filename: String = ".env",
    raise_error_if_not_found: Bool = False,
) raises -> String:
    """
    Find .env file by searching parent directories.
    
    Args:
        filename: Name of .env file to find
        raise_error_if_not_found: Raise error if not found
    
    Returns:
        Path to found .env file, or empty string
    
    Raises:
        Error: If raise_error_if_not_found and file not found
    """

Internal API

parser.mojo:

fn parse_line(line: String) -> Optional[Tuple[String, String]]:
    """Parse a single line, returning (key, value) or None."""

fn strip_quotes(value: String) -> String:
    """Remove surrounding quotes from value."""

fn expand_variables(value: String, env: Dict[String, String]) -> String:
    """Expand ${VAR} references in value."""

loader.mojo:

fn set_env_var(key: String, value: String, override: Bool):
    """Set environment variable if appropriate."""

fn load_env_file(path: String, override: Bool) -> Dict[String, String]:
    """Load and parse .env file."""

๐ŸŽ“ Learning Goals

This project teaches:

  1. File I/O - Reading and parsing text files
  2. String manipulation - Parsing, splitting, trimming
  3. Collections - Dict usage, iteration
  4. Error handling - raises, custom errors
  5. OS interaction - Environment variables
  6. Testing - Unit and integration tests
  7. Python interop - Using python-dotenv for validation
  8. Packaging - Creating distributable Mojo packages

๐Ÿ“š Resources

Mojo Documentation

Reference Implementations

Specifications

๐Ÿค Collaboration

This project is open source (Apache 2.0). Contributions welcome!

Ways to Contribute

  • Report bugs or request features
  • Improve documentation
  • Add test cases
  • Implement features
  • Review code

Acknowledgments

  • itsdevcoffee - Original mojoenv author, pioneered .env in Mojo
  • Modular - For creating Mojo
  • python-dotenv - Reference implementation

๐Ÿ—“๏ธ Timeline Estimate

  • Phase 0 (Research): 1-2 days
  • Phase 1 (MVP): 3-5 days
  • Phase 2 (Testing): 2-3 days
  • Phase 3 (Advanced): 5-7 days
  • Phase 4 (Hardening): 3-5 days
  • Phase 5 (Distribution): 2-3 days

Total: ~3-4 weeks of focused work

๐Ÿ“ˆ Success Metrics

  • Compiles with latest Mojo
  • Passes all tests
  • 90%+ compatibility with python-dotenv (basic features)
  • Used by at least 3 other projects
  • Documented and easy to understand
  • Performance: Parse 1000 lines in <10ms

๐Ÿšฆ Current Status

Phase: 0 - Research & Validation Next Steps:

  1. Test mojoenv compatibility
  2. Document Mojo package management
  3. Set up development environment
  4. Begin Phase 1 implementation

Last Updated: 2026-01-05 Maintainer: @mjboothaus