neoclide/coc.nvim · error · TypeError

folder should be string

Error message

folder should be string

What it means

coc.nvim's addWorkspaceFolder API only accepts a string path; it throws a TypeError immediately when the argument is not a string. This is an eager argument validation so bad input fails fast before any filesystem or workspace-folder state is touched.

Source

Thrown at src/handler/workspace.ts:180

    }
    await workspace.jumpTo(URI.file(path.join(dir, CONFIG_FILE_NAME)))
  }

  public async renameCurrent(): Promise<void> {
    let { nvim } = this
    let oldPath = await nvim.call('coc#util#get_fullpath', []) as string
    let newPath = await callAsync(nvim, 'input', ['New path: ', oldPath, 'file']) as string
    newPath = newPath.trim()
    if (newPath === oldPath || !newPath) return
    if (oldPath.toLowerCase() != newPath.toLowerCase() && fs.existsSync(newPath)) {
      let overwrite = await window.showPrompt(`${newPath} exists, overwrite?`)
      if (!overwrite) return
    }
    await workspace.renameFile(oldPath, newPath, { overwrite: true })
  }

  public addWorkspaceFolder(folder: string): void {
    if (!Is.string(folder)) throw TypeError(`folder should be string`)
    folder = workspace.expand(folder)
    if (!isDirectory(folder)) throw directoryNotExists(folder)
    workspace.workspaceFolderControl.addWorkspaceFolder(folder, true)
  }

  public removeWorkspaceFolder(folder: string): void {
    if (!Is.string(folder)) throw TypeError(`folder should be string`)
    folder = workspace.expand(folder)
    if (!isDirectory(folder)) throw directoryNotExists(folder)
    workspace.workspaceFolderControl.removeWorkspaceFolder(folder)
  }

  public async bufferCheck(): Promise<void> {
    let doc = await workspace.document
    if (!doc.attached) {
      await window.showDialog({
        title: 'Buffer check result',
        content: `Document not attached, ${doc.notAttachReason}`,

View on GitHub (pinned to 50e974d969)

Solutions

  1. Convert the argument to a string before calling (e.g. folder.uri or String(folder)).
  2. If the value may be undefined, check it and bail out or use a default before calling.
  3. Ensure the path exists as a directory too, or the next error (directory not exists) will fire.

Example fix

// before
workspace.addWorkspaceFolder(folder?.uri)
// after
if (folder && typeof folder.uri === 'string') workspace.addWorkspaceFolder(folder.uri)
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof folder !== 'string' || folder.length === 0) throw new Error('addWorkspaceFolder needs a non-empty string path')

Type guard

function isFolderString(v: unknown): v is string { return typeof v === 'string' }

Try / catch

try { workspace.addWorkspaceFolder(folder) } catch (e) { if (String(e).includes('folder should be string')) { /* fix input type */ } else throw e }

Prevention

When it happens

Trigger: Calling workspace.addWorkspaceFolder with a non-string value, e.g. null/undefined, a URI object, or a number.

Common situations: Passing a workspaceFolder object instead of its name/uri string; passing an optional config value that is undefined; callers that resolved a folder from an async API and forgot `await`.

Understand the failure class

Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.

Related errors


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