Skip to content

fix: Gradient background error#70

Merged
dcalhoun merged 1 commit intotrunkfrom
fix/gradient-background-error
Feb 5, 2025
Merged

fix: Gradient background error#70
dcalhoun merged 1 commit intotrunkfrom
fix/gradient-background-error

Conversation

@dcalhoun
Copy link
Member

@dcalhoun dcalhoun commented Feb 3, 2025

What?

Fix a can't find variable result error when setting a background gradient for
a block.

Why?

Fixes #67. The error prevents setting a background gradient, resulting in a broken block.

How?

The current gradient-parser version used by @wordpress/components
contains error-generating code for certain environments. While it is
unclear which specific environments, it does appear to occur for this
project's build environment.

See: rafaelcaricio/gradient-parser#11

Testing Instructions

  1. Insert a Group block.
  2. Open the block settings.
  3. Select the style tab.
  4. Set a gradient background for the block.
  5. Verify the gradient is set and displayed, without an error in the console.

Accessibility Testing Instructions

N/A, no user-facing changes.

Screenshots or screencast

N/A, no user-facing changes.

The current `gradient-parser` version used by `@wordpress/components`
contains error-generating code for certain environments. While it is
unclear which specific environments, it does appear to occur for this
project's build environment.

See: rafaelcaricio/gradient-parser#11

Error:

```
can't find variable result
```
@dcalhoun dcalhoun added the [Type] Bug An existing feature does not function as intended label Feb 3, 2025
@dcalhoun dcalhoun marked this pull request as ready for review February 3, 2025 12:46
@dcalhoun dcalhoun requested a review from nbradbury February 3, 2025 12:47
@nbradbury nbradbury self-assigned this Feb 3, 2025
@nbradbury
Copy link
Contributor

@dcalhoun I must be doing something wrong. I've uncommented localGutenbergKitPath in local-builds.gradle and pulled this branch but when I try to test the gradient background for a gallery group I see this in the log:

wp.components.CustomGradientPicker failed to parse the gradient with error ReferenceError: result is not defined
2025-02-03 08:27:39.368 16602-16602 GutenbergView           com.jetpack.android.beta             I  wp.components.CustomGradientPicker failed to parse the gradient with error ReferenceError: result is not defined
2025-02-03 08:27:39.402 16602-16602 GutenbergView           com.jetpack.android.beta             I  ReferenceError: result is not defined

@dcalhoun
Copy link
Member Author

dcalhoun commented Feb 3, 2025

@nbradbury given the proposed changes include a dependency version change, you'll need to ensure that updated dependency is installed and your local build is relying upon the development server. Will you let me know if the following resolves the issue?

  1. Stop GutenbergKit's dev server if it is running.
  2. npm install
  3. npm run dev
  4. Build the app with the GUTENBERG_EDITOR_URL pointing to your local development server URL.

If the above does not work, try repeating the steps and running rm -rf ./node_modules between steps 1 and 2.

If it continues to fail, please share the version the return of:

cat ./node_modules/gradient-parser/package.json | grep "version"

@nbradbury
Copy link
Contributor

  • Stop GutenbergKit's dev server if it is running.

How do I do that?

@dcalhoun
Copy link
Member Author

dcalhoun commented Feb 3, 2025

  • Stop GutenbergKit's dev server if it is running.

How do I do that?

You would press Control+C in the terminal session where the development server is running—i.e., wherever you previously ran npm run dev or make dev-server before.

If you are unable to find that session, you could use the following to find the process and kill it:

ps -ax | grep vite
kill -9 <relevant_process_id>

@nbradbury
Copy link
Contributor

That doesn't appear to do anything for me, but perhaps this provides a clue?

Screenshot 2025-02-03 at 2 35 00 PM

@dcalhoun
Copy link
Member Author

dcalhoun commented Feb 3, 2025

That doesn't appear to do anything for me [...]

Is that to say that the steps I outlined did not resolve the issue?

Yes, your screenshot showcases the incorrect version is installed on your machine, which explains why you continue to see the ReferenceError: result is not defined error. The steps I outlined should resolve the incorrect package version installation. Namely, npm install.

Will you please complete the following and share the result?

  1. Stop the development server if it is running.
  2. git fetch
  3. git checkout fix/gradient-background-error
  4. rm -rf ./node_modules
  5. npm install
  6. cat ./node_modules/gradient-parser/package.json | grep "version"

For good measure, will you please also share the result of the following?

node --version && npm --version

@nbradbury
Copy link
Contributor

@dcalhoun I've attached the output of running npm install. Here's the output from node --version && npm --version:

v10.16.1
6.9.0

npm-install.txt

@dcalhoun
Copy link
Member Author

dcalhoun commented Feb 4, 2025

[...] Here's the output from node --version && npm --version:

v10.16.1
6.9.0

@nbradbury you appear to be encountering the same Node.js version issue we've discussed in the past (see p1738079888116949/1738074782.197839-slack-C011BKNU1V5). Node.js v20 is required for the project, and the likely reason the correct package version is failing to install.

I highly recommend configuring nvm to auto-switch Node.js versions when you cd into a directory to avoid this confusion moving forward.

npm WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@3. I'll try to do my best with it!

For additional insight, the first warning in the npm install log1 indicates the same issue, where it warns of an incompatible (old) version of npm.

Footnotes

  1. I recognize the several warnings make it difficult to note relevant warnings. Unfortunately, this is an upstream issue in the core Gutenberg project.

@dcalhoun dcalhoun enabled auto-merge (squash) February 4, 2025 12:14
@kean
Copy link
Contributor

kean commented Feb 5, 2025

Hey, should I be able to see the "Styles" in the attached demo project?

Screenshot 2025-02-04 at 8 08 57 PM

Preemptively approving because the changes look good.

I also noticed there is still a typo in the testing instructions:

// Current
Launch Xcode and open the ios/Demo-iOS/GutenbergKit.xcodeproj project.

// Should be
Launch Xcode and open the ios/Demo-iOS/Gutenberg.xcodeproj project.

@kean kean self-requested a review February 5, 2025 01:10
@dcalhoun dcalhoun merged commit 0827a0a into trunk Feb 5, 2025
9 checks passed
@dcalhoun dcalhoun deleted the fix/gradient-background-error branch February 5, 2025 01:10
@dcalhoun
Copy link
Member Author

dcalhoun commented Feb 5, 2025

Hey, should I be able to see the "Styles" in the attached demo project?

No, not in its current state. This stems from the fact that the demo editor does not attach to a site. No site → no theme → no theme settings → no style configuration support.

Mocking the relevant endpoints in the demo app and returning reasonable fixture data would improve the development experience. It'd also be helpful to implement handlers for undo/redo, code editor, etc.

I also noticed there is still a typo in the testing instructions:

Thanks for catching this. 🙇🏻‍♂️ I opened #73 to fix the typo via GitHub's web interface.

Screenshot 2025-02-05 at 07 49 49

dcalhoun added a commit that referenced this pull request Dec 8, 2025
This was added to fix a `can't find variable result` error. It appears
that since then `@wordpress/components` upgraded the version it depends
upon, making this override unnecessary.

See:

- #70
- WordPress/gutenberg#69783
dcalhoun added a commit that referenced this pull request Dec 8, 2025
* build: Upgrade dev dependencies to latest versions

* build: Upgrade various `@wordpress` dependencies

* build: Patch `@wordpress/editor` and `@wordpress/format-library` exports

Enable importing the package's CSS files, which is promoted as an option
in the Gutenberg Framework documentation: https://wordpress.org/gutenberg-framework/docs/intro#bootstrap-your-block-editor

The package `exports` field was added in: WordPress/gutenberg@f13dcfa

Without these exports, Vite's build fails with the following error:

```
(!) Failed to run dependency scan. Skipping dependency pre-bundling. Error:   Failed to scan for dependencies from entries:
  ./GutenbergKit/src/index.html

  ✘ [ERROR] Missing "./build-style/style.css" specifier in "@wordpress/format-library" package [plugin vite:dep-scan]

    src/utils/editor-styles.js:11:7:
      11 │ import '@wordpress/format-library/build-style/style.css';
         ╵        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  This error came from the "onResolve" callback registered here:

    node_modules/esbuild/lib/main.js:1151:20:
      1151 │       let promise = setup({
           ╵                     ^

    at setup (file://./GutenbergKit/node_modules/vite/dist/node/chunks/config.js:31620:12)
    at handlePlugins (./GutenbergKit/node_modules/esbuild/lib/main.js:1151:21)

```

* build: Remove `@wordpress/core-commands` usage

This existing import resulted in the following Vite build error. This
began occurring after upgrading the package. Presumably, this began
occurring due to the addition of the `exports` field to the package in:
WordPress/gutenberg@f13dcfa

```
(!) Failed to run dependency scan. Skipping dependency pre-bundling. Error: The following dependencies are imported but could not be resolved:

  @wordpress/core-commands (imported by ./GutenbergKit/src/utils/wordpress-globals.js)

Are they installed?
    at file://./GutenbergKit/node_modules/vite/dist/node/chunks/config.js:31896:33
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async file://./GutenbergKit/node_modules/vite/dist/node/chunks/config.js:34104:15

```

* build: Upgrade `@wordpress/block-library`

* build: Upgrade `@wordpress/rich-text`

* build: Vite `wordpress-externals-plugin` ignores raw file imports

Disable externalization of `raw` imports as the global does not provide
the required ability to inline the raw imported value.

* build: Upgrade `@wordpress/components` to latest version

* build: Upgrade `@wordpress/block-editor` to the latest version

* refactor: Replace broken private `getLayoutStyles` API usage

This once private API is now a public export of the new
`@wordpress/global-styles-engine` package.

WordPress/gutenberg@fa08e73df3#diff-3203defa3f9fa7d0f14af961ece72d1a2dabc639b1d746b975ab0d98fcfb7ab1L9-L10

* refactor: Scope editor content styles to block canvas

Load these styles in the editor block canvas `iframe` to minimize
potential style collisions in the top-level editor. These styles are
only intended for content within the editor, not the editor UI itself.

* fix: Improve toolbar styling consistency

Rely upon the "toolbar" `variant` configuration to ensure each toolbar
group renders a separating right border.

* build: Fix lint errors from module resolution failures

This
`@wordpress/eslint-plugin@22.21.0` changed the import resolution
behavior. This is incompatible with the GutenbergKit project for an
unknown reason, possibly the lack of TypeScript setup.

```
Unable to resolve path to module '[module_name]'  import/no-unresolved
```

See: https://github.com/WordPress/gutenberg/blob/2d2571507d8ea76f263089c98c46110403e3eba3/packages/eslint-plugin/CHANGELOG.md#22210-2025-11-12

* test: Avoid test failures from unlocking undefined stores

With the previous mock, the entire `@wordpress/data` module was
replaced. This led to emptying utilities like `createReduxStore`. This
resulted in other `@wordpress` modules throwing errors when attempting
to act upon stores.

```
 FAIL  src/utils/editor.test.jsx [ src/utils/editor.test.jsx ]
Error: Cannot unlock an undefined object.
 ❯ unlock node_modules/@wordpress/private-apis/src/implementation.ts:186:9
 ❯ node_modules/@wordpress/blocks/src/store/index.js:31:1
 ❯ node_modules/@wordpress/blocks/src/index.js:11:1
```

* build: Upgrade `@vitejs/plugin-react`

The latest version of `@vitejs/plugin-react` is ESM-only. This is
incompatible with `@wordpress/eslint-plugin`'s usage of
`eslint-import-resolver-node`. Once `eslint-import-resolver-node` is
upgraded or replaced with a more capable alternative, we can remove
the `eslint-disable-line` comments.

Resulting error:

```
Unable to resolve path to module '@vitejs/plugin-react'  import/no-unresolved
```

See: vitejs/vite-plugin-react#691 (comment)

* build: Upgrade `@wordpress/eslint-plugin` to latest

`tsconfig.json` introduced as `@wordpress/eslint-plugin`'s replaced its
usage of `eslint-import-resolver-node` with
`eslint-import-resolver-typescript`. Those changes now require a
`tsconfig.json` file to ensure the resolver works properly. The
`tsconfig.json` does not add TypeScript support.

See: WordPress/gutenberg@a4ba451#diff-691525462f8b4cac47c3c2e0e2668606f539ed0b3f62f8f6b79dffe7ef38bd9d

`legacy-peer-deps=true` introduced to address a dependency version
conflict. See Claude's explanation:

> 1. eslint-import-resolver-typescript@4.4.4 has an optional peer dependency on eslint-plugin-import-x
> 2. eslint-plugin-import-x requires @typescript-eslint/utils@^8.0.0
> 3. @wordpress/eslint-plugin@22.22.0 depends on @typescript-eslint/eslint-plugin@^6.4.1, which uses @typescript-eslint/utils@6.21.0
>
> So we have:
> - @typescript-eslint/utils@6.21.0 (from @wordpress/eslint-plugin chain)
> - @typescript-eslint/utils@8.48.1 (wanted by eslint-plugin-import-x, optional peer of the resolver)
>
> Since eslint-plugin-import-x is marked as optional in the resolver, using --legacy-peer-deps should be safe. The resolver will work without it - it's
> an alternative to eslint-plugin-import for those who want to use it.

Once `@wordpress/eslint-plugin` updates its `@typescript-eslint`
dependencies to v8.x, we can remove the `legacy-peer-deps` usage.

* fix: Remove block library theme styles

These styles overrode foundational styles causing unexpected layout
bugs. E.g., the theme styles removed the top margin from image blocks.

* refactor: Load component styles into block editor canvas

This is no strictly necessary currently as we disable `shouldIframe`,
but it further aligns GutenbergKit with Gutenberg.

* docs: Remove outdated `@wordpress/components` patch reference

The patch was removed in 6fc0ca5.

* build: Install dependencies without `legacy-peer-deps`

This was deemed to broad of a solution to a deep dependency version
conflict described in e9f99f4.

* build: Remove now unnecessary `gradient-parser` version override

This was added to fix a `can't find variable result` error. It appears
that since then `@wordpress/components` upgraded the version it depends
upon, making this override unnecessary.

See:

- #70
- WordPress/gutenberg#69783

* build: Upgrade a few dev dependencies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error thrown when setting a gradient as a block background

3 participants