Skip to content

chore: ensure imports are unused before removing#1030

Merged
almas-x merged 1 commit intomasterfrom
almas/chore
May 13, 2025
Merged

chore: ensure imports are unused before removing#1030
almas-x merged 1 commit intomasterfrom
almas/chore

Conversation

@almas-x
Copy link
Contributor

@almas-x almas-x commented May 12, 2025

📑 Description

This change adds a check to ensure that an import is actually unused before it is removed from the source file.
It prevents accidentally deleting shared imports that are still required by other packages.

For example, multiple filesystem drivers like github.com/goravel/cloudinary and github.com/goravel/minio may each import github.com/goravel/framework/contracts/filesystem. When both are installed, this import is shared. If one of them is uninstalled, we should not remove the shared import unless it is truly unused.

This fix avoids such cases by checking whether the import alias is still referenced in the AST before removing it.

✅ Checks

  • Added test cases for my code

Copilot AI review requested due to automatic review settings May 12, 2025 14:37
@almas-x almas-x requested a review from a team as a code owner May 12, 2025 14:37
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds a safeguard to remove an import only when it is confirmed to be unused, helping avoid accidental deletion of shared imports. Key changes include:

  • Addition of a new helper function (UsesImport) with accompanying test cases in utils.go and utils_test.go.
  • Updates to the RemoveImport action to conditionally skip removal if the import is still used.
  • A reordering of modifications in the package_make_command_stubs.go file to reflect the new logic.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
packages/modify/utils_test.go Added tests for the new UsesImport function.
packages/modify/utils.go Introduced the UsesImport helper function for checking import usage.
packages/modify/actions_test.go Added test cases to cover scenarios for removing imports.
packages/modify/actions.go Updated RemoveImport to conditionally remove imports based on usage.
foundation/console/package_make_command_stubs.go Reordered modification calls for providers and imports removal.

@@ -144,8 +144,8 @@ func main() {
).
Uninstall(
Copy link

Copilot AI May 12, 2025

Choose a reason for hiding this comment

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

[nitpick] The reordering of the modification calls for removing providers and imports may affect the intended side effects. Consider adding an inline comment to clarify that this new order maintains the original behavior.

Suggested change
Uninstall(
Uninstall(
// The order of operations here (removing providers before imports) is intentional
// and does not affect the original behavior. Providers are removed first to ensure
// that any dependencies on the import are handled before the import is removed.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: The order of removing references before imports is intentional and should be documented.
It helps prevent accidentally removing imports that are still in use. @hwbrzzl

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, will document it.

@codecov
Copy link

codecov bot commented May 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 70.40%. Comparing base (d23800c) to head (f343918).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1030      +/-   ##
==========================================
+ Coverage   70.35%   70.40%   +0.05%     
==========================================
  Files         176      176              
  Lines       12297    12318      +21     
==========================================
+ Hits         8651     8672      +21     
  Misses       3265     3265              
  Partials      381      381              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

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

Good catch 👍

@@ -144,8 +144,8 @@ func main() {
).
Uninstall(
Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, will document it.

@almas-x almas-x requested a review from hwbrzzl May 13, 2025 02:30
Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

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

LGTM

@almas-x almas-x merged commit 5c6bf41 into master May 13, 2025
12 checks passed
@almas-x almas-x deleted the almas/chore branch May 13, 2025 02:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants