Skip to content

Gradle init script injection causes "Could not get unknown property 'com'" during project import (Gradle 9.2.0, Groovy 4) #1735

@Missdrop

Description

@Missdrop

Environment

  • Extension: Gradle for Java (vscjava.vscode-gradle) 3.17.1 (server plugin bundled in extension)
  • OS: macOS 15.5 (x86_64)
  • Gradle: 9.2.0
  • Groovy: 4.0.28
  • Java: OpenJDK 21.0.9 (Zulu)
  • Project: simple multi-project Java build with rootProject.name = 'CPT111-Group76' and subproject app

Summary

When the vscjava.vscode-gradle extension injects its init.gradle (which adds plugin-0.4.0.jar to the initscript classpath and then applies com.microsoft.java.bs.gradle.plugin.GradleBuildServerPlugin), Gradle fails during initialization with:

Could not get unknown property 'com' for root project 'CPT111-Group76' of type org.gradle.api.Project.

This prevents the Java Language Server from importing the workspace. Disabling the extension makes the project import successfully.

Reproduction steps

  1. Open the Java project in VS Code with vscjava.vscode-gradle enabled.
  2. The extension injects an init script located at <extension>/server/plugins/init.gradle with (simplified):
initscript {
  dependencies {
    classpath files('plugin-0.4.0.jar')
  }
}
allprojects {
  apply plugin: com.microsoft.java.bs.gradle.plugin.GradleBuildServerPlugin
}
  1. The VS Code Java Language Server / Gradle tooling client fails to import build targets and logs the MissingPropertyException.

Observed logs

  • Excerpt from the language server / extension logs:
[Error] Initialization failed
java.util.concurrent.CompletionException: ... Could not get unknown property 'com' for root project 'CPT111-Group76' of type org.gradle.api.Project.
Caused by: groovy.lang.MissingPropertyException: Could not get unknown property 'com' for root project 'CPT111-Group76' of type org.gradle.api.Project.
  at init_...(/.../init.gradle:8)
  • Running from terminal with the extension init script reproduces the same error:
./gradlew --init-script /.../init.gradle tasks --stacktrace
FAILURE: Build failed with an exception.
* Where:
Initialization script '/.../init.gradle' line: 8
* What went wrong:
Could not get unknown property 'com' for root project 'CPT111-Group76' of type org.gradle.api.Project.

Notes about attempts to workaround

  • I tried creating a plugin-0.4.0.jar file in the project root (and also a symlink pointing to the extension's plugin-0.4.0.jar) so that initscript { classpath files('plugin-0.4.0.jar') } can find the jar. The jar is present and contains com.microsoft.java.bs.gradle.plugin.GradleBuildServerPlugin, but the init script still throws the MissingPropertyException when evaluating the apply plugin: com.microsoft... line. It appears Groovy may parse the bare class reference before the class is available in the classloader, causing com to be treated as an unknown property of the Project object.

Why this is likely a classloading/parse-time issue

  • The error indicates Groovy tried to resolve com as a property on the Project (MissingPropertyException) instead of recognizing it as a package/class reference, which suggests the plugin class wasn't resolvable at that parse time or that the script evaluation order makes the bare reference unsafe.
  • Different Gradle/Groovy versions can change script parsing/classloading timing. In my environment (Gradle 9.2.0, Groovy 4.0.28) the injected script fails to apply the plugin using the bare class reference.

Suggested fixes / mitigation options

  1. Use safe plugin application in init.gradle:
    • After adding classpath, attempt to load the class via classloader and apply via project.plugins.apply(pluginClass), wrapped in try/catch. Example:
initscript {
  dependencies {
    classpath files('plugin-0.4.0.jar')
  }
}
allprojects {
  try {
    def pluginClass = this.class.classLoader.loadClass('com.microsoft.java.bs.gradle.plugin.GradleBuildServerPlugin')
    project.getPlugins().apply(pluginClass)
  } catch (Throwable t) {
    // Log and continue without failing the build initialization
  }
}
  1. If the plugin is or can be published with a plugin id, prefer applying by id (string) instead of a bare class reference:
apply plugin: 'com.microsoft.java.bs.gradle.plugin' // if published as plugin id
  1. Add defensive checks in the injected init script to avoid throwing during project configuration time; if a class can't be loaded, skip applying the plugin and log a clear warning.

  2. Document the compatibility matrix for injected init scripts vs Gradle/Groovy versions and provide a toggle for users to opt-out of init script injection.

Request

  • Please consider changing the injected init.gradle to apply the plugin in a way that won't throw MissingPropertyException during Gradle initialization (see suggested code above).
  • If you need further logs, I can attach the full client.log and the terminal stacktrace output.

Local environment summary (collected):

  • Gradle 9.2.0 (Launcher JVM: 21.0.9)
  • Groovy 4.0.28
  • Java: OpenJDK 21.0.9 (Zulu)
  • OS: macOS 15.5 x86_64
  • Extension version tested: vscjava.vscode-gradle 3.17.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions