Skip to content

Commit 1b99852

Browse files
authored
EIM Path discovery improvements (#1446)
* EIM Path discovery improvements * Fix EIM path resolver: existence-check defaults, remove dead code - resolveEimExecutablePath now validates default path exists before returning it, preventing bogus EIM_PATH when EIM is not installed - Added EIM_PATH env variable as fallback step in resolution - Unified isEimInstalled() to delegate to resolveEimExecutablePath - Removed dead findAndSetEimPath() and setEimPathInEnvVar() methods * fix for bug reported from Andrii
1 parent f0537fc commit 1b99852

2 files changed

Lines changed: 106 additions & 61 deletions

File tree

bundles/com.espressif.idf.core/src/com/espressif/idf/core/tools/ToolInitializer.java

Lines changed: 95 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.espressif.idf.core.IDFCorePlugin;
2323
import com.espressif.idf.core.IDFEnvironmentVariables;
2424
import com.espressif.idf.core.ProcessBuilderFactory;
25+
import com.espressif.idf.core.SystemExecutableFinder;
2526
import com.espressif.idf.core.logging.Logger;
2627
import com.espressif.idf.core.tools.exceptions.EimVersionMismatchException;
2728
import com.espressif.idf.core.tools.vo.EimJson;
@@ -48,16 +49,59 @@ public ToolInitializer(Preferences preferences)
4849

4950
public boolean isEimInstalled()
5051
{
52+
return !StringUtil.isEmpty(resolveEimExecutablePath(null));
53+
}
54+
55+
/**
56+
* Looks for an {@code eim} executable on the process {@code PATH}, using the same rules as other tools in this
57+
* plugin ({@link SystemExecutableFinder}: PATHEXT on Windows, plain name on Linux/macOS).
58+
*
59+
* @return absolute path to the executable, or empty if not found
60+
*/
61+
private String findEimOnSystemPath()
62+
{
63+
IPath eimPath = new SystemExecutableFinder().find("eim"); //$NON-NLS-1$
64+
return eimPath != null ? eimPath.toOSString() : StringUtil.EMPTY;
65+
}
66+
67+
/**
68+
* Resolves the EIM executable path: <strong>system {@code PATH} first</strong>, then {@code eimPath} from
69+
* {@code eim_idf.json} when the path exists on disk, then {@code EIM_PATH} env variable, then
70+
* {@link #getDefaultEimPath()} (existence-checked).
71+
*
72+
* @param eimJson parsed JSON or {@code null}
73+
* @return resolved absolute path string, or empty if nothing could be resolved
74+
*/
75+
public String resolveEimExecutablePath(EimJson eimJson)
76+
{
77+
String fromPath = findEimOnSystemPath();
78+
if (!StringUtil.isEmpty(fromPath))
79+
{
80+
return fromPath;
81+
}
82+
83+
if (eimJson != null && !StringUtil.isEmpty(eimJson.getEimPath()))
84+
{
85+
String jsonPath = eimJson.getEimPath();
86+
if (Files.exists(Paths.get(jsonPath)))
87+
{
88+
return jsonPath;
89+
}
90+
}
91+
5192
String eimExePathEnv = idfEnvironmentVariables.getEnvValue(IDFEnvironmentVariables.EIM_PATH);
52-
boolean exists = !StringUtil.isEmpty(eimExePathEnv) && Files.exists(Paths.get(eimExePathEnv));
53-
if (!exists)
93+
if (!StringUtil.isEmpty(eimExePathEnv) && Files.exists(Paths.get(eimExePathEnv)))
94+
{
95+
return eimExePathEnv;
96+
}
97+
98+
Path defaultEimPath = getDefaultEimPath();
99+
if (defaultEimPath != null && Files.exists(defaultEimPath))
54100
{
55-
// Fallback: check in user home .espressif/eim_gui folder
56-
Path defaultEimPath = getDefaultEimPath();
57-
if (defaultEimPath != null)
58-
exists = Files.exists(defaultEimPath);
101+
return defaultEimPath.toString();
59102
}
60-
return exists;
103+
104+
return StringUtil.EMPTY;
61105
}
62106

63107
public boolean isEimIdfJsonPresent()
@@ -138,38 +182,50 @@ public boolean isEspIdfSet()
138182
public Path getDefaultEimPath()
139183
{
140184
String userHome = System.getProperty("user.home"); //$NON-NLS-1$
141-
Path defaultEimPath;
142-
String os = Platform.getOS();
143-
if (os.equals(Platform.OS_WIN32))
144-
{
145-
defaultEimPath = Paths.get(userHome, ".espressif", "eim_gui", //$NON-NLS-1$//$NON-NLS-2$
146-
"eim.exe"); //$NON-NLS-1$
147-
}
148-
else if (os.equals(Platform.OS_MACOSX))
149-
{
150-
defaultEimPath = Paths.get("/Applications", //$NON-NLS-1$
151-
"eim.app", "Contents", //$NON-NLS-1$//$NON-NLS-2$
152-
"MacOS", "eim"); //$NON-NLS-1$ //$NON-NLS-2$
153-
}
154-
else
155-
{
156-
defaultEimPath = Paths.get(userHome, ".espressif", //$NON-NLS-1$
157-
"eim_gui", "eim"); //$NON-NLS-1$//$NON-NLS-2$
158-
}
159-
160-
return defaultEimPath;
161-
}
162-
163-
public void findAndSetEimPath()
164-
{
165-
Path defaultEimPath = getDefaultEimPath();
166-
167-
if (defaultEimPath != null)
168-
setEimPathInEnvVar(defaultEimPath.toString());
185+
Path defaultEimPath;
186+
String os = Platform.getOS();
187+
if (os.equals(Platform.OS_WIN32))
188+
{
189+
defaultEimPath = Paths.get(userHome, ".espressif", "eim_gui", //$NON-NLS-1$//$NON-NLS-2$
190+
"eim.exe"); //$NON-NLS-1$
191+
if (!Files.exists(defaultEimPath))
192+
{
193+
Path eimGuiDir = Paths.get(userHome, ".espressif", "eim_gui"); //$NON-NLS-1$ //$NON-NLS-2$
194+
if (Files.isDirectory(eimGuiDir))
195+
{
196+
try (var entries = Files.list(eimGuiDir))
197+
{
198+
Path found = entries
199+
.filter(Files::isRegularFile)
200+
.filter(p -> p.getFileName().toString().toLowerCase().startsWith("eim") //$NON-NLS-1$
201+
&& p.getFileName().toString().toLowerCase().endsWith(".exe")) //$NON-NLS-1$
202+
.findFirst()
203+
.orElse(null);
204+
if (found != null)
205+
{
206+
return found;
207+
}
208+
}
209+
catch (IOException e)
210+
{
211+
Logger.log(e);
212+
}
213+
}
214+
}
215+
}
216+
else if (os.equals(Platform.OS_MACOSX))
217+
{
218+
defaultEimPath = Paths.get("/Applications", //$NON-NLS-1$
219+
"eim.app", "Contents", //$NON-NLS-1$//$NON-NLS-2$
220+
"MacOS", "eim"); //$NON-NLS-1$ //$NON-NLS-2$
221+
}
222+
else
223+
{
224+
defaultEimPath = Paths.get(userHome, ".espressif", //$NON-NLS-1$
225+
"eim_gui", "eim"); //$NON-NLS-1$//$NON-NLS-2$
226+
}
227+
228+
return defaultEimPath;
169229
}
170230

171-
private void setEimPathInEnvVar(String eimPath)
172-
{
173-
idfEnvironmentVariables.addEnvVariable(IDFEnvironmentVariables.EIM_PATH, eimPath);
174-
}
175231
}

bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/EspressifToolStartup.java

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,11 @@ else if (toolInitializer.isEimIdfJsonPresent() && !toolInitializer.isEspIdfSet()
115115
promptUserToOpenToolManager(eimJson);
116116
}
117117

118-
// Set EimPath on every startup to ensure proper path in configurations
119-
if (eimJson != null)
118+
// Set EimPath on every startup: system PATH first, then eim_idf.json, then default locations
119+
String resolvedEimPath = toolInitializer.resolveEimExecutablePath(eimJson);
120+
if (!StringUtil.isEmpty(resolvedEimPath))
120121
{
121-
idfEnvironmentVariables.addEnvVariable(IDFEnvironmentVariables.EIM_PATH, eimJson.getEimPath());
122-
}
123-
else
124-
{
125-
// Fail-safe call to ensure if the eim is in Applications or user.home it is setup in env vars
126-
toolInitializer.findAndSetEimPath();
122+
idfEnvironmentVariables.addEnvVariable(IDFEnvironmentVariables.EIM_PATH, resolvedEimPath);
127123
}
128124

129125
}
@@ -163,22 +159,15 @@ private void startExportOldConfig()
163159
{
164160
try
165161
{
166-
// if eim json is present it means that it contains the updated path and we use that else we fallback to
167-
// finding eim in default paths
168-
Path eimPath;
169-
String eimPathEnvVar = idfEnvironmentVariables.getEnvValue(IDFEnvironmentVariables.EIM_PATH);
170-
if (eimJson != null)
162+
// Same resolution order as workspace EIM_PATH: PATH, then eim_idf.json, then defaults
163+
String resolved = toolInitializer.resolveEimExecutablePath(eimJson);
164+
if (StringUtil.isEmpty(resolved))
171165
{
172-
eimPath = Paths.get(eimJson.getEimPath());
173-
}
174-
else if (!StringUtil.isEmpty(eimPathEnvVar))
175-
{
176-
eimPath = Paths.get(eimPathEnvVar);
177-
}
178-
else
179-
{
180-
eimPath = toolInitializer.getDefaultEimPath();
166+
Logger.log("Cannot export old config: EIM executable path could not be resolved."); //$NON-NLS-1$
167+
writeToErrorConsoleStream(Messages.OldConfigExportCompleteFailMsg);
168+
return;
181169
}
170+
Path eimPath = Paths.get(resolved);
182171

183172
IStatus status = toolInitializer.exportOldConfig(eimPath);
184173
Logger.log("Tools Conversion Process Message: ");

0 commit comments

Comments
 (0)