From b65381166c1c9d6850e403641e38e64f1a4c1954 Mon Sep 17 00:00:00 2001 From: Alice Trifu Date: Tue, 28 Oct 2025 12:10:04 +0200 Subject: [PATCH] Changed directory field to reflect PWD. This is fixing #1371. Directory field in JSON is not the working build directory. --- .../CompilationDatabaseGenerationTest.java | 45 +++++++++++++++++++ .../CompilationDatabaseGenerator.java | 31 ++++++++++++- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/build/org.eclipse.cdt.managedbuilder.core.tests/tests/org/eclipse/cdt/managedbuilder/core/tests/CompilationDatabaseGenerationTest.java b/build/org.eclipse.cdt.managedbuilder.core.tests/tests/org/eclipse/cdt/managedbuilder/core/tests/CompilationDatabaseGenerationTest.java index 3d058dea1b9..a746eec02ed 100644 --- a/build/org.eclipse.cdt.managedbuilder.core.tests/tests/org/eclipse/cdt/managedbuilder/core/tests/CompilationDatabaseGenerationTest.java +++ b/build/org.eclipse.cdt.managedbuilder.core.tests/tests/org/eclipse/cdt/managedbuilder/core/tests/CompilationDatabaseGenerationTest.java @@ -33,7 +33,9 @@ import org.eclipse.cdt.utils.CommandLineUtil; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IncrementalProjectBuilder; +import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.preferences.InstanceScope; @@ -246,4 +248,47 @@ private static String escapeArgsForCompileCommand(List args) { }).collect(Collectors.joining(" ")); } + @Test + public void testCompileCommandsDirectory() throws Exception { + setWorkspace("regressions"); + final IProject app = loadProject("helloworldC"); + IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(app); + IManagedProject mProj = info.getManagedProject(); + IConfiguration cfg = mProj.getConfigurations()[0]; + IToolChain toolChain = cfg.getToolChain(); + IBuilder builder = toolChain.getBuilder(); + setGenerateFileOptionEnabled(true); + app.build(IncrementalProjectBuilder.FULL_BUILD, null); + + IFile commandsFile = app.getFile("Debug/compile_commands.json"); + + if (commandsFile.exists()) { + try (FileReader reader = new FileReader(commandsFile.getLocation().toFile())) { + Gson gson = new Gson(); + JsonArray jsonArray = gson.fromJson(reader, JsonArray.class); + IPath buildDir = ManagedBuildManager.getBuildFullPath(cfg, builder); + + IResource rc = ResourcesPlugin.getWorkspace().getRoot().findMember(buildDir); + String workspacePath = rc != null ? rc.getLocation().toOSString() : buildDir.toOSString(); + + java.nio.file.Path workspaceNormalized = java.nio.file.Paths.get(workspacePath).normalize(); + + for (JsonElement element : jsonArray) { + CompilationDatabaseInformation compileCommand = gson.fromJson(element, + CompilationDatabaseInformation.class); + + String directory = compileCommand.directory(); + assertNotNull("Directory field should not be null", directory); + assertFalse(directory.isEmpty(), "Directory field should not be empty"); + + java.nio.file.Path directoryNormalized = java.nio.file.Paths.get(directory).normalize(); + + assertTrue(directoryNormalized.startsWith(workspaceNormalized), + "Directory should start with workspace path.\nExpected prefix: " + workspaceNormalized + + "\nBut got: " + directoryNormalized); + } + } + } + } + } diff --git a/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/core/jsoncdb/generator/CompilationDatabaseGenerator.java b/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/core/jsoncdb/generator/CompilationDatabaseGenerator.java index 243a8df3bd4..d651811b954 100644 --- a/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/core/jsoncdb/generator/CompilationDatabaseGenerator.java +++ b/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/core/jsoncdb/generator/CompilationDatabaseGenerator.java @@ -28,6 +28,7 @@ import org.eclipse.cdt.core.settings.model.ICSourceEntry; import org.eclipse.cdt.core.settings.model.util.CDataUtil; import org.eclipse.cdt.managedbuilder.core.BuildException; +import org.eclipse.cdt.managedbuilder.core.IBuilder; import org.eclipse.cdt.managedbuilder.core.IConfiguration; import org.eclipse.cdt.managedbuilder.core.IFileInfo; import org.eclipse.cdt.managedbuilder.core.IFolderInfo; @@ -164,6 +165,10 @@ private List populateObjList(IProject project, I List objList = new ArrayList<>(); for (IResource resource : getFileList()) { + IPath buildDir = getFullBuildDirectory(config); + if (buildDir == null) { + buildDir = resource.getProject().getLocation(); + } IPath moduleRelativePath = resource.getParent().getProjectRelativePath(); String relativePath = moduleRelativePath.toString(); IFolder folder = project.getFolder(config.getName()); @@ -249,8 +254,8 @@ private List populateObjList(IProject project, I new FileContextData(sourceLocation, outputLocation, null, tool)); } - objList.add(new CompilationDatabaseInformation(project.getLocation().toString(), - resolvedOptionFileContents, resource.getLocation().toString())); + objList.add(new CompilationDatabaseInformation(buildDir.toString(), resolvedOptionFileContents, + resource.getLocation().toString())); } } @@ -501,4 +506,26 @@ private static String escArgsForCompileCommand(final List args) { }).collect(Collectors.joining(" ")); //$NON-NLS-1$ } + public static IPath getFullBuildDirectory(IConfiguration config) { + if (config == null) { + throw new IllegalArgumentException("Configuration cannot be null"); //$NON-NLS-1$ + } + + IBuilder builder = config.getBuilder(); + IPath buildDir = ManagedBuildManager.getBuildFullPath(config, builder); + IPath fullBuildPath = null; + + if (buildDir != null) { + IResource rc = ResourcesPlugin.getWorkspace().getRoot().findMember(buildDir); + + if (rc != null && rc.getLocation() != null) { + fullBuildPath = rc.getLocation(); + } else if (buildDir.toFile().isAbsolute()) { + fullBuildPath = buildDir; + } + } + + return fullBuildPath; + } + }