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

IOOBE in project loading prevents any analysis #151

Closed
kubukoz opened this issue May 16, 2024 · 3 comments
Closed

IOOBE in project loading prevents any analysis #151

kubukoz opened this issue May 16, 2024 · 3 comments

Comments

@kubukoz
Copy link
Contributor

kubukoz commented May 16, 2024

To reproduce:

  1. Prepare a file
$version: "2"

namespace hello.world

@documentation("aa")
@mixin
structure Mix {
    @required
    a: String
    @required
    b: String
    c: String
}

structure Struct with [Mix] {}
  1. Open a project that has this file in sources
  2. See an IndexOutOfBoundsException in FileCachingCollector, resulting in any analysis/navigation afterwards throwing a NPE (project is null)
@kubukoz
Copy link
Contributor Author

kubukoz commented May 16, 2024

Similar to #110. Here's the stack trace:

java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 15
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
	at java.base/java.util.Objects.checkIndex(Objects.java:361)
	at java.base/java.util.ArrayList.get(ArrayList.java:427)
	at software.amazon.smithy.lsp.ext.FileCachingCollector.collectMemberLocations(FileCachingCollector.java:186)
	at java.base/java.util.HashMap.forEach(HashMap.java:1421)
	at software.amazon.smithy.lsp.ext.FileCachingCollector.collectDefinitionLocations(FileCachingCollector.java:75)
	at software.amazon.smithy.lsp.ext.SmithyProject.collectLocations(SmithyProject.java:205)
	at software.amazon.smithy.lsp.ext.SmithyProject.lambda$new$0(SmithyProject.java:73)
	at java.base/java.util.Optional.ifPresent(Optional.java:178)
	at software.amazon.smithy.lsp.ext.SmithyProject.<init>(SmithyProject.java:73)
	at software.amazon.smithy.lsp.ext.SmithyProject.load(SmithyProject.java:184)
	at software.amazon.smithy.lsp.ext.SmithyProject.load(SmithyProject.java:149)
	at software.amazon.smithy.lsp.SmithyTextDocumentService.createProject(SmithyTextDocumentService.java:149)
	at software.amazon.smithy.lsp.SmithyTextDocumentService.createProject(SmithyTextDocumentService.java:210)
	at software.amazon.smithy.lsp.SmithyLanguageServer.lambda$loadSmithyBuild$0(SmithyLanguageServer.java:62)
	at java.base/java.util.Optional.ifPresent(Optional.java:178)
	at software.amazon.smithy.lsp.SmithyLanguageServer.loadSmithyBuild(SmithyLanguageServer.java:62)
	at software.amazon.smithy.lsp.SmithyLanguageServer.initialize(SmithyLanguageServer.java:108)

However, the IOOBE is just a symptom: if we print the already-collected locations, we'll see that they're already wrong:

diff --git a/src/main/java/software/amazon/smithy/lsp/ext/FileCachingCollector.java b/src/main/java/software/amazon/smithy/lsp/ext/FileCachingCollector.java
index 334bcd2..a4f91af 100644
--- a/src/main/java/software/amazon/smithy/lsp/ext/FileCachingCollector.java
+++ b/src/main/java/software/amazon/smithy/lsp/ext/FileCachingCollector.java
@@ -202,6 +202,7 @@ final class FileCachingCollector implements ShapeLocationCollector {
                     memberEndMarker = traits.get(0).getSourceLocation().getLine();
                 }
 
+                LspLog.println("Indexing " + memberShape.getId() + " as " + createLocation(modelFile.filename, startPosition, endPosition));
                 locations.put(memberShape.getId(), createLocation(modelFile.filename, startPosition, endPosition));
                 previousLine = currentLine;
             }
[00:52:23] Indexing hello.world#Mix$c as Location [
  uri = "file:/Users/kubukoz/projects/demos/src/main/smithy/hello.smithy"
  range = Range [
    start = Position [
      line = 11
      character = 4
    ]
    end = Position [
      line = 2
      character = 21
    ]
  ]
]
[00:52:23] Indexing hello.world#Mix$b as Location [
  uri = "file:/Users/kubukoz/projects/demos/src/main/smithy/hello.smithy"
  range = Range [
    start = Position [
      line = 10
      character = 4
    ]
    end = Position [
      line = 0
      character = 13
    ]
  ]
]

So we're crashing during the analysis of a, but b and c already have their positions broken, as end is before start.

BTW. this is fixed in #146, but it's still in progress and this is affecting my teams today.

@kubukoz
Copy link
Contributor Author

kubukoz commented May 16, 2024

Problems are starting even earlier: the container (the mixin)'s range seems like it's one line

image

so from the very first member we see (c, they're being traversed from the end), we're already looking outside the container's braces.

@milesziemer
Copy link
Contributor

Closing as this is fixed by #146

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

No branches or pull requests

2 participants