Skip to content

bug: Scripts defined with defineUnlistedScript always return a Promise #1906

@rxliuli

Description

@rxliuli

Describe the bug

For example, the following inject.ts script will be executed using browser.devtools.inspectedWindow.eval.

export default defineUnlistedScript(() => {
  // @ts-expect-error
  if (!globalThis.$0) {
    return
  }
  // @ts-expect-error
  return globalThis.$0.childElementCount
})

The expected return value is number/undefined, but it always returns {}. After careful inspection, I found this script is compiled into

var inject = (function() {
  "use strict";
  function defineUnlistedScript(arg) {
    if (arg == null || typeof arg === "function") return { main: arg };
    return arg;
  }
  const definition = defineUnlistedScript(() => {
    if (!globalThis.$0) {
      return;
    }
    return globalThis.$0.childElementCount;
  });
  function initPlugins() {
  }
  const result = (async () => {
    try {
      initPlugins();
      return await definition.main();
    } catch (err) {
      logger.error(
        `The unlisted script "${"inject"}" crashed on startup!`,
        err
      );
      throw err;
    }
  })();
  return result;
})();

The key issue is that await definition.main() assumes all defined functions return Promises, when in fact they don't. The expected behavior should properly handle both Promise and non-Promise scenarios.

Image

Reproduction

https://github.com/rxliuli/wxt-error-define-unlisted-script-promise

Steps to reproduce

git clone https://github.com/rxliuli/wxt-error-define-unlisted-script-promise
pnpm i && pnpm dev
Open any webpage
Open Devtools > Element
Select Element

System Info

System:
    OS: macOS 26.0
    CPU: (12) arm64 Apple M2 Max
    Memory: 21.25 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.14.0 - /usr/local/bin/node
    npm: 10.9.2 - /usr/local/bin/npm
    pnpm: 10.10.0 - /usr/local/bin/pnpm
    bun: 1.1.6 - ~/.bun/bin/bun
  Browsers:
    Brave Browser: 131.1.73.105
    Chrome: 140.0.7339.186
    Edge: 128.0.2739.54
    Safari: 26.0
  npmPackages:
    wxt: ^0.20.6 => 0.20.11

Used Package Manager

pnpm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    pending-triageSomeone (usually a maintainer) needs to look into this to see if it's a bug

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions