FuelLabs/sway · error · anyhow::Error
failed to find a template in {}
Error message
failed to find a template in {} What it means
When `forc template <url> <project-name>` is invoked without --template-name, the whole fetched repo root must itself be a forc package: PackageManifest::from_file(repo_path/Forc.toml) is attempted and .is_err() triggers this bail. It means neither a template name was given nor a root Forc.toml exists.
Source
Thrown at forc/src/ops/forc_template.rs:59
);
if !repo_path.exists() {
println_action_green("Fetching", git_source.to_string().as_str());
source::git::fetch(fetch_id, &local_repo_name, &git_source)?;
}
let from_path = match command.template_name {
Some(ref template_name) => manifest::find_dir_within(&repo_path, template_name)
.ok_or_else(|| {
anyhow!(
"failed to find a template `{}` in {}",
template_name,
command.url
)
})?,
None => {
let manifest_path = repo_path.join(constants::MANIFEST_FILE_NAME);
if PackageManifest::from_file(manifest_path).is_err() {
anyhow::bail!("failed to find a template in {}", command.url);
}
repo_path
}
};
// Create the target dir
let target_dir = current_dir.join(&command.project_name);
println_action_green(
"Creating",
&format!("{} from template", &command.project_name),
);
// Copy contents from template to target dir
copy_template_to_target(&from_path, &target_dir)?;
// Edit forc.toml
edit_forc_toml(&target_dir, &command.project_name, &whoami::realname())?;
if target_dir.join("test").exists() {View on GitHub (pinned to 47e5e902fa)
Solutions
- Add --template-name pointing at the subdirectory that contains the Forc.toml
- Verify with the repo's docs which subdirectory is the intended template
- If you own the repo, add a root Forc.toml for the default template
Example fix
# before forc template https://github.com/example/templates myapp # after forc template https://github.com/example/templates myapp --template-name counter
Defensive patterns
Strategy: validation
Validate before calling
// A repo works without --template-name only if Forc.toml sits at its root.
if !std::path::Path::new(&format!("{repo_root}/Forc.toml")).is_file() {
anyhow::bail!("repo has no root Forc.toml; pass --template-name pointing at the template subdir");
} Type guard
fn repo_is_root_package(root: &Path) -> bool {
root.join("Forc.toml").is_file()
} Try / catch
match run_forc_template(url, project, None) {
Ok(()) => Ok(()),
Err(e) if e.to_string().contains("failed to find a template in") => {
run_forc_template(url, project, Some("counter")) // retry with an explicit template dir
}
Err(e) => Err(e),
} Prevention
- Default to passing --template-name for collection-style template repos
- If you maintain a template repo, keep a root Forc.toml so bare `forc template <url>` works
When it happens
Trigger: Cloning a multi-template repository (templates in subdirectories, no manifest at the repo root) without passing --template-name.
Common situations: Users pointing at template collections where the root only has a README, or expecting forc to auto-detect the sole template directory, which it does not.
Related errors
- failed to write toml file: {}
- the dependency `{}` could not be found in `{}`
- hex salt declaration needs to start with 0x
- {e}
- failed to read manifest at {:?}: {}
AI-assisted analysis of FuelLabs/sway@47e5e902fa (2026-08-16).
Data as JSON: /api/errors/df737447e91e189b.
Report an issue: GitHub.