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 recovery of haste collisions #7329

Merged
merged 3 commits into from
Nov 7, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
- `[jest-runtime]` Ensure error message text is not lost on errors with code frames ([#7319](https://github.com/facebook/jest/pull/7319))
- `[jest-haste-map]` Fix to resolve path that is start with words same as rootDir ([#7324](https://github.com/facebook/jest/pull/7324))
- `[expect]` Fix toMatchObject matcher when used with `Object.create(null)` ([#7334](https://github.com/facebook/jest/pull/7334))
- `[jest-haste-map]` [**BREAKING**] Recover files correctly after haste name collisions are fixed ([#7329](https://github.com/facebook/jest/pull/7329))

### Chore & Maintenance

Expand Down
35 changes: 19 additions & 16 deletions packages/jest-haste-map/src/ModuleMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import type {
import * as fastPath from './lib/fast_path';
import H from './constants';

const EMPTY_MAP = {};
const EMPTY_OBJ = {};
const EMPTY_MAP = new Map();

export opaque type SerializableModuleMap = {
// There is no easier way to extract the type of the entries of a Map
Expand Down Expand Up @@ -117,14 +118,14 @@ export default class ModuleMap {
platform: ?string,
supportsNativePlatform: boolean,
): ?ModuleMetaData {
const map = this._raw.map.get(name) || EMPTY_MAP;
const map = this._raw.map.get(name) || EMPTY_OBJ;
const dupMap = this._raw.duplicates.get(name) || EMPTY_MAP;
if (platform != null) {
this._assertNoDuplicates(
name,
platform,
supportsNativePlatform,
dupMap[platform],
dupMap.get(platform),
);
if (map[platform] != null) {
return map[platform];
Expand All @@ -135,7 +136,7 @@ export default class ModuleMap {
name,
H.NATIVE_PLATFORM,
supportsNativePlatform,
dupMap[H.NATIVE_PLATFORM],
dupMap.get(H.NATIVE_PLATFORM),
);
if (map[H.NATIVE_PLATFORM]) {
return map[H.NATIVE_PLATFORM];
Expand All @@ -145,7 +146,7 @@ export default class ModuleMap {
name,
H.GENERIC_PLATFORM,
supportsNativePlatform,
dupMap[H.GENERIC_PLATFORM],
dupMap.get(H.GENERIC_PLATFORM),
);
if (map[H.GENERIC_PLATFORM]) {
return map[H.GENERIC_PLATFORM];
Expand All @@ -163,17 +164,19 @@ export default class ModuleMap {
return;
}
// Force flow refinement
const previousSet: DuplicatesSet = relativePathSet;
const set = Object.keys(previousSet).reduce((set, relativePath) => {
const previousSet = relativePathSet;
const duplicates = new Map();

for (const [relativePath, type] of previousSet) {
const duplicatePath = fastPath.resolve(this._raw.rootDir, relativePath);
set[duplicatePath] = previousSet[relativePath];
return set;
}, Object.create(null));
duplicates.set(duplicatePath, type);
}

throw new DuplicateHasteCandidatesError(
name,
platform,
supportsNativePlatform,
set,
duplicates,
);
}

Expand Down Expand Up @@ -206,12 +209,12 @@ class DuplicateHasteCandidatesError extends Error {
`files, or packages, that provide a module for ` +
`that particular name and platform. ${platformMessage} You must ` +
`delete or blacklist files until there remains only one of these:\n\n` +
Object.keys(duplicatesSet)
Array.from(duplicatesSet)
.map(
([dupFilePath, dupFileType]) =>
` * \`${dupFilePath}\` (${getTypeMessage(dupFileType)})\n`,
)
.sort()
.map(dupFilePath => {
const typeMessage = getTypeMessage(duplicatesSet[dupFilePath]);
return ` * \`${dupFilePath}\` (${typeMessage})\n`;
})
.join(''),
);
this.hasteName = name;
Expand Down
75 changes: 63 additions & 12 deletions packages/jest-haste-map/src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,9 +783,12 @@ describe('HasteMap', () => {
).build();
expect(normalizeMap(data.duplicates)).toEqual(
createMap({
Strawberry: {
g: {'fruits/Strawberry.js': 0, 'fruits/another/Strawberry.js': 0},
},
Strawberry: createMap({
g: createMap({
'fruits/Strawberry.js': H.MODULE,
'fruits/another/Strawberry.js': H.MODULE,
}),
}),
}),
);
expect(data.map.get('Strawberry')).toEqual({});
Expand All @@ -806,10 +809,54 @@ describe('HasteMap', () => {
).build();
expect(normalizeMap(data.duplicates)).toEqual(new Map());
expect(data.map.get('Strawberry')).toEqual({
g: ['fruits/Strawberry.js', 0],
g: ['fruits/Strawberry.js', H.MODULE],
});
// Make sure the other files are not affected.
expect(data.map.get('Banana')).toEqual({g: ['fruits/Banana.js', 0]});
expect(data.map.get('Banana')).toEqual({
g: ['fruits/Banana.js', H.MODULE],
});
});

it('recovers with the correct type when a duplicate file is deleted', async () => {
mockFs['/project/fruits/strawberryPackage/package.json'] = `
{"name": "Strawberry"}
`;

const {__hasteMapForTest: data} = await new HasteMap(
defaultConfig,
).build();

expect(normalizeMap(data.duplicates)).toEqual(
createMap({
Strawberry: createMap({
g: createMap({
'fruits/Strawberry.js': H.MODULE,
'fruits/another/Strawberry.js': H.MODULE,
'fruits/strawberryPackage/package.json': H.PACKAGE,
}),
}),
}),
);

delete mockFs['/project/fruits/another/Strawberry.js'];
delete mockFs['/project/fruits/strawberryPackage/package.json'];

mockChangedFiles = object({
'/project/fruits/another/Strawberry.js': null,
'/project/fruits/strawberryPackage/package.json': null,
});
mockClocks = createMap({
fruits: 'c:fake-clock:4',
});

const {__hasteMapForTest: correctData} = await new HasteMap(
defaultConfig,
).build();

expect(normalizeMap(correctData.duplicates)).toEqual(new Map());
expect(correctData.map.get('Strawberry')).toEqual({
g: ['fruits/Strawberry.js', H.MODULE],
});
});

it('recovers when a duplicate module is renamed', async () => {
Expand All @@ -829,13 +876,15 @@ describe('HasteMap', () => {
).build();
expect(normalizeMap(data.duplicates)).toEqual(new Map());
expect(data.map.get('Strawberry')).toEqual({
g: ['fruits/Strawberry.js', 0],
g: ['fruits/Strawberry.js', H.MODULE],
});
expect(data.map.get('Pineapple')).toEqual({
g: ['fruits/another/Pineapple.js', 0],
g: ['fruits/another/Pineapple.js', H.MODULE],
});
// Make sure the other files are not affected.
expect(data.map.get('Banana')).toEqual({g: ['fruits/Banana.js', 0]});
expect(data.map.get('Banana')).toEqual({
g: ['fruits/Banana.js', H.MODULE],
});
});
});

Expand Down Expand Up @@ -1243,10 +1292,12 @@ describe('HasteMap', () => {
expect(error.hasteName).toBe('Pear');
expect(error.platform).toBe('g');
expect(error.supportsNativePlatform).toBe(false);
expect(error.duplicatesSet).toEqual({
'/project/fruits/Pear.js': 0,
'/project/fruits/another/Pear.js': 0,
});
expect(error.duplicatesSet).toEqual(
createMap({
'/project/fruits/Pear.js': H.MODULE,
'/project/fruits/another/Pear.js': H.MODULE,
}),
);
expect(error.message).toMatchSnapshot();
}
}
Expand Down
48 changes: 31 additions & 17 deletions packages/jest-haste-map/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,20 +430,24 @@ class HasteMap extends EventEmitter {
}
let dupsByPlatform = hasteMap.duplicates.get(id);
if (dupsByPlatform == null) {
dupsByPlatform = Object.create(null);
dupsByPlatform = new Map();
hasteMap.duplicates.set(id, dupsByPlatform);
}
const dups = (dupsByPlatform[platform] = Object.create(null));
dups[module[H.PATH]] = module[H.TYPE];
dups[existingModule[H.PATH]] = existingModule[H.TYPE];

const dups = new Map([
[module[H.PATH], module[H.TYPE]],
[existingModule[H.PATH], existingModule[H.TYPE]],
]);
dupsByPlatform.set(platform, dups);

return;
}

const dupsByPlatform = hasteMap.duplicates.get(id);
if (dupsByPlatform != null) {
const dups = dupsByPlatform[platform];
const dups = dupsByPlatform.get(platform);
if (dups != null) {
dups[module[H.PATH]] = module[H.TYPE];
dups.set(module[H.PATH], module[H.TYPE]);
}
return;
}
Expand Down Expand Up @@ -928,31 +932,37 @@ class HasteMap extends EventEmitter {
const platform =
getPlatformExtension(relativeFilePath, this._options.platforms) ||
H.GENERIC_PLATFORM;
let dups = dupsByPlatform[platform];
let dups = dupsByPlatform.get(platform);
if (dups == null) {
return;
}

dupsByPlatform = copy(dupsByPlatform);
dupsByPlatform = copyMap(dupsByPlatform);
hasteMap.duplicates.set(moduleName, dupsByPlatform);
dups = copy(dups);
dupsByPlatform[platform] = dups;

const dedupType = dups[relativeFilePath];
delete dups[relativeFilePath];
const filePaths = Object.keys(dups);
if (filePaths.length > 1) {
dups = copyMap(dups);
dupsByPlatform.set(platform, dups);
dups.delete(relativeFilePath);

if (dups.size !== 1) {
return;
}

const uniqueModule = dups.entries().next().value;

if (!uniqueModule) {
return;
}

let dedupMap = hasteMap.map.get(moduleName);

if (dedupMap == null) {
dedupMap = Object.create(null);
hasteMap.map.set(moduleName, dedupMap);
}
dedupMap[platform] = [filePaths[0], dedupType];
delete dupsByPlatform[platform];
if (Object.keys(dupsByPlatform).length === 0) {
dedupMap[platform] = uniqueModule;
dupsByPlatform.delete(platform);
if (dupsByPlatform.size === 0) {
hasteMap.duplicates.delete(moduleName);
}
}
Expand Down Expand Up @@ -1026,6 +1036,10 @@ class HasteMap extends EventEmitter {

const copy = object => Object.assign(Object.create(null), object);

function copyMap<K, V>(input: Map<K, V>): Map<K, V> {
return new Map(input);
}

HasteMap.H = H;
HasteMap.ModuleMap = HasteModuleMap;

Expand Down
10 changes: 2 additions & 8 deletions types/HasteMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,8 @@ export type ModuleMapData = Map<string, ModuleMapItem>;
export type WatchmanClocks = Map<Path, string>;
export type HasteRegExp = RegExp | ((str: string) => boolean);

export type DuplicatesSet = {
[filePath: string]: /* type */ number,
__proto__: null,
};
export type DuplicatesIndex = Map<
string,
{[platform: string]: DuplicatesSet, __proto__: null},
>;
export type DuplicatesSet = Map<string, /* type */ number>;
export type DuplicatesIndex = Map<string, Map<string, DuplicatesSet>>;

export type InternalHasteMap = {|
clocks: WatchmanClocks,
Expand Down