Skip to content

Comments

Refactor DriverResolver to create temporary directory for driver file management#433

Closed
TharmiganK wants to merge 1 commit intomainfrom
fix-driver-file
Closed

Refactor DriverResolver to create temporary directory for driver file management#433
TharmiganK wants to merge 1 commit intomainfrom
fix-driver-file

Conversation

@TharmiganK
Copy link
Contributor

Purpose

$Subject

Examples

N/A

Checklist

  • Linked to an issue
  • Updated the specification
  • Updated the changelog
  • Added tests

@TharmiganK TharmiganK requested review from Copilot and removed request for daneshk, lnash94 and shafreenAnfar December 2, 2025 10:51
@sonarqubecloud
Copy link

sonarqubecloud bot commented Dec 2, 2025

Quality Gate Failed Quality Gate failed

Failed conditions
1 Security Hotspot

See analysis details on SonarQube Cloud

Copy link

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 refactors the DriverResolver class to manage temporary directories internally rather than relying on a user-provided source path. The constructor now creates a temporary directory automatically and stores the driver file within it, simplifying the API and making the class more self-contained.

Key Changes:

  • Modified DriverResolver constructor to create a temporary directory internally instead of accepting a sourcePath parameter
  • Updated cleanup logic to handle both driver file and temporary directory deletion
  • Simplified writeOutputFile() method signature by using instance variable instead of parameter

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
persist-core/src/main/java/io/ballerina/persist/nodegenerator/DriverResolver.java Refactored to create and manage temporary directory internally; updated constructor, writeOutputFile method, and deleteDriverFile cleanup logic
persist-core/src/main/java/io/ballerina/persist/introspect/Introspector.java Updated DriverResolver instantiation calls to use new constructor signature without sourcePath parameter

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

Comment on lines +74 to +75
if (Files.exists(tempDirectory)) {
Files.delete(tempDirectory);
Copy link

Copilot AI Dec 2, 2025

Choose a reason for hiding this comment

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

The Files.delete(tempDirectory) call will fail if the directory is not empty. Files.delete() only works for empty directories or files. Since buildDriverFile() calls SingleFileProject.load() which may create additional cache or build artifacts in the temp directory, the deletion may fail. Consider checking if the directory is empty first, or using a recursive deletion approach to ensure all contents are removed before deleting the directory itself.

Copilot uses AI. Check for mistakes.
Comment on lines +38 to 48
public DriverResolver(String datastore) throws BalException {
try {
// Create a temporary directory along with some prefix
this.tempDirectory = Files.createTempDirectory("persist-driver-test-");
// Set the driver file path in the temp directory
this.driverImportFile = this.tempDirectory.resolve("driver.bal");
this.datastore = datastore;
} catch (IOException e) {
throw new BalException("failed to create temporary directory: " + e.getMessage());
}
}
Copy link

Copilot AI Dec 2, 2025

Choose a reason for hiding this comment

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

The refactored constructor introduces new behavior (temporary directory creation and error handling) that should be tested. Consider adding unit tests to verify: 1) successful temp directory creation, 2) proper error handling when directory creation fails, 3) cleanup behavior in deleteDriverFile() including edge cases where the directory is not empty or deletion fails.

Copilot uses AI. Check for mistakes.
Comment on lines +38 to 48
public DriverResolver(String datastore) throws BalException {
try {
// Create a temporary directory along with some prefix
this.tempDirectory = Files.createTempDirectory("persist-driver-test-");
// Set the driver file path in the temp directory
this.driverImportFile = this.tempDirectory.resolve("driver.bal");
this.datastore = datastore;
} catch (IOException e) {
throw new BalException("failed to create temporary directory: " + e.getMessage());
}
}
Copy link

Copilot AI Dec 2, 2025

Choose a reason for hiding this comment

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

Consider implementing AutoCloseable for DriverResolver to ensure proper cleanup of the temporary directory. The current design relies on the caller to explicitly invoke deleteDriverFile(), which could lead to resource leaks if an exception occurs before cleanup or if callers forget to call the cleanup method. Using try-with-resources would provide more robust resource management.

Copilot uses AI. Check for mistakes.
public DriverResolver(String datastore) throws BalException {
try {
// Create a temporary directory along with some prefix
this.tempDirectory = Files.createTempDirectory("persist-driver-test-");
Copy link

Copilot AI Dec 2, 2025

Choose a reason for hiding this comment

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

The prefix "persist-driver-test-" suggests this is for testing purposes, but this class appears to be production code. Consider using a more appropriate prefix like "persist-driver-" instead.

Suggested change
this.tempDirectory = Files.createTempDirectory("persist-driver-test-");
this.tempDirectory = Files.createTempDirectory("persist-driver-");

Copilot uses AI. Check for mistakes.
@TharmiganK TharmiganK closed this Dec 2, 2025
@TharmiganK TharmiganK deleted the fix-driver-file branch December 2, 2025 11:22
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.

1 participant