Skip to content

Farm adapter: this.warn and this.error abort the process #647

Open

Description

@zloirock

This issue was drafted with the help of LLMs (Claude and Codex); I reviewed it and verified the reproductions.

Description

The Supported Context table marks this.warn and this.error as supported for Farm. However, calling either of them from resolveId, load or transform of a plugin created with createUnplugin(...).farm() terminates the Node.js process with a Rust panic (SIGABRT, exit code 134). The warning is never printed, the error never reaches Farm's own error reporting, and the failure cannot be caught from JavaScript.

Minimal reproduction

In an empty directory:

npm init -y
npm install --save-exact unplugin@3.7.0 @farmfe/core@2.0.1

Save the following as repro.mjs and run node repro.mjs. It creates its own entry module.

import { writeFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { build } from '@farmfe/core';
import { createUnplugin } from 'unplugin';

const root = fileURLToPath(new URL('.', import.meta.url));
const entry = fileURLToPath(new URL('./entry.js', import.meta.url));
writeFileSync(entry, 'export default 1;\n');

const plugin = createUnplugin(() => ({
  name: 'warn-repro',
  transform(code, id) {
    if (id === entry) this.warn('something worth a warning');
    return null;
  },
}));

await build({
  root,
  plugins: [plugin.farm()],
  compilation: {
    input: { index: entry },
    output: { path: 'dist' },
    persistentCache: false,
  },
});
console.log('build finished');

Expected behavior

Farm prints the warning and the build completes:

[warn] something worth a warning
[ Farm ] Build completed in 26ms  Resources emitted to dist.
build finished

This is the output of the same script with the change from "Additional context" applied (Farm's table of output files omitted).

Actual behavior

thread '<unnamed>' (165713801) panicked at crates/node/src/plugin_adapters/js_plugin_adapter/context.rs:246:6:
Argument 0 should be a string when calling warn: Error { status: "InvalidArg", reason: "invalid type: map, expected a string" }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

The process exits with code 134. build finished is not printed, and a try / catch around build() does not see anything.

Environment

  • unplugin: 3.7.0
  • @farmfe/core: 2.0.1 (1.7.11 behaves the same, the panic is reported at context.rs:233:6 there)
  • Node.js: 26.13.0
  • macOS, arm64

Additional context

The adapter's unpluginContext turns a string into an Error before handing it to Farm, and passes any other value through. Farm's CompilationContext declares warn(message: string) and error(message: string), and its native binding unwraps the argument as a string, so a non-string argument panics instead of throwing.

As a result no argument shape works. On both Farm versions, all 18 combinations of hook (resolveId, load, transform), method (this.warn, this.error) and argument (a string, an Error, a { message } object) abort the process the same way. resolveId needs enforce: 'pre' to be reached in this setup.

As a control, a plain Farm JS plugin behaves as expected with a string: context.warn('text') prints [warn] text and the build completes, and context.error('text') fails the build with the message and exit code 1. The same plugin with context.warn(new Error('text')) panics as above.

This is not a regression: the conversion has been in src/farm/context.ts since Farm support was added in #341 (unplugin 1.10.0).

farm-fe/farm#2053, open since January 2025, shows the same panic for error with unplugin-vue-components under farm dev.

Passing the message as a string fixes it:

export function unpluginContext(context: CompilationContext): UnpluginContext {
  return {
    error: (error: any) =>
      context!.error(typeof error === 'string' ? error : error.message),
    warn: (error: any) =>
      context!.warn(typeof error === 'string' ? error : error.message),
  }
}

With these two lines changed in the built dist/index.mjs of 3.7.0, the same 18 combinations behave like the plain Farm plugin above: every this.warn prints the message and the build completes, and every this.error fails the build with the message and exit code 1.

Could the adapter pass the message to Farm as a string?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions