Get rid of outdated_dependencies

Reviewed By: jeanlauliac

Differential Revision: D6485979

fbshipit-source-id: 8cf21a8c531d96df93f66e1cc465fe7ae044ef8e
This commit is contained in:
Rafael Oleza 2017-12-05 10:38:05 -08:00 committed by Facebook Github Bot
parent d8cb904104
commit 0e583a981f
4 changed files with 24 additions and 52 deletions

View File

@ -37,10 +37,7 @@ export type CachedResult = {
map: CompactRawMappings, map: CompactRawMappings,
}; };
export type TransformCacheResult = {| export type TransformCacheResult = ?CachedResult;
+result: ?CachedResult,
+outdatedDependencies: $ReadOnlyArray<string>,
|};
export type CacheOptions = { export type CacheOptions = {
reporter: Reporter, reporter: Reporter,
@ -74,8 +71,6 @@ export type TransformCache = {
readSync(props: ReadTransformProps): TransformCacheResult, readSync(props: ReadTransformProps): TransformCacheResult,
}; };
const EMPTY_ARRAY = [];
/* 1 day */ /* 1 day */
const GARBAGE_COLLECTION_PERIOD = 24 * 60 * 60 * 1000; const GARBAGE_COLLECTION_PERIOD = 24 * 60 * 60 * 1000;
/* 4 days */ /* 4 days */
@ -181,7 +176,7 @@ class FileBasedCache {
return this._readFilesSync(props); return this._readFilesSync(props);
} catch (error) { } catch (error) {
if (error.code === 'ENOENT') { if (error.code === 'ENOENT') {
return {result: null, outdatedDependencies: EMPTY_ARRAY}; return null;
} }
throw error; throw error;
} }
@ -191,11 +186,11 @@ class FileBasedCache {
const cacheFilePaths = this._getCacheFilePaths(props); const cacheFilePaths = this._getCacheFilePaths(props);
const metadata = readMetadataFileSync(cacheFilePaths.metadata); const metadata = readMetadataFileSync(cacheFilePaths.metadata);
if (metadata == null) { if (metadata == null) {
return {result: null, outdatedDependencies: EMPTY_ARRAY}; return null;
} }
const sourceHash = hashSourceCode(props); const sourceHash = hashSourceCode(props);
if (sourceHash !== metadata.cachedSourceHash) { if (sourceHash !== metadata.cachedSourceHash) {
return {result: null, outdatedDependencies: metadata.dependencies}; return null;
} }
const transformedCode = fs.readFileSync( const transformedCode = fs.readFileSync(
cacheFilePaths.transformedCode, cacheFilePaths.transformedCode,
@ -206,15 +201,12 @@ class FileBasedCache {
.update(transformedCode) .update(transformedCode)
.digest('hex'); .digest('hex');
if (metadata.cachedResultHash !== codeHash) { if (metadata.cachedResultHash !== codeHash) {
return {result: null, outdatedDependencies: metadata.dependencies}; return null;
} }
return { return {
result: { code: transformedCode,
code: transformedCode, dependencies: metadata.dependencies,
dependencies: metadata.dependencies, map: metadata.sourceMap,
map: metadata.sourceMap,
},
outdatedDependencies: EMPTY_ARRAY,
}; };
} }
@ -411,10 +403,7 @@ function unlinkIfExistsSync(filePath: string) {
function none(): TransformCache { function none(): TransformCache {
return { return {
writeSync: () => {}, writeSync: () => {},
readSync: () => ({ readSync: () => null,
result: null,
outdatedDependencies: [],
}),
}; };
} }

View File

@ -49,10 +49,7 @@ class TransformCacheMock {
} }
readSync(props) { readSync(props) {
return { return transformCache.get(transformCacheKeyOf(props));
result: transformCache.get(transformCacheKeyOf(props)),
outdatedDependencies: [],
};
} }
} }

View File

@ -87,7 +87,7 @@ describe('TransformCaching.FileBasedCache', () => {
...args, ...args,
cacheOptions: {reporter: {}, resetCache: false}, cacheOptions: {reporter: {}, resetCache: false},
}); });
expect(cachedResult.result).toEqual(result); expect(cachedResult).toEqual(result);
}); });
}); });
@ -119,7 +119,7 @@ describe('TransformCaching.FileBasedCache', () => {
...args, ...args,
cacheOptions: {reporter: {}, resetCache: false}, cacheOptions: {reporter: {}, resetCache: false},
}); });
expect(cachedResult.result).toEqual(result); expect(cachedResult).toEqual(result);
}); });
allCases.pop(); allCases.pop();
allCases.forEach(entry => { allCases.forEach(entry => {
@ -127,8 +127,7 @@ describe('TransformCaching.FileBasedCache', () => {
...argsFor(entry), ...argsFor(entry),
cacheOptions: {reporter: {}, resetCache: false}, cacheOptions: {reporter: {}, resetCache: false},
}); });
expect(cachedResult.result).toBeNull(); expect(cachedResult).toBeNull();
expect(cachedResult.outdatedDependencies).toEqual(['foo', 'bar']);
}); });
}); });
}); });

View File

@ -19,7 +19,7 @@ const invariant = require('fbjs/lib/invariant');
const isAbsolutePath = require('absolute-path'); const isAbsolutePath = require('absolute-path');
const jsonStableStringify = require('json-stable-stringify'); const jsonStableStringify = require('json-stable-stringify');
const {join: joinPath, relative: relativePath, extname} = require('path'); const {join: joinPath, relative: relativePath} = require('path');
import type { import type {
TransformedCode, TransformedCode,
@ -44,10 +44,7 @@ export type ReadResult = {
+source: string, +source: string,
}; };
export type CachedReadResult = {| export type CachedReadResult = ?ReadResult;
+result: ?ReadResult,
+outdatedDependencies: $ReadOnlyArray<string>,
|};
export type TransformCode = ( export type TransformCode = (
module: Module, module: Module,
@ -248,7 +245,7 @@ class Module {
* To what we read from the cache or worker, we need to add id and source. * To what we read from the cache or worker, we need to add id and source.
*/ */
_finalizeReadResult(source: string, result: TransformedCode): ReadResult { _finalizeReadResult(source: string, result: TransformedCode): ReadResult {
return {...result, id: this._getHasteName(), source}; return {...result, source};
} }
async _transformCodeFor( async _transformCodeFor(
@ -298,8 +295,9 @@ class Module {
read(transformOptions: WorkerOptions): Promise<ReadResult> { read(transformOptions: WorkerOptions): Promise<ReadResult> {
return Promise.resolve().then(() => { return Promise.resolve().then(() => {
const cached = this.readCached(transformOptions); const cached = this.readCached(transformOptions);
if (cached.result != null) {
return cached.result; if (cached != null) {
return cached;
} }
return this.readFresh(transformOptions); return this.readFresh(transformOptions);
}); });
@ -334,19 +332,11 @@ class Module {
transformOptionsKey, transformOptionsKey,
); );
const cachedResult = this._options.transformCache.readSync(cacheProps); const cachedResult = this._options.transformCache.readSync(cacheProps);
if (cachedResult.result == null) {
return { if (cachedResult == null) {
result: null, return null;
outdatedDependencies: cachedResult.outdatedDependencies,
};
} }
return { return this._finalizeReadResult(cacheProps.sourceCode, cachedResult);
result: this._finalizeReadResult(
cacheProps.sourceCode,
cachedResult.result,
),
outdatedDependencies: [],
};
} }
/** /**
@ -368,10 +358,7 @@ class Module {
cacheProps.sourceCode, cacheProps.sourceCode,
freshResult, freshResult,
); );
this._readResultsByOptionsKey.set(key, { this._readResultsByOptionsKey.set(key, finalResult);
result: finalResult,
outdatedDependencies: [],
});
return finalResult; return finalResult;
})(); })();
this._readPromises.set(key, freshPromise); this._readPromises.set(key, freshPromise);