aquaproj / aqua

Declarative CLI Version manager written in Go. Support Lazy Install, Registry, and continuous update with Renovate. CLI version is switched seamlessly
https://aquaproj.github.io
887 stars 39 forks source link

`aqua g -i` raises panic if `packages` has only comments #3200

Open mpal9000 opened 1 month ago

mpal9000 commented 1 month ago

aqua info

$ aqua info
{
  "version": "2.36.2",
  "commit_hash": "716d9ac6f2de2beffe0db87caf418443155170ab",
  "os": "linux",
  "arch": "amd64",
  "pwd": "/home/(USER)/tmp/aqua-test/a",
  "root_dir": "/home/(USER)/.local/share/aqua",
  "env": {
    "AQUA_DISABLE_LAZY_INSTALL": "true",
    "AQUA_GLOBAL_CONFIG": "/home/(USER)/.config/aqua/aqua.yaml",
    "AQUA_POLICY_CONFIG": "/home/(USER)/.config/aqua/policy.yaml",
    "AQUA_ROOT_DIR": "/home/(USER)/.local/share/aqua"
  },
  "config_files": [
    {
      "path": "/home/(USER)/tmp/aqua-test/a/aqua.yaml"
    }
  ]
}

Overview

When running aqua g -i with an empty packages field, a panic occurs after a selection is done.

How to reproduce

aqua.yaml

checksum:
  supported_envs:
    - linux
registries:
  - type: local
    name: local
    path: registry.yaml
packages:

Executed command and output

$  aqua g -i
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x38 pc=0x8b6283]

goroutine 1 [running]:
github.com/goccy/go-yaml/parser.(*parser).createMapValueNode(0xc00060adc8, 0xc000794d40, {0xf367a8, 0xc000790aa0}, 0xc0001317c0)
    /home/runner/go/pkg/mod/github.com/goccy/go-yaml@v1.12.0/parser/parser.go:167 +0x163
github.com/goccy/go-yaml/parser.(*parser).parseMapValue(0xc000794040?, 0xc000794d40, {0xf367a8?, 0xc000790aa0?}, 0xc00060a4c0?)
    /home/runner/go/pkg/mod/github.com/goccy/go-yaml@v1.12.0/parser/parser.go:142 +0x26
github.com/goccy/go-yaml/parser.(*parser).parseMappingValue(0xc00060adc8, 0xc000794040)
    /home/runner/go/pkg/mod/github.com/goccy/go-yaml@v1.12.0/parser/parser.go:258 +0x33b
github.com/goccy/go-yaml/parser.(*parser).createNodeFromToken(0x24?, 0x455c3e?, 0xc0004c2520?)
    /home/runner/go/pkg/mod/github.com/goccy/go-yaml@v1.12.0/parser/parser.go:647 +0x266
github.com/goccy/go-yaml/parser.(*parser).parseToken(0xc?, 0xc000794040, 0x1?)
    /home/runner/go/pkg/mod/github.com/goccy/go-yaml@v1.12.0/parser/parser.go:632 +0x1c
github.com/goccy/go-yaml/parser.(*parser).parseComment(0xc00060adc8, 0xc000794040)
    /home/runner/go/pkg/mod/github.com/goccy/go-yaml@v1.12.0/parser/parser.go:604 +0x5f
github.com/goccy/go-yaml/parser.(*parser).createNodeFromToken(0xc00060adc8, 0xc000794040, 0xc000130be0)
    /home/runner/go/pkg/mod/github.com/goccy/go-yaml@v1.12.0/parser/parser.go:659 +0x15f
github.com/goccy/go-yaml/parser.(*parser).parseToken(0xc000794040?, 0xc000794040, 0xa?)
    /home/runner/go/pkg/mod/github.com/goccy/go-yaml@v1.12.0/parser/parser.go:632 +0x1c
github.com/goccy/go-yaml/parser.(*parser).parseMappingValue(0xc00060adc8, 0xc000794040)
    /home/runner/go/pkg/mod/github.com/goccy/go-yaml@v1.12.0/parser/parser.go:276 +0x813
github.com/goccy/go-yaml/parser.(*parser).createNodeFromToken(0xc00060a8e0?, 0xc000366360?, 0x1?)
    /home/runner/go/pkg/mod/github.com/goccy/go-yaml@v1.12.0/parser/parser.go:647 +0x266
github.com/goccy/go-yaml/parser.(*parser).parseToken(0xc000794040?, 0xc000794040, 0x8?)
    /home/runner/go/pkg/mod/github.com/goccy/go-yaml@v1.12.0/parser/parser.go:632 +0x1c
github.com/goccy/go-yaml/parser.(*parser).parseMappingValue(0xc00060adc8, 0xc000794040)
    /home/runner/go/pkg/mod/github.com/goccy/go-yaml@v1.12.0/parser/parser.go:276 +0x813
github.com/goccy/go-yaml/parser.(*parser).createNodeFromToken(0x0?, 0x8?, 0xc0004c2080?)
    /home/runner/go/pkg/mod/github.com/goccy/go-yaml@v1.12.0/parser/parser.go:647 +0x266
github.com/goccy/go-yaml/parser.(*parser).parseToken(0x50060aa00?, 0xc000794040, 0x64?)
    /home/runner/go/pkg/mod/github.com/goccy/go-yaml@v1.12.0/parser/parser.go:632 +0x1c
github.com/goccy/go-yaml/parser.(*parser).parseComment(0xc00060adc8, 0xc000794040)
    /home/runner/go/pkg/mod/github.com/goccy/go-yaml@v1.12.0/parser/parser.go:604 +0x5f
github.com/goccy/go-yaml/parser.(*parser).createNodeFromToken(0xc00060adc8, 0xc000794040, 0xc000130140)
    /home/runner/go/pkg/mod/github.com/goccy/go-yaml@v1.12.0/parser/parser.go:659 +0x15f
github.com/goccy/go-yaml/parser.(*parser).parseToken(0x8b8a65?, 0xc000794040, 0xc0001300f0?)
    /home/runner/go/pkg/mod/github.com/goccy/go-yaml@v1.12.0/parser/parser.go:632 +0x1c
github.com/goccy/go-yaml/parser.(*parser).parseDocument(0xc00060adc8, 0xc000794040)
    /home/runner/go/pkg/mod/github.com/goccy/go-yaml@v1.12.0/parser/parser.go:554 +0x8c
github.com/goccy/go-yaml/parser.(*parser).createNodeFromToken(0xc00060adc8, 0xc000794040, 0xc0001300f0)
    /home/runner/go/pkg/mod/github.com/goccy/go-yaml@v1.12.0/parser/parser.go:663 +0xa5
github.com/goccy/go-yaml/parser.(*parser).parseToken(0x41a956?, 0xc000794040, 0x0?)
    /home/runner/go/pkg/mod/github.com/goccy/go-yaml@v1.12.0/parser/parser.go:632 +0x1c
github.com/goccy/go-yaml/parser.(*parser).parse(0xc00060adc8, {0xc0005dc488?, 0x1?, 0x0?}, 0x0?)
    /home/runner/go/pkg/mod/github.com/goccy/go-yaml@v1.12.0/parser/parser.go:688 +0x14d
github.com/goccy/go-yaml/parser.Parse({0xc0005dc488?, 0xc000848000?, 0x4f0?}, 0xc000848000?)
    /home/runner/go/pkg/mod/github.com/goccy/go-yaml@v1.12.0/parser/parser.go:724 +0x29
github.com/goccy/go-yaml/parser.ParseBytes({0xc000848000?, 0x1647fc0?, 0xc0004245a0?}, 0x1)
    /home/runner/go/pkg/mod/github.com/goccy/go-yaml@v1.12.0/parser/parser.go:714 +0x3d
github.com/aquaproj/aqua/v2/pkg/controller/generate/output.(*Outputter).generateInsert(0xc0000be980, {0xc0004245a0, 0x24}, {0xc00009e040, 0x1, 0x1})
    /home/runner/work/aqua/aqua/pkg/controller/generate/output/insert.go:23 +0xc5
github.com/aquaproj/aqua/v2/pkg/controller/generate/output.(*Outputter).Output(0xc00048ca10?, 0xc000598080)
    /home/runner/work/aqua/aqua/pkg/controller/generate/output/output.go:41 +0x1ea
github.com/aquaproj/aqua/v2/pkg/controller/generate.(*Controller).Generate(0xc00040a510, {0xf30ce0, 0xc000137380}, 0xc00048ca10, 0xc000466580, {0x1647fc0, 0x0, 0x0})
    /home/runner/work/aqua/aqua/pkg/controller/generate/generate.go:56 +0x20e
github.com/aquaproj/aqua/v2/pkg/cli/generate.(*command).action(0xc000698040, 0xc0001366c0)
    /home/runner/work/aqua/aqua/pkg/cli/generate/command.go:83 +0x265
github.com/urfave/cli/v2.(*Command).Run(0xc00069ac60, 0xc0001366c0, {0xc00012f3a0, 0x2, 0x2})
    /home/runner/go/pkg/mod/github.com/urfave/cli/v2@v2.27.5/command.go:276 +0x7e2
github.com/urfave/cli/v2.(*Command).Run(0xc0004662c0, 0xc0000e2ac0, {0xc000034150, 0x3, 0x3})
    /home/runner/go/pkg/mod/github.com/urfave/cli/v2@v2.27.5/command.go:269 +0xa65
github.com/urfave/cli/v2.(*App).RunContext(0xc000690000, {0xf30ce0, 0xc000137380}, {0xc000034150, 0x3, 0x3})
    /home/runner/go/pkg/mod/github.com/urfave/cli/v2@v2.27.5/app.go:333 +0x5a5
github.com/aquaproj/aqua/v2/pkg/cli.Run({0xf30ce0, 0xc000137380}, 0xc00068c000, {0xc000034150, 0x3, 0x3})
    /home/runner/work/aqua/aqua/pkg/cli/runner.go:111 +0x825
main.core(0xc00048ca10, 0xc00012f100)
    /home/runner/work/aqua/aqua/cmd/aqua/main.go:45 +0x225
main.main()
    /home/runner/work/aqua/aqua/cmd/aqua/main.go:31 +0x4c

Debug output

No response

Expected behaviour

I would expect the command to work normally, when the packages field is empty.

Actual behaviour

A panic occurs.

Note

No response

suzuki-shunsuke commented 1 month ago

Thank you for your report.

https://github.com/aquaproj/aqua/blob/46a05be40b44e24a0466c6e7da2c088e7d00197b/pkg/controller/generate/output/insert.go#L23

github.com/aquaproj/aqua/v2/pkg/controller/generate/output.(*Outputter).generateInsert(0xc0000be980, {0xc0004245a0, 0x24}, {0xc00009e040, 0x1, 0x1})
    /home/runner/work/aqua/aqua/pkg/controller/generate/output/insert.go:23 +0xc5

The panic occurs in the third party library goccy/go-yaml.

mpal9000 commented 1 month ago

Other problems of the same dependency: https://github.com/aquaproj/aqua/issues/584 https://github.com/aquaproj/aqua/issues/585

suzuki-shunsuke commented 1 month ago

This issue doesn't occur when I create aqua.yaml by aqua init.

$ aqua init
$ cat aqua.yaml 
---
# aqua - Declarative CLI Version Manager
# https://aquaproj.github.io/
# checksum:
#   enabled: true
#   require_checksum: true
#   supported_envs:
#   - all
registries:
- type: standard
  ref: v4.239.1  # renovate: depName=aquaproj/aqua-registry
packages:

$ aqua g -i # choose 1xyz/pryrite
$ cat aqua.yaml
---
# aqua - Declarative CLI Version Manager
# https://aquaproj.github.io/
# checksum:
#   enabled: true
#   require_checksum: true
#   supported_envs:
#   - all
registries:
- type: standard
  ref: v4.239.1 # renovate: depName=aquaproj/aqua-registry
packages:
- name: 1xyz/pryrite@0.10.20
suzuki-shunsuke commented 1 month ago

Related: #584 #585

I don't think they are related. They are issues related to goccy/go-yaml, but they are different from this issue.

mpal9000 commented 1 month ago

Yes that's what I meant, but I edited the comment. I wanted to link this one with the other two.

suzuki-shunsuke commented 1 month ago

Hmm. I couldn't reproduce the issue.

$ aqua info
{
  "version": "2.36.2",
  "commit_hash": "716d9ac6f2de2beffe0db87caf418443155170ab",
  "os": "darwin",
  "arch": "arm64",
  "pwd": "/Users/(USER)/Documents/test/aqua/3200",
  "root_dir": "/Users/(USER)/.local/share/aquaproj-aqua",
  "env": {
    "AQUA_GLOBAL_CONFIG": ":/Users/(USER)/repos/src/github.com/aquaproj/aqua-registry/aqua-all.yaml",
    "AQUA_PROGRESS_BAR": "true"
  },
  "config_files": [
    {
      "path": "/Users/(USER)/Documents/test/aqua/3200/aqua.yaml"
    }
  ]
}

Set up local registry and policy.

$ cp ~/repos/src/github.com/aquaproj/aqua-registry/registry.yaml . # Copy registry.yaml from aqua-registry
$ cat aqua-policy.yaml
---
registries:
  - name: local
    type: local
    path: registry.yaml
  - type: standard
    ref: semver(">= 3.0.0")
packages:
  - registry: local # allow all packages in the Registry
  - registry: standard
$ git init
$ aqua policy allow

Run aqua g -i

$ cat aqua.yaml 
checksum:
  supported_envs:
    - linux
registries:
  - type: local
    name: local
    path: registry.yaml
packages:

$ aqua g -i # choose 1xyz/pryrite
$ cat aqua.yaml
checksum:
  supported_envs:
    - linux
registries:
  - type: local
    name: local
    path: registry.yaml
packages:
- name: 1xyz/pryrite@0.10.20
  registry: local
mpal9000 commented 1 month ago

I checked again because I saw about your try with aqua init and actually it is a comment at the end that I omitted from the bug report. I.e.

aqua.yaml

---
registries:
  - type: local
    name: local
    path: registry.yaml
packages:
#
mpal9000 commented 1 month ago

btw aqua init could also (optionally?) add the comment for the schema provided to LSP: # yaml-language-server: $schema=https://raw.githubusercontent.com/aquaproj/aqua/main/json-schema/aqua-yaml.json

suzuki-shunsuke commented 1 month ago

📝 I thought maybe missing a newline at the end of file is related to this issue, so I removed it and tried again but I couldn't reproduce the issue.

suzuki-shunsuke commented 1 month ago

btw aqua init could also (optionally?) add the comment for the schema provided to LSP: # yaml-language-server: $schema=https://raw.githubusercontent.com/aquaproj/aqua/main/json-schema/aqua-yaml.json

Could you open an issue for this? Now aqua doesn't support it.

suzuki-shunsuke commented 1 month ago

I checked again because I saw about your try with aqua init and actually it is a comment at the end that I omitted from the bug report. I.e.

aqua.yaml

---
registries:
  - type: local
    name: local
    path: registry.yaml
packages:
#

I see. I could reproduce the issue. Thank you. I could reproduce the issue without local registry.

$ cat aqua.yaml 
---
# aqua - Declarative CLI Version Manager
# https://aquaproj.github.io/
# checksum:
#   enabled: true
#   require_checksum: true
#   supported_envs:
#   - all
registries:
- type: standard
  ref: v4.239.1  # renovate: depName=aquaproj/aqua-registry
packages:
#

From the next time, could you use standard registry rather than local registry and github_content registry in the reproduction code if they are unnecessary to reproduce the issue? Then we can reproduce the issue easily.

suzuki-shunsuke commented 1 month ago

I think it's difficult to solve the issue by aqua. This is an issue of goccy/go-yaml.

mpal9000 commented 1 month ago

Regarding the schema comment, I opened #3202.

Regarding the panic I can reproduce it with local registries and multiple empty lines at the end of the file, e.g.:

---
registries:
  - type: local
    name: local
    path: $HOME/.config/aqua/registry.yaml
packages:
# 3 empty lines below

Sure, I will use the standard registry next time as an example.

This is not blocking for me. Maybe the yaml library should solve this one and the other two issues.

suzuki-shunsuke commented 1 month ago

Yeah, I reported an issue related to #584 #585 before, but it is stale.

suzuki-shunsuke commented 3 weeks ago

I guess this is the cause.