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

Empty module ID causes unreliable file hashes #308

Closed
erykpiast opened this issue Nov 19, 2018 · 11 comments
Closed

Empty module ID causes unreliable file hashes #308

erykpiast opened this issue Nov 19, 2018 · 11 comments

Comments

@erykpiast
Copy link

erykpiast commented Nov 19, 2018

Original discussion: #284

ID of CssModule is currently set to empty string during initialization. When module ID is somehow empty, NamedModulesPlugin inside webpack creates name like ?abcdef. For regular ones (or Normal in webpack's nomenclature) it outputs something like ./foo/bar/baz.js?abcdef. Until that ?... hash is based on FULL request path (ex. /home/user/projects/foo/bar/baz.js), it's different for different repository clones, ex. on different workstations and on CI. It makes file hashes quite unpredictable as sorting modules by id gives different results on different machines and hash calculation depends on that order (hash.update('a') && hash.update('b') gives different results than hash.update('b') && hash.update('a')). It may be an issue even on CI, if repository is cloned to directory prefixed with build ID, so absolute path differs from build to build. In the worst case, file hash changes when its contents did not change, so the whole idea of hashing loses its sense.

The issue was discussed already in https://github.com/webpack-contrib/mini-css-extract-plugin/issues/281#issuecomment-423300860, but for some not obvious reason soultion proposed by @lfre was rejected.

@alexander-akait
Copy link
Member

@erykpiast please create minimum reproducible test repo

@erykpiast
Copy link
Author

@erykpiast
Copy link
Author

@evilebottnawi Have you had time to look at example I've created?

@alexander-akait
Copy link
Member

alexander-akait commented Dec 7, 2018

@erykpiast sorry, no, working on sass-loader/style-loader/terser-webpack-plugin and webpack-dev-server, feel free to investigate and feedback, a lot of issues and very few developers 😞

@erykpiast
Copy link
Author

Sure, I understand. The thing is I actually have a solution (as described in the issue itself), but I need some people with knowledge and authority to judge if it's OK conceptually.

@lfre
Copy link
Contributor

lfre commented Dec 7, 2018

@erykpiast I took a look at your repo and there are a couple of issues.

First, both the identifier and context contain the full disk path. These are the ones causing the issue because even if you change the id, those identifiers produce the hash after. e.g ?abcd. I think you could either trim that to be just the filename or root path (which would fix your issue with CI producing different build id folders). More generally, this exposes a larger problem within webpack, that if you move the file to another folder or rename the filename, you'll get a different hash even when the contents haven't changed. However, if you change _identifier to be dependency.content, that'd fix the issue but I think the best way is for wherever the piece of code that produces the hash ?abcd to pick up this.content instead. I'm not sure where that is happening.

Second, there is an issue with the order as you mentioned. From chunk.modulesIterable https://github.com/webpack-contrib/mini-css-extract-plugin/blob/master/src/index.js#L242. In dir1 the order is foo.css, bar.css, but dir2 is bar.css, and foo.css, and since they have different content, triggering hash.update in a different order, will give you a different final hash. This would still change the hash even if you fixed the first issue. That's an issue on webpack. Ideally, the order should always match the import order from the files.

I can't speak for what possible issues changing this._identifier to dependency.content would do, so I'll defer to @evilebottnawi here.

Hope that helps everyone!

@alexander-akait
Copy link
Member

@lfre i am very busy, feel free to send a PR if you have ideas how we can fix it

@alexander-akait
Copy link
Member

Fixed in webpack@5

@alexander-akait
Copy link
Member

Output:

// ...
dir1/dist/main_26753f81.css
dir2/dist/main_26753f81.css
dir3/dist/main_26753f81.css
dir1/dist/main_0f0128e1.js
dir2/dist/main_0f0128e1.js
dir3/dist/main_0f0128e1.js

@alexander-akait
Copy link
Member

After investigation I want to say it is realy hard to fix on webpack@4 due some internal problems, and it can even break the hashes for other cases, so my recommendation - consider to update webpack to v5, anyway if you faced with the same problem again please open a new issue

@erykpiast
Copy link
Author

Perfect! Thank you for handling this @evilebottnawi ❤️

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

No branches or pull requests

3 participants