From eaf3f0d9bec14011bb0bf1ce3f10b9cfcf4ca5f2 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 12 Nov 2024 10:20:52 +0100 Subject: [PATCH] Better defined sketch-vendored libraries priority --- .../arduino/libraries/libraries_location.go | 11 ++++++++++ internal/arduino/libraries/libraries_test.go | 1 + .../libraries/librariesresolver/cpp.go | 5 ++++- internal/arduino/sketch/sketch.go | 2 +- internal/cli/feedback/result/rpc.go | 3 +++ rpc/cc/arduino/cli/commands/v1/lib.pb.go | 20 ++++++++++++------- rpc/cc/arduino/cli/commands/v1/lib.proto | 2 ++ 7 files changed, 35 insertions(+), 9 deletions(-) diff --git a/internal/arduino/libraries/libraries_location.go b/internal/arduino/libraries/libraries_location.go index a2a4423a54d..8070cd99d1f 100644 --- a/internal/arduino/libraries/libraries_location.go +++ b/internal/arduino/libraries/libraries_location.go @@ -40,6 +40,8 @@ const ( // Unmanaged is for libraries set manually by the user in the CLI command or from the gRPC function. // Ideally it's used for `libraries` outside folders managed by the CLI. Unmanaged + // Sketch is for libraries that are part of the sketch (inside the `libraries` subfolder of the sketch). + Sketch ) func (d *LibraryLocation) String() string { @@ -54,6 +56,8 @@ func (d *LibraryLocation) String() string { return "user" case Unmanaged: return "unmanaged" + case Sketch: + return "sketch" default: panic(fmt.Sprintf("invalid LibraryLocation value %d", *d)) } @@ -86,6 +90,9 @@ func (d *LibraryLocation) UnmarshalJSON(b []byte) error { case "unmanaged": *d = Unmanaged return nil + case "sketch": + *d = Sketch + return nil default: return errors.New(i18n.Tr("invalid library location: %s", s)) } @@ -104,6 +111,8 @@ func (d *LibraryLocation) ToRPCLibraryLocation() rpc.LibraryLocation { return rpc.LibraryLocation_LIBRARY_LOCATION_USER case Unmanaged: return rpc.LibraryLocation_LIBRARY_LOCATION_UNMANAGED + case Sketch: + return rpc.LibraryLocation_LIBRARY_LOCATION_SKETCH default: panic(fmt.Sprintf("invalid LibraryLocation value %d", *d)) } @@ -122,6 +131,8 @@ func FromRPCLibraryLocation(l rpc.LibraryLocation) LibraryLocation { return User case rpc.LibraryLocation_LIBRARY_LOCATION_UNMANAGED: return Unmanaged + case rpc.LibraryLocation_LIBRARY_LOCATION_SKETCH: + return Sketch default: panic(fmt.Sprintf("invalid rpc.LibraryLocation value %d", l)) } diff --git a/internal/arduino/libraries/libraries_test.go b/internal/arduino/libraries/libraries_test.go index 33b0de48a3b..bcec281ce99 100644 --- a/internal/arduino/libraries/libraries_test.go +++ b/internal/arduino/libraries/libraries_test.go @@ -50,6 +50,7 @@ func TestLibLayoutAndLocationJSONUnMarshaler(t *testing.T) { testLocation(ReferencedPlatformBuiltIn) testLocation(User) testLocation(Unmanaged) + testLocation(Sketch) } func TestLibrariesLoader(t *testing.T) { diff --git a/internal/arduino/libraries/librariesresolver/cpp.go b/internal/arduino/libraries/librariesresolver/cpp.go index e2ae916d396..3597fe28606 100644 --- a/internal/arduino/libraries/librariesresolver/cpp.go +++ b/internal/arduino/libraries/librariesresolver/cpp.go @@ -207,9 +207,12 @@ func ComputePriority(lib *libraries.Library, header, arch string) int { priority += 2 case libraries.User: priority += 3 + case libraries.Sketch: + // Bonus for sketch libraries, those libraries get a better priority than others + priority += 10000 case libraries.Unmanaged: // Bonus for libraries specified via --libraries flags, those libraries gets the highest priority - priority += 10000 + priority += 20000 default: panic(fmt.Sprintf("Invalid library location: %d", lib.Location)) } diff --git a/internal/arduino/sketch/sketch.go b/internal/arduino/sketch/sketch.go index 880e03c0340..cf119d329b8 100644 --- a/internal/arduino/sketch/sketch.go +++ b/internal/arduino/sketch/sketch.go @@ -152,7 +152,7 @@ func New(path *paths.Path) (*Sketch, error) { } libDirs.FilterDirs() for _, libDir := range libDirs { - lib, err := libraries.Load(libDir, libraries.Unmanaged) + lib, err := libraries.Load(libDir, libraries.Sketch) if err != nil { return nil, fmt.Errorf("%s: %w", i18n.Tr("reading sketch libraries"), err) } diff --git a/internal/cli/feedback/result/rpc.go b/internal/cli/feedback/result/rpc.go index a1b464a89b1..379dcb14542 100644 --- a/internal/cli/feedback/result/rpc.go +++ b/internal/cli/feedback/result/rpc.go @@ -172,6 +172,7 @@ const ( LibraryLocationPlatformBuiltin LibraryLocation = "platform" LibraryLocationReferencedPlatformBuiltin LibraryLocation = "ref-platform" LibraryLocationUnmanged LibraryLocation = "unmanaged" + LibraryLocationSketch LibraryLocation = "sketch" ) func NewLibraryLocation(r rpc.LibraryLocation) LibraryLocation { @@ -186,6 +187,8 @@ func NewLibraryLocation(r rpc.LibraryLocation) LibraryLocation { return LibraryLocationUser case rpc.LibraryLocation_LIBRARY_LOCATION_UNMANAGED: return LibraryLocationUnmanged + case rpc.LibraryLocation_LIBRARY_LOCATION_SKETCH: + return LibraryLocationSketch } return LibraryLocationIDEBuiltin } diff --git a/rpc/cc/arduino/cli/commands/v1/lib.pb.go b/rpc/cc/arduino/cli/commands/v1/lib.pb.go index 0910be91d8a..b4b23e3d0b3 100644 --- a/rpc/cc/arduino/cli/commands/v1/lib.pb.go +++ b/rpc/cc/arduino/cli/commands/v1/lib.pb.go @@ -200,6 +200,8 @@ const ( LibraryLocation_LIBRARY_LOCATION_REFERENCED_PLATFORM_BUILTIN LibraryLocation = 3 // Outside the `libraries` folders managed by the CLI. LibraryLocation_LIBRARY_LOCATION_UNMANAGED LibraryLocation = 4 + // Inside the `libraries` folder of the sketch. + LibraryLocation_LIBRARY_LOCATION_SKETCH LibraryLocation = 5 ) // Enum value maps for LibraryLocation. @@ -210,6 +212,7 @@ var ( 2: "LIBRARY_LOCATION_PLATFORM_BUILTIN", 3: "LIBRARY_LOCATION_REFERENCED_PLATFORM_BUILTIN", 4: "LIBRARY_LOCATION_UNMANAGED", + 5: "LIBRARY_LOCATION_SKETCH", } LibraryLocation_value = map[string]int32{ "LIBRARY_LOCATION_BUILTIN": 0, @@ -217,6 +220,7 @@ var ( "LIBRARY_LOCATION_PLATFORM_BUILTIN": 2, "LIBRARY_LOCATION_REFERENCED_PLATFORM_BUILTIN": 3, "LIBRARY_LOCATION_UNMANAGED": 4, + "LIBRARY_LOCATION_SKETCH": 5, } ) @@ -3206,7 +3210,7 @@ var file_cc_arduino_cli_commands_v1_lib_proto_rawDesc = []byte{ 0x49, 0x42, 0x52, 0x41, 0x52, 0x59, 0x5f, 0x4c, 0x41, 0x59, 0x4f, 0x55, 0x54, 0x5f, 0x46, 0x4c, 0x41, 0x54, 0x10, 0x00, 0x12, 0x1c, 0x0a, 0x18, 0x4c, 0x49, 0x42, 0x52, 0x41, 0x52, 0x59, 0x5f, 0x4c, 0x41, 0x59, 0x4f, 0x55, 0x54, 0x5f, 0x52, 0x45, 0x43, 0x55, 0x52, 0x53, 0x49, 0x56, 0x45, - 0x10, 0x01, 0x2a, 0xc3, 0x01, 0x0a, 0x0f, 0x4c, 0x69, 0x62, 0x72, 0x61, 0x72, 0x79, 0x4c, 0x6f, + 0x10, 0x01, 0x2a, 0xe0, 0x01, 0x0a, 0x0f, 0x4c, 0x69, 0x62, 0x72, 0x61, 0x72, 0x79, 0x4c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x1c, 0x0a, 0x18, 0x4c, 0x49, 0x42, 0x52, 0x41, 0x52, 0x59, 0x5f, 0x4c, 0x4f, 0x43, 0x41, 0x54, 0x49, 0x4f, 0x4e, 0x5f, 0x42, 0x55, 0x49, 0x4c, 0x54, 0x49, 0x4e, 0x10, 0x00, 0x12, 0x19, 0x0a, 0x15, 0x4c, 0x49, 0x42, 0x52, 0x41, 0x52, 0x59, 0x5f, @@ -3218,12 +3222,14 @@ var file_cc_arduino_cli_commands_v1_lib_proto_rawDesc = []byte{ 0x45, 0x4e, 0x43, 0x45, 0x44, 0x5f, 0x50, 0x4c, 0x41, 0x54, 0x46, 0x4f, 0x52, 0x4d, 0x5f, 0x42, 0x55, 0x49, 0x4c, 0x54, 0x49, 0x4e, 0x10, 0x03, 0x12, 0x1e, 0x0a, 0x1a, 0x4c, 0x49, 0x42, 0x52, 0x41, 0x52, 0x59, 0x5f, 0x4c, 0x4f, 0x43, 0x41, 0x54, 0x49, 0x4f, 0x4e, 0x5f, 0x55, 0x4e, 0x4d, - 0x41, 0x4e, 0x41, 0x47, 0x45, 0x44, 0x10, 0x04, 0x42, 0x48, 0x5a, 0x46, 0x67, 0x69, 0x74, 0x68, - 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x2f, 0x61, - 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x2d, 0x63, 0x6c, 0x69, 0x2f, 0x72, 0x70, 0x63, 0x2f, 0x63, - 0x63, 0x2f, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x2f, 0x63, 0x6c, 0x69, 0x2f, 0x63, 0x6f, - 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x73, 0x2f, 0x76, 0x31, 0x3b, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, - 0x64, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x41, 0x4e, 0x41, 0x47, 0x45, 0x44, 0x10, 0x04, 0x12, 0x1b, 0x0a, 0x17, 0x4c, 0x49, 0x42, 0x52, + 0x41, 0x52, 0x59, 0x5f, 0x4c, 0x4f, 0x43, 0x41, 0x54, 0x49, 0x4f, 0x4e, 0x5f, 0x53, 0x4b, 0x45, + 0x54, 0x43, 0x48, 0x10, 0x05, 0x42, 0x48, 0x5a, 0x46, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, + 0x63, 0x6f, 0x6d, 0x2f, 0x61, 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x2f, 0x61, 0x72, 0x64, 0x75, + 0x69, 0x6e, 0x6f, 0x2d, 0x63, 0x6c, 0x69, 0x2f, 0x72, 0x70, 0x63, 0x2f, 0x63, 0x63, 0x2f, 0x61, + 0x72, 0x64, 0x75, 0x69, 0x6e, 0x6f, 0x2f, 0x63, 0x6c, 0x69, 0x2f, 0x63, 0x6f, 0x6d, 0x6d, 0x61, + 0x6e, 0x64, 0x73, 0x2f, 0x76, 0x31, 0x3b, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x73, 0x62, + 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/rpc/cc/arduino/cli/commands/v1/lib.proto b/rpc/cc/arduino/cli/commands/v1/lib.proto index 1878b07ec53..ef13812103b 100644 --- a/rpc/cc/arduino/cli/commands/v1/lib.proto +++ b/rpc/cc/arduino/cli/commands/v1/lib.proto @@ -379,6 +379,8 @@ enum LibraryLocation { LIBRARY_LOCATION_REFERENCED_PLATFORM_BUILTIN = 3; // Outside the `libraries` folders managed by the CLI. LIBRARY_LOCATION_UNMANAGED = 4; + // Inside the `libraries` folder of the sketch. + LIBRARY_LOCATION_SKETCH = 5; } message ZipLibraryInstallRequest {