Skip to content

[BUG] Painless Script Single Line Comment at EOF Causes Parsing Error #11815

Closed
@camerondurham

Description

@camerondurham

Describe the bug

The Painless language as implemented has a papercut that scripts cannot end in a single-line comment without a newline/carriage-return.

Was there a specific motivation why EOF wasn't a valid end token for single-line comments?

If so, it could be helpful to document that as leaving a comment without newline leads to an un-intuitive error: "type":"illegal_argument_exception","reason":"unexpected character [/ I am an unparsable comment].","caused_by":{"type":"lexer_no_viable_alt_exception","reason":null

If not, then suggesting a change to the lexer/grammar (like #11816)

Related component

Other

From my understanding, this is actually a bug from when this was forked from ElasticSearch, based on this definition of a comment: https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-comments.html

To Reproduce

docker run -d -p 9200:9200 -p 9600:9600 -e "discovery.type=single-node" public.ecr.aws/opensearchproject/opensearch:2.11.1

# wait for single-node cluster to init

curl -ku 'admin:admin' -XGET "https://localhost:9200/_scripts/painless/_execute" -H 'Content-Type: application/json' -d'{ 
    "script": 
        { 
            "source": "return 5 // I am an unparsable comment" 
        } 
}'


{"error":{"root_cause":[{"type":"script_exception","reason":"compile error","script_stack":["return 5 // I am an unparsable comm ...","          ^---- HERE"],"script":"return 5 // I am an unparsable comment","lang":"painless","position":{"offset":10,"start":0,"end":35}}],"type":"script_exception","reason":"compile error","script_stack":["return 5 // I am an unparsable comm ...","          ^---- HERE"],"script":"return 5 // I am an unparsable comment","lang":"painless","position":{"offset":10,"start":0,"end":35},"caused_by":{"type":"illegal_argument_exception","reason":"unexpected character [/ I am an unparsable comment].","caused_by":{"type":"lexer_no_viable_alt_exception","reason":null}}},"status":400}%

Expected behavior

Comments at EOF without newline do not

curl -ku 'admin:admin' -XGET "https://localhost:9200/_scripts/painless/_execute" -H 'Content-Type: application/json' -d' { "script": { "source": "return 5 // I am an unparsable comment" } }'
{"result":"5"}

Additional Details

Plugins
Please list all plugins currently enabled.:

N/A NONE

Screenshots
If applicable, add screenshots to help explain your problem.

Host/Environment (please complete the following information):

  • OS: Linux (from Docker Desktop for Mac)
  • Version 2.11.1

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    OtherbugSomething isn't workingv2.12.0Issues and PRs related to version 2.12.0v3.0.0Issues and PRs related to version 3.0.0

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions