astral-sh/ruff · error

Failed to get the current working directory while creating a

Error message

Failed to get the current working directory while creating a default workspace.

What it means

During the initialize handshake the client provided no workspace folders, so ty falls back to the server process's current working directory as the default workspace. If that directory cannot be determined (fallback system current_dir() is None), initialization fails with this error (server.rs:139).

Source

Thrown at crates/ty_server/src/server.rs:139

                    .map(|folder| folder.uri)
                    .collect::<Vec<_>>()
            })
            .or_else(|| {
                let current_dir = native_system
                    .current_directory()
                    .as_std_path()
                    .to_path_buf();
                tracing::warn!(
                    "No workspace(s) were provided during initialization. \
                    Using the current working directory from the fallback system as a \
                    default workspace: {}",
                    current_dir.display()
                );
                let uri = Uri::from_file_path(current_dir).ok()?;
                Some(vec![uri])
            })
            .ok_or_else(|| {
                anyhow::anyhow!(
                    "Failed to get the current working directory while creating a \
                    default workspace."
                )
            })?;

        Ok(Self {
            connection,
            worker_threads,
            main_loop_receiver,
            main_loop_sender,
            session: Session::new(
                resolved_client_capabilities,
                position_encoding,
                workspace_urls,
                initialization_options,
                native_system,
                ClientName::from(client_info),
                in_test,

View on GitHub (pinned to 672bb4edf0)

Solutions

  1. Send workspaceFolders (or rootUri) in the initialize params so no cwd fallback is needed
  2. Ensure the process the server is spawned in has an existing, readable cwd
  3. Restart the editor from the project directory so it inherits a valid cwd

Example fix

// before
const params = { capabilities: clientCaps };  // no workspaceFolders

// after
const params = {
  capabilities: clientCaps,
  workspaceFolders: [{ uri: rootUri, name: 'my-project' }],
};
Defensive patterns

Strategy: validation

Validate before calling

// TS: always provide at least one file workspace folder
const workspaceFolders = vscode.workspace.workspaceFolders?.length
  ? vscode.workspace.workspaceFolders.map(f => ({ uri: f.uri.toString(), name: f.name }))
  : [{ uri: pathToFileUri(path.resolve('.')), name: 'root' }];
await sendRequest('initialize', { capabilities, workspaceFolders });

Type guard

const hasFileWorkspace = (folders?: { uri: string }[]): boolean =>
  !!folders?.some(f => f.uri.startsWith('file:'));

Prevention

When it happens

Trigger: initialize request with empty/absent workspaceFolders AND no rootUri, while the server process's cwd was deleted, unlinked, or is otherwise unreadable.

Common situations: Client spawns ty in a temp directory that is later cleaned up, a wrapper script cd'ing into a removed dir, minimal LSP harnesses that omit workspaceFolders, or a race where the editor deletes the project dir before the server starts.

Related errors


AI-assisted analysis of astral-sh/ruff@672bb4edf0 (2026-08-16). Data as JSON: /api/errors/c0b02190c2fbafdd. Report an issue: GitHub.