Refactor DriverResolver to create temporary directory for driver file management#433
Refactor DriverResolver to create temporary directory for driver file management#433TharmiganK wants to merge 1 commit intomainfrom
Conversation
|
There was a problem hiding this comment.
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
DriverResolverconstructor to create a temporary directory internally instead of accepting asourcePathparameter - 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.
| if (Files.exists(tempDirectory)) { | ||
| Files.delete(tempDirectory); |
There was a problem hiding this comment.
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.
| 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()); | ||
| } | ||
| } |
There was a problem hiding this comment.
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.
| 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()); | ||
| } | ||
| } |
There was a problem hiding this comment.
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.
| public DriverResolver(String datastore) throws BalException { | ||
| try { | ||
| // Create a temporary directory along with some prefix | ||
| this.tempDirectory = Files.createTempDirectory("persist-driver-test-"); |
There was a problem hiding this comment.
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.
| this.tempDirectory = Files.createTempDirectory("persist-driver-test-"); | |
| this.tempDirectory = Files.createTempDirectory("persist-driver-"); |


Purpose
Examples
N/A
Checklist
Linked to an issueUpdated the specificationUpdated the changelogAdded tests