diff --git a/internal/cli/compile/compile.go b/internal/cli/compile/compile.go index 9695bc19e98..3eb8708ed16 100644 --- a/internal/cli/compile/compile.go +++ b/internal/cli/compile/compile.go @@ -226,6 +226,14 @@ func runCompileCommand(cmd *cobra.Command, args []string) { stdOut, stdErr, stdIORes = feedback.OutputStreams() } + var libraryAbs []string + for _, libPath := range paths.NewPathList(library...) { + if libPath, err = libPath.Abs(); err != nil { + feedback.Fatal(tr("Error converting path to absolute: %v", err), feedback.ErrGeneric) + } + libraryAbs = append(libraryAbs, libPath.String()) + } + compileRequest := &rpc.CompileRequest{ Instance: inst, Fqbn: fqbn, @@ -244,7 +252,7 @@ func runCompileCommand(cmd *cobra.Command, args []string) { Clean: clean, CreateCompilationDatabaseOnly: compilationDatabaseOnly, SourceOverride: overrides, - Library: library, + Library: libraryAbs, KeysKeychain: keysKeychain, SignKey: signKey, EncryptKey: encryptKey, diff --git a/internal/integrationtest/compile_3/compile_test.go b/internal/integrationtest/compile_3/compile_test.go index 2af68d2b03b..11aaeceef5e 100644 --- a/internal/integrationtest/compile_3/compile_test.go +++ b/internal/integrationtest/compile_3/compile_test.go @@ -116,3 +116,41 @@ func TestCompilerErrOutput(t *testing.T) { compilerErr := requirejson.Parse(t, out).Query(".compiler_err") compilerErr.MustContain(`"error"`) } + +func TestCompileRelativeLibraryPath(t *testing.T) { + env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) + defer env.CleanUp() + + // Initialize configs to enable --zip-path flag + _, _, err := cli.Run("config", "init", "--dest-dir", ".") + require.NoError(t, err) + _, _, err = cli.Run("config", "set", "library.enable_unsafe_install", "true", "--config-file", "arduino-cli.yaml") + require.NoError(t, err) + configFile := cli.WorkingDir().Join("arduino-cli.yaml") + + _, _, err = cli.Run("core", "install", "arduino:avr") + require.NoError(t, err) + + // Install library and its dependencies + zipPath, err := paths.New("..", "testdata", "FooLib.zip").Abs() + require.NoError(t, err) + // Manually install the library and move into one of the example's directories + FooLib := cli.WorkingDir().Join("FooLib") + err = paths.New("..", "testdata", "FooLib").CopyDirTo(FooLib) + require.NoError(t, err) + cli.SetWorkingDir(FooLib.Join("examples", "FooSketch")) + + // Compile using a relative path to the library + _, _, err = cli.Run("compile", "-b", "arduino:avr:uno", "--library", "../../") + require.NoError(t, err) + + // Install the same library using lib install and compile again using the relative path. + // The manually installed library should be chosen + _, _, err = cli.Run("lib", "install", "--zip-path", zipPath.String(), "--config-file", configFile.String()) + require.NoError(t, err) + stdout, _, err := cli.Run("compile", "-b", "arduino:avr:uno", "--library", "../../", "-v") + require.NoError(t, err) + require.Contains(t, string(stdout), "Multiple libraries were found for \"FooLib.h\"") + require.Contains(t, string(stdout), "Used: "+FooLib.String()) + require.Contains(t, string(stdout), "Not used: "+cli.SketchbookDir().Join("libraries", "FooLib").String()) +} diff --git a/internal/integrationtest/testdata/FooLib.zip b/internal/integrationtest/testdata/FooLib.zip new file mode 100644 index 00000000000..f12d87aa297 Binary files /dev/null and b/internal/integrationtest/testdata/FooLib.zip differ diff --git a/internal/integrationtest/testdata/FooLib/ArduinoIoTCloud.cpp b/internal/integrationtest/testdata/FooLib/ArduinoIoTCloud.cpp new file mode 100644 index 00000000000..e26b0ab556c --- /dev/null +++ b/internal/integrationtest/testdata/FooLib/ArduinoIoTCloud.cpp @@ -0,0 +1 @@ +// This file intentionally left empty. diff --git a/internal/integrationtest/testdata/FooLib/ArduinoIoTCloud.h b/internal/integrationtest/testdata/FooLib/ArduinoIoTCloud.h new file mode 100644 index 00000000000..e26b0ab556c --- /dev/null +++ b/internal/integrationtest/testdata/FooLib/ArduinoIoTCloud.h @@ -0,0 +1 @@ +// This file intentionally left empty. diff --git a/internal/integrationtest/testdata/FooLib/FooLib.cpp b/internal/integrationtest/testdata/FooLib/FooLib.cpp new file mode 100644 index 00000000000..e26b0ab556c --- /dev/null +++ b/internal/integrationtest/testdata/FooLib/FooLib.cpp @@ -0,0 +1 @@ +// This file intentionally left empty. diff --git a/internal/integrationtest/testdata/FooLib/FooLib.h b/internal/integrationtest/testdata/FooLib/FooLib.h new file mode 100644 index 00000000000..e26b0ab556c --- /dev/null +++ b/internal/integrationtest/testdata/FooLib/FooLib.h @@ -0,0 +1 @@ +// This file intentionally left empty. diff --git a/internal/integrationtest/testdata/FooLib/examples/FooSketch/FooSketch.ino b/internal/integrationtest/testdata/FooLib/examples/FooSketch/FooSketch.ino new file mode 100644 index 00000000000..ea144a785da --- /dev/null +++ b/internal/integrationtest/testdata/FooLib/examples/FooSketch/FooSketch.ino @@ -0,0 +1,3 @@ +#include +void setup() {} +void loop() {}