JetBrains/intellij-community · error · IncorrectOperationException

File not in

Error message

File not in 

What it means

PsiSnippetAttributeValueImpl models the file attribute of an @snippet javadoc tag (JDK 18+ inline snippets), where external snippet files are addressed relative to a snippet root package. bindToElement computes a dot-separated relative path from the snippet root; if the target file is not under that root, getRelativePath returns null and it throws IncorrectOperationException("File not in <root>: <file>").

Source

Thrown at java/java-psi-impl/src/com/intellij/psi/impl/source/javadoc/PsiSnippetAttributeValueImpl.java:230

    public ResolveResult @NotNull [] multiResolve(boolean incompleteCode) {
      PsiElement element = resolve();
      return element == null ? ResolveResult.EMPTY_ARRAY : PsiElementResolveResult.createResults(element);
    }

    @Override
    public PsiElement handleElementRename(@NotNull String newElementName) throws IncorrectOperationException {
      // TODO
      return null;
    }

    @Override
    public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
      if (!(element instanceof PsiFile)) throw new IncorrectOperationException("PsiFile expected");
      if (isReferenceTo(element)) return PsiSnippetAttributeValueImpl.this;
      VirtualFile virtualFile = ((PsiFile)element).getVirtualFile();
      String relativePath = VfsUtilCore.getRelativePath(virtualFile, mySnippetRoot, '.');
      if (relativePath == null) {
        throw new IncorrectOperationException("File not in " + mySnippetRoot + ": " + virtualFile);
      }
      if (myExtension != null) {
        if (!StringUtil.endsWithIgnoreCase(relativePath, "." + myExtension)) {
          throw new IncorrectOperationException("File name must end with ." + myExtension + ": " + virtualFile);
        }
        relativePath = relativePath.substring(0, relativePath.length() - myExtension.length() - 1);
      }
      String newText;
      if (!relativePath.contains(mySeparator)) {
        newText = relativePath;
      }
      else if (getText().startsWith("'")) {
        newText = "'" + relativePath + "'";
      }
      else {
        newText = "\"" + relativePath + "\"";
      }
      ASTNode node = getNode();

View on GitHub (pinned to be881553f2)

Solutions

  1. Keep referenced snippet files under the snippet root (same source root/package hierarchy as the javadoc file).
  2. Before binding, verify VfsUtilCore.getRelativePath(file.getVirtualFile(), snippetRoot, '.') != null; if null, copy or move the file into the root first.
  3. When the IDE move refactoring refuses, move the snippet file manually into the snippet root and update the attribute text.

Example fix

// before
snippetAttrRef.bindToElement(targetPsiFile); // file outside snippet root -> throws

// after
VirtualFile vf = targetPsiFile.getVirtualFile();
if (VfsUtilCore.getRelativePath(vf, snippetRoot, '.') == null) {
  // move/copy the file under snippetRoot first, then bind
} else {
  snippetAttrRef.bindToElement(targetPsiFile);
}
Defensive patterns

Strategy: validation

Validate before calling

VirtualFile vf = targetFile.getVirtualFile();
if (VfsUtilCore.getRelativePath(vf, snippetRoot, '.') == null) return; // outside snippet root

Try / catch

try { snippetRef.bindToElement(f); } catch (IncorrectOperationException e) { /* move file into snippet root */ }

Prevention

When it happens

Trigger: Calling bindToElement on an @snippet file reference with a PsiFile located outside the configured snippet root (by default the package containing the javadoc's file, or the snippet-package root); e.g. moving a snippet file to another source root and rebinding the reference.

Common situations: Move refactorings of snippet files across source roots that are not snippet roots; projects with multiple source roots where the snippet lives in a non-resource root; misconfigured 'snippet' packages.

Related errors


AI-assisted analysis of JetBrains/intellij-community@be881553f2 (2026-08-14). Data as JSON: /api/errors/902c511688b8c0b8. Report an issue: GitHub.