-
-
Notifications
You must be signed in to change notification settings - Fork 105
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
Conversation
…atmos into feat/DEV-1856_relative-paths
📝 WalkthroughWalkthroughThe pull request introduces a new function Changes
Assessment against linked issues
Possibly related PRs
Suggested reviewers
📜 Recent review detailsConfiguration used: .coderabbit.yaml 📒 Files selected for processing (3)
✅ Files skipped from review due to trivial changes (3)
⏰ Context from checks skipped due to timeout of 90000ms (3)
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
Documentation and Community
|
There was a problem hiding this 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 logicThe 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
📒 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.
There was a problem hiding this 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
📒 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:
- Direct string imports with relative paths
- StackImport structs with relative paths
This implementation successfully fulfills the PR's objective of supporting relative path imports.
Also applies to: 1805-1805
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
There was a problem hiding this 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 documentationThe 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
📒 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:
- Add protection against directory traversal attacks
- Use filepath.IsAbs() as suggested in previous review
- 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.
@milldr please resolve the conflicts |
There was a problem hiding this 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
📒 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
These changes were released in v1.143.0. |
what
./
or../
or
why
references
Summary by CodeRabbit
Release Notes
New Features
Bug Fixes
Chores
.gitignore
Tests