[vim/vim] Vim9: cannot refer to exported autoload function via Vim9 dot notation without sourcing its script too early (Issue #9578)

2 views
Skip to first unread message

lacygoill

unread,
Jan 20, 2022, 2:49:22 PM1/20/22
to vim/vim, Subscribed

Steps to reproduce

Run this shell command:

vim -Nu NONE -S <(tee <<'EOF'
    vim9script
    var dir = '/tmp/.vim'
    &runtimepath = dir
    dir ..= '/autoload'
    dir->mkdir('p')
    var lines =<< trim END
        vim9script
        export def ThesaurusFunc(findbase: bool, _): any
            if findbase
                return 1
            endif
            return [
                'check',
                'experiment',
                'test',
                'verification'
            ]
        enddef
        sleep 5
    END
    lines->writefile(dir .. '/completion.vim')
    import autoload 'completion.vim'
    &thesaurusfunc = completion.ThesaurusFunc
    feedkeys("i\<C-X>\<C-T>")
EOF
)

It takes 5 seconds for Vim to start, then the completion menu is opened.

Run this other shell command:

vim -Nu NONE -S <(tee <<'EOF'
    vim9script
    var dir = '/tmp/.vim'
    &runtimepath = dir
    dir ..= '/autoload'
    dir->mkdir('p')
    var lines =<< trim END
        vim9script
        export def ThesaurusFunc(findbase: bool, _): any
            if findbase
                return 1
            endif
            return [
                'check',
                'experiment',
                'test',
                'verification'
            ]
        enddef
        sleep 5
    END
    lines->writefile(dir .. '/completion.vim')
    import autoload 'completion.vim'
    &thesaurusfunc = 'completion.ThesaurusFunc'
    feedkeys("i\<C-X>\<C-T>")
EOF
)

Vim starts immediately, but E117 is given:

E117: Unknown function: completion.ThesaurusFunc

Run this last shell command:

vim -Nu NONE -S <(tee <<'EOF'
    vim9script
    var dir = '/tmp/.vim'
    &runtimepath = dir
    dir ..= '/autoload'
    dir->mkdir('p')
    var lines =<< trim END
        vim9script
        export def ThesaurusFunc(findbase: bool, _): any
            if findbase
                return 1
            endif
            return [
                'check',
                'experiment',
                'test',
                'verification'
            ]
        enddef
        sleep 5
    END
    lines->writefile(dir .. '/completion.vim')
    import autoload 'completion.vim'
    &thesaurusfunc = function('completion.ThesaurusFunc')
    feedkeys("i\<C-X>\<C-T>")
EOF
)

Vim starts after 5 seconds, and E117 is given:

E117: Unknown function: completion.ThesaurusFunc

Expected behavior

In at least one of these shell commands, Vim starts immediately, then sleeps for 5 seconds, then opens the completion menu. Otherwise, it means we have no way to refer to an exported autoload function via the Vim9 dot notation without sourcing its script too early, which defeats the whole purpose of writing it inside an autoload script in the first place.

Version of Vim

8.2 Included patches: 1-4162

Environment

Operating system: Ubuntu 20.04.3 LTS
Terminal: xterm
Value of $TERM: xterm-256color
Shell: zsh 5.8

Additional context

If we refer to the autoload function via the legacy name completion#ThesaurusFunc(), then we can get the desired result simply by quoting the name:

&thesaurusfunc = 'completion#ThesaurusFunc'
                 ^                        ^

Test:

vim9script
var dir = '/tmp/.vim'
&runtimepath = dir
dir ..= '/autoload'
dir->mkdir('p')
var lines =<< trim END
    vim9script
    export def ThesaurusFunc(findbase: bool, _): any
        if findbase
            return 1
        endif
        return [
            'check',
            'experiment',
            'test',
            'verification'
        ]
    enddef
    sleep 5
END
lines->writefile(dir .. '/completion.vim')
import autoload 'completion.vim'
&thesaurusfunc = 'completion#ThesaurusFunc'
feedkeys("i\<C-X>\<C-T>")
Vim starts immediately
Vim sleeps for 5 seconds
Vim opens the pum


Reply to this email directly, view it on GitHub.
Triage notifications on the go with GitHub Mobile for iOS or Android.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/9578@github.com>

lacygoill

unread,
Jan 20, 2022, 2:56:39 PM1/20/22
to vim/vim, Subscribed

Sorry, it might not be easy to understand the purpose of the 3 shell commands.

They test these assignments:

&thesaurusfunc = completion.ThesaurusFunc
&thesaurusfunc = 'completion.ThesaurusFunc'
&thesaurusfunc = function('completion.ThesaurusFunc')

Summary of the results:

┌──────────────────────────────────────┬───────────────────────────────────┐
│ completion.ThesaurusFunc             │ autoload script sourced too early │
│                                      │ completion working                │
├──────────────────────────────────────┼───────────────────────────────────┤
│ 'completion.ThesaurusFunc'           │ completion not working            │
├──────────────────────────────────────┼───────────────────────────────────┤
│ function('completion.ThesaurusFunc') │ autoload script sourced too early │
│                                      │ completion not working            │
└──────────────────────────────────────┴───────────────────────────────────┘

┌──────────────────────────────────────┬───────────────────────────────────┐
│ completion#ThesaurusFunc             │ autoload script sourced too early │
│                                      │ completion working                │
├──────────────────────────────────────┼───────────────────────────────────┤
│ 'completion#ThesaurusFunc'           │ autoload script lazy-loaded       │
│                                      │ completion working                │
├──────────────────────────────────────┼───────────────────────────────────┤
│ function('completion#ThesaurusFunc') │ autoload script lazy-loaded       │
│                                      │ completion working                │
└──────────────────────────────────────┴───────────────────────────────────┘

Notice that the 2 tables are inconsistent. The right cell gives identical results on the first line, but not on the 2nd and 3rd lines.


Reply to this email directly, view it on GitHub, or unsubscribe.


Triage notifications on the go with GitHub Mobile for iOS or Android.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/9578/1017868713@github.com>

Bram Moolenaar

unread,
Jan 21, 2022, 7:06:56 AM1/21/22
to vim/vim, Subscribed

If you use the completion it is normal the autoload script is actually loaded. But when setting 'thesaurusfunc' to an autoloaded function it should not be loaded yet. it appears that it does happen with just this:

import autoload 'completion.vim'
&thesaurusfunc = completion.ThesaurusFunc

It should be possible to avoid that.


Reply to this email directly, view it on GitHub.
Triage notifications on the go with GitHub Mobile for iOS or Android.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/9578/1018447982@github.com>

Bram Moolenaar

unread,
Jan 21, 2022, 7:57:26 AM1/21/22
to vim/vim, Subscribed

Actually, using the assignment style won't work, because the expression will be evaluated, causing the autoload script to be loaded. Using a string should work though.


Reply to this email directly, view it on GitHub.
Triage notifications on the go with GitHub Mobile for iOS or Android.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/9578/1018481582@github.com>

Bram Moolenaar

unread,
Jan 21, 2022, 8:30:55 AM1/21/22
to vim/vim, Subscribed

Closed #9578 via f0e7e63.


Reply to this email directly, view it on GitHub.
Triage notifications on the go with GitHub Mobile for iOS or Android.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issue/9578/issue_event/5932141197@github.com>

Reply all
Reply to author
Forward
0 new messages