-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Copy lock file only when needed #2823
base: main
Are you sure you want to change the base?
Conversation
|
@denis256 I had some trailing whitespaces. Fixed in the latest commit by running terragrunt/.circleci/config.yml Line 105 in e5081e1
|
util/file.go
Outdated
return errors.WithStackTrace(destReadErr) | ||
} | ||
|
||
if string(sourceContents) == string(destinationContents) { |
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.
util/file.go:613:5: stringXbytes: suggestion: bytes.Equal(sourceContents, destinationContents) (gocritic)
if string(sourceContents) == string(destinationContents) {
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.
Resolved in b3b9287
Failed integration tests:
|
Will be also helpful to add an integration test which will track that the lock copy works as expected. |
9fa1844
to
889aca5
Compare
I added some unit tests. Integration tests seem a lot more involved and I'm not super familiar with Go. Are unit tests sufficient? |
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. |
cc @denis256 |
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. |
WalkthroughThis pull request refines the Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant CopyLockFile
participant FileSystem
participant Logger
Caller->>CopyLockFile: Call CopyLockFile(src, dest)
CopyLockFile->>FileSystem: Compute absolute paths for src & dest
alt Paths are identical
CopyLockFile->>Logger: Log identical path message
CopyLockFile-->>Caller: Return early
else
CopyLockFile->>FileSystem: Check source file existence
alt Source file not found
CopyLockFile->>Logger: Log missing source
CopyLockFile-->>Caller: Return early
else
CopyLockFile->>FileSystem: Read source file
CopyLockFile->>FileSystem: Check destination file existence
alt Destination file not found
CopyLockFile->>Logger: Log action to copy file
CopyLockFile->>FileSystem: Copy file preserving permissions
else
CopyLockFile->>FileSystem: Read destination file
alt Contents are identical
CopyLockFile->>Logger: Log no copy needed
CopyLockFile-->>Caller: Return early
else
CopyLockFile->>FileSystem: Copy file preserving permissions
end
end
end
end
Poem
✨ Finishing Touches
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. 🪧 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
CodeRabbit Configuration File (
|
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 (2)
util/file_test.go (1)
5-6
: Consider usingos
package instead ofioutil
.
Althoughioutil
still works, it’s encouraged in modern Go to useos.ReadFile
andos.WriteFile
directly for new code. This helps ensure consistency and avoids reliance on soon-to-be fully deprecated APIs.Here’s a sample diff showing how you might replace
ioutil
calls:-import ( - "io/ioutil" - ... -) +import ( + "os" + ... +) -err := ioutil.WriteFile(sourceLockFilePath, []byte("lock file contents"), 0644) +err := os.WriteFile(sourceLockFilePath, []byte("lock file contents"), 0644) -contents, err := ioutil.ReadFile(destinationLockFilePath) +contents, err := os.ReadFile(destinationLockFilePath)util/file.go (1)
588-629
: Consider concurrency safety if multiple processes run at the same time.
If there’s a chance of parallel execution, we could add locking or concurrency checks. That might prevent partial overwrites or race conditions.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
util/file.go
(2 hunks)util/file_test.go
(3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: Pull Request has non-contributor approval
🔇 Additional comments (3)
util/file_test.go (1)
344-454
: Great coverage of lock file copying scenarios.
All your sub-tests nicely verify key conditions like missing files, unchanged contents, etc. This thorough approach helps ensure reliability.util/file.go (2)
4-4
: Thanks for switching tobytes.Equal
.
Comparing byte slices directly is more idiomatic and efficient than converting to strings.
588-629
: Solid implementation aligned with the PR objective.
TheCopyLockFile
function checks path equality, file existence, and content differences before copying. This avoids unnecessary writes, which matches Terraform’s approach and resolves read-only filesystem concerns.
Description
Terragrunt copies lock files into the working directory every time Terraform is initialized. This is inconsistent with the Terraform behaviour where the lock file is only written to when changed.
This is important when the working directory is on a readonly filesystem. I work with an organization that does this to prevent unintentional code changes during provisioning.
This PR makes it so that copying the lock file is avoided when the file has no changes.
TODOs
Read the Gruntwork contribution guidelines.
Release Notes (draft)
Added / Removed / Updated [X].
Updated the lock file copy mechanism to avoid copying when the lock file is unchanged. Useful for using a readonly working directory.
Summary by CodeRabbit
Refactor
Tests