neoclide/coc.nvim · error · TypeError

name and doComplete required for createSource

Error message

name and doComplete required for createSource

What it means

The sources registry's createSource validates its configuration: it requires a string 'name' and a function 'doComplete'. Passing a malformed config is a programming error and throws a TypeError instead of registering a half-working source.

Source

Thrown at src/completion/sources.ts:396

        disabled: !item.enable
      })
    }
    return stats
  }

  private onDocumentEnter(bufnr: number): void {
    let { sources } = this
    for (let s of sources) {
      if (s.enable && Is.func(s.onEnter)) {
        s.onEnter(bufnr)
      }
    }
  }

  public createSource(config: SourceConfig): Disposable {
    if (typeof config.name !== 'string' || typeof config.doComplete !== 'function') {
      logger.error(`Bad config for createSource:`, config)
      throw new TypeError(`name and doComplete required for createSource`)
    }
    let source = new Source(Object.assign({ sourceType: SourceType.Service } as any, config))
    return this.addSource(source)
  }
  /**
   * @internal
   */

  public dispose(): void {
    disposeAll(this.disposables)
  }
}

export function logError(err: any): void {
  logger.error('Error on source create', err)
}

export function getSourceType(sourceType: SourceType): string {

View on GitHub (pinned to 50e974d969)

Solutions

  1. Pass an object with both name (string) and doComplete (function)
  2. Fix the config construction so doComplete is not undefined
  3. Check the config object logged by coc before createSource (it logs 'Bad config for createSource')

Example fix

// before
sources.createSource({ name: 'mytag' })
// after
sources.createSource({ name: 'mytag', doComplete: async (opt) => ({ items: [] }), triggerPatterns: [] })
Defensive patterns

Strategy: validation

Validate before calling

// validate config before createSource
const ok = typeof cfg?.name === 'string' && typeof cfg?.doComplete === 'function'
if (!ok) throw new TypeError('source config needs name:string and doComplete:function')

Type guard

const isSourceConfig = (c): c is SourceConfig =>
  !!c && typeof c.name === 'string' && typeof c.doComplete === 'function'

Try / catch

try {
  sources.createSource(config)
} catch (e) {
  if (e instanceof TypeError) logger.error('bad source config', config)
  else throw e
}

Prevention

When it happens

Trigger: Calling coc.sources.createSource({}) or with a config object where doComplete is a non-function (e.g. a promise result, undefined after bad import, or name accidentally overwritten).

Common situations: Extension authors building source configs dynamically and omitting fields; refactors that renamed doComplete in a custom source but not in the config passed to createSource.

Related errors


AI-assisted analysis of neoclide/coc.nvim@50e974d969 (2026-08-31). Data as JSON: /api/errors/564176cc9658d6f8. Report an issue: GitHub.