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

ChunkNames collision when dynamically importing files with same name in different directory #1099

Closed
Jarred-Sumner opened this issue Apr 1, 2021 · 12 comments

Comments

@Jarred-Sumner
Copy link
Contributor

Jarred-Sumner commented Apr 1, 2021

Exception

> error: Two output files share the same path but have different contents: static/chunk/[slug]-NCFBE2JN.js

This is with BuildConfig.ChunkNames set like this (in Go):

Config.ChunkNames = "chunk/[name]-[hash]"

Context

Say I have a dozen-ish pages like this:

posts/[slug].js
profiles/[slug].js
[slug].js

To facilitate client-side routing with code splitting, it code-generates a manifest like this:

export const PAGES = {
   "posts/[slug]": () => import("pages/posts/[slug]"),
   "profiles/[slug]": () => import("pages/profiles/[slug]"),
   "[slug]": () => import("pages/[slug]"),
}

This one manifest is imported into each entry point and loaded via a plugin. Pages are lazily added to BuildConfig.EntryPointsAdvanced on request (in development). When pages are eagerly added to BuildConfig.EntryPointsAdvanced, this exception does not occur. Note that since its a single manifest for the configuration, there is a cyclic dynamic import but it is never called at runtime.

For a reproducible example, would an invite to a repo be okay?

Suggestion

Maybe a [dir] option for ChunkNames is warranted for cases like this? Or maybe the [hash] could include the relative path of the files the chunk originated from?

@evanw
Copy link
Owner

evanw commented Apr 2, 2021

I think the problem here is that the [hash] calculation is wrong somehow. The point of the hash is that files with the different contents should not have the same hash. Really sorry about these issues. Hopefully we can get them fixed soon.

Or maybe the [hash] could include the relative path of the files the chunk originated from?

It's supposed to do this already:

// Then include the file path
hashWriteLengthPrefixed(hash, []byte(filePath))

For a reproducible example, would an invite to a repo be okay?

That sounds perfect. Thanks!

@Jarred-Sumner
Copy link
Contributor Author

Okay actually I found a simple-ish way to repro.

Here's a repository: https://github.com/Jarred-Sumner/dynamic-import-repro/tree/main

Just run node build.js (requires a version of Node that has fs.rm which is >= 14.14)

Debug log

 > debug: Started the scan phase

 > debug: Resolving import "./index-entry.js" in directory "/Users/jarredsumner/Code/dynamic-import-repro" of type "entry-point"
   note: Failed to find file "/index.tsx"
   note: Failed to find file "/index.ts"
   note: Failed to find file "/index.jsx"
   note: Failed to find file "/index.js"
   note: Failed to find file "/index.css"
   note: Failed to find file "/index.json"
   note: Read 19 entries for directory "/"
   note: Failed to find file "/Users/index.tsx"
   note: Failed to find file "/Users/index.ts"
   note: Failed to find file "/Users/index.jsx"
   note: Failed to find file "/Users/index.js"
   note: Failed to find file "/Users/index.css"
   note: Failed to find file "/Users/index.json"
   note: Read 3 entries for directory "/Users"
   note: The file "/Users/jarredsumner/package.json" exists
   note: Failed to find file "/Users/jarredsumner/index.tsx"
   note: Failed to find file "/Users/jarredsumner/index.ts"
   note: Failed to find file "/Users/jarredsumner/index.jsx"
   note: Failed to find file "/Users/jarredsumner/index.js"
   note: Failed to find file "/Users/jarredsumner/index.css"
   note: Failed to find file "/Users/jarredsumner/index.json"
   note: Read 186 entries for directory "/Users/jarredsumner"
   note: Failed to find file "/Users/jarredsumner/Code/index.tsx"
   note: Failed to find file "/Users/jarredsumner/Code/index.ts"
   note: Failed to find file "/Users/jarredsumner/Code/index.jsx"
   note: Found file "/Users/jarredsumner/Code/index.js"
   note: Read 172 entries for directory "/Users/jarredsumner/Code"
   note: The file "/Users/jarredsumner/Code/dynamic-import-repro/package.json" exists
   note: Attempting to load "/Users/jarredsumner/Code/dynamic-import-repro/index.js" as a file
   note: Found file "/Users/jarredsumner/Code/dynamic-import-repro/index.js"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/index.tsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/index.ts"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/index.jsx"
   note: Found file "/Users/jarredsumner/Code/dynamic-import-repro/index.js"
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro"
   note: Attempting to load "/Users/jarredsumner/Code/dynamic-import-repro/index-entry.js" as a file
   note: Found file "/Users/jarredsumner/Code/dynamic-import-repro/index-entry.js"
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro"
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro"
   note: Primary path is "/Users/jarredsumner/Code/dynamic-import-repro/index-entry.js" in namespace "file"

 > debug: Resolving import "./index" in directory "/Users/jarredsumner/Code/dynamic-import-repro" of type "import-statement"
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro"
   note: Attempting to load "/Users/jarredsumner/Code/dynamic-import-repro/index" as a file
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/index"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/index.tsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/index.ts"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/index.jsx"
   note: Found file "/Users/jarredsumner/Code/dynamic-import-repro/index.js"
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro"
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro"
   note: Primary path is "/Users/jarredsumner/Code/dynamic-import-repro/index.js" in namespace "file"

 > debug: Resolving import "react" in directory "/Users/jarredsumner/Code/dynamic-import-repro" of type "import-statement"
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro"
   note: Searching for "react" in "node_modules" directories starting from "/Users/jarredsumner/Code/dynamic-import-repro"
   note: Parsed package name "react" and package subpath "."
   note: Checking for a package in the directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react"
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/index.tsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/index.ts"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/index.jsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/index.js"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/index.css"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/index.json"
   note: Read 5 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules"
   note: Resolved symlink "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react" to "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react"
   note: The file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react/package.json" exists
   note: Attempting to load "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react/index.js" as a file
   note: Found file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react/index.js"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react/index.tsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react/index.ts"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react/index.jsx"
   note: Found file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react/index.js"
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react"
   note: Read 5 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules"
   note: Attempting to load "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react" as a file
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react.tsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react.ts"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react.jsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react.js"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react.css"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react.json"
   note: Attempting to load "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react" as a directory
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react"
   note: Resolved to "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react/index.js" using the "main" field in "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react/package.json"
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react"
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react"
   note: Resolved symlink "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react/index.js" to "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/index.js"
   note: Primary path is "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/index.js" in namespace "file"

 > debug: Resolving import "./cjs/react.development.js" in directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react" of type "require-call"
   note: Read 5 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/index.tsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/index.ts"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/index.jsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/index.js"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/index.css"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/index.json"
   note: Read 7 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/index.tsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/index.ts"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/index.jsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/index.js"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/index.css"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/index.json"
   note: Read 1 entry for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/index.tsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/index.ts"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/index.jsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/index.js"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/index.css"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/index.json"
   note: Read 3 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules"
   note: The file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/package.json" exists
   note: Attempting to load "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/index.js" as a file
   note: Found file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/index.js"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/index.tsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/index.ts"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/index.jsx"
   note: Found file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/index.js"
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/cjs/index.tsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/cjs/index.ts"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/cjs/index.jsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/cjs/index.js"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/cjs/index.css"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/cjs/index.json"
   note: Read 8 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/cjs"
   note: Attempting to load "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/cjs/react.development.js" as a file
   note: Found file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/cjs/react.development.js"
   note: Read 8 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/cjs"
   note: Read 8 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/cjs"
   note: Primary path is "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/cjs/react.development.js" in namespace "file"

 > debug: Resolving import "/Users/jarredsumner/Code/dynamic-import-repro/hi/[name]" in directory "/Users/jarredsumner/Code/dynamic-import-repro" of type "dynamic-import"
   note: The import "/Users/jarredsumner/Code/dynamic-import-repro/hi/[name]" is being treated as an absolute paths
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro"
   note: Attempting to load "/Users/jarredsumner/Code/dynamic-import-repro/hi/[name]" as a file
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/hi/[name]"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/hi/[name].tsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/hi/[name].ts"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/hi/[name].jsx"
   note: Found file "/Users/jarredsumner/Code/dynamic-import-repro/hi/[name].js"
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/hi/index.tsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/hi/index.ts"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/hi/index.jsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/hi/index.js"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/hi/index.css"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/hi/index.json"
   note: Read 1 entry for directory "/Users/jarredsumner/Code/dynamic-import-repro/hi"
   note: Primary path is "/Users/jarredsumner/Code/dynamic-import-repro/hi/[name].js" in namespace "file"

 > debug: Resolving import "/Users/jarredsumner/Code/dynamic-import-repro/[name]" in directory "/Users/jarredsumner/Code/dynamic-import-repro" of type "dynamic-import"
   note: The import "/Users/jarredsumner/Code/dynamic-import-repro/[name]" is being treated as an absolute paths
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro"
   note: Attempting to load "/Users/jarredsumner/Code/dynamic-import-repro/[name]" as a file
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/[name]"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/[name].tsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/[name].ts"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/[name].jsx"
   note: Found file "/Users/jarredsumner/Code/dynamic-import-repro/[name].js"
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro"
   note: Primary path is "/Users/jarredsumner/Code/dynamic-import-repro/[name].js" in namespace "file"

 > debug: Resolving import "/Users/jarredsumner/Code/dynamic-import-repro/index" in directory "/Users/jarredsumner/Code/dynamic-import-repro" of type "dynamic-import"
   note: The import "/Users/jarredsumner/Code/dynamic-import-repro/index" is being treated as an absolute paths
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro"
   note: Attempting to load "/Users/jarredsumner/Code/dynamic-import-repro/index" as a file
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/index"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/index.tsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/index.ts"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/index.jsx"
   note: Found file "/Users/jarredsumner/Code/dynamic-import-repro/index.js"
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro"
   note: Primary path is "/Users/jarredsumner/Code/dynamic-import-repro/index.js" in namespace "file"

 > debug: Resolving import "react" in directory "/Users/jarredsumner/Code/dynamic-import-repro/hi" of type "import-statement"
   note: Read 1 entry for directory "/Users/jarredsumner/Code/dynamic-import-repro/hi"
   note: Searching for "react" in "node_modules" directories starting from "/Users/jarredsumner/Code/dynamic-import-repro/hi"
   note: Parsed package name "react" and package subpath "."
   note: Checking for a package in the directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react"
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react"
   note: Read 5 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules"
   note: Attempting to load "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react" as a file
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react.tsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react.ts"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react.jsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react.js"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react.css"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react.json"
   note: Attempting to load "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react" as a directory
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react"
   note: Resolved to "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react/index.js" using the "main" field in "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react/package.json"
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react"
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react"
   note: Resolved symlink "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react/index.js" to "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/index.js"
   note: Primary path is "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/index.js" in namespace "file"

 > debug: Resolving import "react" in directory "/Users/jarredsumner/Code/dynamic-import-repro" of type "import-statement"
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro"
   note: Searching for "react" in "node_modules" directories starting from "/Users/jarredsumner/Code/dynamic-import-repro"
   note: Parsed package name "react" and package subpath "."
   note: Checking for a package in the directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react"
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react"
   note: Read 5 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules"
   note: Attempting to load "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react" as a file
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react.tsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react.ts"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react.jsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react.js"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react.css"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react.json"
   note: Attempting to load "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react" as a directory
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react"
   note: Resolved to "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react/index.js" using the "main" field in "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react/package.json"
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react"
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react"
   note: Resolved symlink "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/react/index.js" to "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/index.js"
   note: Primary path is "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/index.js" in namespace "file"

 > debug: Resolving import "object-assign" in directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/cjs" of type "require-call"
   note: Read 8 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/cjs"
   note: Searching for "object-assign" in "node_modules" directories starting from "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/cjs"
   note: Parsed package name "object-assign" and package subpath "."
   note: Checking for a package in the directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/node_modules/object-assign"
   note: Read 10 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/node_modules/index.tsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/node_modules/index.ts"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/node_modules/index.jsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/node_modules/index.js"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/node_modules/index.css"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/node_modules/index.json"
   note: Read 1 entry for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/node_modules"
   note: Failed to read directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/node_modules/object-assign": open /Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/node_modules/object-assign: no such file or directory
   note: Failed to read directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/node_modules/object-assign"
   note: Read 1 entry for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/node_modules"
   note: Attempting to load "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/node_modules/object-assign" as a file
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/node_modules/object-assign"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/node_modules/object-assign.tsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/node_modules/object-assign.ts"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/node_modules/object-assign.jsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/node_modules/object-assign.js"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/node_modules/object-assign.css"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/node_modules/object-assign.json"
   note: Attempting to load "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/node_modules/object-assign" as a directory
   note: Failed to read directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/react/node_modules/object-assign"
   note: Checking for a package in the directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/object-assign"
   note: Read 3 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules"
   note: Resolved symlink "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/object-assign" to "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/object-assign@4.1.1/node_modules/object-assign"
   note: The file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/object-assign/package.json" exists
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/object-assign/index.tsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/object-assign/index.ts"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/object-assign/index.jsx"
   note: Found file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/object-assign/index.js"
   note: Read 4 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/object-assign"
   note: Read 3 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules"
   note: Attempting to load "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/object-assign" as a file
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/object-assign"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/object-assign.tsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/object-assign.ts"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/object-assign.jsx"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/object-assign.js"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/object-assign.css"
   note: Failed to find file "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/object-assign.json"
   note: Attempting to load "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/object-assign" as a directory
   note: Read 4 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/object-assign"
   note: Resolved to "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/object-assign/index.js" using the "index" rule
   note: Read 4 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/object-assign"
   note: Read 4 entries for directory "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/object-assign"
   note: Resolved symlink "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/react@17.0.2/node_modules/object-assign/index.js" to "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/object-assign@4.1.1/node_modules/object-assign/index.js"
   note: Primary path is "/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/object-assign@4.1.1/node_modules/object-assign/index.js" in namespace "file"

 > debug: Ended the scan phase (10ms)

 > debug: Started the compile phase

 > error: Two output files share the same path but have different contents: out/I-AM-A-CHUNK-[name]-M2CMZ5JO.js

 > debug: Ended the compile phase (4ms)

1 error
/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/esbuild@0.11.3/node_modules/esbuild/lib/main.js:1283
  let error = new Error(`${text}${summary}`);
              ^

Error: Build failed with 1 error:
error: Two output files share the same path but have different contents: out/I-AM-A-CHUNK-[name]-M2CMZ5JO.js
    at failureErrorWithLog (/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/esbuild@0.11.3/node_modules/esbuild/lib/main.js:1283:15)
    at buildResponseToResult (/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/esbuild@0.11.3/node_modules/esbuild/lib/main.js:972:32)
    at /Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/esbuild@0.11.3/node_modules/esbuild/lib/main.js:1065:20
    at /Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/esbuild@0.11.3/node_modules/esbuild/lib/main.js:582:9
    at handleIncomingPacket (/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/esbuild@0.11.3/node_modules/esbuild/lib/main.js:671:9)
    at Socket.readFromStdout (/Users/jarredsumner/Code/dynamic-import-repro/node_modules/.pnpm/esbuild@0.11.3/node_modules/esbuild/lib/main.js:549:7)
    at Socket.emit (node:events:378:20)
    at addChunk (node:internal/streams/readable:313:12)
    at readableAddChunk (node:internal/streams/readable:288:9)
    at Socket.Readable.push (node:internal/streams/readable:227:10) {
  errors: [
    {
      detail: undefined,
      location: null,
      notes: [],
      text: 'Two output files share the same path but have different contents: out/I-AM-A-CHUNK-[name]-M2CMZ5JO.js'
    }
  ],
  warnings: []
}

If the manifest is a file that exists on the filesystem, the bug doesn't happen. If the manifest is returned by a plugin, it does happen. Whether or not the imports are absolute or relative paths are irrelevant

@Jarred-Sumner
Copy link
Contributor Author

Jarred-Sumner commented Apr 2, 2021

I think the problem here is that the [hash] calculation is wrong somehow. The point of the hash is that files with the different contents should not have the same hash. Really sorry about these issues. Hopefully we can get them fixed soon.

Ah that makes sense

It seems specific to the onResolve callback. If I instead have onResolve return a path to a file, the behavior is the same as the onLoad callback.

    build.onResolve({ filter: /^manifest$/ }, (args) => {
      return {
        namespace: "file",
        path: `${process.cwd()}/manifest-file.js`,
      };
    });

Tangent: from looking at the debug level logs -- if an entry point starts with ./, it appears that esbuild starts looking for files at /? Is that expected behavior? When I type ./file in a shell it looks in cwd

@evanw
Copy link
Owner

evanw commented Apr 2, 2021

Tangent: from looking at the debug level logs -- if an entry point starts with ./, it appears that esbuild starts looking for files at /? Is that expected behavior? When I type ./file in a shell it looks in cwd

Well people don't expect it to work this way. But it's a deliberate aspect of esbuild's directory cache. You can read more about it here: #254 (comment).

Edit: What you're probably seeing is searches for files named index. These searches are currently done eagerly when a directory is cached so that other searches can just check the cache entry. It's only caused by the directory not being cached already and isn't directly related to the first query. Although I agree it's confusing to see it as part of the steps taken to resolve the first query. This could potentially be done lazily instead of eagerly and guarded by a mutex.

@Jarred-Sumner
Copy link
Contributor Author

Jarred-Sumner commented Apr 2, 2021

Well people don't expect it to work this way. But it's a deliberate aspect of esbuild's directory cache. You can read more about it here: #254 (comment).
Edit: What you're probably seeing is searches for files named index. These searches are currently done eagerly when a directory is cached so that other searches can just check the cache entry. It's only caused by the directory not being cached already and isn't directly related to the first query. Although I agree it's confusing to see it as part of the steps taken to resolve the first query. This could potentially be done lazily instead of eagerly and guarded by a mutex.

Oh, I didn't realize how many filesystem lookups bundlers need to do. The node module resolution algorithm is a good developer experience but seems to slowly mimic text editor autocomplete.

If the manifest is a file that exists on the filesystem, the bug doesn't happen.

After testing more, this isn't true. The plugin seems to have absolutely nothing to do with the issue.

If I rename the file from [name].js to unique-name.js it works fine.
image

The strange thing is that the contents of each of these three files being dynamically imported are different.

More notes:

  • If the import cycle is incomplete -- if any of [name], hi/[name] or index does not import the manifest, then it builds successfully.
  • It only happens with dynamic imports -- this snippet builds successfully:
import * as hi from "./hi/[name]";
import * as name from "./[name]";
import * as index from "./index";

export default {
  hi,
  name,
  index,
};

Also, thanks for looking at the (many) issues I've posted at this point.

@Jarred-Sumner
Copy link
Contributor Author

Jarred-Sumner commented Apr 2, 2021

Workaround: If you create a file with a different filename that just export * from "path-to-file-in-here" it works.

So instead of this:

export const hi = () => import("./hi/[name]");
export const name = () => import("./[name]");
export const index = () => import("./index");

export default {
  hi,
  name,
  index,
};

You can do this:

export const hi = () => import("./hi/[name]");
export const name = () => import("./[name]-stub");
export const index = () => import("./index");

export default {
  hi,
  name,
  index,
};

And [name]-stub is just:

export * from "./[name]";

@evanw evanw closed this as completed in 33ee772 Apr 2, 2021
@evanw
Copy link
Owner

evanw commented Apr 2, 2021

Thanks for all of the detail. Turns out this was caused by an incorrect optimization that tried to skip hashing when it wasn't needed (or at least an optimization that is now incorrect since dynamic entry points now use chunkNames instead of entryNames).

@Jarred-Sumner
Copy link
Contributor Author

Thanks for all of the detail. Turns out this was caused by an incorrect optimization that tried to skip hashing when it wasn't needed (or at least an optimization that is now incorrect since dynamic entry points now use chunkNames instead of entryNames).

Thanks for fixing!

With this release, chunk contents will now always be hashed regardless of the chunk type. This makes esbuild somewhat slower than before in the common case, but it fixes this correctness issue.

Doubt this would make up for the performance difference but I wonder if using xxHash instead of SHA128 would be faster. There's a Go implementation

@evanw
Copy link
Owner

evanw commented Apr 3, 2021

Thanks for the pointer. It looks like the pure Go implementation of xxHash reduces the hash time for the benchmark from 25ms to 4ms (6x faster) which is pretty great. I did some reading and I think the trade-offs for it sound good too (better at large sizes, designed to be fairly collision-resistant). I think it's worth a try.

@LifeIsStrange
Copy link

LifeIsStrange commented Oct 14, 2021

BTW Webpack is migrating to xxhash64 by default too
webpack/webpack#14306
If I understand correctly, you are currently using xxhash 64, which is great but since xxhash3 is significantly faster, wouldn't it make more sense to switch to it?

@Jarred-Sumner
Copy link
Contributor Author

Jarred-Sumner commented Oct 14, 2021 via email

@LifeIsStrange
Copy link

LifeIsStrange commented Oct 14, 2021

right, wyhash probably is the second fastest hash function. The #1 being https://github.com/tkaitchuck/aHash , rust use it for its standard library hashbrown

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 a pull request may close this issue.

3 participants