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

ast/parser: add hint to future-proof imports #6968

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion ast/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,12 @@ func (p *Parser) parseImport() *Import {

path := imp.Path.Value.(Ref)

if !RootDocumentNames.Contains(path[0]) && !FutureRootDocument.Equal(path[0]) && !RegoRootDocument.Equal(path[0]) {
switch {
case RootDocumentNames.Contains(path[0]):
case FutureRootDocument.Equal(path[0]):
case RegoRootDocument.Equal(path[0]):
default:
p.hint("if this is unexpected, try updating OPA")
p.errorf(imp.Path.Location, "unexpected import path, must begin with one of: %v, got: %v",
RootDocumentNames.Union(NewSet(FutureRootDocument, RegoRootDocument)),
path[0])
Expand Down
5 changes: 5 additions & 0 deletions ast/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,11 @@ func TestFutureAndRegoV1ImportsExtraction(t *testing.T) {
}
}

func TestHintsOnUnknownImport(t *testing.T) {
assertParseErrorContains(t, "unknown", "import unknown",
"unexpected import path, must begin with one of: {data, future, input, rego}, got: unknown (hint: if this is unexpected, try updating OPA)")
}

func TestRegoV1Import(t *testing.T) {
assertParseErrorContains(t, "rego", "import rego", "invalid import `rego`, must be `rego.v1`")
assertParseErrorContains(t, "rego.foo", "import rego.foo", "invalid import `rego.foo`, must be `rego.v1`")
Expand Down