fix: disable caching when babel could not read/write cache (#10557)
* fix: disable caching when babel could not read/write cache * emit warning when cache folder resides in readonly fs * fix: always register save handler * cache: maintain old behaviour * test: add more test case * fix: next tick tasks are FIFO * test: disable test on Windows
This commit is contained in:
parent
5800fc97b3
commit
487f10f84d
@ -14,11 +14,17 @@ const DEFAULT_FILENAME = path.join(
|
|||||||
const FILENAME: string = process.env.BABEL_CACHE_PATH || DEFAULT_FILENAME;
|
const FILENAME: string = process.env.BABEL_CACHE_PATH || DEFAULT_FILENAME;
|
||||||
let data: Object = {};
|
let data: Object = {};
|
||||||
|
|
||||||
|
let cacheDisabled = false;
|
||||||
|
|
||||||
|
function isCacheDisabled() {
|
||||||
|
return process.env.BABEL_DISABLE_CACHE ?? cacheDisabled;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Write stringified cache to disk.
|
* Write stringified cache to disk.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function save() {
|
export function save() {
|
||||||
|
if (isCacheDisabled()) return;
|
||||||
let serialised: string = "{}";
|
let serialised: string = "{}";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -32,8 +38,30 @@ export function save() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mkdirpSync(path.dirname(FILENAME));
|
try {
|
||||||
fs.writeFileSync(FILENAME, serialised);
|
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":
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -41,18 +69,37 @@ export function save() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
export function load() {
|
export function load() {
|
||||||
if (process.env.BABEL_DISABLE_CACHE) return;
|
if (isCacheDisabled()) {
|
||||||
|
data = {};
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
process.on("exit", save);
|
process.on("exit", save);
|
||||||
process.nextTick(save);
|
process.nextTick(save);
|
||||||
|
|
||||||
if (!fs.existsSync(FILENAME)) return;
|
let cacheContent;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
data = JSON.parse(fs.readFileSync(FILENAME));
|
cacheContent = fs.readFileSync(FILENAME);
|
||||||
} catch (err) {
|
} catch (e) {
|
||||||
return;
|
switch (e.code) {
|
||||||
|
// check EACCES only as fs.readFileSync will never throw EPERM on Windows
|
||||||
|
// https://github.com/libuv/libuv/blob/076df64dbbda4320f93375913a728efc40e12d37/src/win/fs.c#L735
|
||||||
|
case "EACCES":
|
||||||
|
console.warn(
|
||||||
|
`Babel could not read cache file: ${FILENAME}
|
||||||
|
due to a permission issue. Cache is disabled.`,
|
||||||
|
);
|
||||||
|
cacheDisabled = true;
|
||||||
|
/* fall through */
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
data = JSON.parse(cacheContent);
|
||||||
|
} catch {}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -7,12 +7,12 @@ const oldBabelDisableCacheValue = process.env.BABEL_DISABLE_CACHE;
|
|||||||
process.env.BABEL_CACHE_PATH = testCacheFilename;
|
process.env.BABEL_CACHE_PATH = testCacheFilename;
|
||||||
delete process.env.BABEL_DISABLE_CACHE;
|
delete process.env.BABEL_DISABLE_CACHE;
|
||||||
|
|
||||||
function writeCache(data) {
|
function writeCache(data, mode = 0o666) {
|
||||||
if (typeof data === "object") {
|
if (typeof data === "object") {
|
||||||
data = JSON.stringify(data);
|
data = JSON.stringify(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.writeFileSync(testCacheFilename, data);
|
fs.writeFileSync(testCacheFilename, data, { mode });
|
||||||
}
|
}
|
||||||
|
|
||||||
function cleanCache() {
|
function cleanCache() {
|
||||||
@ -73,5 +73,39 @@ describe("@babel/register - caching", () => {
|
|||||||
expect(fs.existsSync(testCacheFilename)).toBe(true);
|
expect(fs.existsSync(testCacheFilename)).toBe(true);
|
||||||
expect(get()).toEqual({});
|
expect(get()).toEqual({});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should create the cache after load", cb => {
|
||||||
|
load();
|
||||||
|
|
||||||
|
process.nextTick(() => {
|
||||||
|
expect(fs.existsSync(testCacheFilename)).toBe(true);
|
||||||
|
expect(get()).toEqual({});
|
||||||
|
cb();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Only write mode is effective on Windows
|
||||||
|
if (process.platform !== "win32") {
|
||||||
|
it("should be disabled when CACHE_PATH is not allowed to read", () => {
|
||||||
|
writeCache({ foo: "bar" }, 0o266);
|
||||||
|
|
||||||
|
load();
|
||||||
|
|
||||||
|
expect(get()).toEqual({});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
it("should be disabled when CACHE_PATH is not allowed to write", cb => {
|
||||||
|
writeCache({ foo: "bar" }, 0o466);
|
||||||
|
|
||||||
|
load();
|
||||||
|
|
||||||
|
expect(get()).toEqual({ foo: "bar" });
|
||||||
|
process.nextTick(() => {
|
||||||
|
load();
|
||||||
|
expect(get()).toEqual({});
|
||||||
|
cb();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user