Skip to content
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

fix: disable caching when babel could not read/write cache #10557

Merged
merged 7 commits into from Oct 17, 2019
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 52 additions & 7 deletions packages/babel-register/src/cache.js
Expand Up @@ -14,11 +14,17 @@ const DEFAULT_FILENAME = path.join(
const FILENAME: string = process.env.BABEL_CACHE_PATH || DEFAULT_FILENAME;
let data: Object = {};

let cacheDisabled = false;

function isCacheDisabled() {
return process.env.BABEL_DISABLE_CACHE ?? cacheDisabled;
}
/**
* Write stringified cache to disk.
*/

export function save() {
if (isCacheDisabled()) return;
let serialised: string = "{}";

try {
Expand All @@ -32,27 +38,66 @@ export function save() {
}
}

mkdirpSync(path.dirname(FILENAME));
fs.writeFileSync(FILENAME, serialised);
try {
mkdirpSync(path.dirname(FILENAME));
fs.writeFileSync(FILENAME, serialised);
} catch (e) {
switch (e.code) {
case "EACCES":
case "EPERM":
console.warn(
`Babel could not write cache to file: ${FILENAME}
due to a permission issue. Cache is disabled.`,
);
cacheDisabled = true;
break;
case "EROFS":
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disclaimer: I have not tested on a readonly filesystem, this error code is copied from man 2 open

[EROFS] The named file resides on a read-only file system, and the file is to be modified.

console.warn(
`Babel could not write cache to file: ${FILENAME}
because it resides in a readonly filesystem. Cache is disabled.`,
);
cacheDisabled = true;
break;
default:
throw e;
}
}
}

/**
* Load cache from disk and parse.
*/

export function load() {
if (process.env.BABEL_DISABLE_CACHE) return;
if (isCacheDisabled()) return;

process.on("exit", save);
process.nextTick(save);

if (!fs.existsSync(FILENAME)) return;
let cacheContent;

try {
data = JSON.parse(fs.readFileSync(FILENAME));
} catch (err) {
return;
cacheContent = fs.readFileSync(FILENAME);
} catch (e) {
switch (e.code) {
case "ENOENT":
return;
case "EACCES":
case "EPERM":
console.warn(
`Babel could not read cache file: ${FILENAME}
due to a permission issue. Cache is disabled.`,
);
cacheDisabled = true;
return;
default:
throw e;
}
}

try {
data = JSON.parse(cacheContent);
} catch (err) {}
JLHwung marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down