Skip to content

Commit d10dfeb

Browse files
JoshwinThomasIBMJoshwinThomasIBM
authored andcommitted
fixes the refresh issue when build files are generated in build directories
1 parent b248418 commit d10dfeb

File tree

3 files changed

+104
-11
lines changed

3 files changed

+104
-11
lines changed

src/definitions/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) 2020, 2024 IBM Corporation.
2+
* Copyright (c) 2020, 2025 IBM Corporation.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0 which is available at
@@ -32,7 +32,7 @@ export const COMMAND_AND_PROJECT_TYPE_MAP: { [command: string]: string[] } = {
3232
"gradle":[ LIBERTY_GRADLE_PROJECT, LIBERTY_GRADLE_PROJECT_CONTAINER],
3333
"liberty.dev.debug": [LIBERTY_MAVEN_PROJECT, LIBERTY_GRADLE_PROJECT, LIBERTY_MAVEN_PROJECT_CONTAINER, LIBERTY_GRADLE_PROJECT_CONTAINER],
3434
};
35-
export const EXCLUDED_DIR_PATTERN = "**/{bin,classes,target}/**";
35+
export const EXCLUDED_DIR_PATTERN = "**/{bin,classes,target,build}/**";
3636
export const COMMAND_TITLES = new Map();
3737
export const UNTITLED_WORKSPACE="Untitled (Workspace)";
3838
COMMAND_TITLES.set(localize("hotkey.commands.title.refresh"), "liberty.explorer.refresh");

src/extension.ts

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { localize } from "./util/i18nUtil";
2020
import { RequirementsData, resolveRequirements, resolveLclsRequirements } from "./util/requirements";
2121
import { prepareExecutable } from "./util/javaServerStarter";
2222
import * as helperUtil from "./util/helperUtil";
23+
import path = require('path');
2324

2425
const LIBERTY_CLIENT_ID = "LANGUAGE_ID_LIBERTY";
2526
const JAKARTA_CLIENT_ID = "LANGUAGE_ID_JAKARTA";
@@ -184,15 +185,49 @@ export function deactivate(): Promise<void[]> {
184185
*/
185186
export function registerFileWatcher(projectProvider: ProjectProvider): void {
186187
const watcher: vscode.FileSystemWatcher = vscode.workspace.createFileSystemWatcher("{**/pom.xml,**/build.gradle,**/settings.gradle,**/src/main/liberty/config/server.xml}");
187-
watcher.onDidCreate(async () => {
188-
projectProvider.refresh();
189-
});
190-
watcher.onDidChange(async () => {
191-
projectProvider.refresh();
192-
});
193-
watcher.onDidDelete(async () => {
194-
projectProvider.refresh();
195-
});
188+
// Async handler for the file system events (create, change, delete)
189+
const handleUri = async (uri: vscode.Uri) => {
190+
const workspaceFolders = vscode.workspace.workspaceFolders;
191+
192+
if (!workspaceFolders) {
193+
return; // No workspace folders to process
194+
}
195+
196+
// Loop through all workspace folders
197+
for (let folder of workspaceFolders) {
198+
const projectRoot = folder.uri.fsPath;
199+
const relativePath = path.relative(projectRoot, uri.fsPath);
200+
201+
// Ensure that the file belongs to this project (starts with the projectRoot path)
202+
if (!uri.fsPath.startsWith(projectRoot)) {
203+
continue; // Skip if the file is outside the current project folder
204+
}
205+
206+
// Check if the path includes 'target' or 'build' directly under the project root
207+
if (/(target\/|build\/)/.test(relativePath)) {
208+
209+
const fileType = path.basename(uri.fsPath);
210+
const siblingFileExists = await helperUtil.checkSiblingFilesInTargetOrBuildParent(uri.fsPath);
211+
if(!siblingFileExists){
212+
console.log(`No sibling ${fileType} found, refreshing project... for ` + uri.fsPath);
213+
// Refresh the project if no sibling file is found
214+
await projectProvider.refresh();
215+
}else{
216+
console.log(`Skipping refresh: Sibling ${fileType} found for `+uri.fsPath);
217+
return; // Do not refresh
218+
}
219+
} else {
220+
// If the file generated is **outside** the `target` directory, always refresh
221+
console.log('Refreshing project...');
222+
await projectProvider.refresh();
223+
}
224+
}
225+
};
226+
227+
228+
watcher.onDidCreate(handleUri);
229+
watcher.onDidChange(handleUri);
230+
watcher.onDidDelete(handleUri);
196231
}
197232

198233
function startLangServer(context: ExtensionContext, requirements: RequirementsData, isLiberty: boolean) {

src/util/helperUtil.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import * as fs from "fs";
1313
import * as vscode from "vscode";
1414
import { LibertyProject } from "./../liberty/libertyProject";
1515
import { COMMAND_AND_PROJECT_TYPE_MAP, LIBERTY_DASHBOARD_WORKSPACE_STORAGE_KEY } from "../definitions/constants";
16+
import path = require('path');
1617

1718

1819
export async function getAllPaths(projectRootPath: string, pattern: string): Promise<string[]> {
@@ -80,3 +81,60 @@ export function clearDataSavedInGlobalState(context: vscode.ExtensionContext) {
8081
context.globalState.update('selectedProject', undefined);
8182
}
8283

84+
/**
85+
* Traverse upwards to check for target/build directory and its corresponding sibling build file (pom.xml or build.gradle).
86+
* @param filePath The file path to start the search from.
87+
* @returns true if sibling file found, false otherwise.
88+
*/
89+
export async function checkSiblingFilesInTargetOrBuildParent(filePath: string): Promise<boolean> {
90+
91+
try {
92+
let currentDir = path.dirname(filePath); // Start with the directory of the provided file
93+
94+
// Traverse upwards through the directories
95+
while (currentDir !== path.parse(currentDir).root) {
96+
let targetDir: string | undefined;
97+
let siblingFilePath: string | undefined;
98+
99+
// Determine the file type to look for (either pom.xml or build.gradle)
100+
if (filePath.endsWith('pom.xml')) {
101+
targetDir = 'target'; // For Maven projects, look for target directory
102+
siblingFilePath = path.join(currentDir, targetDir, 'pom.xml');
103+
} else if (filePath.endsWith('build.gradle') || filePath.endsWith('settings.gradle')) {
104+
targetDir = 'build'; // For Gradle projects, look for build directory
105+
siblingFilePath = path.join(currentDir, targetDir, 'build.gradle');
106+
} else {
107+
console.log("Invalid file type. Only 'pom.xml' or 'build.gradle' are supported.");
108+
return false;
109+
}
110+
111+
// Ensure that targetDir and siblingFilePath are assigned before proceeding
112+
if (!targetDir || !siblingFilePath) {
113+
console.error("Error: targetDir or siblingFilePath not properly assigned.");
114+
return false;
115+
}
116+
117+
// Check if the target/build directory exists
118+
const currentTargetDir = path.join(currentDir, targetDir);
119+
if (fs.existsSync(currentTargetDir) && fs.statSync(currentTargetDir).isDirectory()) {
120+
121+
// Check if the expected sibling file (pom.xml or build.gradle) exists
122+
if (fs.existsSync(siblingFilePath) && fs.statSync(siblingFilePath).isFile()) {
123+
console.log(`Found sibling ${filePath.endsWith('pom.xml') ? 'pom.xml' : 'build.gradle'} in directory: ${currentTargetDir}`);
124+
return true; // Found the sibling file, return true to indicate file should be ignored
125+
}
126+
}
127+
128+
// Move up to the parent directory
129+
currentDir = path.dirname(currentDir);
130+
}
131+
132+
// If no sibling file was found and we reached the root, return false to allow refresh
133+
console.log("Reached project root, no sibling file found. Allowing refresh.");
134+
return false;
135+
} catch (err) {
136+
console.error('Error during directory traversal:', err);
137+
return false;
138+
}
139+
}
140+

0 commit comments

Comments
 (0)