Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn against certain values in notebook.codeActionsOnSave #686

Merged
merged 2 commits into from
Feb 18, 2025

Conversation

dhruvmanila
Copy link
Member

@dhruvmanila dhruvmanila commented Feb 10, 2025

Summary

This PR adds a check for notebook.codeActionsOnSave to do the following:

  • For source.fixAll and source.organizeImports, log at info level
  • For source.fixAll.ruff and source.organizeImports.ruff, log at warn level and provide a warning message to use notebook. prefixed code actions instead

Test Plan

Using the following settings:

{
  "notebook.codeActionsOnSave": {
    "source.fixAll": "explicit",
    "source.organizeImports": "explicit",
    "source.organizeImports.ruff": "explicit",
    "source.fixAll.ruff": "explicit"
  }
}

Logs:

2025-02-17 20:06:34.796 [info] The following code actions in 'notebook.codeActionsOnSave' could lead to unexpected behavior: ["source.fixAll","source.organizeImports"]. Consider using ["notebook.source.fixAll","notebook.source.organizeImports"] instead. For more information, refer to [this FAQ section](https://docs.astral.sh/ruff/faq/#source-code-actions-in-notebooks).
2025-02-17 20:06:34.796 [warning] The following code actions in 'notebook.codeActionsOnSave' will lead to unexpected behavior: ["source.organizeImports.ruff","source.fixAll.ruff"]. Please use ["notebook.source.organizeImports.ruff","notebook.source.fixAll.ruff"] instead. For more information, refer to [this FAQ section](https://docs.astral.sh/ruff/faq/#source-code-actions-in-notebooks).

Notification preview:

Screenshot 2025-02-17 at 8 06 54 PM

@dhruvmanila dhruvmanila force-pushed the dhruv/warn-notebook-ca branch from fdbbfc0 to 8119f38 Compare February 14, 2025 06:45
@dhruvmanila dhruvmanila marked this pull request as ready for review February 14, 2025 06:48
Comment on lines 361 to 370
if (genericCodeActions.length > 0) {
// This is at info level because other extensions might be using these code actions but we still want to inform the user.
logger.info(
`The following code actions in 'notebook.codeActionsOnSave' could lead to unexpected behavior: ${JSON.stringify(
genericCodeActions,
)}. Consider using ${JSON.stringify(
genericCodeActions.map((action) => `notebook.${action}`),
)} instead.`,
);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm a bit unsure if we even want this but a lot of users seems to be using the ones without .ruff suffix so at least the logs could help them and us.

I'm also thinking of updating the README to group config to avoid a long list of code snippets which are mostly similar and it's hard to know which config should a user pickup from them.

Comment on lines 363 to 369
logger.info(
`The following code actions in 'notebook.codeActionsOnSave' could lead to unexpected behavior: ${JSON.stringify(
genericCodeActions,
)}. Consider using ${JSON.stringify(
genericCodeActions.map((action) => `notebook.${action}`),
)} instead.`,
);
Copy link
Member

@MichaReiser MichaReiser Feb 14, 2025

Choose a reason for hiding this comment

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

It seems you also added a similar check on the LSP side. Do we only include the warning here because this is a VS code specific fix or is this something we could handle on the LSP side?

Should we include a link to some documentation explaining why? For example, we could add an FAQ entry or a section on the website explaining why using notebook-specific actions is preferred. It might also be could to link to the issue, but that's something we could do in the FAQ section where we have a bit more space than here.

Copy link
Member Author

@dhruvmanila dhruvmanila Feb 17, 2025

Choose a reason for hiding this comment

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

It seems you also added a similar check on the LSP side. Do we only include the warning here because this is a VS code specific fix or is this something we could handle on the LSP side?

Yes, this is VS Code specific fix as we're looking into notebook.codeActionsOnSave which specific to VS Code and can provide suggestion to change it.

Using editor specific fix allows us to provide a notification popup which when implemented on the server side would result in multiple popups because the client sends the code action request whenever cursor position is changed.

Should we include a link to some documentation explaining why? For example, we could add an FAQ entry or a section on the website explaining why using notebook-specific actions is preferred. It might also be could to link to the issue, but that's something we could do in the FAQ section where we have a bit more space than here.

Thanks, that's a great suggestion. I'll add that first.

@dhruvmanila dhruvmanila merged commit 1897fb9 into main Feb 18, 2025
6 checks passed
@dhruvmanila dhruvmanila deleted the dhruv/warn-notebook-ca branch February 18, 2025 04:57
dhruvmanila added a commit to astral-sh/ruff that referenced this pull request Feb 18, 2025
## Summary

Related to astral-sh/ruff-vscode#686, this PR
ignores handling source code actions for notebooks which are not
prefixed with `notebook`.

The main motivation is that the native server does not actually handle
it well which results in gibberish code. There's some context about this
in
astral-sh/ruff-vscode#680 (comment)
and the following comments.

closes: astral-sh/ruff-vscode#680

## Test Plan

Running a notebook with the following does nothing except log the
message:
```json
  "notebook.codeActionsOnSave": {
    "source.organizeImports.ruff": "explicit",
  },
```

while, including the `notebook` code actions does make the edit (as
usual):
```json
  "notebook.codeActionsOnSave": {
    "notebook.source.organizeImports.ruff": "explicit"
  },
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants