Skip to content

Conversation

@xterao
Copy link
Collaborator

@xterao xterao commented Jul 1, 2025

Fix an issue where generating SQL files from DAO methods in a project with multiple modules (e.g., using Gradle source set configurations) results in an IOException.

Problem

When a subproject contains multiple modules and source sets, the existing logic caches a single directory structure per module, using the module as a key.
This leads to incorrect path resolution when DAO classes exist in different source roots, causing the SQL file generator to produce invalid paths (e.g., mixing build and source paths).

Fix

Cache Update via ModuleRootListener

The module directory structure cache is updated only when IntelliJ IDEA detects changes in the directory structure, using ModuleRootListener.

Minimal Use of Cache for File Resolution

For both DAO → SQL and SQL → DAO resolution, path generation is based on the file's own source root and content root, rather than relying on cached directory mappings.

File Resolution Logic

DAO → SQL

Generate the expected SQL file path from the DAO's content root:

/META-INF/[dao package path]/[DaoName].sql

Check if the corresponding file exists within the module’s resource directories using:
ResourceFileUtil.findResourceFileInScope

If the SQL file does not yet exist, fallback to cached resource directory information.
If no resource directory is explicitly marked, use a default path.

SQL → DAO

From the SQL file path under /META-INF, extract the corresponding package name.

Use the package name and DAO file name to construct a fully qualified class name.

Resolve the class using JavaPsiFacade, and set the matching DAO file as the navigation target.

This approach ensures accurate file linkage in dynamic project structures, improves consistency, and reduces reliance on potentially outdated cached data.

@xterao xterao requested a review from Copilot July 1, 2025 06:53
@xterao xterao self-assigned this Jul 1, 2025
@github-actions github-actions bot added the fix Bug fixes label Jul 1, 2025

This comment was marked as outdated.

@xterao xterao requested a review from Copilot July 2, 2025 09:53
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 enhances SQL file generation in multi-module projects by collecting all source roots per module and matching DAO file paths against them, ensuring correct relative paths and avoiding mixed build/source paths.

  • Add a ModuleRootListener and initial cache refresh to detect module root changes.
  • Update DAO SQL path resolution to use multiple source roots and getSourceRootDir.
  • Refactor CommonPathParameterUtil to cache per-module paths (now keyed by module name) and filter out generated directories.

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/main/resources/META-INF/plugin.xml Register DomaToolsModuleRootListener as a project listener for root changes.
src/main/kotlin/.../DomaToolsModuleRootListener.kt Implement listener to refresh module path cache on rootsChanged.
src/main/kotlin/.../DomaToolStartupActivity.kt Trigger initial cache build and define registerModuleRootListener.
src/main/kotlin/.../ProjectExtensions.kt Add getSourceRootDir extension to retrieve a file’s source root.
src/main/kotlin/.../PsiDaoMethod.kt Refactor SQL file generation to use source-root-aware path resolution.
src/main/kotlin/.../DaoMethodUtil.kt Rewrite DAO lookup logic to derive class from SQL path via source roots.
src/main/kotlin/.../CommonPathParameter.kt Collect and cache all source/test/resource dirs, excluding generated dirs.
Comments suppressed due to low confidence (3)

src/main/kotlin/org/domaframework/doma/intellij/common/psi/PsiDaoMethod.kt:193

  • [nitpick] The variable name parenDirPathSpirit appears misspelled and unclear. Consider renaming to parentDirSegments or parentDirParts. Also remove or use it explicitly if needed.
            val parenDirPathSpirit = parentDir.split("/").toTypedArray()

src/main/kotlin/org/domaframework/doma/intellij/common/dao/DaoMethodUtil.kt:137

  • The KDoc above this method still describes a String return type and path logic. Update the documentation to accurately reflect that it now returns a PsiClass? and describe the new lookup behavior.
): PsiClass? {

src/main/kotlin/org/domaframework/doma/intellij/extension/ProjectExtensions.kt:21

  • [nitpick] This import is no longer used in this file and can be removed to keep the code clean.
import com.intellij.openapi.roots.ProjectRootManager

@xterao
Copy link
Collaborator Author

xterao commented Jul 3, 2025

Follow the listener registration guidelines as documented here:
IntelliJ Plugin Listeners Guide

@xterao xterao merged commit 8f04540 into main Jul 3, 2025
5 checks passed
@xterao xterao deleted the fix/generate-sql-file-path-from-source-set branch July 3, 2025 02:14
@xterao xterao added this to the 1.1.1 Release milestone Jul 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot Generate SQL File from DAO Located in a Source Directory Created via Gradle Source Set

2 participants