SeleniumHQ/selenium · error · Error

Unexpected compiled output format. Expected it to start with

Error message

Unexpected compiled output format. Expected it to start with "(function ", got: ${input.slice(0, 80)}

What it means

wrap-find-elements-as-global.js asserts the compiled findElements TypeScript output starts with '(function ' before prepending window.bot.locators.typescript.findElements =. If the compiler output a different envelope (ESM, class, System.register, bare statement), the assignment prepend would yield invalid JS, so it fails fast. Note: this script asserts the '(function ' prefix (without the trailing paren that getAttribute's wrapper checks), matching the isShown atom's output shape.

Source

Thrown at javascript/atoms/typescript/wrap-find-elements-as-global.js:56

//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.`

const preamble = `
window.bot = window.bot || {};
window.bot.locators = window.bot.locators || {};
window.bot.locators.typescript = window.bot.locators.typescript || {};
window.bot.locators.typescript.findElements = `

const input = fs.readFileSync(inputPath, 'utf8')

if (!input.trimStart().startsWith('(function ')) {
  throw new Error(
    `Unexpected compiled output format. Expected it to start with "(function ", got: ${input.slice(0, 80)}`,
  )
}

const output = licenseHeader + preamble + input.trimStart()
fs.writeFileSync(outputPath, output)

View on GitHub (pinned to aa36b38e69)

Solutions

  1. Ensure tsc emits an IIFE starting with '(function ' (module:'none' or equivalent).
  2. Supply the correct compiled findElements artifact as input.
  3. Add a format/bundle step if the compiler now emits ESM.
  4. Diff input against last-good output to identify the envelope change.

Example fix

// before (tsconfig emits ESM)
{ "compilerOptions": { "module": "es2020" } }

// after (IIFE-compatible)
{ "compilerOptions": { "module": "none", "target": "es2018" } }
Defensive patterns

Strategy: validation

Validate before calling

const input = fs.readFileSync(inputPath, 'utf8')
if (!input.trimStart().startsWith('(function ')) {
  console.error('Compiled findElements output does not start with (function ; check module format')
  process.exit(2)
}

Prevention

When it happens

Trigger: A tsconfig/module change that emits ESM (export/import) instead of an IIFE. Feeding the wrong file (source, sourcemap, or a different atom's output). A bundler transform that wrapped the output in a module-system envelope. Manual edits stripping the leading IIFE.

Common situations: TypeScript/compiler upgrades altering the output module format. Bazel rule pointing the wrap step at the wrong compile artifact. Inserting an intermediate transform that changes the envelope.

Related errors


AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14). Data as JSON: /api/errors/89ef9b5c1e3b1e74. Report an issue: GitHub.