marktext/marktext · warning · Error

Expected non-empty language for spell checker.

Error message

Expected non-empty language for spell checker.

What it means

Thrown by SpellChecker.switchLanguage() on non-macOS platforms when the supplied `lang` is falsy. macOS short-circuits (OS spell checker auto-detects). On Windows/Linux an empty language cannot be forwarded to the Chromium spell checker IPC, so it is rejected before invoking mt::spellchecker-switch-language.

Source

Thrown at packages/desktop/src/renderer/src/spellchecker/index.ts:79

    }
    return ''
  }

  set lang(lang: string) {
    this.currentSpellcheckerLanguage = lang
  }

  /**
   * Explicitly switch the language to a specific language.
   *
   * NOTE: This function can throw an exception.
   */
  async switchLanguage(lang: string): Promise<boolean> {
    if (isOsx) {
      // NB: macOS uses the OS spell checker and detects language automatically.
      return true
    } else if (!lang) {
      throw new Error('Expected non-empty language for spell checker.')
    } else if (this.isEnabled) {
      await window.electron.ipcRenderer.invoke('mt::spellchecker-switch-language', lang)
      this.lang = lang
      return true
    }
    return false
  }

  /**
   * Returns a list of available dictionaries.
   */
  static async getAvailableDictionaries(): Promise<string[]> {
    if (isOsx) {
      // NB: macOS uses the OS spell checker and detects language automatically.
      return []
    }
    return window.electron.ipcRenderer.invoke('mt::spellchecker-get-available-dictionaries')
  }

View on GitHub (pinned to e52106fd1c)

Solutions

  1. Pass a concrete language code (e.g. 'en-US') to switchLanguage / activateSpellchecker.
  2. In activateSpellchecker, default to a detected system language when the stored value is empty.
  3. Validate languageCode in switchSpellcheckLanguage (editor.vue) before invoking the spellchecker.
  4. Ensure preferences.language is non-empty before initializing the SpellChecker.

Example fix

// before
async switchLanguage(lang: string): Promise<boolean> {
  if (isOsx) return true
  else if (!lang) throw new Error('Expected non-empty language for spell checker.')
}
// after — default + validate
async switchLanguage(lang: string): Promise<boolean> {
  if (isOsx) return true
  const code = (lang || this.currentSpellcheckerLanguage || 'en-US').trim()
  if (!code) throw new Error('Expected non-empty language for spell checker.')
}
Defensive patterns

Strategy: validation

Validate before calling

function nonEmptyLang(lang: string | undefined | null): string | null {
  const v = (lang || '').trim()
  return v.length ? v : null
}

Type guard

function isNonEmptyLang(lang: unknown): lang is string {
  return typeof lang === 'string' && lang.trim().length > 0
}

Prevention

When it happens

Trigger: Calling switchLanguage('') or switchLanguage(undefined); currentSpellcheckerLanguage is empty and activateSpellchecker() is called with no argument (it passes this.currentSpellcheckerLanguage which defaults to '').

Common situations: First-run on Windows/Linux before a language has been chosen. A preferences value of language: '' propagated to the spell checker. A bus event delivers an empty languageCode.

Related errors


AI-assisted analysis of marktext/marktext@e52106fd1c (2026-08-12). Data as JSON: /api/errors/d0a8ee9af6c3511d. Report an issue: GitHub.