From 37fceda7e8c60c7908dc099d9413bf2dd7c2daf3 Mon Sep 17 00:00:00 2001 From: Caitlin Potter Date: Tue, 16 Jun 2015 11:24:29 -0500 Subject: [PATCH] chore(broccoli): improve `overwrite` error in merge-trees Modified the error message to include the relative duplicate path, to help in diagnosing the cause of the error message. Closes #2521 --- tools/broccoli/broccoli-merge-trees.spec.ts | 8 ++++++-- tools/broccoli/broccoli-merge-trees.ts | 11 ++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/tools/broccoli/broccoli-merge-trees.spec.ts b/tools/broccoli/broccoli-merge-trees.spec.ts index 8e5f5a5d35..c1c659f546 100644 --- a/tools/broccoli/broccoli-merge-trees.spec.ts +++ b/tools/broccoli/broccoli-merge-trees.spec.ts @@ -55,7 +55,9 @@ describe('MergeTrees', () => { let treeDiffer = MakeTreeDiffers(['tree1', 'tree2', 'tree3']); let treeMerger = mergeTrees(['tree1', 'tree2', 'tree3'], 'dest', {}); expect(() => treeMerger.rebuild(treeDiffer.diffTrees())) - .toThrowError("`overwrite` option is required for handling duplicates."); + .toThrowError( + 'Duplicate path found while merging trees. Path: "foo.js".\n' + + 'Either remove the duplicate or enable the "overwrite" option for this merge.'); testDir = { 'tree1': {'foo.js': mockfs.file({content: 'tree1/foo.js content', mtime: new Date(1000)})}, @@ -82,6 +84,8 @@ describe('MergeTrees', () => { testDir.tree2['foo.js'] = mockfs.file({content: 'tree2/foo.js content', mtime: new Date(1000)}); mockfs(testDir); expect(() => treeMerger.rebuild(treeDiffer.diffTrees())) - .toThrowError("`overwrite` option is required for handling duplicates."); + .toThrowError( + 'Duplicate path found while merging trees. Path: "foo.js".\n' + + 'Either remove the duplicate or enable the "overwrite" option for this merge.'); }); }); diff --git a/tools/broccoli/broccoli-merge-trees.ts b/tools/broccoli/broccoli-merge-trees.ts index ac2e6043ec..b749e4c92f 100644 --- a/tools/broccoli/broccoli-merge-trees.ts +++ b/tools/broccoli/broccoli-merge-trees.ts @@ -16,6 +16,11 @@ function outputFileSync(sourcePath, destPath) { symlinkOrCopySync(sourcePath, destPath); } +function pathOverwrittenError(path) { + const msg = 'Either remove the duplicate or enable the "overwrite" option for this merge.'; + return new Error(`Duplicate path found while merging trees. Path: "${path}".\n${msg}`); +} + export class MergeTrees implements DiffingBroccoliPlugin { private pathCache: {[key: string]: number[]} = Object.create(null); public options: MergeTreesOptions; @@ -59,7 +64,7 @@ export class MergeTrees implements DiffingBroccoliPlugin { // ASSERT(contains(pathsToEmit, changedPath)); cache.unshift(index); } else { - throw new Error("`overwrite` option is required for handling duplicates."); + throw pathOverwrittenError(changedPath); } }); }); @@ -79,7 +84,7 @@ export class MergeTrees implements DiffingBroccoliPlugin { this.pathCache[removedPath] = undefined; } else if (!emitted[removedPath]) { if (cache.length === 1 && !overwrite) { - throw new Error("`overwrite` option is required for handling duplicates."); + throw pathOverwrittenError(removedPath); } emit(removedPath); } @@ -102,7 +107,7 @@ export class MergeTrees implements DiffingBroccoliPlugin { cache.push(index); cache.sort((a, b) => a - b); if (cache.length > 1 && !overwrite) { - throw new Error("`overwrite` option is required for handling duplicates."); + throw pathOverwrittenError(changedPath); } if (cache[cache.length - 1] === index && !emitted[changedPath]) { emit(changedPath);