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

Changing the entryNames placeholder value generates different (inconsistent) hashes. #1857

Closed
barca-reddit opened this issue Dec 16, 2021 · 2 comments

Comments

@barca-reddit
Copy link

Hello.

Forgive me in advance if I am missing something obvious here, but shouldn't the hashes generated by adding a [hash] inside the template value for entryNames be based on the contents of the output file itself?

Consider the following super simple example:

style.css

html,
body {
  margin: 0;
}

esbuild.js

const esbuild = require('esbuild');

esbuild.build({
    entryPoints: ['style.css'],
    entryNames: 'foo.[hash]',
    outdir: 'out',
    logLevel: 'info'
});

As expected, running this would produce a foo.KRQXUUXA.css file inside a newly created out directory. So far so good. But consider changing the template a little bit:

const esbuild = require('esbuild');

esbuild.build({
    entryPoints: ['style.css'],
    entryNames: 'bar.[hash]', // <= foo becomes bar here
    outdir: 'out',
    logLevel: 'info'
});

This produces a the absolute same file, but named differently - bar.HKT67FGH.css. You can even change the template of entryNames to entryNames: '[dir]/bar.[hash]' and it will once again produce another file with a different hash. Why is that?

$ md5sum out/foo.KRQXUUXA.css out/bar.HKT67FGH.css

c629501285affcfba9a62e093b927f6e  out/foo.KRQXUUXA.css
c629501285affcfba9a62e093b927f6e  out/bar.HKT67FGH.css

I am using "esbuild": "^0.14.5" on Ubuntu 20.04 if that is of any importance.

Thanks in advance!

@barca-reddit barca-reddit changed the title Changing the entryNames placeholder value generates a different (inconsistent) hashes. Changing the entryNames placeholder value generates different (inconsistent) hashes. Dec 16, 2021
@evanw
Copy link
Owner

evanw commented Dec 16, 2021

shouldn't the hashes generated by adding a [hash] inside the template value for entryNames be based on the contents of the output file itself?

No. Using the contents of the output file for the hash is incorrect, and causes bugs like this: #928. In JavaScript, two input files with the same contents are not considered the same file by the runtime. Loading both of them will load the module twice, not once, and the functions and objects in each file have their own object identity. The hash needs to take into account the file path too because it needs to respect JavaScript's semantics.

@barca-reddit
Copy link
Author

barca-reddit commented Dec 16, 2021

If this is something unique to esbuild, perhaps consider adding a quick paragraph about it in the docs, because I think when most people hear the word hash, they imagine it relates to the file contents alone, so a quick disclaimer about it would be really nice to avoid potential confusion in the future.

I could be wrong about either of those things, but at least that's how I imagined this works. But in any case this is not a big issue so thanks for the quick response and thanks for building this amazing tool!

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

2 participants