Preserve Windows PATH when launching musrfit through WSL

Keep the inherited Windows search path with semicolon separators, honor case-insensitive Path names, and emit only one PATH entry for spawned tools. Do not prepend Linux runtime markers or Unix directories to the Windows host PATH.

Add regression coverage for mixed-case and duplicate PATH keys, configured native tools, and an unchanged parent environment. The Electron suite passes; the Windows installer is rebuilt for on-device verification.
This commit is contained in:
salman committed 2026-10-26 16:44:28 +01:00
1 parent 2fda8c563d
commit f248cb515b
3 files changed
+60 -2

No files matched your search

+4
View File
@@ -86,6 +86,10 @@ release path documented in [`TODO.md`](TODO.md).
selection and actual RUN/ADDRUN entries in the MSR.
### Bundled ROOT + musrfit runtime
- Windows Fit/Plot preserves the host executable search path, including the
usual mixed-case `Path` key and semicolon separators. This fixes
`spawn wsl.exe ENOENT` after successful runtime setup. Linux runtime paths
are no longer prepended to the Windows host search path.
- Linux Preferences now offers **Check tools** and confirmed offline runtime
installation from `runtime-download.json`. Installs verify the archive and
extracted tools in a private user directory, support cancellation, and retain
+22 -2
View File
@@ -1368,6 +1368,13 @@ function buildToolEnvironment() {
? libDir + ":" + env.LD_LIBRARY_PATH
: libDir;
}
if (nodeProcess.platform === "win32") {
// Plain bridge snapshots can contain Path and PATH. Node forwards only
// one case-insensitive match, so emit a single, canonical search path.
for (let key of Object.keys(env)) {
if (key.toUpperCase() === "PATH") delete env[key];
}
}
env.PATH = localCommandPathBias();
let dataSearchPath = localRunDataSearchPath(false);
if (dataSearchPath) env.MUSRFULLDATAPATH = dataSearchPath;
@@ -1582,6 +1589,21 @@ function resolveLocalCommand(commandName) {
// usual PSI/local locations so children of musrfit that resolve their own
// helpers follow the preference.
function localCommandPathBias() {
let configuredBinDir = typeof getConfiguredBinDir === "function"
? getConfiguredBinDir() : "";
if (nodeProcess.platform === "win32") {
// Windows normally calls this variable Path. Preserve its directories
// and use semicolons; Linux prefixes can hide System32 and wsl.exe.
// Bundled Linux tools run inside WSL, not on the Windows host PATH.
let env = nodeProcess.env || {};
let pathKey = Object.keys(env).sort().find(function(key) {
return key.toUpperCase() === "PATH";
});
let dirs = configuredBinDir ? [configuredBinDir] : [];
if (pathKey && env[pathKey]) dirs.push(env[pathKey]);
return dirs.join(";");
}
let dirs = ["/usr/local/bin", "/usr/bin", "/bin"];
let systemBinDir = systemRuntimeBinDir();
if (systemBinDir !== "") {
@@ -1591,8 +1613,6 @@ function localCommandPathBias() {
if (bundledBinDir !== "") {
dirs.unshift(bundledBinDir);
}
let configuredBinDir = typeof getConfiguredBinDir === "function"
? getConfiguredBinDir() : "";
if (configuredBinDir !== "") {
dirs.unshift(configuredBinDir);
}
+34
View File
@@ -224,6 +224,40 @@ fs.writeFileSync(path.join(separateRuntimeRoot, "musrfit_startup.xml"), "<musrfi
"C:\\tools\\msr2data.exe x");
})();
// The main-process WSL import uses the inherited environment. Fit/Plot must
// preserve that same Windows search path when constructing their child env,
// including its usual mixed-case Path key and semicolon-separated entries.
(function testWindowsToolEnvironmentPreservesExecutableSearchPath() {
const windowsPath = String.raw`C:\Windows\System32;C:\Windows;C:\Program Files\WSL`;
for (const pathKey of ['Path', 'PATH', 'pAtH']) {
const sandbox = buildSandbox(resourcesPath, { [pathKey]: windowsPath }, 'win32');
loadLibraries(sandbox);
const originalEnvironment = Object.assign({}, sandbox.process.env);
const env = sandbox.buildToolEnvironment();
assert.strictEqual(env.PATH, windowsPath,
'Fit/Plot must keep Windows directories intact for wsl.exe lookup (' + pathKey + ')');
assert.deepStrictEqual(Object.keys(env).filter(key => key.toUpperCase() === 'PATH'), ['PATH']);
assert.deepStrictEqual(sandbox.process.env, originalEnvironment);
assert(!env.PATH.includes('/usr/bin'), 'Linux search paths do not belong in the Windows host PATH');
assert(!env.PATH.includes(runtimeRoot), 'The Linux runtime markers must not shadow Windows programs');
const nativeTools = String.raw`D:\Scientific tools\musrfit\bin`;
sandbox.getConfiguredBinDir = function() {
return nativeTools;
};
assert.strictEqual(sandbox.buildToolEnvironment().PATH, nativeTools + ';' + windowsPath);
}
// A plain object can contain both spellings, even though Windows itself
// treats them as one variable. Match Node's precedence and emit one key.
const sandbox = buildSandbox(resourcesPath, { Path: 'ignored', PATH: windowsPath }, 'win32');
loadLibraries(sandbox);
const env = sandbox.buildToolEnvironment();
assert.strictEqual(env.PATH, windowsPath);
assert.deepStrictEqual(Object.keys(env).filter(key => key.toUpperCase() === 'PATH'), ['PATH']);
})();
// On a Linux host the WSL translation is inert even when the bundle is present.
(function testWslTranslationInertOnLinux() {
const sandbox = buildSandbox(resourcesPath, {}, "linux");