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

Resolves #183: pass the transformed list of files to metalsmith.match… #184

Merged
merged 1 commit into from
May 15, 2022

Conversation

exortech
Copy link
Contributor

… so that renamed files can be matched

#183

…mith.match so that renamed files can be matched
@webketje
Copy link
Member

webketje commented May 12, 2022

@exortech thx, can you give me a short step-by-step instruction to reproduce the issue (or a GH repo or repl.it). The 'cached' copy of metalsmith files that metalsmith.match uses when you omit the second param, should be a reference to one and the same object. So even when file keys change, they should still be matched.

If this only occurs on repeat runs using metalsmith-watch that plugin probably does something unexpected with the files object (like, cloning it). FWIW I agree with comments from previous metalsmith owners & maintainers in previous ms-watch issues (don't remember which ones in which repo by heart) that watching should rather be done with a separate tool like https://www.npmjs.com/package/chokidar instead of as an ms plugin.

That's not an excuse, and this PR is really good to go, but before merging I would like to understand the exact cause.

Update
just had a look at metalsmith-watch source: it looks unmaintained, with outdated deps and likely to break as it contains a lot of old fixes for ms-collections. And I confirmed my suspicion that it re-used a metalsmith instance to run on a different files object. However I may port this issue to the main metalsmith codebase and release a bugfix there to re-set files on every run.

@exortech
Copy link
Contributor Author

@webketje this is the repo for one of the static sites we build with Metalsmith that was having the issue: https://github.com/doteco/www. If you check out build.js you can see the list of plugins used in the build.

It's a bit complicated to produce a simple repro; however, the gist is that, as you noted, metalsmith-watch creates a new file list (containing only the modified files to rebuild) and then invokes metalsmith.run.

I'm not that familiar with the metalsmith codebase, so I can't speak to the best way to fix it. However, while perhaps unmaintained, metalsmith-watch is a really useful plugin. And I imagine that there are potentially other plugins out there that could be doing something similar. To prevent those plugins from breaking, it may be best to preserve the original behaviour - especially as it is a very simple change to ensure that the file list is passed it. It's behaviour that's already supported by the existing interface.

Also, it seems like there's no reason why metalsmith-layouts should assume that the original files list is what is being passed to the plugin. I'm unclear why it's necessary for metalsmith to maintain this state instead of just operating as an immutable, pure function.

@webketje
Copy link
Member

[...] it may be best to preserve the original behaviour - especially as it is a very simple change to ensure that the file list is passed it. It's behaviour that's already supported by the existing interface.
Also, it seems like there's no reason why metalsmith-layouts should assume that the original files list is what is being passed to the plugin.

I totally agree with you on this.

I'm unclear why it's necessary for metalsmith to maintain this state instead of just operating as an immutable, pure function.

It is not necessary. The _files "cache" was a quick solution to provide a super-easy signature for metalsmith.match but the mechanism needs to be made more bullet-proof. The run function should indeed act as an immutable pure function.

@webketje
Copy link
Member

webketje commented May 13, 2022

@exortech Could you help validating an alternative patch to Metalsmith itself works by temporarily replacing node_modules/metalsmith/lib/index.js contents with those in the hotfix branch I just pushed here: https://github.com/metalsmith/metalsmith/blob/hotfix/ms-match/lib/index.js and then do some repeat runs?

@webketje webketje merged commit 6d24e90 into metalsmith:master May 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants