Skip to content

fix: include only react-components in package.json - #18707

Merged
mshabarov merged 10 commits into
mainfrom
fix/18684-package-json-dependencies
Feb 23, 2024
Merged

mshabarov merged 10 commits into
mainfrom
fix/18684-package-json-dependencies

Conversation

@tltv

@tltv tltv commented Feb 14, 2024 •

Copy link
Copy Markdown
Contributor

When React is enabled, package.json update includes only @vaadin/react-components and excludes all other @vaadin/* web-components pinned by platform versions (core and pro). React components are wrappers for web-components. When React is disabled, only web-components are updated in the package.json.

Fixes: #18684

Note! This PR in platform must be merged before this PR.

@github-actions

github-actions Bot commented Feb 14, 2024 •

Copy link
Copy Markdown
Contributor

Test Results

1 094 files  ±0  1 094 suites  ±0   1h 19m 18s ⏱️ -47s
6 929 tests +4  6 881 ✅ +4  48 💤 ±0  0 ❌ ±0 
7 281 runs  ±0  7 221 ✅ ±0  60 💤 ±0  0 ❌ ±0 

Results for commit 127470e. ± Comparison against base commit 2064933.

♻️ This comment has been updated with latest results.

@tltv
tltv marked this pull request as ready for review February 14, 2024 15:53
@mshabarov
mshabarov requested a review from caalador February 16, 2024 12:08
for (String key : platformPinnedDependencies.keys()) {
if (options.isReactEnabled() && isReactModuleAvailable()
&& !FrontendUtils.isPackageKeyReactComponents(key)
&& pinnedPlatformDependencies.contains(key)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the contains(key) is always true here as we are iterating the same set of keys as added to the pinnedPlatformDependencies

Comment on lines +216 to +220
if (options.isReactEnabled() && isReactModuleAvailable()
&& !FrontendUtils.isPackageKeyReactComponents(dep.getKey())
&& pinnedPlatformDependencies.contains(dep.getKey())) {
continue;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Trusting the platformPinnedDependencies content as boolean reactComponents = pinnedPlatformDependencies.contains("@vaadin/react-components"); would make this:

Suggested change
if (options.isReactEnabled() && isReactModuleAvailable()
&& !FrontendUtils.isPackageKeyReactComponents(dep.getKey())
&& pinnedPlatformDependencies.contains(dep.getKey())) {
continue;
}
if (reactComponents
&& pinnedPlatformDependencies.contains(dep.getKey())) {
continue;
}

Also is this no here just to not add the @NpmPackage annotations from server Component definitions?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, basically so. Without this just having vaadin-core dependency would end up adding all web-components. Or having any dependency with @NpmPackage with core components.

* components, {@code false} otherwise
*/
static boolean isPackageKeyReactComponents(String key) {
return key.equals("@vaadin/react-components");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As a string check this sounds a bit easy to fail.
A better way would be to collect all the possibilities in the versions.json found under the react (perhaps in the future react-pro) object which is formatted as:

    "react": {
        "react-components": {
            "jsVersion": "24.4.0-alpha13",
            "npmName": "@vaadin/react-components"
        }
    },

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yep, I agree that making it future proof by reading from the json object would be better. It's just that we only have that @vaadin/react-components in react now so kept it simple as possible for this reason only.

@caalador caalador left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

After removing the unused method it looks fine.

|| !oldHash.equals(newHash);
}

private boolean isReactModuleAvailable() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is now not needed as it's not used anywhere in the class

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

removed

@caalador
caalador requested a review from mshabarov February 16, 2024 13:11

@mshabarov mshabarov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The case where package.json isn't exist looks working well, but if package.json exists before starting the app and it has WCs, then they aren't removed.

Comment on lines +237 to +240
if (reactComponents
&& !FrontendUtils.isPackageKeyReactComponents(key)) {
continue;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks dangerous and probably not correct, as it skips adding everything from platform's versions json that isn't @vaadin/react-components. Besides web-component, versions json has lumo, polymer, bundles, statistics and potentially other "non-component" dependencies that are skipped with this codes.

Also, if I run this patch with the existing package.json containing the web-components, this patch doesn't remove them from, probably because the logic below in this method isn't running, which is incorrect.

But from the other side, how Flow can only extract semantically web-components entries...Should we change the structure of versions.json in the platform, so it would store web-components in a separated sub-list?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We need a sub-list. Either in versions.json or Flow can populate it. Basically it should be a list of all @vaadin/* dependencies in react-component's package.json. We could even start with reading this list from file in flow.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's not removing existing dependency as that's how the package.json update works in general. but if you think, it's safe to remove existing, it could be done too. Just not adding it is probably safer.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Well, retaining them would require package.json removal during migration to effectively trigger this feature, but this is probably fine. Maybe it's indeed safer to only not adding them.

Having a sub-list is discussible, see the corresponding discussion in Slack.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My main concern vs merging this change is these messages in the logs:

2024-02-19T13:28:30.526+02:00  INFO 10210 --- [onPool-worker-1] c.v.f.server.frontend.TaskUpdateImports  : 

  Failed to find the following imports in the `node_modules` tree:
      - @vaadin/polymer-legacy-adapter/style-modules.js
  If the build fails, check that npm packages are installed.

  To fix the build remove `package-lock.json` and `node_modules` directory to reset modules.
  In addition you may run `npm install` to fix `node_modules` tree structure.

@vaadin/bundles npm package is not found, Vaadin component dependency bundles are disabled.

 Error: The following dependencies are imported but could not be resolved:
2024-02-19T13:28:36.541+02:00  INFO 10210 --- [v-server-output] c.v.b.devserver.DevServerOutputTracker   : 
2024-02-19T13:28:36.541+02:00  INFO 10210 --- [v-server-output] c.v.b.devserver.DevServerOutputTracker   :   @vaadin/polymer-legacy-adapter/style-modules.js

These shouldn't be logged at least if we think that there is no impact on applications.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's put on hold this PR until we decide about a solution to add/remove only components.

@mshabarov

mshabarov commented Feb 20, 2024 •

Copy link
Copy Markdown
Contributor

We have to implement the following feature for components packages dynamic management:

  1. Add a new parameter/option into entries in version.json in platform with values react / lit / all (or empty) and key "mode" (discussable), e.g.:
       "accordion": {
            "javaVersion": "{{version}}",
            "jsVersion": "24.4.0-alpha13",
            "npmName": "@vaadin/accordion",
            "mode": "lit"
        },
        ....
        "react-components": {
            "jsVersion": "24.4.0-alpha13",
            "npmName": "@vaadin/react-components",
            "mode": "react"
        }
  1. Remove @NpmPackage annotations from Flow components importing web-components packages. Also double check if Flow components have a mock logic for versions.json file, because now after removing @NpmPackage it would be the only one way to lookup and add the packages. Flow components have a script to get the versions from platform, but this should be checked.
  2. Extend TaskUpdatePackages in Flow with a logic that dynamically adds the components depending on the project config (React/Lit).

continue;
}
JsonObject json = (JsonObject) value;
if (!isIncludedByMode(json)) {

@mshabarov mshabarov Feb 21, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Now with this new method the old approach above (!reactEnabled && isReactObject(key)) isn't needed anymore.
Flow should add or not to add the npm dependency based on this new mode value.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also IMO it would be better to structure it as:

if (isIncludedByMode(json)) {
            if (json.hasKey(NPM_NAME)) {
                addDependency(json);
            } else {
                collectDependencies(json);
            }
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Current code does not require "mode: react" for react dependencies. If we change this so that "mode: react" is needed in version.json, then we need to add it in platform versions.json and wait for next snapshot before we merge this PR (this takes more time naturally).
If we keep it as it is, we can merge this/platform independently. Which path should we take?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Discussed and decided to add react mode in platform and this means that platform PR needs to be merged first before this PR can be merged (after related changes). In other words, we will not depend on "react" and "react-pro" keys anymore. We use "mode: react" feature instead.

private static final String JS_VERSION = "jsVersion";
private static final String NPM_NAME = "npmName";
private static final String NPM_VERSION = "npmVersion";
private static final String MODE = "mode";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Better to make it private so platform's codes can reuse it if necessary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I assume you mean public.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah yes! Public :)

* @return {@code true} if the key is a package key for a Vaadin react
* components, {@code false} otherwise
*/
static boolean isPackageKeyReactComponents(String key) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like this isn't needed anymore.

} else if (reactEnabled) {
return MODE_REACT.equalsIgnoreCase(mode);
} else {
return MODE_LIT.equalsIgnoreCase(mode);

@mshabarov mshabarov Feb 21, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I propose to add a debug logging to each case, showing the package name and it's version.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done (refactored)

+ "\"react\": {\n" +
" \"react-components\": {\n" +
" \"jsVersion\": \"24.4.0-alpha13\",\n" +
" \"npmName\": \"@vaadin/react-components\"\n" +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this needs a react mode ?

@tltv
tltv force-pushed the fix/18684-package-json-dependencies branch from b26c843 to daf0561 Compare February 22, 2024 09:55
@vaadin-bot vaadin-bot added +1.0.0 and removed +0.0.1 labels Feb 22, 2024

@mshabarov mshabarov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, except that for some reason this PR gets not-related changes from other commits.
Needs a clean-up and rebase.

@tltv

tltv commented Feb 22, 2024

Copy link
Copy Markdown
Contributor Author

LGTM, except that for some reason this PR gets not-related changes from other commits. Needs a clean-up and rebase.

It's just how github shows commits after rebasing. So it's already rebased.

@mshabarov

Copy link
Copy Markdown
Contributor

I see some other commits in your branch. I propose to revert them and squash all the commits. Then merge latest main.
Also this PR has some conflicts.

tltv added 9 commits February 22, 2024 12:49
When React is enabled, package.json update includes only @vaadin/react-components and excludes all other @vaadin/* web-components pinned by platform versions (core and pro). React components are wrappers for web-components.
When React is disabled, only web-components are updated in the package.json.

Fixes: #18684
Reverted part of the package.json updating back to previous and implemented support for `mode` in versions.json. Value can be `lit`, `react`, `all` or empty (or non-existing). Dependencies are filtered based on the `mode` property and active React mode.
@tltv
tltv force-pushed the fix/18684-package-json-dependencies branch from daf0561 to bd95de0 Compare February 22, 2024 10:50
@vaadin-bot vaadin-bot added +0.0.1 and removed +1.0.0 labels Feb 22, 2024
@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Passed Quality Gate passed

Issues
54 New issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@mshabarov
mshabarov merged commit 98fbb00 into main Feb 23, 2024
@mshabarov
mshabarov deleted the fix/18684-package-json-dependencies branch February 23, 2024 07:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vaadin 24.4 adds Vaadin web-components into package.json by default

4 participants