Skip to content

The application crashes if there are no commands, but there is a custom resolver #264

@titusjaka

Description

@titusjaka

Hi 👋
First of all, thank you for your awesome work! The Kong is perfect ❤️

What happened

I ran into the following problem. I have an application with no additional commands. Meanwhile, I have a custom config resolver for .env files ("github.com/bluegosolutions/kong-dotenv-go", it's the same as in #156).

Here's a sample of my code:

package main

import (
    // other imports
	"github.com/alecthomas/kong"
	dotenv "github.com/bluegosolutions/kong-dotenv-go"
)

// App defines the service and all its configs
type App struct {
	// You can pass an .env file as a config: <command_name> --env-file=./.env
	EnvFile kong.ConfigFlag `kong:"optional,name=env-file,help='Path to .env file'"`
	// HTTP server configurator
	HTTPServer HTTPServerFlags `kong:"embed"`
}

type HTTPServerFlags struct {
	Address string `kong:"required,group='HTTP Server',name=http-address,env='HTTP_ADDR',default=':8080',help='The HTTP address <host>:<port> of the main server.'"`
}

func main() {
	var app App
	ctx := kong.Parse(&app, kong.Configuration(dotenv.ENVFile))
	ctx.FatalIfErrorf(ctx.Run())
}

When I run the application with environment variables or flags, everything works perfectly.
But when I run the application with the --env-file flag, it crashes with a panic:

go run main.go --env-file=.env

API server listening at: 127.0.0.1:58887
debugserver-@(#)PROGRAM:LLDB  PROJECT:lldb-1300.0.42.3
 for x86_64.
Got a connection, launched process /private/var/folders/df/83mftq9j2rl2tc1z2cxkpxsh0000gq/T/GoLand/___1go_build_gitlab_corp_mail_ru_youla_api_platform_git_v2_cmd_app (pid = 54942).
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x14a402a]

goroutine 1 [running]:
github.com/alecthomas/kong.(*Context).Run(0xc000046c80, {0x0, 0x0, 0x0})
        /Users/d.titusov/go/pkg/mod/github.com/alecthomas/[email protected]/context.go:751 +0x10a
main.main()
        /Users/d.titusov/Projects/youla/platform/cmd/app/main.go:45 +0x15f
Exiting.

My hypothesis as to why this happens

As far as I understand, the issue is that kong is trying to run the application from Node 0, when the 0th node is not ApplicationNode:

kong/context.go

Line 750 in 3987efb

selected := c.Path[0].Node()

The order of the ApplicationNode is changed inside the Resolve method:

kong/context.go

Line 585 in 3987efb

c.Path = append(inserted, c.Path...)

How to fix that

First of all, I assume that I could miss something important. There might be a reason why you put original c.Path nodes at the end of the c.Path slice.

Otherwise, If this is a bug, I'd be happy to fix it.

Thanks for your help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions