neoclide/coc.nvim · error

Required root pattern not resolved.

Error message

Required root pattern not resolved.

What it means

For language servers that declare a required root pattern (requireRootPattern), coc must locate a workspace root by matching rootPatterns (e.g. package.json, .git) starting from the document's directory. If no pattern is found anywhere up the tree, resolveRoot throws because the server cannot be started without a valid workspace root.

Source

Thrown at src/core/documents.ts:200

  public detach(): void {
    this._attached = false
    for (let bufnr of this.buffers.keys()) {
      this.onBufUnload(bufnr)
    }
  }

  public resolveRoot(rootPatterns: string[], requireRootPattern = false): string | undefined {
    let doc = this.getDocument(this.bufnr)
    let resolved: string | undefined
    if (doc && doc.schema == 'file') {
      let dir = path.dirname(URI.parse(doc.uri).fsPath)
      resolved = resolveRoot(dir, rootPatterns, this.cwd)
    } else {
      resolved = resolveRoot(this.cwd, rootPatterns)
    }
    if (requireRootPattern && !resolved) {
      throw new Error(`Required root pattern not resolved.`)
    }
    return resolved
  }

  public get textDocuments(): LinesTextDocument[] {
    let docs: LinesTextDocument[] = []
    for (let b of this.buffers.values()) {
      if (b.attached) docs.push(b.textDocument)
    }
    return docs
  }

  public getDocument(uri: number | string, caseInsensitive = platform.isWindows || platform.isMacintosh): Document | null | undefined {
    if (typeof uri === 'number') {
      return this.buffers.get(uri)
    }
    let u = URI.parse(uri)
    uri = u.toString()

View on GitHub (pinned to 50e974d969)

Solutions

  1. Open the file inside a project containing a root pattern (e.g. .git, package.json)
  2. Add/relax rootPatterns in coc settings for that language server, or unset requireRootPattern
  3. Use :CocOpenLog / workspace.rootPatterns to debug which patterns are searched
  4. Create a minimal root marker (e.g. empty .git or the configured pattern file) in the directory

Example fix

// before (coc-settings.json)
"tsserver": { "requireRootPattern": true }
// after
"tsserver": { "requireRootPattern": false, "rootPatterns": ["tsconfig.json", ".git"] }
Defensive patterns

Strategy: fallback

Validate before calling

// before opening the file, confirm a root pattern exists
const fs = require('fs'); const path = require('path')
let dir = path.dirname(file)
while (true) {
  if (['.git','package.json','tsconfig.json'].some(p => fs.existsSync(path.join(dir, p)))) break
  const parent = path.dirname(dir); if (parent === dir) { notify('no workspace root found'); break }
  dir = parent
}

Try / catch

try {
  await workspace.openTextDocument(uri)
} catch (e) {
  if (String(e.message).includes('root pattern')) notify('Open this file inside a project folder')
  else throw e
}

Prevention

When it happens

Trigger: Opening a file covered by a language server whose configuration sets requireRootPattern:true while the file lives outside any directory containing the configured rootPatterns.

Common situations: Opening a stray .ts file in a temp dir with tsserver configured with required roots; single-file editing outside a project; wrong rootPatterns configured for the language server.

Related errors


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