Skip to content

Commit 51e50e1

Browse files
Use the correct library when a relative path is passed as --library value (#2126)
* Use the correct library when a relative path is passed as `--library` value * Add test related to the changes
1 parent 5a67f3b commit 51e50e1

File tree

8 files changed

+54
-1
lines changed

8 files changed

+54
-1
lines changed

Diff for: internal/cli/compile/compile.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,14 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
226226
stdOut, stdErr, stdIORes = feedback.OutputStreams()
227227
}
228228

229+
var libraryAbs []string
230+
for _, libPath := range paths.NewPathList(library...) {
231+
if libPath, err = libPath.Abs(); err != nil {
232+
feedback.Fatal(tr("Error converting path to absolute: %v", err), feedback.ErrGeneric)
233+
}
234+
libraryAbs = append(libraryAbs, libPath.String())
235+
}
236+
229237
compileRequest := &rpc.CompileRequest{
230238
Instance: inst,
231239
Fqbn: fqbn,
@@ -244,7 +252,7 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
244252
Clean: clean,
245253
CreateCompilationDatabaseOnly: compilationDatabaseOnly,
246254
SourceOverride: overrides,
247-
Library: library,
255+
Library: libraryAbs,
248256
KeysKeychain: keysKeychain,
249257
SignKey: signKey,
250258
EncryptKey: encryptKey,

Diff for: internal/integrationtest/compile_3/compile_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,41 @@ func TestCompilerErrOutput(t *testing.T) {
116116
compilerErr := requirejson.Parse(t, out).Query(".compiler_err")
117117
compilerErr.MustContain(`"error"`)
118118
}
119+
120+
func TestCompileRelativeLibraryPath(t *testing.T) {
121+
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
122+
defer env.CleanUp()
123+
124+
// Initialize configs to enable --zip-path flag
125+
_, _, err := cli.Run("config", "init", "--dest-dir", ".")
126+
require.NoError(t, err)
127+
_, _, err = cli.Run("config", "set", "library.enable_unsafe_install", "true", "--config-file", "arduino-cli.yaml")
128+
require.NoError(t, err)
129+
configFile := cli.WorkingDir().Join("arduino-cli.yaml")
130+
131+
_, _, err = cli.Run("core", "install", "arduino:avr")
132+
require.NoError(t, err)
133+
134+
// Install library and its dependencies
135+
zipPath, err := paths.New("..", "testdata", "FooLib.zip").Abs()
136+
require.NoError(t, err)
137+
// Manually install the library and move into one of the example's directories
138+
FooLib := cli.WorkingDir().Join("FooLib")
139+
err = paths.New("..", "testdata", "FooLib").CopyDirTo(FooLib)
140+
require.NoError(t, err)
141+
cli.SetWorkingDir(FooLib.Join("examples", "FooSketch"))
142+
143+
// Compile using a relative path to the library
144+
_, _, err = cli.Run("compile", "-b", "arduino:avr:uno", "--library", "../../")
145+
require.NoError(t, err)
146+
147+
// Install the same library using lib install and compile again using the relative path.
148+
// The manually installed library should be chosen
149+
_, _, err = cli.Run("lib", "install", "--zip-path", zipPath.String(), "--config-file", configFile.String())
150+
require.NoError(t, err)
151+
stdout, _, err := cli.Run("compile", "-b", "arduino:avr:uno", "--library", "../../", "-v")
152+
require.NoError(t, err)
153+
require.Contains(t, string(stdout), "Multiple libraries were found for \"FooLib.h\"")
154+
require.Contains(t, string(stdout), "Used: "+FooLib.String())
155+
require.Contains(t, string(stdout), "Not used: "+cli.SketchbookDir().Join("libraries", "FooLib").String())
156+
}

Diff for: internal/integrationtest/testdata/FooLib.zip

1.25 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// This file intentionally left empty.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// This file intentionally left empty.

Diff for: internal/integrationtest/testdata/FooLib/FooLib.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// This file intentionally left empty.

Diff for: internal/integrationtest/testdata/FooLib/FooLib.h

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// This file intentionally left empty.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include <FooLib.h>
2+
void setup() {}
3+
void loop() {}

0 commit comments

Comments
 (0)