-
Notifications
You must be signed in to change notification settings - Fork 14
Fix project root computation #1041
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
Open
toinehartman
wants to merge
18
commits into
main
Choose a base branch
from
fix/968-project-roots
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
0aba9b1
Add tests for various project root and path config scenarios.
toinehartman 79d2a2a
Fix path config computation for various edge cases.
toinehartman 55594e5
Re-use project root computation in Rascal.
toinehartman f3c985c
Rename roots class and functions for less verbosity.
toinehartman 4195c18
Simplify while -> if
toinehartman 4505478
Rewrite ternary -> if.
toinehartman cbb77d7
Docs, performance and rewrites (h/t @DavyLandman).
toinehartman 9d2f250
Use absolute project dir in tests.
toinehartman b94cb1c
Always compare paths with separator at the end.
toinehartman 840ad2c
Fix nested project root logic.
toinehartman 49b9df7
Improve and clean up tests.
toinehartman abc744a
Consider Java project roots as well.
toinehartman 436ca05
Check project and module existence in tests by default.
toinehartman b17ffb0
Merge remote-tracking branch 'origin/main' into fix/968-project-roots
toinehartman 254a7b3
Only consider Java project roots.
toinehartman beccba9
Merge remote-tracking branch 'origin/main' into fix/968-project-roots
toinehartman 520ddd0
Do no compute roots for non-source projects.
toinehartman 4a357dd
Split tests over classes.
toinehartman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/rascal/model/Projects.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * Copyright (c) 2018-2025, NWO-I CWI and Swat.engineering | ||
| * All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are met: | ||
| * | ||
| * 1. Redistributions of source code must retain the above copyright notice, | ||
| * this list of conditions and the following disclaimer. | ||
| * | ||
| * 2. Redistributions in binary form must reproduce the above copyright notice, | ||
| * this list of conditions and the following disclaimer in the documentation | ||
| * and/or other materials provided with the distribution. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| * POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package org.rascalmpl.vscode.lsp.rascal.model; | ||
|
|
||
| import org.rascalmpl.interpreter.utils.RascalManifest; | ||
| import org.rascalmpl.uri.URIUtil; | ||
|
|
||
| import io.usethesource.vallang.ISourceLocation; | ||
|
|
||
| public class Projects { | ||
|
|
||
| /** | ||
| * Infers the shallowest possible root of the project that `origin` is in. | ||
| */ | ||
| public ISourceLocation inferRoot(ISourceLocation origin) { | ||
| var innerRoot = inferDeepestRoot(origin); | ||
| var outerRoot = inferDeepestRoot(URIUtil.getParentLocation(innerRoot)); | ||
|
|
||
| if (!innerRoot.equals(outerRoot) && isSameProject(innerRoot, outerRoot)) { | ||
|
toinehartman marked this conversation as resolved.
|
||
| innerRoot = outerRoot; | ||
| outerRoot = inferDeepestRoot(URIUtil.getParentLocation(innerRoot)); | ||
| } | ||
|
|
||
| if (isSameProject(innerRoot, outerRoot)) { | ||
| // Inner root is for the same project, but might be inside the target folder | ||
| return outerRoot; | ||
| } | ||
|
|
||
| // Inner root is for a nested project | ||
| return innerRoot; | ||
| } | ||
|
|
||
| private boolean isSameProject(ISourceLocation root1, ISourceLocation root2) { | ||
| var mf = new RascalManifest(); | ||
| return mf.hasManifest(root1) && mf.getProjectName(root1).equals(mf.getProjectName(root2)); | ||
| } | ||
|
|
||
| /** | ||
| * Infers the longest project root-like path that `member` is in. Might return a sub-directory of `target/`. | ||
| */ | ||
| private ISourceLocation inferDeepestRoot(ISourceLocation origin) { | ||
| var manifest = new RascalManifest(); | ||
| var root = origin; | ||
|
toinehartman marked this conversation as resolved.
|
||
|
|
||
| while (!(manifest.hasManifest(root) || URIUtil.getParentLocation(root).equals(root))) { | ||
| root = URIUtil.getParentLocation(root); | ||
| } | ||
|
toinehartman marked this conversation as resolved.
Outdated
|
||
| return root; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
rascal-lsp/src/test/java/org/rascalmpl/vscode/lsp/rascal/model/PathConfigsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /* | ||
| * Copyright (c) 2018-2025, NWO-I CWI and Swat.engineering | ||
| * All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are met: | ||
| * | ||
| * 1. Redistributions of source code must retain the above copyright notice, | ||
| * this list of conditions and the following disclaimer. | ||
| * | ||
| * 2. Redistributions in binary form must reproduce the above copyright notice, | ||
| * this list of conditions and the following disclaimer in the documentation | ||
| * and/or other materials provided with the distribution. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| * POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package org.rascalmpl.vscode.lsp.rascal.model; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.URISyntaxException; | ||
| import java.util.concurrent.Executors; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.mockito.Mock; | ||
| import org.rascalmpl.uri.URIResolverRegistry; | ||
| import org.rascalmpl.values.IRascalValueFactory; | ||
|
|
||
| import io.usethesource.vallang.ISourceLocation; | ||
|
|
||
| public class PathConfigsTest { | ||
| private static final IRascalValueFactory VF = IRascalValueFactory.getInstance(); | ||
| private static final URIResolverRegistry reg = URIResolverRegistry.getInstance(); | ||
|
|
||
| private final Projects projects = new Projects(); | ||
|
|
||
| private void checkRoot(ISourceLocation project, String modulePath) throws URISyntaxException { | ||
| var m = VF.sourceLocation(project.getScheme(), project.getAuthority(), project.getPath() + "/" + modulePath); | ||
|
toinehartman marked this conversation as resolved.
Outdated
|
||
| var root = projects.inferRoot(m); | ||
| assertEquals(project, root); | ||
| } | ||
|
|
||
| @Mock PathConfigDiagnostics diagnostics; | ||
|
DavyLandman marked this conversation as resolved.
|
||
| private PathConfigs configs; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| configs = new PathConfigs(Executors.newCachedThreadPool(), diagnostics); | ||
| } | ||
|
|
||
| @Test | ||
| public void standardRoot() throws URISyntaxException { | ||
| checkRoot(VF.sourceLocation("std", "", ""), "IO.rsc"); | ||
| } | ||
|
|
||
| @Test | ||
| public void nestedStandardRoot() throws URISyntaxException { | ||
| checkRoot(VF.sourceLocation("std", "", ""), "util/Maybe.rsc"); | ||
| } | ||
|
|
||
| @Test | ||
| public void lspRoot() throws URISyntaxException { | ||
| checkRoot(VF.sourceLocation("cwd", "", ""), "src/main/rascal/library/util/LanguageServer.src"); | ||
| } | ||
|
|
||
| @Test | ||
| public void lspTargetRoot() throws URISyntaxException { | ||
| checkRoot(VF.sourceLocation("cwd", "", ""), "target/classes/library/util/LanguageServer.rsc"); | ||
| } | ||
|
|
||
| @Test | ||
| public void nestedProjectRoot() throws URISyntaxException { | ||
| checkRoot(VF.sourceLocation("cwd", "", "src/test/resources/project-a"), "src/Module.rsc"); | ||
| } | ||
|
|
||
| @Test | ||
| public void pathConfigForStandardModule() throws URISyntaxException, IOException { | ||
| var pcfg = configs.lookupConfig(VF.sourceLocation("std", "", "IO.rsc")); | ||
| assertEquals(reg.logicalToPhysical(VF.sourceLocation("std", "", "")), pcfg.getProjectRoot()); | ||
| } | ||
|
|
||
| @Test | ||
| public void pathConfigForLsp() throws URISyntaxException, IOException { | ||
| var pcfg = configs.lookupConfig(VF.sourceLocation("project", "rascal-lsp", "")); | ||
| assertEquals(reg.logicalToPhysical(VF.sourceLocation("project", "rascal-lsp", "")), pcfg.getProjectRoot()); | ||
| } | ||
|
|
||
| @Test | ||
| public void pathConfigForLspModule() throws URISyntaxException, IOException { | ||
| var pcfg = configs.lookupConfig(VF.sourceLocation("project", "rascal-lsp", "src/main/rascal/library/util/LanguageServer.src")); | ||
| assertEquals(reg.logicalToPhysical(VF.sourceLocation("project", "rascal-lsp", "")), pcfg.getProjectRoot()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Manifest-Version: 0.0.1 | ||
| Project-Name: project-a | ||
| Source: src/ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.