Skip to content

WithContext doesn't work with 2 functions within 1 call #823

Closed

Description

@ykalchevskiy

Hi @antonmedv

If there are 2 patched function calls within 1 function call, it doesn't compile.

https://go.dev/play/p/9dH2e9KuF2d

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/expr-lang/expr"
)

type env struct {
	Ctx context.Context `expr:"ctx"`
}

func main() {
	p, err := expr.Compile(
		"now2().After(date2())",
		expr.Env(env{}),
		expr.WithContext("ctx"),
		expr.Function(
			"now2",
			func(params ...any) (any, error) { return time.Now(), nil },
			new(func(context.Context) time.Time),
		),
		expr.Function(
			"date2",
			func(params ...any) (any, error) { return time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), nil },
			new(func(context.Context) time.Time),
		),
	)
	if err != nil {
		panic(err)
	}
	r, err := expr.Run(p, &env{Ctx: context.Background()})
	if err != nil {
		panic(err)
	}
	fmt.Println(r)
}

Output:

panic: not enough arguments to call date2 (1:14)
	 | now2().After(date2())
	 | .............^

goroutine 1 [running]:
main.main()
	/tmp/sandbox2261374222/prog.go:32 +0x2d5

Activity

  1. thevilledev commented on Nov 30, 2025

    @thevilledev
    Contributor

    Tried to reproduce this on current master to no avail. But it does occur on the latest tagged version v1.17.6. So:

    I didn't look too deep into what actually fixed this, but probably safe to say the issue will be fixed in v1.17.7.

  2. antonmedv commented on Dec 1, 2025

    @antonmedv
    Member

    I didn't look too deep into what actually fixed this, but probably safe to say the issue will be fixed in v1.17.7.

    Thanks for checking!

  3. ykalchevskiy commented on Dec 12, 2025

    @ykalchevskiy
    Author

    Thanks! When are you going to release a new version?

  4. antonmedv commented on Dec 12, 2025

    @antonmedv
    Member

    This weekend hopefully =)

  5. ykalchevskiy commented on Dec 16, 2025

    @ykalchevskiy
    Author

    It's not really fixed https://go.dev/play/p/TWmBi35GszX
    If I access context.Context as the first argument, which should be inserted by the WithContext patcher, it still panics.

    Can you reopen?

  6. thevilledev commented on Dec 16, 2025

    @thevilledev
    Contributor

    My bad, looks like the regression test didn't check this end-to-end after all. I opened a potential fix in #883. This yields false as the output on the fix branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions