Skip to content

follow symlinks for path_relative_to_include #1550

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

Conversation

Patricol
Copy link

@Patricol Patricol commented Feb 13, 2021

Closes #543

My use case involves wanting to prefix terragrunt dirs with numbers to make the stage order obvious, generate state keys based on path to ensure uniqueness, and use a fair number of terraform_remote_state objects.

Reordering or inserting stages is a time-consuming process involving moving many different state files, adjusting terraform_remote_state objects, and renaming folders.

Also, cding isn't as frictionless as it could be; with the extra often 0-padded prefixes.

Creating numbered symlinks to the terragrunt dirs (but having terragrunt ignore those symlinks) would be an ideal solution.

Things to consider:

  • I think path_relative_to_include should take a boolean argument if possible; defaulting to the old behavior but allowing the use of this behavior.
  • Should this be done for path_relative_from_include too?
  • Haven't yet tested. Especially if not implementing the bool toggle; need to see how this behaves when a relative include path is valid from one but not both of the real and symlinked directories.
  • Is it alright that this runs filepath.Clean on the path too?

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of symbolic links in configuration paths to ensure more accurate path resolution.

@Patricol
Copy link
Author

@brikis98 @yorinasub17

Happy to work on this more if I get confirmation that the 'help wanted' tag on #543 is still accurate. (Maybe after >2 years the change is no longer desired?)

@brikis98
Copy link
Member

@Patricol Sorry for the delay. We are super, super buried right now. The PR is very much appreciated and we'll review it as soon as we can!

@brikis98
Copy link
Member

Thanks for the PR and your patience as we got around to reviewing it!

Things to consider:

  • I think path_relative_to_include should take a boolean argument if possible; defaulting to the old behavior but allowing the use of this behavior.

Unless you know of a specific use case where the current approach would break as a result, I'd rather avoid extra flags / branches.

  • Should this be done for path_relative_from_include too?

Yes, probably.

  • Is it alright that this runs filepath.Clean on the path too?

Sure.

Note that for symlinks to work, we may also need to solve #1611.

Copy link

github-actions bot commented May 4, 2025

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for submitting this pull request.

@github-actions github-actions bot added the stale Stale label May 4, 2025
Copy link
Contributor

coderabbitai bot commented May 4, 2025

Walkthrough

This change updates the pathRelativeToInclude function in config_helpers.go to use filepath.EvalSymlinks on the currentPath variable. Now, before any further processing, the function ensures that the path is resolved to its canonical form, following any symlinks. No other logic or error handling is changed, and the function's signature remains the same.

Changes

File(s) Change Summary
config/config_helpers.go Added filepath.EvalSymlinks to resolve symlinks for currentPath in pathRelativeToInclude.

Assessment against linked issues

Objective Addressed Explanation
Ensure path_relative_to_include() resolves symlinks consistently (#543)

Poem

Symlinks once led us astray,
Paths twisted in a subtle ballet.
Now with a tweak, the routes are clear,
Canonical journeys, no need to fear!
Through helpers and links, our configs unite—
Terragrunt’s path-finding shines ever bright.
🗺️✨

✨ Finishing Touches
  • 📝 Generate Docstrings

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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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 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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🔭 Outside diff range comments (3)
config/config_helpers.go (3)

302-310: ⚠️ Potential issue

Good addition, but needs error handling!

Adding filepath.EvalSymlinks helps follow symlinks when determining paths, which is great for the use case of numbered symlinks to Terragrunt directories. However, this function can return an error that's not being handled. If the symlink is broken or can't be resolved, this will likely panic at runtime.

Consider handling the error by modifying the code like this:

 currentPath := filepath.Dir(terragruntOptions.TerragruntConfigPath)
-currentPath = filepath.EvalSymlinks(currentPath)
+resolvedPath, err := filepath.EvalSymlinks(currentPath)
+if err != nil {
+    return "", errors.WithStackTrace(err)
+}
+currentPath = resolvedPath

313-327: 🛠️ Refactor suggestion

Update pathRelativeFromInclude for consistency

For complete symlink support, pathRelativeFromInclude should also evaluate symlinks like we're doing in pathRelativeToInclude. According to the PR discussion, brikis98 agreed this function should likely receive similar treatment.

Add symlink evaluation to this function too:

 includePath := filepath.Dir(include.Path)
 currentPath := filepath.Dir(terragruntOptions.TerragruntConfigPath)
+
+resolvedPath, err := filepath.EvalSymlinks(currentPath)
+if err != nil {
+    return "", errors.WithStackTrace(err)
+}
+currentPath = resolvedPath

302-310: 💡 Verification agent

🧩 Analysis chain

Need to add tests for symlink behavior

The PR mentioned that testing wasn't completed yet, particularly for relative include paths. Adding tests would be valuable to ensure the symlink behavior works as expected in different scenarios.


🏁 Script executed:

#!/bin/bash
# Look for existing tests related to the pathRelativeToInclude function
echo "Searching for existing tests for pathRelativeToInclude..."
rg -A 3 "pathRelativeToInclude" --type go

Length of output: 1584


🏁 Script executed:

#!/bin/bash
# Check if existing tests simulate or reference symlinks
echo "Searching for 'Symlink' or 'symlink' in config_helpers tests..."
rg -n "Symlink" -A5 config/config_helpers_test.go || rg -n "symlink" -A5 config/config_helpers_test.go || echo "No symlink references found in tests."

Length of output: 333


Add symlink-specific tests for pathRelativeToInclude

We’ve got good coverage for normal include paths, but there are no tests hitting the EvalSymlinks branch. Let’s add a few cases in config/config_helpers_test.go to exercise include and config paths that go through symlinks:

• Create a temp dir, make a real folder (realDir), then a symlink (linkDir) pointing at it.
• Point terragruntOptions.TerragruntConfigPath at linkDir/config.hcl and an include.Path inside that symlink.
• Also test an absolute include.Path that resolves via a symlink.
• Assert that the returned relative path matches what you get by manually calling filepath.EvalSymlinks + GetPathRelativeTo.

That way we catch any regressions around EvalSymlinks behavior. Thanks!

🧹 Nitpick comments (1)
config/config_helpers.go (1)

304-304: Consider also evaluating the includePath

For complete symlink resolution, it might make sense to also resolve symlinks in the includePath, not just the currentPath. This would ensure that relative path calculations work correctly regardless of which path contains symlinks.

 includePath := filepath.Dir(include.Path)
 currentPath := filepath.Dir(terragruntOptions.TerragruntConfigPath)
 currentPath = filepath.EvalSymlinks(currentPath)
+
+resolvedIncludePath, err := filepath.EvalSymlinks(includePath)
+if err != nil {
+    return "", errors.WithStackTrace(err)
+}
+includePath = resolvedIncludePath
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b4f39d2 and d21474d.

📒 Files selected for processing (1)
  • config/config_helpers.go (1 hunks)

@github-actions github-actions bot removed the stale Stale label May 6, 2025
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.

path_relative_to_include and symlinks
2 participants