Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(config): workaround failure creating temporary file (take 2) #19952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

orgads
Copy link

@orgads orgads commented Apr 27, 2025

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.

If node_modules is not writable, loadConfigFromBundledFile fails to
write its temp file and vite aborts with an exception.

This was previously fixed in vitejs#13269, but was reverted after several
reports of failure to load config with relative imports (see vitejs#13631
and vitejs#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.
@orgads orgads changed the title fix(config): Workaround failure creating temporary file (take 2) fix(config): workaround failure creating temporary file (take 2) Apr 27, 2025
@hi-ogawa
Copy link
Collaborator

Have you tried --configLoader runner or native? https://vite.dev/config/#configuring-vite I'm not sure we should add workaround for bundle mode.

@sapphi-red
Copy link
Member

If node_modules is not writable (when the temp directory cannot be created), loadConfigFromBundledFile writes the temp file to the same directory where the config file exists.

// Storing the bundled file in node_modules/ is avoided for Deno
// because Deno only supports Node.js style modules under node_modules/
// and configs with `npm:` import statements will fail when executed.
let nodeModulesDir =
typeof process.versions.deno === 'string'
? undefined
: findNearestNodeModules(path.dirname(fileName))
if (nodeModulesDir) {
try {
await fsp.mkdir(path.resolve(nodeModulesDir, '.vite-temp/'), {
recursive: true,
})
} catch (e) {
if (e.code === 'EACCES') {
// If there is no access permission, a temporary configuration file is created by default.
nodeModulesDir = undefined
} else {
throw e
}
}
}
const hash = `timestamp-${Date.now()}-${Math.random().toString(16).slice(2)}`
const tempFileName = nodeModulesDir
? path.resolve(
nodeModulesDir,
`.vite-temp/${path.basename(fileName)}.${hash}.mjs`,
)
: `${fileName}.${hash}.mjs`
await fsp.writeFile(tempFileName, bundledCode)

How does the error happen in your case?

@orgads
Copy link
Author

orgads commented Apr 28, 2025

How does the error happen in your case?

The project directory is also not writable.

@orgads
Copy link
Author

orgads commented Apr 28, 2025

Have you tried --configLoader runner or native? https://vite.dev/config/#configuring-vite I'm not sure we should add workaround for bundle mode.

Interesting, I'll try that. Is it possible to use them as fallback automatically in case writing of the temp file fails?

@orgads
Copy link
Author

orgads commented May 1, 2025

Or at least print a useful error message with instructions on failure?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants