Skip to content
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

feat: Support Relative Path Imports #891

Merged
merged 48 commits into from
Jan 9, 2025
Merged

Conversation

milldr
Copy link
Member

@milldr milldr commented Dec 24, 2024

what

  • Add support for relative paths in imports, when the path starts with ./ or ../
  • Support all path types:
import:
  - ./relative_path
  - ../relative_path

or

import:
  - path: ./relative_path
  - path: ../relative_path

why

  • Allow less path duplication and support simpler code

references

Summary by CodeRabbit

Release Notes

  • New Features

    • Enhanced path resolution for stack imports, improving handling of relative paths in configuration files
  • Bug Fixes

    • Improved import path resolution to ensure correct referencing of default configurations
  • Chores

    • Updated example configuration files to use more consistent relative path imports
    • Added new cache file pattern to .gitignore
  • Tests

    • Added new test case for validating relative path processing in stack configurations

@mergify mergify bot added the triage Needs triage label Dec 24, 2024
@milldr milldr added minor New features that do not break anything and removed triage Needs triage labels Dec 26, 2024
@milldr milldr marked this pull request as ready for review December 26, 2024 19:45
@milldr milldr requested a review from a team as a code owner December 26, 2024 19:45
Copy link
Contributor

coderabbitai bot commented Dec 26, 2024

📝 Walkthrough

Walkthrough

The pull request introduces a new function resolveRelativePath in the stack processor utilities to support relative path imports in YAML configuration files. This enhancement allows developers to use more concise and context-aware import paths, resolving imports relative to the current file's directory. The changes impact path resolution logic, test cases, and example configuration files to demonstrate the new relative path import functionality.

Changes

File Change Summary
internal/exec/stack_processor_utils.go Added resolveRelativePath function to handle relative path resolution
pkg/stack/stack_processor_test.go Added new test case TestStackProcessorRelativePaths to validate relative path imports
.gitignore Added **/cache.*.txt pattern to ignore cache files
examples/demo-context/stacks/deploy/*/demo.yaml Updated import paths from deploy/_defaults to ../_defaults

Assessment against linked issues

Objective Addressed Explanation
Support relative path imports
Resolve imports relative to current file's directory
Simplify configuration file imports

Possibly related PRs

Suggested reviewers

  • kevcube
  • aknysh
  • osterman

📜 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 dc897c7 and fb6ac32.

📒 Files selected for processing (3)
  • examples/demo-context/stacks/deploy/dev/demo.yaml (1 hunks)
  • examples/demo-context/stacks/deploy/prod/demo.yaml (1 hunks)
  • examples/demo-context/stacks/deploy/staging/demo.yaml (1 hunks)
✅ Files skipped from review due to trivial changes (3)
  • examples/demo-context/stacks/deploy/staging/demo.yaml
  • examples/demo-context/stacks/deploy/prod/demo.yaml
  • examples/demo-context/stacks/deploy/dev/demo.yaml
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Build (windows-latest, windows)
  • GitHub Check: Analyze (go)
  • GitHub Check: Summary

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.

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. (Beta)
  • @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 or @auto-summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @auto-title 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.

coderabbitai[bot]
coderabbitai bot previously approved these changes Dec 26, 2024
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

🧹 Nitpick comments (1)
internal/exec/stack_processor_utils.go (1)

1798-1804: Consider extracting duplicated relative path handling logic

The relative path handling logic is duplicated between StackImport structs and string imports. Consider extracting this into a helper function to improve maintainability.

+ // resolveRelativePath resolves a path relative to the given base file path
+ func resolveRelativePath(path string, filePath string) string {
+     if strings.HasPrefix(path, "./") || strings.HasPrefix(path, "../") {
+         baseDir := filepath.Dir(filePath)
+         path = filepath.Join(baseDir, path)
+         path = filepath.Clean(path)
+     }
+     return path
+ }

 func ProcessImportSection(stackMap map[string]any, filePath string) ([]schema.StackImport, error) {
     // ... existing code ...
     
     for _, imp := range importsList {
         // ... existing code ...
         
         if err == nil {
-            if strings.HasPrefix(importObj.Path, "./") || strings.HasPrefix(importObj.Path, "../") {
-                baseDir := filepath.Dir(filePath)
-                importObj.Path = filepath.Join(baseDir, importObj.Path)
-                importObj.Path = filepath.Clean(importObj.Path)
-            }
+            importObj.Path = resolveRelativePath(importObj.Path, filePath)
             result = append(result, importObj)
             continue
         }
         
         // ... existing code ...
         
-        if strings.HasPrefix(s, "./") || strings.HasPrefix(s, "../") {
-            baseDir := filepath.Dir(filePath)
-            s = filepath.Join(baseDir, s)
-            s = filepath.Clean(s)
-        }
+        s = resolveRelativePath(s, filePath)
         
         result = append(result, schema.StackImport{Path: s})
     }
     
     return result, nil
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 4f6170c and 8d76059.

📒 Files selected for processing (1)
  • internal/exec/stack_processor_utils.go (3 hunks)
🔇 Additional comments (2)
internal/exec/stack_processor_utils.go (2)

1747-1750: Documentation looks good!

The function documentation clearly describes all supported import path types, including the new relative path support.


1776-1784: Implementation follows OS-agnostic path handling best practices!

The relative path handling correctly uses filepath.Join and filepath.Clean for OS-agnostic path manipulation.

coderabbitai[bot]
coderabbitai bot previously approved these changes Dec 27, 2024
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: 1

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 8d76059 and 1beeb8a.

📒 Files selected for processing (1)
  • internal/exec/stack_processor_utils.go (3 hunks)
🔇 Additional comments (1)
internal/exec/stack_processor_utils.go (1)

1791-1791: LGTM! Implementation aligns with PR objectives

The usage of resolveRelativePath correctly handles both import formats:

  1. Direct string imports with relative paths
  2. StackImport structs with relative paths

This implementation successfully fulfills the PR's objective of supporting relative path imports.

Also applies to: 1805-1805

internal/exec/stack_processor_utils.go Outdated Show resolved Hide resolved
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
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

🧹 Nitpick comments (1)
internal/exec/stack_processor_utils.go (1)

1776-1779: Enhance import section documentation

The comments describing the import section types are good but could be more detailed.

Add examples for each type of import:

 // The `import` section can contain:
-// 1. Project-relative paths (e.g. "mixins/region/us-east-2")
-// 2. Paths relative to the current stack file (e.g. "./_defaults")
-// 3. StackImport structs containing either of the above path types (e.g. "path: mixins/region/us-east-2")
+// 1. Project-relative paths from repository root:
+//    import:
+//      - mixins/region/us-east-2
+// 
+// 2. Paths relative to the current stack file:
+//    import:
+//      - ./_defaults
+//      - ../common/base
+// 
+// 3. StackImport structs with additional options:
+//    import:
+//      - path: mixins/region/us-east-2
+//        skip_templates_processing: true
+//      - path: ./_defaults
+//        skip_if_missing: true
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 1beeb8a and dd55c0f.

📒 Files selected for processing (1)
  • internal/exec/stack_processor_utils.go (3 hunks)
🔇 Additional comments (2)
internal/exec/stack_processor_utils.go (2)

1805-1806: LGTM: Relative path resolution implementation

The integration of resolveRelativePath in the ProcessImportSection function is clean and maintains backward compatibility.

Also applies to: 1819-1820


1746-1773: 🛠️ Refactor suggestion

Enhance security and cross-platform compatibility of path resolution

The path resolution implementation needs improvements in several areas:

  1. Add protection against directory traversal attacks
  2. Use filepath.IsAbs() as suggested in previous review
  3. Ensure consistent path separator handling across platforms

Apply this diff to improve the implementation:

 func resolveRelativePath(path string, currentFilePath string) string {
     if path == "" {
         return path
     }
     
-    // Normalize input path
-    path = filepath.FromSlash(path)
-    currentFilePath = filepath.FromSlash(currentFilePath)
+    // Check if path is absolute
+    if filepath.IsAbs(path) {
+        return path
+    }
+    
+    // Normalize paths for consistent handling
+    normalizedPath := filepath.FromSlash(path)
+    normalizedCurrentPath := filepath.FromSlash(currentFilePath)

-    // Get the first element of the path
-    firstElement := strings.Split(path, string(filepath.Separator))[0]
+    // Get the first element safely
+    parts := strings.Split(normalizedPath, string(filepath.Separator))
+    if len(parts) == 0 {
+        return path
+    }
+    firstElement := parts[0]

     // Check if the path starts with "." or ".."
     if firstElement == "." || firstElement == ".." {
-        baseDir := filepath.Dir(currentFilePath)
+        baseDir := filepath.Dir(normalizedCurrentPath)
+        
         // Clean the path and convert to forward slashes
-        result := filepath.ToSlash(filepath.Clean(filepath.Join(baseDir, path)))
+        joined := filepath.Join(baseDir, normalizedPath)
+        cleaned := filepath.Clean(joined)
+        result := filepath.ToSlash(cleaned)
         
         // Ensure the resolved path is still under the base directory
-        if !strings.HasPrefix(result, filepath.ToSlash(filepath.Clean(baseDir))) {
-            return path
+        cleanedBase := filepath.ToSlash(filepath.Clean(baseDir))
+        if !strings.HasPrefix(result, cleanedBase) {
+            // Path traversal attempt detected
+            return path
         }
         return result
     }
     return path
 }

Likely invalid or redundant comment.

coderabbitai[bot]
coderabbitai bot previously approved these changes Dec 27, 2024
@aknysh
Copy link
Member

aknysh commented Jan 7, 2025

@milldr please resolve the conflicts

tests/test_cases.yaml Outdated Show resolved Hide resolved
@mergify mergify bot removed the conflict This PR has conflicts label Jan 9, 2025
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: 2

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 29b390d and 187e61b.

📒 Files selected for processing (4)
  • .gitignore (1 hunks)
  • internal/exec/stack_processor_utils.go (4 hunks)
  • pkg/stack/stack_processor_test.go (1 hunks)
  • tests/fixtures/scenarios/complete/stacks/mixins/stage/test2.yaml (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • .gitignore
  • tests/fixtures/scenarios/complete/stacks/mixins/stage/test2.yaml
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/stack/stack_processor_test.go
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Build (windows-latest, windows)
  • GitHub Check: Summary

internal/exec/stack_processor_utils.go Show resolved Hide resolved
internal/exec/stack_processor_utils.go Show resolved Hide resolved
coderabbitai[bot]
coderabbitai bot previously approved these changes Jan 9, 2025
@milldr milldr merged commit b13a2fd into main Jan 9, 2025
44 checks passed
@milldr milldr deleted the feat/DEV-1856_relative-paths branch January 9, 2025 20:32
Copy link

github-actions bot commented Jan 9, 2025

These changes were released in v1.143.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
minor New features that do not break anything
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Feature Request: Relative Path Imports
3 participants