Skip to content

Fix inconsistent handling of working dir and include processing for relative paths. #4566

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 2 commits into
base: main
Choose a base branch
from

Conversation

pseudomorph
Copy link
Contributor

@pseudomorph pseudomorph commented Jul 25, 2025

Description

Fixes #4565

Ensure consistent behavior between absolute and relative paths for working dir and include processing.

TODOs

Read the Gruntwork contribution guidelines.

  • I authored this code entirely myself
  • I am submitting code based on open source software (e.g. MIT, MPL-2.0, Apache)]
  • I am adding or upgrading a dependency or adapted code and confirm it has a compatible open source license
  • Update the docs.
  • Run the relevant tests successfully, including pre-commit checks.
  • Include release notes. If this PR is backward incompatible, include a migration guide.

Release Notes (draft)

Fixed relative --working-dir path handling causing filepath.Rel errors in find command.

Migration Guide

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of relative working directory paths to prevent errors when using the find command with relative paths.
  • Tests

    • Added integration tests to verify correct behavior of the find command when specifying the working directory as a relative path.

Copy link

vercel bot commented Jul 25, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
terragrunt-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 25, 2025 5:11pm

@pseudomorph
Copy link
Contributor Author

Should we merge the integration test from #4492 here as well?

@pseudomorph pseudomorph changed the title Working dir relpath Fix inconsistent handling of working dir and include processing for relative paths. Jul 25, 2025
Copy link
Contributor

coderabbitai bot commented Jul 25, 2025

📝 Walkthrough

Walkthrough

The changes normalize the handling of working directory paths in both CLI flag parsing and internal configuration logic by converting them to absolute, slash-separated paths. An integration test is added to verify that the terragrunt find command works correctly with relative working directories, addressing a previously reported bug.

Changes

File(s) Change Summary
cli/flags/global/flags.go Added a setter to the "working-dir" CLI flag to normalize paths to absolute and slash-separated before assignment.
config/include.go Modified include path handling to ensure all include paths are converted to absolute paths if not already.
test/integration_find_relative_working_dir_test.go Added a new integration test to verify correct behavior of terragrunt find with relative working directories.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CLI
    participant Flags
    participant Config
    participant FileSystem

    User->>CLI: Run 'terragrunt find --working-dir ./deploy-2'
    CLI->>Flags: Parse and set working-dir flag
    Flags->>FileSystem: Convert to absolute, normalize slashes
    Flags->>Config: Pass normalized working-dir
    Config->>FileSystem: Resolve include paths to absolute
    Config->>CLI: Return resolved configuration
    CLI->>User: Output find results
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 19aea07 and 7321671.

📒 Files selected for processing (3)
  • cli/flags/global/flags.go (2 hunks)
  • config/include.go (1 hunks)
  • test/integration_find_relative_working_dir_test.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.go

⚙️ CodeRabbit Configuration File

Review the Go code for quality and correctness. Make sure that the Go code follows best practices, is performant, and is easy to understand and maintain.

Files:

  • config/include.go
  • cli/flags/global/flags.go
  • test/integration_find_relative_working_dir_test.go
🔇 Additional comments (7)
cli/flags/global/flags.go (2)

7-7: LGTM! Required import for path normalization functionality.

The path/filepath import is correctly added to support the new path manipulation functions used in the working directory flag setter.


153-167: Excellent implementation! This effectively resolves the filepath.Rel errors.

The custom setter properly normalizes working directory paths by:

  • Converting relative paths to absolute paths using filepath.Abs()
  • Ensuring consistent path separators with filepath.ToSlash()
  • Including proper error handling for the conversion process

This addresses the core issue where relative --working-dir paths were causing errors in the find command.

config/include.go (1)

770-773: Consistent path normalization! This complements the CLI flag changes perfectly.

The include path normalization ensures consistency by converting relative include paths to absolute paths using the already-normalized working directory. This works in tandem with the CLI flag setter to provide comprehensive path normalization throughout the application.

test/integration_find_relative_working_dir_test.go (4)

15-33: Well-structured test setup with clear documentation.

The test properly:

  • Documents the specific issue being validated
  • Uses parallel execution for efficiency
  • Creates appropriate temporary directory structure
  • Implements proper cleanup to avoid test pollution

The setup effectively mirrors the bug report scenario.


34-66: Realistic test configuration that reproduces the bug scenario.

The test creates authentic terragrunt configurations:

  • Parent configuration file with includes
  • Dependency relationship using find_in_parent_folders() - this is crucial for reproducing the original filepath.Rel error
  • Complete setup with both terragrunt.hcl and main.tf files
  • Proper file permissions

This effectively reproduces the conditions that caused the original issue.


68-82: Comprehensive test cases covering different relative path formats.

The test cases appropriately cover:

  • Relative paths with dot-slash prefix (./path)
  • Relative paths without prefix (path)

This ensures the fix works for the various ways users naturally specify relative working directories.


84-123: Thorough validation ensures the fix works correctly.

The test validation effectively:

  • Executes the exact problematic command from the bug report
  • Confirms no filepath.Rel errors occur (primary fix validation)
  • Validates JSON output structure and content
  • Verifies expected modules and dependencies are found
  • Uses descriptive assertion messages for clear failure diagnosis

This comprehensive approach ensures the fix resolves the issue while maintaining expected functionality.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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.

Find breaks on dependency processing when using relative working dir
1 participant