Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

fix(rome_lsp): improve the pattern matching logic for ignored files #4024

Merged
merged 5 commits into from
Dec 13, 2022

Conversation

leops
Copy link
Contributor

@leops leops commented Dec 9, 2022

Summary

Fixes #3790

This PR fixes how ignore patterns are parsed and changes how paths are represented in the language server to reduce discrepancies with the CLI. For ignore patterns, the parse will now strip the ./ prefix from the input string, and prepend the pattern with a ** group if the input string doesn't start with an absolute filesystem path. For the Language Server, the logic converting URIs into RomePath will now try to strip the path to the root of the workspace from the path of the file, which has the effect of making most paths relative much like they generally are on the CLI.

Test Plan

I've update the tests for the file ignores on the CLI to check that various forms of ignore patterns are all working correctly. I also modified the LSP tests to ensure it only constructs URIs that can be turned into valid paths for the underlying OS.

@leops leops requested a review from ematipico as a code owner December 9, 2022 16:46
@netlify
Copy link

netlify bot commented Dec 9, 2022

Deploy Preview for docs-rometools canceled.

Name Link
🔨 Latest commit c95ea00
🔍 Latest deploy log https://app.netlify.com/sites/docs-rometools/deploys/6398576966b73100082a594e

@leops leops added A-LSP Area: language server protocol A-Project Area: project configuration and loading labels Dec 9, 2022

// If a pattern is not absolute, insert a "**" sequence in front
if !is_absolute {
tokens.push(AnyRecursiveSequence);
Copy link
Contributor

Choose a reason for hiding this comment

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

What about patterns that already start with a *:

  • *.js
  • **/*.js

Are these handled correctly (is ***.js a valid pattern?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These patterns are handled correctly since the new logic only modifies the internal representation of the pattern and not its string representation, so while I don't think ***.js is a valid pattern this works because it only prepending an "any sequence" in front of the pattern (for the case of *.js this fix also applie to this pattern and makes it work as expected, without it the pattern would match test.js but not src/test.js for instance which is probably not what the user wants)

RomePath::new(url.path(), file_id)
let mut path_to_file = url
.to_file_path()
.map_err(|()| anyhow!("failed to convert {url} to a filesystem path"))?;
Copy link
Contributor

Choose a reason for hiding this comment

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

What does this macro do?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The anyhow! macro works like format! except it returns an instance of anyhow::Error instead of a String

use tower_lsp::lsp_types::VersionedTextDocumentIdentifier;
use tower_lsp::lsp_types::WorkDoneProgressParams;
use tower_lsp::LspService;
use tower_lsp::{jsonrpc::Request, lsp_types::InitializeParams};

macro_rules! url {
Copy link
Contributor

@ematipico ematipico Dec 12, 2022

Choose a reason for hiding this comment

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

new_url? Also, could we document the macro, please? I feel there's some important information about that "workspace" and windows. I can't understand what this macro is doing...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I preferred to call the macro url instead of new_url as it is used many times in the tests, and I wanted it to have a short unobtrusive name (like vec! from the standard library, or T! from out syntax crates)

crates/rome_service/src/matcher/pattern.rs Outdated Show resolved Hide resolved
crates/rome_service/src/matcher/pattern.rs Show resolved Hide resolved
@@ -122,6 +122,30 @@ impl Pattern {
let mut is_recursive = false;
let mut i = 0;

// A pattern is relative if it starts with "." followed by a separator,
// eg. "./test" or ".\test"
let is_relative = matches!(chars.get(..2), Some(['.', sep]) if path::is_separator(*sep));
Copy link
Contributor

Choose a reason for hiding this comment

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

TIL: Yu can write if conditions with matches


#[test]
fn test_pattern_absolute() {
assert!(Pattern::new("/a/b")
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add tests for the *.js and **.js patterns

@leops leops merged commit e6c6a4b into main Dec 13, 2022
@leops leops deleted the fix/ignore-pattern branch December 13, 2022 10:45
lgarron added a commit to cubing/cubing.js that referenced this pull request Dec 20, 2022
This picks up a fix for ignoring files: rome/tools#4024
lgarron added a commit to cubing/cubing.js that referenced this pull request Dec 21, 2022
This picks up a fix for ignoring files: rome/tools#4024
lgarron added a commit to cubing/cubing.js that referenced this pull request Dec 21, 2022
This picks up a fix for ignoring files: rome/tools#4024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-LSP Area: language server protocol A-Project Area: project configuration and loading
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 LSP ignores ignores
3 participants