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

fix: VFS should not confuse paths with source roots that have the same prefix #17019

Merged
merged 1 commit into from Apr 13, 2024

Conversation

Wilfred
Copy link
Contributor

@Wilfred Wilfred commented Apr 5, 2024

Previously, the VFS would assign paths to the source root that had the longest string prefix match. This would break when we had source roots in subdirectories:

/foo
/foo/bar

Given a file /foo/bar_baz.rs, we would attribute it to the /foo/bar source root, which is wrong.

As a result, we would attribute paths to the wrong crate when a crate was in a subdirectory of another one. This is more common in larger monorepos, but could occur in any Rust project.

Fix this in the VFS, and add a test.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 5, 2024
@Wilfred Wilfred marked this pull request as ready for review April 5, 2024 22:57
crates/vfs/src/file_set.rs Outdated Show resolved Hide resolved
crates/vfs/src/file_set.rs Outdated Show resolved Hide resolved
crates/vfs/src/file_set.rs Outdated Show resolved Hide resolved
@Veykril
Copy link
Member

Veykril commented Apr 6, 2024

Thanks!
@bors r+

@bors
Copy link
Collaborator

bors commented Apr 6, 2024

📌 Commit 8885d30 has been approved by Veykril

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Apr 6, 2024

⌛ Testing commit 8885d30 with merge 4d46cc5...

bors added a commit that referenced this pull request Apr 6, 2024
fix: VFS should not confuse paths with source roots that have the same prefix

Previously, the VFS would assign paths to the source root that had the longest string prefix match. This would break when we had source roots in subdirectories:

```
/foo
/foo/bar
```

Given a file `/foo/bar_baz.rs`, we would attribute it to `/foo/bar` source root, which is wrong.

As a result, we would attribute paths to the wrong crate when a crate was in a subdirectory of another one. This is more common in larger monorepos, but could occur in any Rust project.

Fix this in the VFS, and add a test.
@bors
Copy link
Collaborator

bors commented Apr 6, 2024

💔 Test failed - checks-actions

@Veykril
Copy link
Member

Veykril commented Apr 6, 2024

🤔 why is this failing on merge only, judging from the commit parents this is a recent branch so there shouldn't be any semantic conflicts I think?

@ShoyuVanilla
Copy link
Contributor

🤔 why is this failing on merge only, judging from the commit parents this is a recent branch so there shouldn't be any semantic conflicts I think?

According to CI log, the following tests fail on Windows

 failures:
    tests::ancestor_can_be_parent
    tests::ancestor_can_be_parent_2
    tests::basic_child_parent
    tests::basic_child_parent_with_unrelated_parents_sib
    tests::child_binds_ancestor_if_parent_nonlocal
    tests::non_locals_are_skipped

and it seems that CI runs Windows tests on merges, but not on PRs, so it fails on merge only.

- name: Test
if: matrix.os == 'ubuntu-latest' || github.event_name == 'push'
run: cargo test ${{ env.USE_SYSROOT_ABI }} -- --nocapture --quiet

@Wilfred
Copy link
Contributor Author

Wilfred commented Apr 8, 2024

Yeah, I wondered whether I should be unconditionally appending / or use the platform-specific path separator.

There doesn't seem to be a .parent() method on VfsPath, it's apparently an opaque type.

@ShoyuVanilla huh, that seems unfortunate. Maybe PRs should also run Windows tests?

@Wilfred
Copy link
Contributor Author

Wilfred commented Apr 8, 2024

Aha, there is a .parent() method, so I've updated the PR to use that instead. Let me know what you think.

Wilfred added a commit to Wilfred/rust-analyzer that referenced this pull request Apr 8, 2024
Previously PRs would only do a build on Windows, which confusingly
meant that PRs got a green tick for Windows despite not testing them.

See discussion in rust-lang#17019.
@ShoyuVanilla
Copy link
Contributor

huh, that seems unfortunate. Maybe PRs should also run Windows tests?

Though I don't know about the r-a CI policy but in my naive opinion, running tests for each major platforms would be good for cases like filesystem or environment variables level changes like in this issue and running it wouldn't slow down CI much because each platform CI is done in different instances.

@Wilfred
Copy link
Contributor Author

Wilfred commented Apr 11, 2024

@Veykril I think this PR is good to merge now, it's using .parent() which should do the right thing on Windows too.

bors added a commit that referenced this pull request Apr 13, 2024
Run Windows tests on PRs too

Previously PRs would only do a build on Windows, which confusingly meant that PRs got a green tick for Windows despite not testing them.

See discussion in #17019.
@Veykril
Copy link
Member

Veykril commented Apr 13, 2024

Thanks!
@bors r+

@bors
Copy link
Collaborator

bors commented Apr 13, 2024

📌 Commit b03844d has been approved by Veykril

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Apr 13, 2024

⌛ Testing commit b03844d with merge 773b4a5...

@bors
Copy link
Collaborator

bors commented Apr 13, 2024

☀️ Test successful - checks-actions
Approved by: Veykril
Pushing 773b4a5 to master...

@bors bors merged commit 773b4a5 into rust-lang:master Apr 13, 2024
11 checks passed
@bill-myers
Copy link

bill-myers commented Apr 15, 2024

Is this enough? Seems to me this will still result in /foo/bar_baz/quux.rs being attributed to /foo/bar rather than /foo.

The correct solution is of course to add a final slash to prefixes, and add a final slash to the string to search as well if matching the prefix itself is needed.

And of course normalize separators and UNC long paths beforehand if running on Windows.

@Wilfred Wilfred deleted the source_root_prefixes branch April 19, 2024 23:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants