Skip to content

Commit c659748

Browse files
aykevldeadprogram
authored andcommitted
ide-integration: update to use proper GOROOT
1 parent a2cf784 commit c659748

File tree

1 file changed

+37
-119
lines changed

1 file changed

+37
-119
lines changed

content/ide-integration/_index.md

Lines changed: 37 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -3,133 +3,51 @@ title: "IDE Integration"
33
weight: 8
44
---
55

6+
IDEs need to have certain environment variables set before they work with TinyGo: `GOROOT` and `GOFLAGS`. You can determine the correct values from the `tinygo info` command (starting with TinyGo 0.15).
67

7-
## Downloading the source
8-
9-
Either git clone the tinygo source from.
10-
11-
> https://github.com/tinygo-org/tinygo
12-
13-
## Go mod init in your project
14-
15-
Navigate to your project and type:
16-
17-
> go mod init
18-
19-
## Replace needed imports
20-
21-
Replace each std package you need using the replace keyword in the go.mod file.
22-
23-
So if you want the machine package to be resolved replace it using.
24-
25-
> replace machine => /path/to/tinygo/machine
26-
27-
Repeat this step for every package, which needs to be resolved in your project.
28-
29-
### Example
30-
31-
```go
32-
module github.com/Nerzal/tinygo-playground
33-
34-
go 1.14
35-
36-
replace machine => /home/tobias/go/src/github.com/tinygo-org/tinygo/src/machine
37-
38-
require (
39-
machine v0.0.0-00010101000000-000000000000
40-
tinygo.org/x/drivers v0.13.0
41-
)
42-
43-
```
44-
45-
## Add go module files to the packages
46-
47-
In order to get your package resolved, you also have to add a go mod file for the packages.
48-
49-
So navigate to `/path/to/tinygo/src/neededpackage`
50-
51-
And then do `go mod init`
52-
53-
### Example
54-
8+
To get the correct values for these flags, run the following command. Here it is for `microbit`, replace it with something else if you need it.
9+
5510
```bash
56-
cd ~/go/src/github.com/tinygo-org/tinygo/src/machine
57-
go mod init
11+
$ tinygo info microbit
12+
LLVM triple: armv6m-none-eabi
13+
GOOS: linux
14+
GOARCH: arm
15+
build tags: cortexm baremetal linux arm nrf51822 nrf51 nrf microbit tinygo gc.conservative scheduler.tasks
16+
garbage collector: conservative
17+
scheduler: tasks
18+
cached GOROOT: /home/user/.cache/tinygo/goroot-go1.14-f930d5b5f36579e8cbd1c139012b3d702281417fb6bdf67303c4697195b9ef1f-syscall
5819
```
5920

60-
## GoPath
61-
62-
In order to get IDE support like autocomplete you have to add the tinygo src path to your [GOPATH](https://github.com/golang/go/wiki/GOPATH). So setup your Gopath first.
63-
After adding the tinygo source path to the GOPATH go will know, where to look for the packages like `machine`
64-
65-
### Ubuntu Example
21+
* The `GOROOT` value needed is the cached GOROOT given in the output (`/home/user/.cache/tinygo/goroot-go1.14-f930d5b5f36579e8cbd1c139012b3d702281417fb6bdf67303c4697195b9ef1f-syscall` in the example).
6622

67-
#### Check if GOPATH is set
23+
* The `GOFLAGS` value can be determined from the build tags. Take the entire list of build tags, replace spaces with commas, and add `-tags=` in front. For this example, the output would be `-tags=cortexm,baremetal,linux,arm,nrf51822,nrf51,nrf,microbit,tinygo,gc.conservative,scheduler.tasks`.
6824

69-
> echo $GOPATH
25+
Now you need to configure your IDE with these values.
7026

71-
If you result is empty, you need to set the GOPATH fist
27+
### Visual Studio Code
7228

73-
You can just append the path to your tinygo installation in your GOPATH.
29+
In VS Code, you can edit the file `.vscode/settings.json` in the root of your project. If the `.vscode` directory does not yet exist, create it. It's a normal JSON file where you need to set the `go.toolsEnvVars` property. An example file (again, using the above configuration) is the following:
7430

75-
> export GOPATH=$GOPATH:/path/to/your/tinygo
76-
77-
### Windows Example
78-
79-
Windows uses a semicolon to separate the different paths. So you can just append the path to your tinygo using the example below.
80-
81-
> set GOPATH=%GOPATH%;C:\path\to\your\tinygo
82-
83-
At this point, if you are in src/examples/bliny1/blinky.go, machine.Output can be handled by gopls.
84-
You may want to prompt them to check it once.
85-
86-
At this point, machine.LED cannot be processed by gopls.
87-
This is because you do not have build-tag or other settings in place.
88-
89-
## Starting your editor with variables
90-
91-
The last step needed to get full code completion support is to start your editor with environment variables.
92-
93-
You could also just set the environment variables for your complete environment, but this would interfere you when, writing normal go code.
94-
95-
So we use another way.
96-
97-
### Gather the needed information
98-
99-
We need different environment variables for different microcontrollers.
100-
101-
You can use `tinygo info controllerName`
102-
103-
**Example**
104-
To gather information needed to work with an Arduino use:
105-
106-
> tinygo info arduino
107-
108-
```bash
109-
$ tinygo info arduino
110-
LLVM triple: avr-unknown-unknown
111-
GOOS: linux
112-
GOARCH: arm
113-
build tags: avr baremetal linux arm atmega328p atmega avr5 arduino tinygo gc.conservative scheduler.none
114-
garbage collector: conservative
115-
scheduler: none
31+
```json
32+
{
33+
"go.toolsEnvVars": {
34+
"GOROOT": "/home/user/.cache/tinygo/goroot-go1.14-f930d5b5f36579e8cbd1c139012b3d702281417fb6bdf67303c4697195b9ef1f-syscall",
35+
"GOFLAGS": "-tags=cortexm,baremetal,linux,arm,nrf51822,nrf51,nrf,microbit,tinygo,gc.conservative,scheduler.tasks"
36+
}
37+
}
11638
```
11739

118-
So now you now, that.
119-
120-
1. GOOS needs to be set to linux
121-
2. GOARCH needs to be set to arm
122-
3. GOFLAGS needs to be set to `-tags=avr,baremetal,linux,arm,atmega328p,atmega,avr5,arduino,tinygo,gc.conservative,scheduler.none`
123-
124-
Important is that you need to comma separate the tags.
40+
After creating or modifying this file, you will likely need to restart VS Code to apply these settings.
12541

126-
### Start the editor
42+
### Other IDEs
12743

128-
The following example should work for all editors. And more or less all operating systems. The syntax may vary depending on your os/shell.
44+
Other IDEs will likely need a different setup. You can try starting them with these environment variables set in your shell or configuring these environment variables somewhere in your Go language server settings.
12945

130-
VSCode Example:
46+
As an example, this is an alternative way to start VS Code with the settings above (using `bash`):
13147

132-
> export GOOS=linux; export GOARCH=arm; export GOFLAGS=-tags=avr,baremetal,linux,arm,atmega328p,atmega,avr5,arduino,tinygo,gc.conservative,scheduler.none; code
48+
```
49+
GOROOT=/home/user/.cache/tinygo/goroot-go1.14-f930d5b5f36579e8cbd1c139012b3d702281417fb6bdf67303c4697195b9ef1f-syscall GOFLAGS=-tags=cortexm,baremetal,linux,arm,nrf51822,nrf51,nrf,microbit,tinygo,gc.conservative,scheduler.tasks code
50+
```
13351

13452
### Using tinygo-edit
13553

@@ -147,16 +65,16 @@ export EDITOR="$VISUAL"
14765
alias startTinyGoArduino="GOOS=linux GOARCH=arm GOFLAGS=-tags=$(tinygo info arduino|grep 'build tags'|awk -F: '{print $2}' | sed -e 's/^[[:space:]]*//'|sed -e 's/[[:space:]]/,/g') $EDITOR"
14866
```
14967

150-
### Note
68+
## TinyGo Drivers
15169

152-
This process has only been tested with the [gopls](https://github.com/golang/tools/blob/master/gopls/doc/user.md) language server. It might or might not work with other language servers.
70+
There are already lot's of drivers for common hardware. See [this](https://github.com/tinygo-org/drivers) for more information.
15371

154-
To install gopls follow the instructions in the link above.
72+
### Using drivers with Go modules
15573

156-
## TinyGo Drivers
74+
When you're using Go modules, drivers are automatically added when you import a driver (such as `tinygo.org/x/drivers/ws2812`).
15775

158-
There are already lot's of drivers for common hardware. See [this](https://github.com/tinygo-org/drivers) for more information.
76+
### Install drivers using GOPATH
15977

160-
### Install drivers
78+
When you're not using Go modules, you need to download the drivers separately. This works like any Go package:
16179

162-
> go get tinygo.org/x/drivers
80+
> go get -d tinygo.org/x/drivers

0 commit comments

Comments
 (0)