collections: Repeat runs with collections adds duplicates

Currently, repeated calls to metalsmith.build() is non-idempotent because metalsmith-collections will find the same files that belongs to a collection, and add them to the same array as exists in memory from the previous run.

This is due to metalsmith storing collections in the global metadata object. Maybe it should empty out the collections arrays every time it runs?

My workaround right now is essentially

metalsmith.build(function(err, files) {
  if (err) throw err;
  metalsmith.metadata(origMetadata);
});

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 5
  • Comments: 25 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Forked this for now and added the metadata[key] fix mentioned by @deltamualpha

https://github.com/spacedawwwg/metalsmith-collections

use this forked version by adding

"devDependencies": {
    "metalsmith-collections": "spacedawwwg/metalsmith-collections"
}

to your package.json

I’ve submitted a pull request, but I’ll keep the fork updated until this finally gets fixed (it has been a year since this issue was posted)

Here’s another fix to force the metadata to reset every build. Similar idea to @AndyOGo.

metalsmith('./src')
	.use((files, metalsmith, done) => {
		setImmediate(done);
		metalsmith.metadata({
			site: {},
			package: require( './package.json')
		});
	})

@AndyOGo please see the commit 891b24c which adds a test for this. In the latest published version, I did quite some refactoring and suspected I incidentally fixed it (I think it was even fixed long ago but just in a messy way and without updating this issue) See also https://github.com/metalsmith/collections/blob/master/lib/index.js#L91

EDIT: My test should have been sequential, not parallel. Updated locally, but the conclusion remains, yes it’s fixed.

To avoid this issue, install by https the new version of metalsmith-collections.

npm install --save-dev git+https://github.com/segmentio/metalsmith-collections.git

This will be install the latest version of plugin, since metalsmith org still does not have permission to push to NPM yet.

Did this ever get fixed @spacedawwwg ? BTW - thanks for the temp fix.

Another quickfix is to monkey patch the build method, like:

var build = metalsmith.build;
metalsmith.build = function() {
  build.apply(ms, arguments)
  ms.metadata(originalMeta));
}

This whipes it after every build and works with metalsmith-browser-sync too

👍 fixed it locally by checking the metadata[key] array for the same file before adding it. A real fix would be nice.