From 6aed0479eed3c6ee1e444753acafaa9293c15464 Mon Sep 17 00:00:00 2001 From: Mickael Istria Date: Tue, 15 Nov 2022 08:38:05 +0100 Subject: [PATCH] Debug TS looks for tsconfig file in parent directories Fixes https://github.com/eclipse/wildwebdeveloper/issues/960 --- .../debug/node/NodeRunDAPDebugDelegate.java | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/org.eclipse.wildwebdeveloper/src/org/eclipse/wildwebdeveloper/debug/node/NodeRunDAPDebugDelegate.java b/org.eclipse.wildwebdeveloper/src/org/eclipse/wildwebdeveloper/debug/node/NodeRunDAPDebugDelegate.java index ab183ab6fc..1deb15e532 100644 --- a/org.eclipse.wildwebdeveloper/src/org/eclipse/wildwebdeveloper/debug/node/NodeRunDAPDebugDelegate.java +++ b/org.eclipse.wildwebdeveloper/src/org/eclipse/wildwebdeveloper/debug/node/NodeRunDAPDebugDelegate.java @@ -150,13 +150,14 @@ private boolean configureAdditionalParameters(Map param) { return false; } + File programFile = new File(program); if (Platform.getContentTypeManager().getContentType("org.eclipse.wildwebdeveloper.ts") - .isAssociatedWith(new File(program).getName())) { + .isAssociatedWith(programFile.getName())) { // TypeScript Source Mappings Configuration - String tsConfigPath = cwd + "/tsconfig.json"; + File parentDirectory = cwd == null ? programFile.getParentFile() : new File(cwd); + File tsConfigFile = findTSConfigFile(parentDirectory); String errorMessage = null; - File tsConfigFile = new File(tsConfigPath); - Map tsConfig = readTsConfig(tsConfigFile); + Map tsConfig = readJSonFile(tsConfigFile); Map co = tsConfig == null ? null : (Map)tsConfig.get("compilerOptions"); if (co == null) { errorMessage = Messages.NodeDebug_TSConfirError_NoTsConfig; @@ -222,7 +223,7 @@ private boolean configureAdditionalParameters(Map param) { Display.getDefault().asyncExec(new Runnable() { @Override public void run() { - IFile file = createNewEmptyFile(tsConfigPath); + IFile file = createNewEmptyFile(new File(parentDirectory, "tsconfig.json")); if (file != null) { try { IDE.openEditor( @@ -236,10 +237,10 @@ public void run() { } } - private IFile createNewEmptyFile(String tsConfigPath) { + private IFile createNewEmptyFile(File fsFile) { IWorkspace ws = ResourcesPlugin.getWorkspace(); IWorkspaceRoot wr = ws.getRoot(); - IFile file = wr.getFileForLocation(new Path(tsConfigPath)); + IFile file = wr.getFileForLocation(new Path(fsFile.getAbsolutePath())); if (!(file.exists() && file.isAccessible())) { IFile[] result = new IFile[1]; try { @@ -279,7 +280,7 @@ void createContainers(IResource resource) throws CoreException { return true; } else if (Platform.getContentTypeManager().getContentType("org.eclipse.wildwebdeveloper.js") - .isAssociatedWith(new File(program).getName())) { + .isAssociatedWith(programFile.getName())) { // JavaScript configuration @@ -291,8 +292,23 @@ void createContainers(IResource resource) throws CoreException { } return false; } + + private File findTSConfigFile(File parentDirectory) { + File tsConfigFile; + do { + tsConfigFile = new File(parentDirectory, "tsconfig.json"); + if (tsConfigFile.isFile()) { + return tsConfigFile; + } + parentDirectory = parentDirectory.getParentFile(); + } while (parentDirectory != null && parentDirectory.isDirectory()); + return null; + } - public Map readTsConfig(File tsConfgFile) { + public Map readJSonFile(File tsConfgFile) { + if (tsConfgFile == null || !tsConfgFile.isFile()) { + return Map.of(); + } try (BufferedReader in = new BufferedReader(new FileReader(tsConfgFile))) { String inputLine; StringBuffer response = new StringBuffer(); @@ -302,7 +318,7 @@ public Map readTsConfig(File tsConfgFile) { Type type = new TypeToken>() {}.getType(); return new Gson().fromJson(response.toString(), type); } catch (IOException e) { - return null; + return Map.of(); } } }