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
Show file tree
Hide file tree
Changes from all 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
61 changes: 54 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,68 @@ 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()) {
data = {};
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) {
// 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 {}
}

/**
Expand Down
38 changes: 36 additions & 2 deletions packages/babel-register/test/cache.js
Expand Up @@ -7,12 +7,12 @@ const oldBabelDisableCacheValue = process.env.BABEL_DISABLE_CACHE;
process.env.BABEL_CACHE_PATH = testCacheFilename;
delete process.env.BABEL_DISABLE_CACHE;

function writeCache(data) {
function writeCache(data, mode = 0o666) {
if (typeof data === "object") {
data = JSON.stringify(data);
}

fs.writeFileSync(testCacheFilename, data);
fs.writeFileSync(testCacheFilename, data, { mode });
}

function cleanCache() {
Expand Down Expand Up @@ -73,5 +73,39 @@ describe("@babel/register - caching", () => {
expect(fs.existsSync(testCacheFilename)).toBe(true);
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();
});
});
});
});