Skip to content

Commit d996b15

Browse files
authored
Fix lib list --all not returning library includes in json output (#1147)
* Fix lib list --all not returning library includes in json output * [skip changelog] Enhance integration test
1 parent 7e55f9e commit d996b15

File tree

4 files changed

+61
-20
lines changed

4 files changed

+61
-20
lines changed

Diff for: arduino/libraries/libraries.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (library *Library) String() string {
8787
}
8888

8989
// ToRPCLibrary converts this library into an rpc.Library
90-
func (library *Library) ToRPCLibrary() *rpc.Library {
90+
func (library *Library) ToRPCLibrary() (*rpc.Library, error) {
9191
pathOrEmpty := func(p *paths.Path) string {
9292
if p == nil {
9393
return ""
@@ -100,6 +100,18 @@ func (library *Library) ToRPCLibrary() *rpc.Library {
100100
}
101101
return p.String()
102102
}
103+
104+
// If the the "includes" property is empty or not included in the "library.properties" file
105+
// we search for headers by reading the library files directly
106+
headers := library.DeclaredHeaders()
107+
if len(headers) == 0 {
108+
var err error
109+
headers, err = library.SourceHeaders()
110+
if err != nil {
111+
return nil, fmt.Errorf("gathering library headers: %w", err)
112+
}
113+
}
114+
103115
return &rpc.Library{
104116
Name: library.Name,
105117
Author: library.Author,
@@ -124,9 +136,9 @@ func (library *Library) ToRPCLibrary() *rpc.Library {
124136
Version: library.Version.String(),
125137
License: library.License,
126138
Examples: library.Examples.AsStrings(),
127-
ProvidesIncludes: library.DeclaredHeaders(),
139+
ProvidesIncludes: headers,
128140
CompatibleWith: library.CompatibleWith,
129-
}
141+
}, nil
130142
}
131143

132144
// SupportsAnyArchitectureIn returns true if any of the following is true:

Diff for: commands/compile/compile.go

+15-16
Original file line numberDiff line numberDiff line change
@@ -194,21 +194,6 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
194194

195195
builderCtx.SourceOverride = req.GetSourceOverride()
196196

197-
// Use defer() to create an rpc.CompileResp with all the information available at the
198-
// moment of return.
199-
defer func() {
200-
if r != nil {
201-
importedLibs := []*rpc.Library{}
202-
for _, lib := range builderCtx.ImportedLibraries {
203-
importedLibs = append(importedLibs, lib.ToRPCLibrary())
204-
}
205-
206-
r.BuildPath = builderCtx.BuildPath.String()
207-
r.UsedLibraries = importedLibs
208-
r.ExecutableSectionsSize = builderCtx.ExecutableSectionsSize.ToRPCExecutableSectionSizeArray()
209-
}
210-
}()
211-
212197
// if --preprocess or --show-properties were passed, we can stop here
213198
if req.GetShowProperties() {
214199
return &rpc.CompileResp{}, builder.RunParseHardwareAndDumpBuildProperties(builderCtx)
@@ -259,6 +244,20 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
259244
}
260245
}
261246

247+
importedLibs := []*rpc.Library{}
248+
for _, lib := range builderCtx.ImportedLibraries {
249+
rpcLib, err := lib.ToRPCLibrary()
250+
if err != nil {
251+
return nil, fmt.Errorf("converting library %s to rpc struct: %w", lib.Name, err)
252+
}
253+
importedLibs = append(importedLibs, rpcLib)
254+
}
255+
262256
logrus.Tracef("Compile %s for %s successful", sketch.Name, fqbnIn)
263-
return &rpc.CompileResp{}, nil
257+
258+
return &rpc.CompileResp{
259+
BuildPath: builderCtx.BuildPath.String(),
260+
UsedLibraries: importedLibs,
261+
ExecutableSectionsSize: builderCtx.ExecutableSectionsSize.ToRPCExecutableSectionSizeArray(),
262+
}, nil
264263
}

Diff for: commands/lib/list.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,12 @@ func LibraryList(ctx context.Context, req *rpc.LibraryListReq) (*rpc.LibraryList
103103
if lib.Available != nil {
104104
release = lib.Available.ToRPCLibraryRelease()
105105
}
106+
rpcLib, err := lib.Library.ToRPCLibrary()
107+
if err != nil {
108+
return nil, fmt.Errorf("converting library %s to rpc struct: %w", lib.Library.Name, err)
109+
}
106110
instaledLib = append(instaledLib, &rpc.InstalledLibrary{
107-
Library: lib.Library.ToRPCLibrary(),
111+
Library: rpcLib,
108112
Release: release,
109113
})
110114
}

Diff for: test/test_lib.py

+26
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,32 @@ def test_list_with_fqbn(run_command):
145145
assert data[0]["library"]["compatible_with"]["arduino:avr:uno"]
146146

147147

148+
def test_list_provides_includes_fallback(run_command):
149+
# Verifies "provides_includes" field is returned even if libraries don't declare
150+
# the "includes" property in their "library.properties" file
151+
assert run_command("update")
152+
153+
# Install core
154+
assert run_command("core install arduino:[email protected]")
155+
assert run_command("lib install [email protected]")
156+
157+
# List all libraries, even the ones installed with the above core
158+
result = run_command("lib list --all --fqbn arduino:avr:uno --format json")
159+
assert result.ok
160+
assert "" == result.stderr
161+
162+
data = json.loads(result.stdout)
163+
assert 6 == len(data)
164+
165+
libs = {l["library"]["name"]: l["library"]["provides_includes"] for l in data}
166+
assert ["SoftwareSerial.h"] == libs["SoftwareSerial"]
167+
assert ["Wire.h"] == libs["Wire"]
168+
assert ["EEPROM.h"] == libs["EEPROM"]
169+
assert ["HID.h"] == libs["HID"]
170+
assert ["SPI.h"] == libs["SPI"]
171+
assert ["ArduinoJson.h", "ArduinoJson.hpp"] == libs["ArduinoJson"]
172+
173+
148174
def test_lib_download(run_command, downloads_dir):
149175

150176
# Download a specific lib version

0 commit comments

Comments
 (0)