diff --git a/Dockerfile b/Dockerfile index 344d0c8b8b..d91722f773 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.15.1 +FROM golang:1.16.2 RUN apt-get update -y --allow-insecure-repositories && \ apt-get install -y build-essential curl git libncurses5-dev python3-pip && \ diff --git a/autoload/go/cmd_test.vim b/autoload/go/cmd_test.vim index 708a3ab9d4..bd813845c5 100644 --- a/autoload/go/cmd_test.vim +++ b/autoload/go/cmd_test.vim @@ -6,7 +6,6 @@ func! Test_GoBuildErrors() try let l:filename = 'cmd/bad.go' let l:tmp = gotest#load_fixture(l:filename) - exe 'cd ' . l:tmp . '/src/cmd' " set the compiler type so that the errorformat option will be set " correctly. diff --git a/autoload/go/debug.vim b/autoload/go/debug.vim index 459a52a6e6..d9a60fe8c2 100644 --- a/autoload/go/debug.vim +++ b/autoload/go/debug.vim @@ -734,8 +734,8 @@ function! go#debug#TestFunc(...) abort call call('go#debug#Start', extend(['test', '.', '-test.run', printf('%s$', l:test)], a:000)) endfunction -" Start the debug mode. The first argument is the package name to compile and -" debug, anything else will be passed to the running program. +" Start the debug mode. The first variadic argument is the package name to +" compile and debug, anything else will be passed to the running program. function! go#debug#Start(mode, ...) abort call go#cmd#autowrite() diff --git a/autoload/go/debug_test.vim b/autoload/go/debug_test.vim index e7f9662a59..b7ce96a2df 100644 --- a/autoload/go/debug_test.vim +++ b/autoload/go/debug_test.vim @@ -15,7 +15,7 @@ function! Test_GoDebugStart_RelativePackage_NullModule() abort endfunction function! Test_GoDebugStart_Package() abort - call s:debug('debug/debugmain') + call s:debug('vim-go.test/debug/debugmain') endfunction function! Test_GoDebugStart_Errors() abort @@ -27,7 +27,7 @@ function! Test_GoDebugStart_Errors() abort let l:tmp = gotest#load_fixture('debug/compilerror/main.go') let l:expected = [ - \ {'lnum': 0, 'bufnr': 0, 'col': 0, 'valid': 0, 'vcol': 0, 'nr': -1, 'type': '', 'pattern': '', 'text': '# debug/compilerror'}, + \ {'lnum': 0, 'bufnr': 0, 'col': 0, 'valid': 0, 'vcol': 0, 'nr': -1, 'type': '', 'pattern': '', 'text': '# vim-go.test/debug/compilerror'}, \ {'lnum': 6, 'bufnr': bufnr('%'), 'col': 22, 'valid': 1, 'vcol': 0, 'nr': -1, 'type': '', 'pattern': '', 'text': ' syntax error: unexpected newline, expecting comma or )'}, \ {'lnum': 0, 'bufnr': 0, 'col': 0, 'valid': 0, 'vcol': 0, 'nr': -1, 'type': '', 'pattern': '', 'text': 'exit status 2'} \] @@ -142,7 +142,7 @@ function! s:debug(...) abort call assert_false(exists(':GoDebugStop')) if a:0 == 0 - call go#util#Chdir('debug/debugmain') + call go#util#Chdir(printf('%s/src/debug/debugmain', l:tmp)) let l:job = go#debug#Start('debug') else let l:job = go#debug#Start('debug', a:1) diff --git a/autoload/go/job_test.vim b/autoload/go/job_test.vim index 42f90ade52..c479821a1f 100644 --- a/autoload/go/job_test.vim +++ b/autoload/go/job_test.vim @@ -10,7 +10,8 @@ func! Test_JobDirWithSpaces() try let l:filename = 'job/dir has spaces/main.go' let l:tmp = gotest#load_fixture(l:filename) - exe 'cd ' . fnameescape(l:tmp . '/src/job/dir has spaces') + call go#util#Chdir(printf('%s/src/job/dir has spaces', l:tmp)) + call go#util#Exec(['go', 'mod', 'init', 'vim-go.test/job']) " set the compiler type so that the errorformat option will be set " correctly. diff --git a/autoload/go/lint_test.vim b/autoload/go/lint_test.vim index 790b760c69..b4cc90ce78 100644 --- a/autoload/go/lint_test.vim +++ b/autoload/go/lint_test.vim @@ -12,17 +12,18 @@ endfunc func! s:gometa(metalinter) abort let RestoreGOPATH = go#util#SetEnv('GOPATH', fnamemodify(getcwd(), ':p') . 'test-fixtures/lint') - silent exe 'e! ' . $GOPATH . '/src/lint/lint.go' + call go#util#Chdir('test-fixtures/lint/src/foo') + silent exe 'e! ' . $GOPATH . '/src/foo/foo.go' try let g:go_metalinter_command = a:metalinter let l:vim = s:vimdir() let expected = [ - \ {'lnum': 1, 'bufnr': bufnr('%')+9, 'col': 1, 'valid': 1, 'vcol': 0, 'nr': -1, 'type': '', 'pattern': '', 'text': 'at least one file in a package should have a package comment (ST1000)'}, + \ {'lnum': 1, 'bufnr': bufnr('%'), 'col': 1, 'valid': 1, 'vcol': 0, 'nr': -1, 'type': '', 'pattern': '', 'text': 'at least one file in a package should have a package comment (ST1000)'}, \ ] if a:metalinter == 'golangci-lint' let expected = [ - \ {'lnum': 5, 'bufnr': bufnr('%')+9, 'col': 1, 'valid': 1, 'vcol': 0, 'nr': -1, 'type': '', 'pattern': '', 'text': 'exported function `MissingFooDoc` should have comment or be unexported (golint)'} + \ {'lnum': 5, 'bufnr': bufnr('%'), 'col': 1, 'valid': 1, 'vcol': 0, 'nr': -1, 'type': '', 'pattern': '', 'text': 'exported function `MissingFooDoc` should have comment or be unexported (golint)'} \ ] endif diff --git a/autoload/go/test-fixtures/lint/src/errcheck/go.mod b/autoload/go/test-fixtures/lint/src/errcheck/go.mod new file mode 100644 index 0000000000..150c26fd9b --- /dev/null +++ b/autoload/go/test-fixtures/lint/src/errcheck/go.mod @@ -0,0 +1,3 @@ +module vim-go.test/errcheck + +go 1.16 diff --git a/autoload/go/test-fixtures/lint/src/foo/go.mod b/autoload/go/test-fixtures/lint/src/foo/go.mod new file mode 100644 index 0000000000..f70afb7417 --- /dev/null +++ b/autoload/go/test-fixtures/lint/src/foo/go.mod @@ -0,0 +1,3 @@ +module vim-go.test/foo + +go 1.16 diff --git a/autoload/go/test-fixtures/lint/src/lint/go.mod b/autoload/go/test-fixtures/lint/src/lint/go.mod new file mode 100644 index 0000000000..a0da6ba585 --- /dev/null +++ b/autoload/go/test-fixtures/lint/src/lint/go.mod @@ -0,0 +1,3 @@ +module vim-go.test/lint + +go 1.16 diff --git a/autoload/go/test-fixtures/lint/src/vet/go.mod b/autoload/go/test-fixtures/lint/src/vet/go.mod new file mode 100644 index 0000000000..136a3630f7 --- /dev/null +++ b/autoload/go/test-fixtures/lint/src/vet/go.mod @@ -0,0 +1,3 @@ +module vim-go.test/vet + +go 1.16 diff --git a/autoload/go/test-fixtures/test/src/compilerror/go.mod b/autoload/go/test-fixtures/test/src/compilerror/go.mod new file mode 100644 index 0000000000..51ced16f77 --- /dev/null +++ b/autoload/go/test-fixtures/test/src/compilerror/go.mod @@ -0,0 +1,3 @@ +module vim-go.test/compilerror + +go 1.16 diff --git a/autoload/go/test-fixtures/test/src/example/go.mod b/autoload/go/test-fixtures/test/src/example/go.mod new file mode 100644 index 0000000000..2a36a882e8 --- /dev/null +++ b/autoload/go/test-fixtures/test/src/example/go.mod @@ -0,0 +1,3 @@ +module vim-go.test/example + +go 1.16 diff --git a/autoload/go/test-fixtures/test/src/play/go.mod b/autoload/go/test-fixtures/test/src/play/go.mod new file mode 100644 index 0000000000..b8aca05250 --- /dev/null +++ b/autoload/go/test-fixtures/test/src/play/go.mod @@ -0,0 +1,3 @@ +module vim-go.test/play + +go 1.16 diff --git a/autoload/go/test-fixtures/test/src/play/play_test.go b/autoload/go/test-fixtures/test/src/play/play_test.go index 0270e89e89..b06c1d23a7 100644 --- a/autoload/go/test-fixtures/test/src/play/play_test.go +++ b/autoload/go/test-fixtures/test/src/play/play_test.go @@ -4,7 +4,7 @@ import ( "sync" "testing" - "play/mock" + "vim-go.test/play/mock" ) func TestTopSubHelper(t *testing.T) { diff --git a/autoload/go/test-fixtures/test/src/showname/go.mod b/autoload/go/test-fixtures/test/src/showname/go.mod new file mode 100644 index 0000000000..bc89e99fc3 --- /dev/null +++ b/autoload/go/test-fixtures/test/src/showname/go.mod @@ -0,0 +1,3 @@ +module vim-go.test/showname + +go 1.16 diff --git a/autoload/go/test-fixtures/test/src/testcompilerror/go.mod b/autoload/go/test-fixtures/test/src/testcompilerror/go.mod new file mode 100644 index 0000000000..5fdc13acab --- /dev/null +++ b/autoload/go/test-fixtures/test/src/testcompilerror/go.mod @@ -0,0 +1,3 @@ +module vim-go.test/testcompilerror + +go 1.16 diff --git a/autoload/go/test-fixtures/test/src/timeout/go.mod b/autoload/go/test-fixtures/test/src/timeout/go.mod new file mode 100644 index 0000000000..e3437b1731 --- /dev/null +++ b/autoload/go/test-fixtures/test/src/timeout/go.mod @@ -0,0 +1,3 @@ +module vim-go.test/timeout + +go 1.16 diff --git a/autoload/go/test-fixtures/test/src/veterror/go.mod b/autoload/go/test-fixtures/test/src/veterror/go.mod new file mode 100644 index 0000000000..45f5b82634 --- /dev/null +++ b/autoload/go/test-fixtures/test/src/veterror/go.mod @@ -0,0 +1,3 @@ +module vim-go.test/veterror + +go 1.16 diff --git a/autoload/go/test_test.vim b/autoload/go/test_test.vim index 322abed6ea..8bd3733ed2 100644 --- a/autoload/go/test_test.vim +++ b/autoload/go/test_test.vim @@ -101,6 +101,7 @@ endfunc func! s:test(file, expected, ...) abort let $GOPATH = fnameescape(fnamemodify(getcwd(), ':p')) . 'test-fixtures/test' + call go#util#Chdir(printf('%s/src/%s', $GOPATH, fnamemodify(a:file, ':h'))) silent exe 'e ' . $GOPATH . '/src/' . a:file " clear the quickfix lists diff --git a/autoload/go/tool_test.vim b/autoload/go/tool_test.vim index 42b8630453..275bf8612d 100644 --- a/autoload/go/tool_test.vim +++ b/autoload/go/tool_test.vim @@ -5,7 +5,9 @@ set cpo&vim func! Test_ExecuteInDir() abort let l:tmp = gotest#write_file('a/a.go', ['package a']) try + let l:cwd = go#util#Exec(['pwd']) let l:out = go#util#ExecInDir(['pwd']) + call assert_notequal(l:cwd, l:out) call assert_equal([l:tmp . "/src/a\n", 0], l:out) finally call delete(l:tmp, 'rf') diff --git a/autoload/go/util.vim b/autoload/go/util.vim index 91226b3751..76178b4f8b 100644 --- a/autoload/go/util.vim +++ b/autoload/go/util.vim @@ -715,7 +715,7 @@ function! go#util#Chdir(dir) abort if !exists('*chdir') let l:olddir = getcwd() let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd ' : 'cd ' - execute cd . fnameescape(a:dir) + execute printf('cd %s', fnameescape(a:dir)) return l:olddir endif return chdir(a:dir) diff --git a/autoload/gotest.vim b/autoload/gotest.vim index 4ee727a951..9b0aaec56a 100644 --- a/autoload/gotest.vim +++ b/autoload/gotest.vim @@ -2,10 +2,18 @@ let s:cpo_save = &cpo set cpo&vim -" Write a Go file to a temporary directory and append this directory to $GOPATH. +" Write a Go file to a temporary directory and append this directory to +" $GOPATH. " -" The file will written to a:path, which is relative to the temporary directory, -" and this file will be loaded as the current buffer. +" The file will written to a:path, which is relative to the temporary +" directory, and this file will be loaded as the current buffer. +" +" A Go module will be configured in the first segment of a:path within the +" temporary directory. The module's name will be prefixed with vim-go.test/ +" followed by the first segment in a:path. +" +" The current directory will be changed to the parent directory of module +" root. " " The cursor will be placed on the character before any 0x1f byte. " @@ -18,7 +26,7 @@ fun! gotest#write_file(path, contents) abort call mkdir(fnamemodify(l:full_path, ':h'), 'p') call writefile(a:contents, l:full_path) - call go#util#Chdir(l:dir . '/src') + call s:setupproject(printf('%s/src', l:dir), a:path) silent exe 'e! ' . a:path @@ -48,6 +56,13 @@ endfun " " The file will be copied to a new GOPATH-compliant temporary directory and " loaded as the current buffer. +" +" A Go module will be configured in the first segment of a:path within the +" temporary directory. The module's name will be prefixed with vim-go.test/ +" followed by the first segment in a:path. +" +" The current directory will be changed to the parent directory of module +" root. fun! gotest#load_fixture(path) abort if go#util#has_job() call go#lsp#CleanWorkspaces() @@ -57,7 +72,7 @@ fun! gotest#load_fixture(path) abort let l:full_path = l:dir . '/src/' . a:path call mkdir(fnamemodify(l:full_path, ':h'), 'p') - call go#util#Chdir(l:dir . '/src') + call s:setupproject(printf('%s/src', l:dir), a:path) silent exe 'noautocmd e! ' . a:path silent exe printf('read %s/test-fixtures/%s', g:vim_go_root, a:path) silent noautocmd w! @@ -147,6 +162,33 @@ func! gotest#assert_quickfix(got, want) abort return l:retval endfunc +" s:setupproject sets up a Go module in dir rooted at the first segment of +" path and changes the current directory to the parent directory of the +" project root. +func! s:setupproject(dir, path) abort + let l:projectdir = s:projectdir(a:path) + let l:mod = printf('vim-go.test/%s', l:projectdir) + let l:modroot = printf('%s/%s', a:dir, l:projectdir) + call s:creategomod(l:mod, l:modroot) + call go#util#Chdir(a:dir) +endfunc + +func! s:creategomod(modname, dir) abort + call go#util#ExecInWorkDir(['go', 'mod', 'init', a:modname], a:dir) +endfunc + +" s:project dir returns the first element of path. +func! s:projectdir(path) abort + let l:path = a:path + let l:next = fnamemodify(l:path, ':h') + while l:next isnot '.' + let l:path = l:next + let l:next = fnamemodify(l:path, ':h') + endwhile + + return l:path +endfunc + " restore Vi compatibility settings let &cpo = s:cpo_save unlet s:cpo_save diff --git a/scripts/install-tools b/scripts/install-tools index 0cc322ddb4..7634a26099 100755 --- a/scripts/install-tools +++ b/scripts/install-tools @@ -17,7 +17,7 @@ installdir="/tmp/vim-go-test/$1-install" # Make sure all Go tools and other dependencies are installed. echo "Installing Go binaries" export GOPATH=$installdir -export GO111MODULE=off +export GO111MODULE=on export PATH=${GOPATH}/bin:$PATH "$vimgodir/scripts/run-vim" $vim +':silent :GoUpdateBinaries' +':qa' diff --git a/scripts/run-vim b/scripts/run-vim index 9e7f7a5dbd..0f3b6d901b 100755 --- a/scripts/run-vim +++ b/scripts/run-vim @@ -23,7 +23,7 @@ fi dir="/tmp/vim-go-test/$1-install" export GOPATH=$dir -export GO111MODULE=auto +export GO111MODULE=on export PATH=${GOPATH}/bin:$PATH shift