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

🐛 biome.requireConfigFile Ignored When Opening Single File in VSCode #390

Open
1 of 3 tasks
LucasOe opened this issue Oct 9, 2024 · 8 comments
Open
1 of 3 tasks

Comments

@LucasOe
Copy link

LucasOe commented Oct 9, 2024

VS Code version

1.94.1

Extension version

2024.10.40751 (pre-release)

Biome version

1.9.3

Operating system

  • Windows
  • macOS
  • Linux

Description

When biome.requireConfigFile is set to true, it is expected that Biome (linter/formatter) should only be activated if a valid configuration file is present. However, this behavior is inconsistent when opening a single file in VSCode. Biome appears to be enabled automatically, even if no configuration file is present, contrary to the setting. This issue does not occur when opening a directory, where Biome correctly respects the biome.requireConfigFile setting.

I only tested this issue on the pre-release version of the Biome Extension.

Steps to reproduce

  1. Set "biome.requireConfigFile": true in your VSCode settings.
  2. Open a single file in VSCode that does not have a Biome configuration file in the current directory or any parent directories.
  3. Observe that Biome is enabled

Expected behavior

Biome should not be activated when opening a single file if no configuration file is present, respecting the "biome.requireConfigFile": true setting.

Does this issue occur when using the CLI directly?

Not sure / Not applicable

Link to a minimal reproduction

No response

Logs

No response

@nhedger
Copy link
Member

nhedger commented Oct 10, 2024

In single-file mode, there won't be a biome.json in your workspace because there is no workspace by definition. Would you expect the extension to check for a configuration file in the file's parent folder?

@LucasOe
Copy link
Author

LucasOe commented Oct 10, 2024

Would you expect the extension to check for a configuration file in the file's parent folder?

Yes, I would expect Biome to behave the same whether a file is open in a workspace or in single file mode. I think most users who enable biome.requireConfigFile only want to see Biome's lints in a project that uses it, so having the file be linted if opened in single-file mode would probably be confusing.

@ematipico
Copy link
Member

@nhedger i think we can move this issue in the main repo.

Biome LSP already accepts this configuration from the client, so Biome LSP should behave consistently, as long as the client sends this settings when reading the client configuration

@nhedger
Copy link
Member

nhedger commented Dec 1, 2024

@ematipico Actually, the extension checks itself for the presence of the configuration file as well. It won't even start an LSP session if the requireConfigFile is set to true and the config file does not exist. This ensures we're not wasting resources by maintaining a session that will go unused.


@LucasOe I'm not sure this is the behavior we want. Opening a single file that is part of an actual project feels like an edge case. However, wanting to occasionally lint and format individual files could be desirable. Following biome.requireConfigFile=true here would mean that users that open a single file would have to change the global setting every time they want to format a single-file.

Happy to discuss this more

@LucasOe
Copy link
Author

LucasOe commented Dec 1, 2024

@nhedger

I choose to enable "prettier.requireConfig": true specifically to avoid unintentional formatting, which can be a common pitfall when working across multiple projects and file types. As someone who works on various open-source projects -- some of which don’t use Prettier -- it’s important for me to ensure that formatting is applied only when explicitly defined by the project itself. This avoids introducing inconsistencies or unintended changes.

In addition to open-source projects, I frequently edit configuration files that don’t adhere to Prettier or Biome styles. For instance:

  • When I open such files directly (as single files) in VS Code, tools like Biome would automatically format them unless I remember to open them as part of a workspace.
  • Prettier, on the other hand, is consistent in this regard because the formatting is strictly governed by the presence of a configuration file.

Opening a single file that is part of an actual project feels like an edge case.

Could you elaborate on this? From my understanding, this issue applies to any single file, regardless if it's part of a project or not.

Following biome.requireConfigFile=true here would mean that users that open a single file would have to change the global setting every time they want to format a single-file.

Alternatively, users can create a biome.json configuration file in the parent directory. This approach provides the flexibility to define formatting and linting rules per file. The key advantage is that this method ensures consistent formatting across both single-file and workspace contexts, without requiring frequent changes to global settings.

Maybe a potential solution could be adding a "Biome: Force Formatting" command to the command palette. That way, people could force formatting for single files, even if biome.requireConfigFile is set to true.


This brings me to a related question. If a single file is opened, will it respect the biome.json configuration if there is one in a parent directory? Otherwise, the current behavior will result in different formatting, depending on how the file is opened.

@nhedger
Copy link
Member

nhedger commented Dec 1, 2024

Could you elaborate on this? From my understanding, this issue applies to any single file, regardless if it's part of a project or not.

Here single file means that VS Code has been opened with in single-file mode (as opposed to workspace mode). When you open a single file this way, VS Code has no concept of workspace folder.

What I mean by edge case, is that I don't see why I would ever open just that single file, if it's already part of a workspace. Let's take this repository as an example. I would always open the whole repository in VS Code, but I would never open say package.json in single-file mode, because it makes little sense (to me).

However, if I was doing so work on an arbitrary JSON file I just downloaded, and it belongs to no repository, I may open it in single-file mode to format it, for example.

Alternatively, users can create a biome.json configuration file in the parent directory. This approach provides the flexibility to define formatting and linting rules per file. The key advantage is that this method ensures consistent formatting across both single-file and workspace contexts, without requiring frequent changes to global settings.

The idea is to have direct access to Biome's formatting and lining capabilities. Following the example in the previous paragraph, I wouldn't want to create biome.json file every time I want to open a single file in VS Code.

This brings me to a related question. If a single file is opened, will it respect the biome.json configuration if there is one in a parent directory? Otherwise, the current behavior will result in different formatting, depending on how the file is opened.

This is the expected behavior indeed. When you open a file in single-file mode, the Biome extension will create a so-called project whose root folder is the parent folder of that single file, and Biome will use the configuration it finds in the root folder.

@LucasOe
Copy link
Author

LucasOe commented Dec 1, 2024

However, if I was doing so work on an arbitrary JSON file I just downloaded, and it belongs to no repository, I may open it in single-file mode to format it, for example.

If that's the behavior you want, can't you just set biome.requireConfigFile to false? What problem does this setting solve, if not to disable formatting outside of Biome projects? I think if you're using this setting, you want to disable formatting for files outside of Biome projects, regardless of how you open them.

Personally, I wouldn't want any arbitrary JSON file to be formatted on save, that's why I enable the biome.requireConfigFile setting. Users who want this, don't have to use this setting. If they want to opt out of formatting for an entire project, they can also set biome.enabled to false in .vscode/settings.json for that project.

What I mean by edge case, is that I don't see why I would ever open just that single file, if it's already part of a workspace. Let's take this repository as an example. I would always open the whole repository in VS Code, but I would never open say package.json in single-file mode, because it makes little sense (to me).

I still don't fully understand how this is relevant to this problem. If I open the package.json of this project in single-file mode, I always want it to be formatted regardless of context because this project contains a biome.json configuration. That's how the extension currently works, the results of this discussion wouldn't really have an effect on that.

This issue only applies to single files that aren't part of a project that uses Biome. Files in projects using Prettier, or various configuration files, for example.


I think it would be nice to hear what other users think. Maybe a small poll on the Discord could be helpful?

@LucasOe
Copy link
Author

LucasOe commented Dec 1, 2024

Related issue for Prettier, although it unfortunately doesn't have much discussion: prettier/prettier-vscode#693

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants