@@ -18,14 +18,14 @@ package builder_utils
1818import (
1919 "fmt"
2020 "os"
21- "os/exec"
2221 "path/filepath"
2322 "runtime"
2423 "strings"
2524 "sync"
2625
2726 bUtils "github.com/arduino/arduino-cli/arduino/builder/utils"
2827 "github.com/arduino/arduino-cli/arduino/globals"
28+ "github.com/arduino/arduino-cli/executils"
2929 "github.com/arduino/arduino-cli/i18n"
3030 "github.com/arduino/arduino-cli/legacy/builder/constants"
3131 "github.com/arduino/arduino-cli/legacy/builder/types"
@@ -173,7 +173,7 @@ func compileFileWithRecipe(ctx *types.Context, sourcePath *paths.Path, source *p
173173 return nil , errors .WithStack (err )
174174 }
175175
176- command , err := PrepareCommandForRecipe (properties , recipe , false , ctx . PackageManager . GetEnvVarsForSpawnedProcess () )
176+ command , err := PrepareCommandForRecipe (properties , recipe , false )
177177 if err != nil {
178178 return nil , errors .WithStack (err )
179179 }
@@ -244,7 +244,7 @@ func ArchiveCompiledFiles(ctx *types.Context, buildPath *paths.Path, archiveFile
244244 properties .SetPath (constants .BUILD_PROPERTIES_ARCHIVE_FILE_PATH , archiveFilePath )
245245 properties .SetPath (constants .BUILD_PROPERTIES_OBJECT_FILE , objectFile )
246246
247- command , err := PrepareCommandForRecipe (properties , constants .RECIPE_AR_PATTERN , false , ctx . PackageManager . GetEnvVarsForSpawnedProcess () )
247+ command , err := PrepareCommandForRecipe (properties , constants .RECIPE_AR_PATTERN , false )
248248 if err != nil {
249249 return nil , errors .WithStack (err )
250250 }
@@ -260,7 +260,7 @@ func ArchiveCompiledFiles(ctx *types.Context, buildPath *paths.Path, archiveFile
260260
261261const COMMANDLINE_LIMIT = 30000
262262
263- func PrepareCommandForRecipe (buildProperties * properties.Map , recipe string , removeUnsetProperties bool , toolEnv [] string ) (* exec. Cmd , error ) {
263+ func PrepareCommandForRecipe (buildProperties * properties.Map , recipe string , removeUnsetProperties bool ) (* executils. Process , error ) {
264264 pattern := buildProperties .Get (recipe )
265265 if pattern == "" {
266266 return nil , errors .Errorf (tr ("%[1]s pattern is missing" ), recipe )
@@ -275,24 +275,30 @@ func PrepareCommandForRecipe(buildProperties *properties.Map, recipe string, rem
275275 if err != nil {
276276 return nil , errors .WithStack (err )
277277 }
278- command := exec .Command (parts [0 ], parts [1 :]... )
279- command .Env = append (os .Environ (), toolEnv ... )
280278
281279 // if the overall commandline is too long for the platform
282280 // try reducing the length by making the filenames relative
283281 // and changing working directory to build.path
282+ var relativePath string
284283 if len (commandLine ) > COMMANDLINE_LIMIT {
285- relativePath : = buildProperties .Get ("build.path" )
286- for i , arg := range command . Args {
284+ relativePath = buildProperties .Get ("build.path" )
285+ for i , arg := range parts {
287286 if _ , err := os .Stat (arg ); os .IsNotExist (err ) {
288287 continue
289288 }
290289 rel , err := filepath .Rel (relativePath , arg )
291290 if err == nil && ! strings .Contains (rel , ".." ) && len (rel ) < len (arg ) {
292- command . Args [i ] = rel
291+ parts [i ] = rel
293292 }
294293 }
295- command .Dir = relativePath
294+ }
295+
296+ command , err := executils .NewProcess (nil , parts ... )
297+ if err != nil {
298+ return nil , errors .WithStack (err )
299+ }
300+ if relativePath != "" {
301+ command .SetDir (relativePath )
296302 }
297303
298304 return command , nil
0 commit comments