Skip to content

Commit 360890e

Browse files
committed
fix(config): workaround failure creating temporary file (take 2)
If node_modules is not writable, loadConfigFromBundledFile fails to write its temp file and vite aborts with an exception. This was previously fixed in #13269, but was reverted after several reports of failure to load config with relative imports (see #13631 and #13730). Instead of replacing the current code, try to load from string only as a workaround, when the directory doesn't exist. This should solve the case of unwritable node_modules when relative imports are not used without breaking anything that works.
1 parent b5f70dd commit 360890e

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

packages/vite/src/node/config.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,11 +2043,20 @@ async function loadConfigFromBundledFile(
20432043
`.vite-temp/${path.basename(fileName)}.${hash}.mjs`,
20442044
)
20452045
: `${fileName}.${hash}.mjs`
2046-
await fsp.writeFile(tempFileName, bundledCode)
20472046
try {
2048-
return (await import(pathToFileURL(tempFileName).href)).default
2049-
} finally {
2050-
fs.unlink(tempFileName, () => {}) // Ignore errors
2047+
await fsp.writeFile(tempFileName, bundledCode)
2048+
try {
2049+
return (await import(pathToFileURL(tempFileName).href)).default
2050+
} finally {
2051+
fs.unlink(tempFileName, () => {}) // Ignore errors
2052+
}
2053+
} catch (e) {
2054+
if (e.code === 'EACCES') {
2055+
const url = `data:text/javascript;base64,${Buffer.from(bundledCode).toString('base64')}`;
2056+
return (await import(url)).default;
2057+
} else {
2058+
throw e;
2059+
}
20512060
}
20522061
}
20532062
// for cjs, we can register a custom loader via `_require.extensions`

0 commit comments

Comments
 (0)