Skip to content

Refactor: Extract formatting utilities to internal/lib#1620

Merged
james00012 merged 3 commits intomainfrom
feature/modularize-terragrunt
Nov 18, 2025
Merged

Refactor: Extract formatting utilities to internal/lib#1620
james00012 merged 3 commits intomainfrom
feature/modularize-terragrunt

Conversation

@james00012
Copy link
Copy Markdown
Contributor

Overview

This PR extracts shared formatting utilities from modules/terraform to a new internal/lib module. This is the first step toward modularizing Terratest while eliminating an unwanted dependency from terragrunt to terraform.

Problem

Currently, modules/terragrunt imports modules/terraform just to use two formatting helper functions:

  • FormatTerraformBackendConfigAsArgs()
  • FormatTerraformPluginDirAsArgs()

This creates an unwanted dependency that pulls in heavyweight HCL parsing libraries even for users who only need terragrunt functionality.

Solution

Extract the shared formatting logic to internal/lib/formatting:

  • These functions are internal implementation details, not public API
  • Both terraform and terragrunt modules now use the shared internal library
  • No breaking changes - all public APIs remain unchanged

Changes

New files:

  • internal/lib/go.mod - New internal module
  • internal/lib/formatting/args.go - Extracted formatting functions

Modified files:

  • modules/terraform/format.go - Delegates to internal/lib
  • modules/terragrunt/init.go - Uses internal/lib directly (no more terraform import)
  • go.mod - Added replace directive for local development

Testing

  • ✅ All modules build successfully
  • ✅ Existing tests pass
  • ✅ No functional changes to behavior

Next Steps

This refactoring prepares the codebase for full modularization (follow-up PR), where we will create independent go.mod files for each module to enable users to import only what they need.

Related

Addresses the dependency concerns raised in #1392, #1492, and avoids the mistakes from the reverted PR #1608.

@james00012 james00012 force-pushed the feature/modularize-terragrunt branch from 63ce890 to 3b7d133 Compare November 16, 2025 23:57
Extract shared formatting functions from modules/terraform to internal/lib/formatting
to eliminate dependency from terragrunt to terraform module.

Changes:
- Create internal/lib/formatting/format.go with extracted functions:
  - FormatBackendConfigAsArgs() - formats backend config as CLI args
  - FormatPluginDirAsArgs() - formats plugin directory path
  - All supporting HCL formatting helpers (toHclString, etc.)
- Add comprehensive tests in internal/lib/formatting/format_test.go
- Update modules/terraform/format.go to delegate to internal/lib
- Update modules/terragrunt/init.go to use internal/lib directly
- Remove duplicate tests from modules/terraform/format_test.go

Benefits:
- Breaks terragrunt -> terraform dependency
- Eliminates heavyweight terraform dependencies for terragrunt users
- Establishes internal/lib pattern for shared utilities
- No breaking changes - all public APIs remain unchanged

Impact:
- terragrunt no longer imports terraform module
- Reduces dependency graph for terragrunt-only users
- All tests passing, no functional changes
@james00012 james00012 force-pushed the feature/modularize-terragrunt branch from 6744be8 to f3c8628 Compare November 17, 2025 00:20
@james00012 james00012 merged commit d09e510 into main Nov 18, 2025
2 of 3 checks passed
@james00012 james00012 deleted the feature/modularize-terragrunt branch November 18, 2025 23:36
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.

2 participants