grafana/k6 · error

parsing file descriptor: %w

Error message

parsing file descriptor: %w

What it means

Thrown while parsing the files argument of ElementHandle.setInputFiles. k6 exports each JavaScript file descriptor object into the Go File struct via rt.ExportTo, and the export fails because the object's shape does not match. The expected descriptor is { name: string, mimeType: string, buffer: string } (File struct in element_handle_options.go:92). Fields with mismatched types (e.g. a buffer that is not a string) make the export fail.

Source

Thrown at internal/js/modules/k6/browser/common/element_handle_options.go:233

// NewElementHandleSetInputFilesOptions creates a new ElementHandleSetInputFilesOption.
func NewElementHandleSetInputFilesOptions(defaultTimeout time.Duration) *ElementHandleSetInputFilesOptions {
	return &ElementHandleSetInputFilesOptions{
		ElementHandleBaseOptions: *NewElementHandleBaseOptions(defaultTimeout),
	}
}

// addFile to the struct. Input value can only be a file descriptor object.
func (f *Files) addFile(ctx context.Context, file sobek.Value) error {
	if common.IsNullish(file) {
		return nil
	}
	rt := k6ext.Runtime(ctx)
	fileType := file.ExportType()
	switch fileType.Kind() {
	case reflect.Map: // file descriptor object
		var parsedFile File
		if err := rt.ExportTo(file, &parsedFile); err != nil {
			return fmt.Errorf("parsing file descriptor: %w", err)
		}
		f.Payload = append(f.Payload, &parsedFile)
	default:
		return fmt.Errorf("invalid parameter type : %s", fileType.Kind().String())
	}

	return nil
}

// Parse parses the Files struct from the given sobek.Value.
func (f *Files) Parse(ctx context.Context, files sobek.Value) error {
	rt := k6ext.Runtime(ctx)
	if common.IsNullish(files) {
		return nil
	}

	optsType := files.ExportType()
	switch optsType.Kind() {

View on GitHub (pinned to 93accf6570)

Solutions

  1. Make each descriptor exactly { name: string, mimeType: string, buffer: string }
  2. Encode binary payloads into a string (e.g. base64 via k6/encoding) before putting them in buffer
  3. Log each descriptor before calling setInputFiles to find the malformed one
  4. For arrays, verify every element is a plain object with string-valued fields

Example fix

// before
await el.setInputFiles([{ name: 'f.bin', mimeType: 'application/octet-stream', buffer: 12345 }]);

// after
import encoding from 'k6/encoding';
const b64 = encoding.b64encode('\x00\x01\x02', 'std');
await el.setInputFiles([{ name: 'f.bin', mimeType: 'application/octet-stream', buffer: b64 }]);
Defensive patterns

Strategy: validation

Validate before calling

function isFileDescriptor(o) {
  return !!o && typeof o === 'object' && !Array.isArray(o) &&
    typeof o.name === 'string' && typeof o.mimeType === 'string' &&
    (o.buffer === undefined || typeof o.buffer === 'string');
}
const files = [{ name: 'a.txt', mimeType: 'text/plain', buffer: 'hello' }];
if (!files.every(isFileDescriptor)) throw new Error('malformed file descriptor');
await el.setInputFiles(files);

Type guard

function isFileDescriptor(o) {
  return !!o && typeof o === 'object' && !Array.isArray(o) &&
    typeof o.name === 'string' && typeof o.mimeType === 'string' &&
    (o.buffer === undefined || typeof o.buffer === 'string');
}

Try / catch

try {
  await el.setInputFiles(files);
} catch (e) {
  if (/parsing file descriptor/.test(e.message)) {
    // normalize every descriptor to {name, mimeType, buffer} strings and retry
  } else throw e;
}

Prevention

When it happens

Trigger: setInputFiles([{ name: 'a.txt', mimeType: 'text/plain', buffer: 123 }]) with a non-string buffer; descriptor objects containing nested objects, arrays, or numbers where strings are expected; one malformed element inside an otherwise valid files array.

Common situations: Porting Playwright scripts: Playwright accepts { name, mimeType, buffer: Buffer } while this k6 browser version expects buffer as a string; building descriptors dynamically without validating field types; passing binary data unwrapped.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/67f4e79f8e777ab0. Report an issue: GitHub.