Skip to content

Commit 888a9a3

Browse files
cmagliesilvanocerza
andcommitted
Improved lib detection: check for matching name in library.properties (#1276)
* Improved lib detection: check for matching name in library.properties The library may be stored in a directory that doesn't match the library name, for example we had a case in the wild where the directories: libraries/onewire_2_3_4/... libraries/onewireng_1_2_3/... were used instead of: libraries/OneWire/... libraries/OneWireNg/... this lead to incorrect selection of onewireng_1_2_3 when using OneWire.h (because the OneWireNg had an architecture=avr that had priority over the architecture=* of onewire_2_3_4). This commit will restore priority straight. * Added test for lib resolve improvement * Lib discovery: always prefer libraries with the correct directory name * [skip changelog] Add integration test Co-authored-by: Silvano Cerza <[email protected]>
1 parent cbe85bb commit 888a9a3

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

Diff for: arduino/libraries/librariesresolver/cpp.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ func computePriority(lib *libraries.Library, header, arch string) int {
121121
header = strings.TrimSuffix(header, filepath.Ext(header))
122122
header = simplify(header)
123123
name := simplify(lib.Name)
124+
realName := simplify(lib.RealName)
124125

125126
priority := 0
126127

@@ -137,15 +138,17 @@ func computePriority(lib *libraries.Library, header, arch string) int {
137138
priority += 0
138139
}
139140

140-
if name == header {
141+
if realName == header && name == header {
142+
priority += 600
143+
} else if realName == header || name == header {
141144
priority += 500
142-
} else if name == header+"-master" {
145+
} else if realName == header+"-master" || name == header+"-master" {
143146
priority += 400
144-
} else if strings.HasPrefix(name, header) {
147+
} else if strings.HasPrefix(realName, header) || strings.HasPrefix(name, header) {
145148
priority += 300
146-
} else if strings.HasSuffix(name, header) {
149+
} else if strings.HasSuffix(realName, header) || strings.HasSuffix(name, header) {
147150
priority += 200
148-
} else if strings.Contains(name, header) {
151+
} else if strings.Contains(realName, header) || strings.Contains(name, header) {
149152
priority += 100
150153
}
151154

Diff for: arduino/libraries/librariesresolver/cpp_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,18 @@ func TestCppHeaderResolver(t *testing.T) {
143143
require.Equal(t, "Calculus Unified Lib", resolve("calculus_lib.h", l6, l7))
144144
require.Equal(t, "Calculus Unified Lib", resolve("calculus_lib.h", l7, l6))
145145
}
146+
147+
func TestCppHeaderResolverWithLibrariesInStrangeDirectoryNames(t *testing.T) {
148+
resolver := NewCppResolver()
149+
librarylist := libraries.List{}
150+
librarylist.Add(&libraries.Library{Name: "onewire_2_3_4", RealName: "OneWire", Architectures: []string{"*"}})
151+
librarylist.Add(&libraries.Library{Name: "onewireng_2_3_4", RealName: "OneWireNg", Architectures: []string{"avr"}})
152+
resolver.headers["OneWire.h"] = librarylist
153+
require.Equal(t, "onewire_2_3_4", resolver.ResolveFor("OneWire.h", "avr").Name)
154+
155+
librarylist2 := libraries.List{}
156+
librarylist2.Add(&libraries.Library{Name: "OneWire", RealName: "OneWire", Architectures: []string{"*"}})
157+
librarylist2.Add(&libraries.Library{Name: "onewire_2_3_4", RealName: "OneWire", Architectures: []string{"avr"}})
158+
resolver.headers["OneWire.h"] = librarylist2
159+
require.Equal(t, "OneWire", resolver.ResolveFor("OneWire.h", "avr").Name)
160+
}

Diff for: test/test_compile.py

+31-4
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,33 @@ def test_recompile_with_different_library(run_command, data_dir):
998998
assert f"Using previously compiled file: {obj_path}" not in res.stdout
999999

10001000

1001+
def test_compile_with_conflicting_libraries_include(run_command, data_dir, copy_sketch):
1002+
assert run_command("update")
1003+
1004+
assert run_command("core install arduino:[email protected]")
1005+
1006+
# Install conflicting libraries
1007+
git_url = "https://github.com/pstolarz/OneWireNg.git"
1008+
one_wire_ng_lib_path = Path(data_dir, "libraries", "onewireng_0_8_1")
1009+
assert Repo.clone_from(git_url, one_wire_ng_lib_path, multi_options=["-b 0.8.1"])
1010+
1011+
git_url = "https://github.com/PaulStoffregen/OneWire.git"
1012+
one_wire_lib_path = Path(data_dir, "libraries", "onewire_2_3_5")
1013+
assert Repo.clone_from(git_url, one_wire_lib_path, multi_options=["-b v2.3.5"])
1014+
1015+
sketch_path = copy_sketch("sketch_with_conflicting_libraries_include")
1016+
fqbn = "arduino:avr:uno"
1017+
1018+
res = run_command(f"compile -b {fqbn} {sketch_path} --verbose")
1019+
assert res.ok
1020+
expected_output = [
1021+
'Multiple libraries were found for "OneWire.h"',
1022+
f" Used: {one_wire_lib_path}",
1023+
f" Not used: {one_wire_ng_lib_path}",
1024+
]
1025+
assert "\n".join(expected_output) in res.stdout
1026+
1027+
10011028
def test_compile_with_invalid_build_options_json(run_command, data_dir):
10021029
assert run_command("update")
10031030

@@ -1051,7 +1078,7 @@ def test_compile_with_esp32_bundled_libraries(run_command, data_dir, copy_sketch
10511078
fqbn = "esp32:esp32:esp32"
10521079

10531080
res = run_command(f"compile -b {fqbn} {sketch_path} --verbose")
1054-
assert res.ok
1081+
assert res.failed
10551082

10561083
core_bundled_lib_path = Path(data_dir, "packages", "esp32", "hardware", "esp32", core_version, "libraries", "SD")
10571084
cli_installed_lib_path = Path(data_dir, "libraries", "SD")
@@ -1060,7 +1087,7 @@ def test_compile_with_esp32_bundled_libraries(run_command, data_dir, copy_sketch
10601087
f" Used: {core_bundled_lib_path}",
10611088
f" Not used: {cli_installed_lib_path}",
10621089
]
1063-
assert "\n".join(expected_output) in res.stdout
1090+
assert "\n".join(expected_output) not in res.stdout
10641091

10651092

10661093
def test_compile_with_esp8266_bundled_libraries(run_command, data_dir, copy_sketch):
@@ -1090,7 +1117,7 @@ def test_compile_with_esp8266_bundled_libraries(run_command, data_dir, copy_sket
10901117
fqbn = "esp8266:esp8266:generic"
10911118

10921119
res = run_command(f"compile -b {fqbn} {sketch_path} --verbose")
1093-
assert res.ok
1120+
assert res.failed
10941121

10951122
core_bundled_lib_path = Path(
10961123
data_dir, "packages", "esp8266", "hardware", "esp8266", core_version, "libraries", "SD"
@@ -1101,4 +1128,4 @@ def test_compile_with_esp8266_bundled_libraries(run_command, data_dir, copy_sket
11011128
f" Used: {core_bundled_lib_path}",
11021129
f" Not used: {cli_installed_lib_path}",
11031130
]
1104-
assert "\n".join(expected_output) in res.stdout
1131+
assert "\n".join(expected_output) not in res.stdout
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "OneWire.h"
2+
3+
void setup() {
4+
}
5+
6+
void loop() {
7+
}

0 commit comments

Comments
 (0)