vlang / v-analyzer

The @vlang language server, for all your editing needs like go-to-definition, code completion, type hints, and more.
MIT License
83 stars 9 forks source link

Go to definition and code completion does not work in test files #106

Open tomialagbe opened 1 month ago

tomialagbe commented 1 month ago

Describe the bug

Given a v project with these two files

// src/file_manager.v
pub struct FileManager {
}

pub fn (f FileManager) read_page() {}
// src/file_manager_test.v
fn test_read_page() {
    file_mgr := FileManager{}
    file_mgr.read_page()
}

Expected Behavior

  1. Completion for methods of struct FileManager should be shown
  2. Ctrl/Cmd + Click on a function name should go to the definition of the function.

Current Behavior

  1. There's no completion for the FileManager struct
  2. Cmd+Click does nothing

Reproduction Steps

  1. Create a v project v new <project_name>
  2. two files file_manager.v and file_manager_test.v
  3. copy this code into file_manager.v
    
    // src/file_manager.v
    pub struct FileManager {
    }

pub fn (f FileManager) read_page() {}

4. Copy this code into `file_manager_test.v`

// src/file_manager_test.v fn test_read_page() { file_mgr := FileManager{} file_mgr.read_page() }



### Possible Solution

_No response_

### Additional Information/Context

_No response_

### Environment details (`v doctor` output)

V full version: V 0.4.6 604eb65.7a36b44
OS: macos, macOS, 14.0, 23A344
Processor: 10 cpus, 64bit, little endian, Apple M1 Pro

getwd: /Users/tomialagbe/dev
vexe: /Users/tomialagbe/v/v
vexe mtime: 2024-05-22 18:07:15

vroot: OK, value: /Users/tomialagbe/v
VMODULES: OK, value: /Users/tomialagbe/.vmodules
VTMP: OK, value: /tmp/v_501

Git version: git version 2.39.3 (Apple Git-146)
Git vroot status: weekly.2023.32-1929-g7a36b44c-dirty
.git/config present: true

CC version: Apple clang version 15.0.0 (clang-1500.3.9.4)
thirdparty/tcc status: thirdparty-macos-arm64 5c1d002f

### Editor name

Vscode

### v-analyzer Version

v-analyzer version: 0.0.4-beta.1.7e11a6f

### VS Code Extension Version

v0.0.2
spytheman commented 3 weeks ago

Hi. I think the problem is not directly related to v-analyzer, but to how V treats test files as internal and external ones.

Try changing file_manager_test.v to:

module main

fn test_read_page() {
        file_mgr := FileManager{}
        file_mgr.read_page()
}

The module main at the start is important, so that V will consider the test as an internal one (see the note for internal tests in https://github.com/vlang/v/blob/master/doc/docs.md#test-files).

Without it, the test file is considered to be an entirely separate program, and is compiled without having access to any of the symbols from the other files in src/ (i.e. you would have to import everything that you want to test in it, and you can not import the main module).

You can also try v test . in the src/ folder. It will pass for a _test.v file, that has module main at its top, and will fail, for one that lacks it: image