gleam-lang/gleam · error
invalid folder
Error message
invalid folder
What it means
get_foldername resolves the target folder name for `gleam new .` by reading the process working directory with env::current_dir().expect("invalid folder"). current_dir() fails with io::Error when the process's cwd has been deleted, unmounted, or its permissions revoked, and the expect turns that into a panic before gleam can produce its nicer UnableToFindProjectRoot error. It only triggers for the `.` special case — passing an explicit name/path never calls env::current_dir().
Source
Thrown at compiler-cli/src/new.rs:491
decided: suggested_name,
},
None => ProjectName::Derived {
folder: initial_name,
decided: suggested_name,
},
});
}
Err(Error::InvalidProjectName {
name: initial_name,
reason: invalid_reason,
})
}
fn get_foldername(path: &str) -> Result<String, Error> {
match path {
"." => env::current_dir()
.expect("invalid folder")
.file_name()
.and_then(|x| x.to_str())
.map(ToString::to_string)
.ok_or(Error::UnableToFindProjectRoot {
path: path.to_string(),
}),
_ => Utf8Path::new(path)
.file_name()
.map(ToString::to_string)
.ok_or(Error::UnableToFindProjectRoot {
path: path.to_string(),
}),
}
}
#[derive(Debug, Clone)]
enum ProjectName {
Provided { decided: String },View on GitHub (pinned to 7e623aa83d)
Solutions
- Re-enter an existing directory in the same shell: `cd .` will fail, so `cd ~` (or restart the shell), then cd into the recreated directory and rerun `gleam new .`.
- Recreate the directory if it was deleted: `mkdir -p /path/to/dir && cd /path/to/dir && gleam new .`.
- Avoid the `.` path entirely: run `gleam new my_project` from a parent directory, which uses Utf8Path::file_name() and never touches current_dir().
Example fix
# before: shell sits in a deleted directory cd /tmp/gone_project # (dir deleted elsewhere) gleam new . # panics: env::current_dir() fails # after: recreate/refresh the cwd first mkdir -p /tmp/gone_project && cd /tmp/gone_project && gleam new . # or sidestep '.' entirely: cd /tmp && gleam new gone_project
Defensive patterns
Strategy: validation
Validate before calling
// Verify the shell's cwd still exists before spawning gleam new
match std::env::current_dir() {
Ok(dir) if dir.exists() => {}
_ => { // cd to a known-good dir first, or fail with a clear message
return Err("working directory was deleted; cd elsewhere".into());
}
}
// shell equivalent: [ -d "$(pwd -P 2>/dev/null)" ] || cd ~ Prevention
- Prefer `gleam new <name>` over `gleam new .` — it never calls env::current_dir().
- In scripts, `cd` into a freshly created directory rather than assuming an inherited cwd.
- After any script that deletes/recreates directories, `cd "$dir"` again to refresh the shell's cwd inode.
When it happens
Trigger: Running `gleam new .` (project_root defaults to '.' at new.rs:435) from a shell whose cwd was rm -rf'd in another terminal, a cwd on an unmounted network drive or dead container mount, or a cwd the user can no longer stat due to permission changes.
Common situations: A rebuild script deleted and recreated the project directory while the shell stayed inside the old inode; tmux/ssh sessions resumed after their directory was removed; CI steps running in a scratch dir cleaned by a parallel job.
Related errors
- `panic` expression evaluated.
- Non Utf-8 Path
- Non-UTF8 path in hardlink_dir
- Writing warning to stderr
- Final result error writing
AI-assisted analysis of gleam-lang/gleam@7e623aa83d (2026-08-17).
Data as JSON: /api/errors/9f4642a909b86d91.
Report an issue: GitHub.