Skip to content

Commit ede5a78

Browse files
authored
Increased library priority for libraries specified via --library flag (#2148)
* Increase priority of libraries specified via --library flag * Added integration tests
1 parent 2a5c83a commit ede5a78

File tree

6 files changed

+75
-3
lines changed

6 files changed

+75
-3
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ func ComputePriority(lib *libraries.Library, header, arch string) int {
212212
case libraries.User:
213213
priority += 3
214214
case libraries.Unmanaged:
215-
priority += 4
215+
// Bonus for libraries specified via --libraries flags, those libraries gets the highest priority
216+
priority += 10000
216217
default:
217218
panic(fmt.Sprintf("Invalid library location: %d", lib.Location))
218219
}

Diff for: docs/sketch-build-process.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ generated for the path to each library dependency and appended to the
5252
If multiple libraries contain a file that matches the `#include` directive, the priority is determined by applying the
5353
following rules, one by one in this order, until a rule determines a winner:
5454

55+
1. A library that has been specified using the [`--library` option](commands/arduino-cli_compile.md#options) of
56+
`arduino-cli compile` wins against a library in other locations
5557
1. A library that is architecture compatible wins against a library that is not architecture compatible (see
5658
[**Architecture Matching**](#architecture-matching))
5759
1. A library with both [library name](#library-name-priority) and [folder name](#folder-name-priority) matching the
@@ -120,8 +122,6 @@ The "folder name priority" is determined as follows (in order of highest to lowe
120122

121123
The "location priority" is determined as follows (in order of highest to lowest priority):
122124

123-
1. The library is specified using the [`--library` option](commands/arduino-cli_compile.md#options) of
124-
`arduino-cli compile`
125125
1. The library is under a custom libraries path specified via the
126126
[`--libraries` option](commands/arduino-cli_compile.md#options) of `arduino-cli compile` (in decreasing order of
127127
priority when multiple custom paths are defined)
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2022 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package compile_test
17+
18+
import (
19+
"testing"
20+
21+
"github.com/arduino/arduino-cli/internal/integrationtest"
22+
"github.com/arduino/go-paths-helper"
23+
"github.com/stretchr/testify/require"
24+
"go.bug.st/testifyjson/requirejson"
25+
)
26+
27+
func TestCompileLibrarySelection(t *testing.T) {
28+
// See: https://github.com/arduino/arduino-cli/issues/2106
29+
30+
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
31+
defer env.CleanUp()
32+
33+
// Run update-index with our test index
34+
_, _, err := cli.Run("core", "install", "arduino:[email protected]")
35+
require.NoError(t, err)
36+
37+
// Prepare sketchbook and sketch
38+
sketchBook, err := paths.New("testdata", "sketchbook_for_testing_lib_priorities").Abs()
39+
require.NoError(t, err)
40+
vars := cli.GetDefaultEnv()
41+
vars["ARDUINO_DIRECTORIES_USER"] = sketchBook.String()
42+
43+
sketch := sketchBook.Join("SketchUsingLibraryA")
44+
anotherLib := sketchBook.Join("libraries", "AnotherLibrary")
45+
46+
// Perform two compile:
47+
// - the first should use LibraryA
48+
stdout, _, err := cli.RunWithCustomEnv(vars, "compile", "-b", "arduino:avr:mega", "--format", "json", sketch.String())
49+
require.NoError(t, err)
50+
requirejson.Contains(t, stdout, `{
51+
"builder_result" : {
52+
"used_libraries" : [
53+
{ "name": "LibraryA" }
54+
]
55+
}
56+
}`)
57+
58+
// - the second should use AnotherLibrary (because it was forced by --library)
59+
stdout, _, err = cli.RunWithCustomEnv(vars, "compile", "-b", "arduino:avr:mega", "--library", anotherLib.String(), "--format", "json", sketch.String())
60+
require.NoError(t, err)
61+
requirejson.Contains(t, stdout, `{
62+
"builder_result" : {
63+
"used_libraries" : [
64+
{ "name": "AnotherLibrary" }
65+
]
66+
}
67+
}`)
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include <LibraryA.h>
2+
void setup() {}
3+
void loop() {}

Diff for: internal/integrationtest/compile_3/testdata/sketchbook_for_testing_lib_priorities/libraries/AnotherLibrary/LibraryA.h

Whitespace-only changes.

Diff for: internal/integrationtest/compile_3/testdata/sketchbook_for_testing_lib_priorities/libraries/LibraryA/LibraryA.h

Whitespace-only changes.

0 commit comments

Comments
 (0)