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

fix(legacy): error in build with --watch and manifest enabled #14450

Conversation

adamkoppede
Copy link
Contributor

Description

Running the build command in version 4.4.9 with --watch flag for a
configuration with manifest and legacy-chunks both enabled fails with
the following error message:

[vite:manifest] Plugin error - Unable to get file name for unknown file "a768f517"

A reproduction example for the issue is available online:
https://github.com/adamkoppede/reproduce-vite-error-when-building-css-with-watch

Additional context

A detailed timeline of the build is available in the commit message body of the first commit: 0f9e22d.

After porting the first patch (1d17388) to current main, the following new error message showed up:

[vite:css-post] Plugin error - The "path" argument must be of type string. Received undefined

It is just a missing undefined check: d839ca8.


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the PR Title Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@stackblitz
Copy link

stackblitz bot commented Sep 22, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@adamkoppede
Copy link
Contributor Author

adamkoppede commented Sep 22, 2023

Do you (vite team) plan on backporting this fix to version 4?

I'm currently using https://github.com/adamkoppede/vite/tree/fix/unable-to-get-file-name-error-during-build-watch-vite-4 for the project in which I first encountered the error, in combination with Dockerfile ADD and yarn link. This works for me, but backporting could help others, especially those who need to adjust their server-side logic due to 74fa024.

Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

packages/vite/src/node/plugins/css.ts Outdated Show resolved Hide resolved
packages/vite/src/node/plugins/manifest.ts Outdated Show resolved Hide resolved
playground/css-entrypoint/package.json Outdated Show resolved Hide resolved
@sapphi-red
Copy link
Member

Do you (vite team) plan on backporting this fix to version 4?

I'm currently using https://github.com/adamkoppede/vite/tree/fix/unable-to-get-file-name-error-during-build-watch-vite-4 for the project in which I first encountered the error, in combination with Dockerfile ADD and yarn link. This works for me, but backporting could help others, especially those who need to adjust their server-side logic due to 74fa024.

If you send a PR to v4 branch, I guess we can. But for 74fa024, you can just set build.manifest: 'manifest.json' to preserve the current behavior.

@sapphi-red sapphi-red added plugin: legacy p3-minor-bug An edge case that only affects very specific usage (priority) labels Nov 16, 2023
@sapphi-red sapphi-red changed the title fix(manifest): error in build with --watch and legacy plugin fix(legacy): error in build with --watch and manifest enabled Nov 16, 2023
Running the build command in version 4.4.9 with --watch flag for a
configuration with manifest and legacy-chunks both enabled fails with
the following error message:

```
[vite:manifest] Plugin error - Unable to get file name for unknown file "a768f517"
```

A reproduction example for the issue is available online:
https://github.com/adamkoppede/reproduce-vite-error-when-building-css-with-watch

The (relevant) timeline of the build with --watch is as following:

1.  A FileEmitter, let's call this instance FileEmitter(0),
    is constructed with an undefined base emitter.
2.  A FileEmitter, let's call this instance FileEmitter(1),
    is constructed with FileEmitter(0) as base emitter.
3.  A FileEmitter, let's call this instance FileEmitter(2),
    is constructed with FileEmitter(0) as base emitter.
4.  Vite clears `generatedAssets` as part of the `renderStart`
    function of the `vite:asset` plugin. Let's call the configuration,
    for which the cleaning occurred, config(1), since it is the first
    distinct config value we encountered.
5.  (If the `buildStart` function of the `vite:asset` plugin is renamed
    to `renderStart`, vite will clear `generatedAssets` for key
    config(1) one more time.)
6.  FileEmitter(2) gets referenceId "d31f18fc" assigned for file
    "script.css"
7.  ReferenceId "d31f18fc" (src/script.css) is added to
    `generatedAssets` with key config(1) from inside the `renderChunk`
    function of the `vite:post-css` plugin
8.  FileEmitter(2).getFileName gets successfully called with referenceId
    "rd31f18fc" from inside `renderChunk` of the `vite:post-css` plugin.
9.  FileEmitter(2) gets referenceId "380b7b38" assigned for file
    "styles.css"
10. ReferenceId "380b7b38" (src/styles.css) is added to
    `generatedAssets` with key config(1) from inside the `renderChunk`
    function of the `vite:post-css` plugin
11. FileEmitter(2).getFileName is successfully called with referenceId
    "380b7b38" from inside `renderChunk` of the `vite:post-css` plugin.
12. `generateBundle` of the `vite:manifest` plugin is executed.
    `generatedAssets` holds two entries for key config(1):
    "d31f18fc" (src/script.css) and "380b7b38" (src/styles.css)
    `generateBundle` issues calls to FileEmitter(2).getFileName for both
    entries.
13. `generateBundle` of the `vite:manifest` plugin is executed.
    `generatedAssets` holds two entries for key config(1):
    "d31f18fc" (src/script.css) and "380b7b38" (src/styles.css)
    `generateBundle` issues calls to FileEmitter(1).getFileName for both
    entries. However, there are no entries in FileEmitter(1),
    thus the calls fail.

My first attempt to fix the problem was to extend or change the key of
the `generatedAssets` map in a way, that it corresponds to the internal
rollup FileEmitter instance that is used. However, I was unable to find
a variable with such a property inside the manifest plugin.
@adamkoppede adamkoppede force-pushed the fix/unable-to-get-file-name-error-during-build-watch-vite-5 branch from f3b4886 to c6b0af0 Compare November 16, 2023 14:04
@adamkoppede
Copy link
Contributor Author

Do you (vite team) plan on backporting this fix to version 4?
I'm currently using https://github.com/adamkoppede/vite/tree/fix/unable-to-get-file-name-error-during-build-watch-vite-4 for the project in which I first encountered the error, in combination with Dockerfile ADD and yarn link. This works for me, but backporting could help others, especially those who need to adjust their server-side logic due to 74fa024.

If you send a PR to v4 branch, I guess we can. But for 74fa024, you can just set build.manifest: 'manifest.json' to preserve the current behavior.

Since major version 5 and a compatible version of the react plugin were released today, I assume that I can just upgrade by using the build.manifest option.

Thank you for your support.

The following error was thrown during the build with --watch of a
project with plugin-legacy and manifest enabled:

```
[vite:css-post] Plugin error - The "path" argument must be of type string. Received undefined
```

The said project is available at
https://github.com/adamkoppede/reproduce-vite-error-when-building-css-with-watch

The error is thrown in the `generateBundle` function:
`pureCssChunks` contains a chunk with a legacy-javascript filename,
which does not exist on the map `prelimaryNameToChunkMap`, since
the code is executed for the modern output.

The following object values were obtained during a trace right before
the assignment of `emptyChunkFiles` inside `generateBundle`:

{
  "prelimaryNameToChunkMap": {
    "assets/script-!~{001}~.js": "assets/script-121d5cbe.js",
    "assets/styles-!~{002}~.js": "assets/styles-5ac85f22.js"
  },
  "pureCssChunkNames": [undefined, "assets/styles-5ac85f22.js"],
  "pureCssChunks": [
    {
      "exports": [],
      "facadeModuleId": "/reproduce/src/styles.css",
      "isDynamicEntry": false,
      "isEntry": true,
      "isImplicitEntry": false,
      "name": "styles",
      "type": "chunk",
      "dynamicImports": [],
      "fileName": "assets/styles-legacy-!~{002}~.js"
    },
    {
      "exports": [],
      "facadeModuleId": "/reproduce/src/styles.css",
      "isDynamicEntry": false,
      "isEntry": true,
      "isImplicitEntry": false,
      "name": "styles",
      "type": "chunk",
      "dynamicImports": [],
      "fileName": "assets/styles-!~{002}~.js"
    }
  ]
}
@adamkoppede adamkoppede force-pushed the fix/unable-to-get-file-name-error-during-build-watch-vite-5 branch from 171d4a4 to 55cf0c6 Compare November 16, 2023 16:07
@patak-dev patak-dev merged commit b9ee620 into vitejs:main Nov 20, 2023
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority) plugin: legacy
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants