Repository navigation
feat: report JavaScript definition imports that can not be resolved - #27491
Conversation
A @JsDefinitionImport module that the build can not find made Vite stop the production build, and in development mode every page showed that the dev server failed to start, without naming the Java interface. The build now checks each module before Vite runs: a ./ path against the frontend folder and the frontend files of jars, as Vite looks for it, and an npm package against the dependencies the project declares. The functions that import a module that is not found are left out of the generated file, in the build and when a definition is hotswapped, and the build log says why. Their identifiers are listed with the module in vaadin-js-definition-ids.json and the bundle stats, and a call of such a method throws on the server, every time, naming the module. Inherited methods with imports that are found keep working. The bundle is rebuilt only when the list of left-out functions changes or a missing module appears, and the bundle check of the production server no longer advises @uses for these functions. Existing tests that imported files they never created now create them, as such imports are left out now. Fixes #27424
A frontend file of a jar is imported from where the build copies it only when the path names it as it is, so a jar file without its extension is reported as not found rather than failing in Vite. Subpath imports starting with # are taken to resolve. Without the frontend dev server the browser keeps the dev bundle, so a hotswap no longer changes the list of left-out functions the server reads. Tests that import an npm package now declare it, as an undeclared package is left out.
Type of change
How to test
Notes:
API changesAll in classes for internal use only. // com.vaadin.flow.internal.BundleUtils — Added
public static final String JS_DEFINITION_UNRESOLVED_IMPORTS_STATS_KEY
public static final String JS_DEFINITION_UNRESOLVED_IMPORTS_KEY
public static Map<String, String> getUnresolvedJsImports()
public static Map<String, String> parseUnresolvedJsImports(JsonNode node)
// com.vaadin.flow.internal.FrontendUtils — Added
public static final String JS_DEFINITION_IDS_FILE_NAME
// com.vaadin.flow.server.frontend.TaskGenerateJsDefinitions — Added
public static void updateUnresolvedImports(Options options, Collection<Class<?>> definitions) |
|
Fixed the four |
- MissingModuleIT: escape the module by hand, as Playwright runs the pattern as a JavaScript regular expression, which has no \Q...\E - README: the baseline was the main nightly 25.5-20261128.033951, not 9197eb4636; results on the PR (11/11, dev mode 8/8, no rebuild loop, installed but undeclared npm package left out)
mcollovati
left a comment
There was a problem hiding this comment.
Tested with the reproducer (https://github.com/mcollovati/skeleton-starter-flow-spring/tree/repro/js-definition-reachability-fallbacks, profile missing-module, results in the README section "Modules the build can not resolve"):
- Production:
MissingModuleIT11 of 11. The other 57 tests of the branch give the same results as before (only I13 fails, on purpose). - Development mode:
MissingModuleIT#u*8 of 8, and the dev bundle builds. - A second build with nothing changed logs
A production mode bundle build is not needed. After the missing file is added, the next build runs Vite, and the stats no longer list that module. - The production bundle names neither the modules nor the Java classes. No
@Usesadvice is logged for these classes.
Thanks for the note on Pattern.quote; fixed in the reproducer (1d9f442).
@totally-not-ai about your question: yes, for an npm package that is not declared, the function should be kept and the reason logged at DEBUG level only. Leave out only ./ files that are not found. See the inline comment on FrontendModuleResolver. Please also look at the other inline comments.
| || isFrontendFolderFile(path) || hasMetaInfResource(path)) { | ||
| return null; | ||
| } | ||
| String packageName = getPackageName(path); |
There was a problem hiding this comment.
A package that is installed but not declared is left out. I checked it with @JsDefinitionImport(from = "@lit/reactive-element", exports = "ReactiveElement"). @lit/reactive-element is in node_modules as a dependency of lit. The build logs ... can not be found, as the npm package '@lit/reactive-element' is not a dependency of the project, which is not true: the module is there.
Looking in node_modules does not help, because this runs before npm install, so a clean CI build would leave out what a local build keeps. Please keep the function for a package that is not declared, and log the reason (interface, method, package) at DEBUG level, so that a normal build has no noise. If the package is really missing, Vite stops the build, and the Maven plugin then says to run again with -X, which shows the line. The same rule covers custom Vite aliases that look like package names. Only a ./ file that is not found should be left out.
There was a problem hiding this comment.
Done. findUnresolvableReason now only checks ./ paths, so a bare import is never left out. A new findUndeclaredPackage names the package when the project does not declare it, and the build logs that at DEBUG level with the interface, the methods and the package. npmPackageNotDeclared_isKeptAndOnlyNamed checks that the functions stay in the file and that the debug line is logged.
| Map<String, String> unresolvedImports = new TreeMap<>(); | ||
| for (RenderedFunction function : functions) { | ||
| if (function.unresolved() != null) { | ||
| getLogger().error(function.unresolved().message()); |
There was a problem hiding this comment.
Minor: in one build each line is logged twice, because BundleValidationUtil.jsDefinitionsChanged also calls render(). It also logs one line for each method, so the three methods of MissingModuleJs in the reproducer give six ERROR lines with the same reason. One line for each interface and module (listing the methods), logged only when the file is generated, would be easier to read.
There was a problem hiding this comment.
Done. render() no longer logs. It returns what to log, and only execute() and the hotswap path (updateUnresolvedImports) log it. There is now one line per interface and module, listing the methods, e.g. Leaving the JavaScript of the methods init, start of ...MissingModuleJs out of the bundle, so calling them fails: .... The same grouping applies to the debug lines for undeclared packages.
| * an inherited method with imports that were found still works. | ||
| */ | ||
| private void checkImportsFound(JsCall call, Method method) { | ||
| Map<String, String> unresolvedImports = BundleUtils |
There was a problem hiding this comment.
Minor: this runs for every call of every definition. In development mode, getUnresolvedJsImports() reads the modification time and the size of the ids file each time, also for interfaces that have no @JsDefinitionImport. Could the check run only for methods whose declaring interface has imports (cached for each method), so that other calls do not touch the file?
There was a problem hiding this comment.
Done. A ReflectionCache keyed by the declaring interface (cleared before hotswap) tells whether it has @JsDefinitionImport. Calls of other interfaces return before getUnresolvedJsImports(), so they don't touch the file. I cached per interface rather than per method because the build takes a method's imports from the interface that declares it. The proxy test now also calls a method of an interface without imports whose id is on the list, and checks that it still runs.
| @@ -56,9 +76,30 @@ class FrontendModuleResolver { | |||
| * prefix, not <code>null</code> | |||
| */ | |||
| FrontendModuleResolver(Options options, Logger logger) { | |||
There was a problem hiding this comment.
FrontendModuleResolver came with #27440 and is not released yet, so there is no need to keep this constructor. Please keep only the one that takes the packages, and pass () -> null in AbstractUpdateImports, its only caller.
There was a problem hiding this comment.
Done. Only the constructor that takes the packages is left, and AbstractUpdateImports passes () -> null.
A package that the project does not declare may still be installed as a dependency of another package, or the module may be a Vite alias, and whether it is installed can not be told before npm install. Only a ./ file that can not be found leaves the function out now. A package that is not declared is logged at debug level, naming the definition, the methods and the package; if it is really missing, Vite stops the build. The functions left out are logged when the file is written rather than when it is rendered, so a bundle check no longer logs them again, and once per definition and module, listing the methods. A call checks the list of left-out functions only when the interface declaring the method imports modules, cached per interface, so other calls do not read the ids file in development mode. FrontendModuleResolver keeps only the constructor that takes the packages, as it is not released yet.
Checks the debug line for an npm package that is not declared, the grouped error on hotswap, that a call of an interface without imports does not consult the list, and that a hotswap with a dev bundle keeps the list the bundle was built with. Two tests no longer declare the npm package their definition imports, as an undeclared one is now kept.
Use a primitive check for the cached Boolean, List.sort() instead of Collections.sort(), and drop an unused throws clause in a test.
getChunkHash and the hotswapper test were already like this before this change, so they are left for a separate cleanup.
|
Fixed the finding this PR introduced: I left the other two alone because that code was already on |
|
One more suggestion, found with the reproducer (
The cause is the same as before this PR: these lines are logged inside Could you do the same for the other two messages? For example, |
- U6 is now an npm package that is installed but not declared (@lit/reactive-element), which the build keeps - missing-package profile: a package that is neither declared nor installed, which stops the Vite build - README: results on 0ef5e484cc (11/11, dev mode 8/8, grouped ERROR lines, DEBUG line with -X)
A build renders the generated file twice, once to check the bundle and once to write it. Wrong definitions and misplaced @JsDefinitionImport annotations were logged while rendering, so each line appeared twice. They are now collected with the import issues and logged where the file is written, and once when a hotswap updates the list of left-out functions.
|
@mcollovati Done. Both older messages are now handled like this PR's import lines. Rendering collects them in
The recent changes from |
|
Thanks, each line is now logged once (checked with the reproducer). One more suggestion for the same output. A build with several wrong definitions still logs one ERROR entry for each of them. The reproducer has 12, which is a lot to read. As all of them are now collected in
|
|
A small change for the same output: the text of The message is logged only when the annotation is on a class, so it can say that directly, and then say where the annotation goes. For one class: With the grouped entry of my previous comment: Could you change it together with the grouping? |
A build with many wrong definitions logged one ERROR entry for each. The definitions left out because what they declare is wrong and those left out because a module they import can not be found now share one entry, one item per definition sorted by name, and the misplaced @JsDefinitionImport annotations another. Nothing is logged when there is nothing to list.
An item no longer repeats the definition name that the reason starts with, and the debug line about an undeclared npm package names the methods the same way as the error items.
|
On the documentation finding: the The latest changes from |
vaadin-review-bot
left a comment
There was a problem hiding this comment.
Reviewed the changes — left 1 comment.
| Finding | |
|---|---|
| 🧪 | No test fails if the isInGeneratedFile fix for the dev-bundle regression is removed |
|
|
||
| classesChanged(MissingModuleGreeterJs.class); | ||
|
|
||
| assertEquals(List.of(MissingModuleGreeterJs.class.getName()), |
There was a problem hiding this comment.
🧪 No test fails if the isInGeneratedFile fix for the dev-bundle regression is removed
This case never writes vaadin-js-definitions.js, so findMissingFromGeneratedFile returns the definition through the generated == null guard, before the new left-out check is reached. The assertion holds equally on the pre-fix code.
updateUnresolvedImports_followsTheDefinitionsThatChanged does not close the gap either: its findMissingFromGeneratedFile assertion is satisfied because FoundImportMixin.run is absent from a file rendered for GreeterJs/PopoverJs only, again without consulting the left-out list.
Writing the generated file first (e.g. writeGeneratedDefinitionsFor(GreeterJs.class)) would exercise the real path: MissingModuleGreeterJs has a single method, so without the fix declared is empty, isInGeneratedFile answers "present", and nothing is reported.
There was a problem hiding this comment.
Fixed. definitionImportingAModuleNotFound_devBundle_listKeptAsBuilt now writes the generated file for GreeterJs first, so the check reaches the left-out list instead of stopping at the missing-file guard. MissingModuleGreeterJs renders no lines into that file. I checked that the test fails when the left-out check in isInGeneratedFile is removed and passes with it.
vaadin-review-bot
left a comment
There was a problem hiding this comment.
Reviewed the changes — collected 1 finding below that could not be attached to the diff.
| Finding | |
|---|---|
| 📝 | Page.executeJs(Class) does not document the new per-call IllegalStateException |
📝 Page.executeJs(Class) does not document the new per-call IllegalStateException
Page.executeJs(Class) goes through the same JsDefinitionProxy.create, so a method whose module the build could not find throws on every call here too. Only Element.executeJs(Class) got the new paragraph about it; the Page javadoc still describes the contract as "nothing about the JavaScript is decided at the call site" and lists IllegalArgumentException as the only failure.
Page.java:581 · documentation
…file throws when called
|
Added the same paragraph to |
vaadin-review-bot
left a comment
There was a problem hiding this comment.
✅ Nothing to flag — the changes look good.
|
|
Documentation Bot: No documentation needed for this change. This is a I could not find a page in vaadin/docs that already documents Nothing was opened in vaadin/docs. If you disagree, the change has to be documented by hand there.
|
Also say in Page.executeModuleJs that its modules load with the views, unlike those of executeJs(Class), and have the development warning name a path the build could not check rather than a missing file, which since #27491 fails on the server instead.
…27491) (CP: 25.4) Fixes #27424 Part of #25814 **build failure** · flow-build-tools, flow-server, vaadin-dev-server · applications with a `@JsDefinitionImport` of a `./` file the build can not find A `@JsDefinitionImport` of a missing `./` file used to stop the production build, and in development mode every page showed that the dev server failed to start. The messages did not name the Java interface. Now only the methods that import the missing file are left out. The build log says which interface, methods and module, and why. Calling such a method throws on the server, every time, naming the module. The rest of the application works. **Risks:** -⚠️ Only `./` files are checked. An npm package the project does not declare is kept, as it may be installed as a dependency of another package or be a Vite alias, which can not be told before npm install. The build logs it at DEBUG level, naming the interface, methods and package. If the package is really missing, Vite stops the build as before, and running again with `-X` shows that line. - ✅ No public API change outside internal classes, no serialization, threading or memory impact. A production server reads the list once from the stats. A development server reads it again only when the file changes, and only for calls of methods whose interface has `@JsDefinitionImport`. - Added a check before Vite runs: a `./` path is looked for in the frontend folder with the extensions and index files Vite tries, and in `META-INF/frontend` / `META-INF/resources/frontend` of the class path as named. - The functions of such methods are left out of the generated file, in the build and when the frontend dev server picks up a hotswapped definition. Their ids are recorded with the module in `vaadin-js-definition-ids.json` and in the bundle stats (`jsDefinitionUnresolvedImports`), keyed by function id, so the production bundle names no Java class. - Without the frontend dev server, the list stays as the bundle was built. A hotswapped definition that now imports a missing file is reported as not applied, with the advice to restart, and the module is logged. - The Javadoc of `@JsDefinitionImport`, `Element.executeJs(Class)` and `Page.executeJs(Class)` says that such a method throws at each call while the rest keeps working. - What is left out of the bundle is logged as one ERROR entry with one item per definition, sorted by class name. This covers definitions whose module can not be found (naming the methods and module) and definitions that declare something wrong. `@JsDefinitionImport` on a class gets a second entry listing the classes. Nothing is logged when there is nothing to list. - These entries are logged only when the file is written, and once on hotswap. Before, the bundle check logged the older messages a second time, because it renders the file too. - `JsDefinitionProxy` throws `IllegalStateException` when such a method is called. The check runs per call, so an inherited method with imports that are found still works. It looks at the list only when the interface declaring the method has imports, cached per interface. Stats without the entry fall back to the check from #27078. - The bundle is rebuilt when the list of left-out functions differs from the stats, or when a missing module appears. Left-out functions do not count as missing, so an unchanged project does not rebuild. - The check from #27078 no longer advises `@Uses` for left-out functions. - Existing tests that imported `./` files they never created now create them, as such imports are left out now. Working on behalf of @mcollovati --------- Co-authored-by: totally-not-ai[bot] <290682512+totally-not-ai[bot]@users.noreply.github.com>
…n a class (#27568) (CP: 25.4) Fixes #27554 Follow-up to #27491 **behavior deviation** · flow-build-tools, flow-server, vaadin-dev-server · applications or add-ons with a `@JsDefinitionImport` of a `./` file that the build can not find, or with one on a class A `@JsDefinitionImport` of a missing `./` file now stops the build in production and development mode, like a missing `@JsModule` file. Before, the build passed with one ERROR line and each call of the affected methods threw on the server. The error names the interface, the methods and the module. A `@JsDefinitionImport` on a class, which no definition can extend, now stops the build too, instead of being ignored with an error in the log. An optimized production build checks only the classes that the application reaches, as it does for definitions. **Risks:** -⚠️ Behaviour change: a broken add-on, with a missing module or with `@JsDefinitionImport` on a class, now stops the build of every application that uses it, as a missing `@JsModule` file already does. `@JsDefinitionImport` (#27457) and the lenient handling (#27491) are not in any release, so no released code depends on it. - ✅ Only internal API is removed (all added in #27491, unreleased). No change to serialization, threading, memory or performance. **Context.** #27491 left such methods out so that one broken add-on would not break every build. Nothing else in Flow works that way, and a CI log line is easy to miss. The check that runs before Vite is kept, because Vite's own error does not name the Java interface. npm packages are still assumed to be found when in doubt. - `TaskGenerateJsDefinitions` throws `ExecutionFailedException` listing each definition, its methods and the module. The methods are rendered as usual, so the bundle check asks for a rebuild and the build reaches this failure. - Removed the left-out list from `vaadin-js-definition-ids.json` and the bundle stats, the per-call `IllegalStateException` in `JsDefinitionProxy`, the bundle-check special case and the `@Uses` check exception. Also removed `BundleUtils.getUnresolvedJsImports`/`parseUnresolvedJsImports`, their keys, `FrontendUtils.JS_DEFINITION_IDS_FILE_NAME` and `TaskGenerateJsDefinitions.updateUnresolvedImports`. - A `@JsDefinitionImport` on a class now fails the build in the same message as missing modules, naming the classes and saying where the annotation goes. This also applies when the class comes from a jar. With the bytecode scanner, a production build checks only the classes that the application reaches, so an unused add-on class does not break the build. Development mode checks every class. Updated the Javadoc of `@JsDefinitionImport` and `findMisplacedImports`. - Hotswap: a method whose module can not be found is not written to the dev server file. The same error is logged, followed by what to do once the module is added: restart the application, or, with the frontend dev server, change the definition again. Adding the file alone does not write the JavaScript. Only the function with the missing module is left out, and the other functions of the definition are written, also when the file is written from scratch. The hotswapper does not also warn about the definition. - Updated the Javadoc of `@JsDefinitionImport`, `Element.executeJs(Class)`, `Page.executeJs(Class)`, `Page.executeModuleJs(Class)` and `JsModuleInvokers`. - Removed the tests of the left-out list and of the per-call throw. Tests now check: the build fails in both modes with the message; the module that a `JsModuleInvokers` interface calls (`this`) is covered too; the bundle is rebuilt; and hotswap logs the error without writing the import, with or without an existing file. The existing test for the annotation on a class, and the test for the issue message format, now expect the build to fail instead of an error in the log. Working on behalf of @mcollovati --------- Co-authored-by: totally-not-ai[bot] <290682512+totally-not-ai[bot]@users.noreply.github.com>



Fixes #27424
Part of #25814
build failure · flow-build-tools, flow-server, vaadin-dev-server · applications with a
@JsDefinitionImportof a./file the build can not findA
@JsDefinitionImportof a missing./file used to stop the production build, and in development mode every page showed that the dev server failed to start. The messages did not name the Java interface. Now only the methods that import the missing file are left out. The build log says which interface, methods and module, and why. Calling such a method throws on the server, every time, naming the module. The rest of the application works.Risks:
./files are checked. An npm package the project does not declare is kept, as it may be installed as a dependency of another package or be a Vite alias, which can not be told before npm install. The build logs it at DEBUG level, naming the interface, methods and package. If the package is really missing, Vite stops the build as before, and running again with-Xshows that line.✅ No public API change outside internal classes, no serialization, threading or memory impact. A production server reads the list once from the stats. A development server reads it again only when the file changes, and only for calls of methods whose interface has
@JsDefinitionImport.Added a check before Vite runs: a
./path is looked for in the frontend folder with the extensions and index files Vite tries, and inMETA-INF/frontend/META-INF/resources/frontendof the class path as named.The functions of such methods are left out of the generated file, in the build and when the frontend dev server picks up a hotswapped definition. Their ids are recorded with the module in
vaadin-js-definition-ids.jsonand in the bundle stats (jsDefinitionUnresolvedImports), keyed by function id, so the production bundle names no Java class.Without the frontend dev server, the list stays as the bundle was built. A hotswapped definition that now imports a missing file is reported as not applied, with the advice to restart, and the module is logged.
The Javadoc of
@JsDefinitionImport,Element.executeJs(Class)andPage.executeJs(Class)says that such a method throws at each call while the rest keeps working.What is left out of the bundle is logged as one ERROR entry with one item per definition, sorted by class name. This covers definitions whose module can not be found (naming the methods and module) and definitions that declare something wrong.
@JsDefinitionImporton a class gets a second entry listing the classes. Nothing is logged when there is nothing to list.These entries are logged only when the file is written, and once on hotswap. Before, the bundle check logged the older messages a second time, because it renders the file too.
JsDefinitionProxythrowsIllegalStateExceptionwhen such a method is called. The check runs per call, so an inherited method with imports that are found still works. It looks at the list only when the interface declaring the method has imports, cached per interface. Stats without the entry fall back to the check from feat: log error for JavaScript definitions missing from bundle #27078.The bundle is rebuilt when the list of left-out functions differs from the stats, or when a missing module appears. Left-out functions do not count as missing, so an unchanged project does not rebuild.
The check from feat: log error for JavaScript definitions missing from bundle #27078 no longer advises
@Usesfor left-out functions.Existing tests that imported
./files they never created now create them, as such imports are left out now.Working on behalf of @mcollovati