Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,18 @@ private boolean populateWithSavedContainer(IJavaProject project, ContainerResolu
case IClasspathEntry.CPE_PROJECT: {
// projects need to be resolved properly so we have all the output folders and exported jars on the classpath
var sourceProject = workspaceRoot.getProject(e.getPath().segment(0));

// Check for cycles BEFORE calling beginResolvingProject to avoid depth counter issues
if (resolutionContext.processedProjects.contains(sourceProject)) {
if (LOG.isDebugEnabled()) {
LOG.debug(
"Skipping already processed project '{}' in thread '{}' to avoid cycle",
sourceProject.getName(),
Thread.currentThread().getName());
}
break;
}

if (resolutionContext.beginResolvingProjectIfNeverProcessedBefore(sourceProject)) {
try {
// only resolve and add the projects if it was never attempted before
Expand All @@ -252,10 +264,9 @@ private boolean populateWithSavedContainer(IJavaProject project, ContainerResolu
resolutionContext.endResolvingProject(sourceProject);
}
} else if (LOG.isDebugEnabled()) {
// this should not happen in theory because Bazel is an acyclic graph as well as Eclipse doesn't like it but who knows...
LOG.debug(
"Skipping recursive resolution attempt for project '{}' in thread '{}' ({})",
sourceProject,
"Skipping already processed project '{}' in thread '{}' to avoid cycle",
sourceProject.getName(),
Thread.currentThread().getName());
}
break;
Expand All @@ -275,9 +286,74 @@ private boolean populateWithSavedContainer(IJavaProject project, ContainerResolu
}
}

// Add the project's own output folders to the runtime classpath
// This ensures that Eclipse-compiled classes (including test classes) are available at runtime
addProjectOutputFolders(project, resolutionContext);
Copy link
Member

Choose a reason for hiding this comment

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

org.eclipse.jdt.launching.JavaRuntime.resolveRuntimeClasspathEntry(IRuntimeClasspathEntry, IJavaProject, boolean) would add the output folders alread. I don't think we should be doing this.

I wonder if the error reported in #46 is caused by missing test dependencies.

Copy link
Author

Choose a reason for hiding this comment

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

When I comment out this line of code, I encounter the following problem.

java.lang.NoClassDefFoundError: com/projectName/demo_app/DemoServiceHandler
 at com.projectName.demo_app.DemoServiceHandlerTest.testBarCopiesFieldsToResponses([DemoServiceHandlerTest.java:22](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html))
Caused by: java.lang.ClassNotFoundException: com.projectName.demo_app.DemoServiceHandler
 at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass([BuiltinClassLoader.java:581](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html))
 at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass([ClassLoaders.java:178](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html))
 at java.base/java.lang.ClassLoader.loadClass([ClassLoader.java:527](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html))
 ... 27 more


// Note: Test framework dependencies (like JUnit) are now pre-cached in the container during
Copy link
Member

Choose a reason for hiding this comment

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

This is weird. Why wouldn't they come from regular Bazel dependencies?

https://github.com/eclipseguru/bazel-eclipse/blob/main/docs/common/classpath.md#basics-classpath

Copy link
Author

Choose a reason for hiding this comment

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

  1. Eclipse compiles to its own output folders:
  • Main classes → eclipse-bin
  • Test classes → eclipse-testbin
  1. Bazel compiles to different locations:
  • Bazel outputs go to bazel-bin, bazel-out, etc.
  1. The runtime classpath is being built from Bazel's dependency graph (via the saved container), but this graph only knows about:
  • JAR dependencies
  • Other project dependencies
  • Bazel-compiled outputs
  1. The project's OWN Eclipse-compiled classes are NOT in Bazel's dependency graph because:
  • You don't declare a dependency on yourself in Bazel
  • Eclipse's incremental compilation outputs are IDE-specific, not part of Bazel's build model

// the container build phase (see BazelClasspathManager.saveAndSetContainer), so we no longer
// need to resolve them at runtime. This significantly improves performance for large projects.

return true;
}

/**
* Checks if the given project is a test project based on naming conventions.
*
* Note: This method is kept for potential future use, but test framework dependencies are now handled during
* container creation rather than runtime resolution.
*
* @param project
* the project to check
* @return <code>true</code> if this appears to be a test project, <code>false</code> otherwise
*/
@SuppressWarnings("unused")
private boolean isTestProject(IJavaProject project) {
var projectName = project.getProject().getName();
// Check if project name contains "test" or ends with "-test"
return projectName.contains("test") || projectName.contains("Test");
}

/**
* Adds the project's own output folders to the runtime classpath. This includes both the regular output folder
* (eclipse-bin) and the test output folder (eclipse-testbin).
*
* @param project
* the project whose output folders should be added
* @param resolutionContext
* the resolution context
* @throws CoreException
*/
private void addProjectOutputFolders(IJavaProject project, ContainerResolutionContext resolutionContext)
throws CoreException {
// Add the project's default output location (main compiled classes)
var defaultOutputLocation = project.getOutputLocation();
if (defaultOutputLocation != null) {
var outputEntry = JavaRuntime.newArchiveRuntimeClasspathEntry(defaultOutputLocation);
outputEntry.setClasspathProperty(IRuntimeClasspathEntry.USER_CLASSES);
resolutionContext.add(outputEntry);
if (LOG.isDebugEnabled()) {
LOG.debug("Added default output location to runtime classpath: {}", defaultOutputLocation);
}
}

// For Bazel projects, also add the test output folder explicitly
var bazelProject = BazelCore.create(project.getProject());
if (bazelProject != null) {
var fileSystemMapper = bazelProject.getBazelWorkspace().getBazelProjectFileSystemMapper();
var testOutputFolder = fileSystemMapper.getOutputFolderForTests(bazelProject);
// Test output folder might be different from the default output location
if (!testOutputFolder.getFullPath().equals(defaultOutputLocation)) {
var testOutputEntry = JavaRuntime.newArchiveRuntimeClasspathEntry(testOutputFolder.getFullPath());
testOutputEntry.setClasspathProperty(IRuntimeClasspathEntry.USER_CLASSES);
resolutionContext.add(testOutputEntry);
if (LOG.isDebugEnabled()) {
LOG.debug("Added test output folder to runtime classpath: {}", testOutputFolder.getFullPath());
}
}
}
}

@Override
public IRuntimeClasspathEntry[] resolveRuntimeClasspathEntry(IRuntimeClasspathEntry entry, IJavaProject project)
throws CoreException {
Expand All @@ -300,15 +376,21 @@ public IRuntimeClasspathEntry[] resolveRuntimeClasspathEntry(IRuntimeClasspathEn
// this method can be entered recursively; luckily only within the same thread
// therefore we use a ThreadLocal LinkedHashSet to keep track of recursive attempts
var resolutionContext = currentThreadResolutionContet.get();

// CRITICAL: Check if this is top-level BEFORE calling beginResolvingProject
// This ensures ThreadLocal cleanup happens correctly
var isTopLevelResolution = resolutionContext.currentDepth == 0;

// Check for recursive resolution BEFORE calling beginResolvingProject
// to avoid depth counter mismatch
if (!resolutionContext.beginResolvingProjectIfNeverProcessedBefore(project.getProject())) {
LOG.warn(
"Detected recursive resolution attempt for project '{}' in thread '{}' ({})",
"Detected recursive resolution attempt for project '{}' in thread '{}' - skipping to avoid cycle",
project.getProject().getName(),
Thread.currentThread().getName());
return new IRuntimeClasspathEntry[0];
}

var isTopLevelResolution = resolutionContext.currentDepth == 0;
var stopWatch = StopWatch.startNewStopWatch();
try {
// try the saved container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ TargetProvisioningStrategy getTargetProvisioningStrategy(BazelWorkspace bazelWor
* @param monitor
* @throws CoreException
*/
public void patchClasspathContainer(BazelProject bazelProject, CompileAndRuntimeClasspath classpath, IProgressMonitor progress)
throws CoreException {
public void patchClasspathContainer(BazelProject bazelProject, CompileAndRuntimeClasspath classpath,
IProgressMonitor progress) throws CoreException {
var monitor = SubMonitor.convert(progress);
try {
monitor.beginTask("Patchig classpath: " + bazelProject.getName(), 2);
Expand Down Expand Up @@ -347,7 +347,14 @@ void saveAndSetContainer(IJavaProject javaProject, CompileAndRuntimeClasspath cl
: new Path(BazelCoreSharedContstants.CLASSPATH_CONTAINER_ID);

var sourceAttachmentProperties = getSourceAttachmentProperties(javaProject.getProject());
var transativeClasspath = classpath.additionalRuntimeEntries().stream().map(ClasspathEntry::build).collect(toList());
var transativeClasspath =
classpath.additionalRuntimeEntries().stream().map(ClasspathEntry::build).collect(toList());

// For test projects, include test framework dependencies (like JUnit) in the container
// This caches them upfront, avoiding runtime resolution overhead
if (isTestProject(javaProject)) {
addTestFrameworkDependenciesToContainer(javaProject, transativeClasspath);
}

var container = new BazelClasspathContainer(
path,
Expand Down Expand Up @@ -376,6 +383,75 @@ private void saveContainerState(IProject project, BazelClasspathContainer contai
}
}

/**
* Checks if the given project is a test project based on naming conventions.
*
* @param project
* the project to check
* @return <code>true</code> if this appears to be a test project, <code>false</code> otherwise
*/
private boolean isTestProject(IJavaProject project) {
var projectName = project.getProject().getName();
return projectName.contains("test") || projectName.contains("Test");
}

/**
* Adds test framework dependencies (like JUnit) to the container's runtime classpath. This pre-caches test
* dependencies in the container, avoiding runtime resolution overhead.
*
* @param project
* the test project
* @param transativeClasspath
* the mutable list of runtime classpath entries to augment
*/
private void addTestFrameworkDependenciesToContainer(IJavaProject project,
List<IClasspathEntry> transativeClasspath) {
try {
// Collect existing paths to avoid duplicates
var existingPaths = new LinkedHashSet<IPath>();
for (IClasspathEntry entry : transativeClasspath) {
existingPaths.add(entry.getPath());
}

// Only process direct classpath entries (containers like JUnit)
// Skip project references to avoid circular dependencies
var rawClasspath = project.getRawClasspath();
for (IClasspathEntry entry : rawClasspath) {
if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
// Skip the Bazel container itself to avoid duplication
if (BazelCoreSharedContstants.CLASSPATH_CONTAINER_ID.equals(entry.getPath().toString())) {
continue;
}

// Get container contents (e.g., JUnit, JRE)
var container = JavaCore.getClasspathContainer(entry.getPath(), project);
if (container != null) {
for (IClasspathEntry containerEntry : container.getClasspathEntries()) {
if (containerEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
var libPath = containerEntry.getPath();
if (!existingPaths.contains(libPath)) {
// Add test framework JAR to runtime classpath
transativeClasspath.add(containerEntry);
existingPaths.add(libPath);

if (LOG.isDebugEnabled()) {
LOG.debug("Added test framework to container: {}", libPath);
}
}
}
}
}
}
}
} catch (Exception e) {
LOG.warn(
"Failed to add test framework dependencies to container for project '{}': {}",
project.getProject().getName(),
e.getMessage());
// Don't fail container creation if this step fails
}
}

/**
* Updates the classpath of multiple projects belonging to a single {@link BazelWorkspace}.
* <p>
Expand Down
Loading