Skip to content

[ty] Add untrustedWorkspace option to the LSP - #27828

Merged
MichaReiser merged 1 commit into
mainfrom
micha/ty-untrusted-workspace
Aug 19, 2026
Merged

MichaReiser merged 1 commit into
mainfrom
micha/ty-untrusted-workspace

Conversation

@MichaReiser

@MichaReiser MichaReiser commented Aug 18, 2026 •

Copy link
Copy Markdown
Member

Summary

This is a generic stop-gap solution for astral-sh/ty#4275.

This PR adds a new untrustedWorkspace initialization option and changes the LSPSystem to always return an IOError when trying to run a command. This seems like a safe default and is all we need for now.

If we have a need in the future to run some commands in untrusted workspace, we can then extend run_command and add an option to bypass the early return.

I plan to add dedicated handling of untrusted workspaces for astral-sh/ty#4275 in the scripts LSP, to always disables uv support in an untrusted workspace. Because there's no point in showing an error that running uv isn't allowed in untrusted workspace for every script. But I still think this is worth landing independently as a general stop-gap solution for other cases where we might forget to add a dedicated check.

This PR takes extra care that we always respect untrustedWorkspace, even if other initializationOptions are invalid. I think it would be nice to have a more general system where ty tries to retain as many valid options as possible instead of falling back to the default when any option is invalid, but that felt like a separate change.

Test Plan

Added tests

@MichaReiser MichaReiser added server Related to the Ruff or ty LSP server ty The ty type checker labels Aug 18, 2026
@MichaReiser MichaReiser changed the title [ty] Respect untrusted workspaces in the language server [ty] Add untrustedWorkspace option to the LSP Aug 18, 2026
@MichaReiser
MichaReiser marked this pull request as ready for review August 18, 2026 11:26
@MichaReiser
MichaReiser requested a review from a team as a code owner August 18, 2026 11:26
@astral-sh-bot
astral-sh-bot Bot requested a review from charliermarsh August 18, 2026 11:26
}
}

#[cfg(test)]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can look into making them e2e tests on top of the scroll support. The issue is that we currently have no behavior that changes based on the untrusted setting. The only way of asserting that the workspace is untrusted is by reading the value from the setting.

The other issue might be that our server setup doesn't allow us to pass invalid settings. But there might be a way around this

Comment on lines 58 to +69
let (initialization_options, deserialization_error) =
InitializationOptions::from_value(initialization_options);
match InitializationOptions::from_value(initialization_options) {
Ok(options) => options,
Err(error) => {
connection.sender.send(Message::Response(Response::new_err(
id,
ErrorCode::InvalidParams as i32,
format!("Invalid initialization options: {error:#}"),
)))?;
return Err(error).context("Failed to deserialize initialization options");
}
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like we're hard-stopping the server initialization if the untrustedWorkspace deserialization fails, is that intentional? Should we still start the server and limit the capabilities instead?

We should also add a test case where the untrustedWorkspace value is invalid to demonstrate the deserialization failure case.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could fall back to untrusted. But I'm not sure it gives you much. I think this is really a case where you should fix your settings.

There's already a test, it's malformed_trust_rejects_initialization

@dhruvmanila dhruvmanila Aug 19, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, is this actually intentional to not start the server for untrusted workspace?

Sounds good!

@MichaReiser
MichaReiser merged commit 309c334 into main Aug 19, 2026
57 checks passed
@MichaReiser
MichaReiser deleted the micha/ty-untrusted-workspace branch August 19, 2026 08:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

server Related to the Ruff or ty LSP server ty The ty type checker

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants