arduino / arduino-ide

Arduino IDE 2.x
https://www.arduino.cc/en/software
GNU Affero General Public License v3.0
2.3k stars 391 forks source link

Language Server (autocomplete) not using menu settings #1029

Closed PaulStoffregen closed 1 year ago

PaulStoffregen commented 2 years ago

Describe the problem

The autocomplete and variable info popups find info corresponding to the Tools menu configuration options at their defaults, even when the user has changed them to a different configuration. The tools menu config can cause the build process to use a different set of defines, changing which code is actually compiled. The IDE should cause the language server to always operate with settings from the currently selected menu config, but today it appears to be always using the menu defaults regardless of what has actually been selected in those menus.

To reproduce

Using Arduino IDE 2.0.0-rc7.

1: add https://www.pjrc.com/teensy/td_156/package_teensy_index.json to additional board manager URLs in File > Preferences

2: in boards manager, search for "teensy" and install Teensy package 1.56.1

3: set the current board to Tools > Board > Teensy > Teensy 4.1

4: open or type any program with Serial, such as

void setup() {
  Serial.begin();
}

void loop() {
}

5: hover your mouse over "Serial". A popup will appear telling you the type is "class usb_serial_class". image

6: click Tools > USB Type and change the configuration to "RawHID" image

7: again hover your mouse over "Serial". The popup will reappear, incorrectly telling you the type is still "class usb_serial_class". image

8: right click Serial and select "Go To Definition". A new tab will open, showing the wrong definition which is not compiled cause CDC_DATA_INTERFACE and CDC_STATUS_INTERFACE are not defined when USB Type RawHID is selected. image

Expected behavior

When USB Type is set to RawHID, the Serial object is defined as "usb_seremu_class Serial" at line 75 in usb_inst.cpp.

image

Changing the menu configuration alters the compiler build flags, causing completely different code to be compiled. The IDE should always configure the Language Server with the defines or other build flags from the menu configuration.

Arduino IDE version

2.0.0-rc7

Operating system

Linux

Operating system version

Ubuntu 20.04

Additional context

No response

Issue checklist

per1234 commented 2 years ago

I can see the problem here in the language server logs:

2022/07/10 16:26:52 Language server launched with arguments:
2022/07/10 16:26:52   arg[0] = c:\ide 2\tip\68-7d96153\resources\app\node_modules\arduino-ide-extension\build\arduino-language-server.exe
2022/07/10 16:26:52   arg[1] = -clangd
2022/07/10 16:26:52   arg[2] = c:\ide 2\tip\68-7d96153\resources\app\node_modules\arduino-ide-extension\build\clangd.exe
2022/07/10 16:26:52   arg[3] = -cli-daemon-addr
2022/07/10 16:26:52   arg[4] = localhost:1048
2022/07/10 16:26:52   arg[5] = -cli-daemon-instance
2022/07/10 16:26:52   arg[6] = 1
2022/07/10 16:26:52   arg[7] = -fqbn
2022/07/10 16:26:52   arg[8] = teensy:avr:teensy41
2022/07/10 16:26:52   arg[9] = -board-name
2022/07/10 16:26:52   arg[10] = "Teensy 4.1"
2022/07/10 16:26:52   arg[11] = -log
2022/07/10 16:26:52   arg[12] = -logpath
2022/07/10 16:26:52   arg[13] = c:\Users\per\Documents\Arduino\lstest

The IDE is not passing the full FQBN via the -fqbn flag when starting the language server. This causes the default values to be used for the custom board options rather than the values the user has selected from the custom board option menus. The IDE must pass the full FQBN (in this case teensy:avr:teensy41:usb=rawhid,speed=600,opt=o2std,keys=en-us) to the language server.