microsoft/semantic-kernel · error · FileNotFoundError
File {path} not found in repository.
Error message
File {path} not found in repository. What it means
Raised by RepoFilePlugin.read_file_by_path when open(path) raises FileNotFoundError. The path is resolved relative to the repository root (four levels up from the plugin file). The original FileNotFoundError is caught and re-raised with a descriptive message.
Source
Thrown at python/samples/demos/document_generator/plugins/repo_file_plugin.py:25
class RepoFilePlugin:
"""A plugin that reads files from this repository.
This plugin assumes that the code is run within the Semantic Kernel repository.
"""
@kernel_function(description="Read a file given a relative path to the root of the repository.")
def read_file_by_path(
self, path: Annotated[str, "The relative path to the file."]
) -> Annotated[str, "Returns the file content."]:
path = os.path.join(os.path.dirname(__file__), "..", "..", "..", "..", path)
try:
with open(path) as file:
return file.read()
except FileNotFoundError:
raise FileNotFoundError(f"File {path} not found in repository.")
@kernel_function(
description="Read a file given the name of the file. Function will search for the file in the repository."
)
def read_file_by_name(
self, file_name: Annotated[str, "The name of the file."]
) -> Annotated[str, "Returns the file content."]:
path = os.path.join(os.path.dirname(__file__), "..", "..", "..", "..")
for root, dirs, files in os.walk(path):
if file_name in files:
print(f"Found file {file_name} in {root}.")
with open(os.path.join(root, file_name)) as file:
return file.read()
raise FileNotFoundError(f"File {file_name} not found in repository.")
@kernel_function(description="List all files or subdirectories in a directory.")
def list_directory(
self, path: Annotated[str, "Path of a directory relative to the root of the repository."]View on GitHub (pinned to c028a0c7dc)
Solutions
- Verify the relative path against the actual repository root layout.
- Confirm the plugin file's location so the four '..' hops resolve to the intended base; adjust if the file was relocated.
- Use read_file_by_name if you only know the filename and want a tree search.
- Normalize/validate the path and check existence with os.path.exists before open().
Example fix
// before
plugin.read_file_by_path('src/main.py')
// after
base = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', '..'))
target = os.path.normpath(os.path.join(base, 'src/main.py'))
if not os.path.isfile(target):
raise FileNotFoundError(f'File not at {target}')
plugin.read_file_by_path('python/samples/.../src/main.py') Defensive patterns
Strategy: validation
Validate before calling
import os
base = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', '..'))
target = os.path.normpath(os.path.join(base, rel_path))
if not os.path.isfile(target):
raise FileNotFoundError(f'File not at {target}')
with open(target) as f:
return f.read() Type guard
import os
def is_valid_repo_path(rel_path: str) -> bool:
base = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', '..'))
target = os.path.normpath(os.path.join(base, rel_path))
return os.path.isfile(target) and target.startswith(base) Try / catch
try:
return plugin.read_file_by_path(rel_path)
except FileNotFoundError as ex:
# fall back to name-based search or inform the model
raise Prevention
- Pin the plugin file location or compute the base dynamically.
- Reject path traversal (target.startswith(base)).
- Prefer read_file_by_name when only the filename is known.
When it happens
Trigger: Calling the kernel function with a relative path that does not exist under the repo root; path traversal that escapes the expected tree; the file lives outside the resolved base directory.
Common situations: An LLM hallucinates a file path; the plugin's relative base assumption (.. x4) is wrong after the file is moved; case-sensitivity mismatches on case-insensitive dev machines vs Linux.
Related errors
- File {file_name} not found in repository.
- Directory {path} not found in repository.
- File '{fileName}' not found.
- Configuration not found, please setup the notebooks first us
- Plugins directory not found. The app needs the plugins from
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/e9ceca9c72f78b0f.
Report an issue: GitHub.