Skip to content
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

nx/gradle plugin not building nx graph correctly for some gradle multi-project configurations #29783

Open
1 of 4 tasks
romanbalayan opened this issue Jan 28, 2025 · 0 comments · May be fixed by #29802
Open
1 of 4 tasks
Assignees
Labels
scope: java Issues related to Java and Gradle type: bug

Comments

@romanbalayan
Copy link

romanbalayan commented Jan 28, 2025

Current Behavior

I have an pre-existing gradle multi-project having this structure:

── settings.gradle
├── buildSrc
|        └── build.gradle.kts
├── apps
│   └── project-one
|        └── build.gradle.kts
└── libs
    └── project-lib
         └── build.gradle.kts

Where build.gradle.kts is defined in the buildSrc directory. I want to keep this structure so that the build.gradle.kts configurations are centralized and not polluted on the project root directory.

With this structure, the gradle project is not included in nx project graph. Take note that i have project-report plugin configured. Running ./gradlew projectReport works and builds the projectReport.

The issue stems from packages/gradle/src/utils/get-project-report-lines.ts

// if there is no build.gradle or build.gradle.kts file, we cannot run the projectReport nor projectReportAll task
  if (
    !existsSync(join(dirname(gradlewFile), 'build.gradle')) &&
    !existsSync(join(dirname(gradlewFile), 'build.gradle.kts'))
  ) {
    logger.warn(
      `Could not find build file near ${gradlewFile}. Please run 'nx generate @nx/gradle:init' to generate the necessary tasks.`
    );
    return [];
  }

Which presumes a specific location of the build.gradle file. Adding even an empty build.gradle.kts file on the root "solves" the issue. But currently unsure if this check can be removed - since the succeeding lines would already handle gracefully if projectReport or projectReportAll task is not configured - regardless of where build.gradle file is located.

Expected Behavior

Currently expecting nx/gradle to work for any properly configured gradle project.

The current implementation seems to have a specific project structure defined to properly cross-work with gradle projects. I presumed that nx/gradle would only require having a gradle task for projectReport or projectReportAll defined - e.g. such that when running ./gradlew projectReport works, nx should be able to use this tool to build the nx graph. The current code seems to be over-specified in that the location of the build.gradle file must be on the same path of the directory where the gradlew file resides.

GitHub Repo

https://github.com/romanbalayan/gradle-demo

Steps to Reproduce

  1. Clone the demo project
  2. Run ./gradlew projectReport to demonstrate projectReport task is configured
  3. Run ./nx graph - gradle projects will not be loaded into nx graph
  4. Run touch build.gradle.kts - to build an empty build.gradle file
  5. Run ./nx graph - gradle projects - demo-app and demo-lib will now be loaded

Nx Report

Node           : 22.13.1
OS             : darwin-arm64
Native Target  : aarch64-macos
npm            : 10.9.2

nx          : 20.4.0
@nx/devkit  : 20.4.0
@nx/gradle  : 20.4.0
---------------------------------------
Registered Plugins:
@nx/gradle

Failure Logs

No failure logs, but nx graph not properly built. Gradle apps not loaded in nx context. 

Package Manager Version

No response

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

Not sure if fix is to remove path check for build.gradle.kts, or include buildSrc directory as location of gradle configs. Seems like removing the check will already work because of the succeeding try catch handling should already cover if there is no gradle task configured for projectReport or projectReportAll.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope: java Issues related to Java and Gradle type: bug
Projects
None yet
3 participants