-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor how diagnostics are pushed through
incremental
; add `queri…
…es.AST` (#404) The `NonFatal` functionality of the incremental package has proven to be a poor abstraction. What I essentially wanted was a way to have queries generate diagnostics, which I could then deduplicate and collect in the end. Collecting diagnostics from dependencies is incorrect, because A -> B, A -> C, B -> D, C -> D would mean diagnostics for D contain the diagnostics from A twice. The rough idea was to instead stash reports in the `NonFatal` area, which required the somewhat unnatural-feeling `report.AsError` type. But this is unnecessary ceremony: all that `NonFatal` will ever be used for is for stashing reports, so the incremental framework should Just Do That. It's not a general library, after all. This PR replaces `Task.NonFatal` with `Task.Report`, which is a report included with each task. When `Run` completes, it collects the set of all queries that were computed (possibly from cache) and merges their reports, and sorts it to eliminate non-determinism. It is not immediately clear to me if having tasks return `(v T, fatal error)` is still useful. Perhaps `(v T, ok bool)` may be more appropriate, since that error should be logged as a diagnostic and will probably just get thrown away.
- Loading branch information
Showing
11 changed files
with
134 additions
and
116 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,60 @@ | ||
// Copyright 2020-2025 Buf Technologies, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package queries | ||
|
||
import ( | ||
"github.com/bufbuild/protocompile/experimental/ast" | ||
"github.com/bufbuild/protocompile/experimental/incremental" | ||
"github.com/bufbuild/protocompile/experimental/parser" | ||
"github.com/bufbuild/protocompile/experimental/source" | ||
) | ||
|
||
// AST is an [incremental.Query] for the contents of a file as provided | ||
// by a [source.Opener]. | ||
// | ||
// AST queries with different Openers are considered distinct. | ||
type AST struct { | ||
source.Opener // Must be comparable. | ||
Path string | ||
} | ||
|
||
var _ incremental.Query[ast.File] = AST{} | ||
|
||
// Key implements [incremental.Query]. | ||
// | ||
// The key for a Contents query is the query itself. This means that a single | ||
// [incremental.Executor] can host Contents queries for multiple Openers. It | ||
// also means that the Openers must all be comparable. As the [Opener] | ||
// documentation states, implementations should take a pointer receiver so that | ||
// comparison uses object identity. | ||
func (a AST) Key() any { | ||
return a | ||
} | ||
|
||
// Execute implements [incremental.Query]. | ||
func (a AST) Execute(t incremental.Task) (ast.File, error) { | ||
t.Report().Options.Stage += stageAST | ||
|
||
r, err := incremental.Resolve(t, File(a)) | ||
if err != nil { | ||
return ast.File{}, err | ||
} | ||
if r[0].Fatal != nil { | ||
return ast.File{}, r[0].Fatal | ||
} | ||
|
||
file, _ := parser.Parse(r[0].Value, t.Report()) | ||
return file, nil | ||
} |
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.