Skip to content

Commit 58c6bc3

Browse files
authored
[skip-changelog] refactoring: Fixing legacy pathnames (#2038)
* Removed builderCtx.BuildCachePath Because it creates a lot of confusion * Removed useless assignment * Do not store sketch build-path but generate it on demand * Removed redundant SketchLocation field and related subroutines * Replaced deprecated calls (from go linter)
1 parent a63aff4 commit 58c6bc3

29 files changed

+132
-255
lines changed

arduino/sketch/sketch.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ import (
3232
type Sketch struct {
3333
Name string
3434
MainFile *paths.Path
35-
FullPath *paths.Path // FullPath is the path to the Sketch folder
36-
BuildPath *paths.Path
35+
FullPath *paths.Path // FullPath is the path to the Sketch folder
3736
OtherSketchFiles paths.PathList // Sketch files that end in .ino other than main file
3837
AdditionalFiles paths.PathList
3938
RootFolderFiles paths.PathList // All files that are in the Sketch root
@@ -81,7 +80,6 @@ func New(path *paths.Path) (*Sketch, error) {
8180
Name: path.Base(),
8281
MainFile: mainFile,
8382
FullPath: path,
84-
BuildPath: GenBuildPath(path),
8583
OtherSketchFiles: paths.PathList{},
8684
AdditionalFiles: paths.PathList{},
8785
RootFolderFiles: paths.PathList{},
@@ -293,14 +291,15 @@ func CheckForPdeFiles(sketch *paths.Path) []*paths.Path {
293291
return files
294292
}
295293

296-
// GenBuildPath generates a suitable name for the build folder.
297-
// The sketchPath, if not nil, is also used to furhter differentiate build paths.
298-
func GenBuildPath(sketchPath *paths.Path) *paths.Path {
299-
path := ""
300-
if sketchPath != nil {
301-
path = sketchPath.String()
302-
}
294+
// DefaultBuildPath generates the default build directory for a given sketch.
295+
// The build path is in a temporary directory and is unique for each sketch.
296+
func (s *Sketch) DefaultBuildPath() *paths.Path {
297+
return paths.TempDir().Join("arduino", "sketch-"+s.Hash())
298+
}
299+
300+
// Hash generate a unique hash for the given sketch.
301+
func (s *Sketch) Hash() string {
302+
path := s.FullPath.String()
303303
md5SumBytes := md5.Sum([]byte(path))
304-
md5Sum := strings.ToUpper(hex.EncodeToString(md5SumBytes[:]))
305-
return paths.TempDir().Join("arduino", "sketch-"+md5Sum)
304+
return strings.ToUpper(hex.EncodeToString(md5SumBytes[:]))
306305
}

arduino/sketch/sketch_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,8 @@ func TestNewSketchFolderSymlink(t *testing.T) {
287287

288288
func TestGenBuildPath(t *testing.T) {
289289
want := paths.TempDir().Join("arduino", "sketch-ACBD18DB4CC2F85CEDEF654FCCC4A4D8")
290-
assert.True(t, GenBuildPath(paths.New("foo")).EquivalentTo(want))
291-
292-
want = paths.TempDir().Join("arduino", "sketch-D41D8CD98F00B204E9800998ECF8427E")
293-
assert.True(t, GenBuildPath(nil).EquivalentTo(want))
290+
assert.True(t, (&Sketch{FullPath: paths.New("foo")}).DefaultBuildPath().EquivalentTo(want))
291+
assert.Equal(t, "ACBD18DB4CC2F85CEDEF654FCCC4A4D8", (&Sketch{FullPath: paths.New("foo")}).Hash())
294292
}
295293

296294
func TestCheckForPdeFiles(t *testing.T) {

commands/compile/compile.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
116116
}
117117
builderCtx.UseCachedLibrariesResolution = req.GetSkipLibrariesDiscovery()
118118
builderCtx.FQBN = fqbn
119-
builderCtx.SketchLocation = sk.FullPath
119+
builderCtx.Sketch = sk
120120
builderCtx.ProgressCB = progressCB
121121

122122
// FIXME: This will be redundant when arduino-builder will be part of the cli
@@ -128,7 +128,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
128128
builderCtx.OtherLibrariesDirs.Add(configuration.LibrariesDir(configuration.Settings))
129129
builderCtx.LibraryDirs = paths.NewPathList(req.Library...)
130130
if req.GetBuildPath() == "" {
131-
builderCtx.BuildPath = sk.BuildPath
131+
builderCtx.BuildPath = sk.DefaultBuildPath()
132132
} else {
133133
builderCtx.BuildPath = paths.New(req.GetBuildPath()).Canonical()
134134
}
@@ -144,8 +144,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
144144
// Optimize for debug
145145
builderCtx.OptimizeForDebug = req.GetOptimizeForDebug()
146146

147-
builderCtx.CoreBuildCachePath = paths.TempDir().Join("arduino", "core-cache")
148-
149147
builderCtx.Jobs = int(req.GetJobs())
150148

151149
builderCtx.USBVidPid = req.GetVidPid()
@@ -154,12 +152,17 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
154152
builderCtx.CustomBuildProperties = append(req.GetBuildProperties(), "build.warn_data_percentage=75")
155153
builderCtx.CustomBuildProperties = append(req.GetBuildProperties(), securityKeysOverride...)
156154

157-
if req.GetBuildCachePath() != "" {
158-
builderCtx.BuildCachePath = paths.New(req.GetBuildCachePath())
159-
err = builderCtx.BuildCachePath.MkdirAll()
155+
if req.GetBuildCachePath() == "" {
156+
builderCtx.CoreBuildCachePath = paths.TempDir().Join("arduino", "core-cache")
157+
} else {
158+
buildCachePath, err := paths.New(req.GetBuildCachePath()).Abs()
160159
if err != nil {
161160
return nil, &arduino.PermissionDeniedError{Message: tr("Cannot create build cache directory"), Cause: err}
162161
}
162+
if err := buildCachePath.MkdirAll(); err != nil {
163+
return nil, &arduino.PermissionDeniedError{Message: tr("Cannot create build cache directory"), Cause: err}
164+
}
165+
builderCtx.CoreBuildCachePath = buildCachePath.Join("core")
163166
}
164167

165168
builderCtx.BuiltInLibrariesDirs = configuration.IDEBuiltinLibrariesDir(configuration.Settings)

commands/debug/debug_info.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,11 @@ func getDebugProperties(req *debug.DebugConfigRequest, pme *packagemanager.Explo
115115
}
116116
}
117117

118-
importPath := sk.BuildPath
118+
var importPath *paths.Path
119119
if importDir := req.GetImportDir(); importDir != "" {
120120
importPath = paths.New(importDir)
121+
} else {
122+
importPath = sk.DefaultBuildPath()
121123
}
122124
if !importPath.Exist() {
123125
return nil, &arduino.NotFoundError{Message: tr("Compiled sketch not found in %s", importPath)}

commands/upload/upload.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ func determineBuildPathAndSketchName(importFile, importDir string, sk *sketch.Sk
575575

576576
// Case 4: only sketch specified. In this case we use the generated build path
577577
// and the given sketch name.
578-
return sk.BuildPath, sk.Name + sk.MainFile.Ext(), nil
578+
return sk.DefaultBuildPath(), sk.Name + sk.MainFile.Ext(), nil
579579
}
580580

581581
func detectSketchNameFromBuildPath(buildPath *paths.Path) (string, error) {

commands/upload/upload_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestDetermineBuildPathAndSketchName(t *testing.T) {
7979
// 03: error: used both importPath and importFile
8080
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", nil, nil, "<nil>", ""},
8181
// 04: only sketch without FQBN
82-
{"", "", blonk, nil, sketch.GenBuildPath(blonk.FullPath).String(), "Blonk.ino"},
82+
{"", "", blonk, nil, blonk.DefaultBuildPath().String(), "Blonk.ino"},
8383
// 05: use importFile to detect build.path and project_name, sketch is ignored.
8484
{"testdata/build_path_2/Blink.ino.hex", "", blonk, nil, "testdata/build_path_2", "Blink.ino"},
8585
// 06: use importPath as build.path and Blink as project name, ignore the sketch Blonk
@@ -95,7 +95,7 @@ func TestDetermineBuildPathAndSketchName(t *testing.T) {
9595
// 11: error: used both importPath and importFile
9696
{"testdata/build_path_2/Blink.ino.hex", "testdata/build_path_2", nil, fqbn, "<nil>", ""},
9797
// 12: use sketch to determine project name and sketch+fqbn to determine build path
98-
{"", "", blonk, fqbn, sketch.GenBuildPath(blonk.FullPath).String(), "Blonk.ino"},
98+
{"", "", blonk, fqbn, blonk.DefaultBuildPath().String(), "Blonk.ino"},
9999
// 13: use importFile to detect build.path and project_name, sketch+fqbn is ignored.
100100
{"testdata/build_path_2/Blink.ino.hex", "", blonk, fqbn, "testdata/build_path_2", "Blink.ino"},
101101
// 14: use importPath as build.path and Blink as project name, ignore the sketch Blonk, ignore fqbn

legacy/builder/add_additional_entries_to_context.go

-9
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,6 @@ func (*AddAdditionalEntriesToContext) Run(ctx *types.Context) error {
5050
ctx.CoreBuildPath = coreBuildPath
5151
}
5252

53-
if ctx.BuildCachePath != nil {
54-
coreBuildCachePath, err := ctx.BuildCachePath.Join(constants.FOLDER_CORE).Abs()
55-
if err != nil {
56-
return errors.WithStack(err)
57-
}
58-
59-
ctx.CoreBuildCachePath = coreBuildCachePath
60-
}
61-
6253
if ctx.WarningsLevel == "" {
6354
ctx.WarningsLevel = DEFAULT_WARNINGS_LEVEL
6455
}

legacy/builder/builder.go

-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"reflect"
2020
"time"
2121

22-
"github.com/arduino/arduino-cli/arduino/sketch"
2322
"github.com/arduino/arduino-cli/i18n"
2423
"github.com/arduino/arduino-cli/legacy/builder/phases"
2524
"github.com/arduino/arduino-cli/legacy/builder/types"
@@ -129,10 +128,6 @@ func (s *PreprocessSketch) Run(ctx *types.Context) error {
129128
type Preprocess struct{}
130129

131130
func (s *Preprocess) Run(ctx *types.Context) error {
132-
if ctx.BuildPath == nil {
133-
ctx.BuildPath = sketch.GenBuildPath(ctx.SketchLocation)
134-
}
135-
136131
if err := ctx.BuildPath.MkdirAll(); err != nil {
137132
return err
138133
}
@@ -188,9 +183,6 @@ func RunBuilder(ctx *types.Context) error {
188183
}
189184

190185
func RunParseHardware(ctx *types.Context) error {
191-
if ctx.BuildPath == nil {
192-
ctx.BuildPath = sketch.GenBuildPath(ctx.SketchLocation)
193-
}
194186
commands := []types.Command{
195187
&ContainerSetupHardwareToolsLibsSketchAndProps{},
196188
}

legacy/builder/container_setup.go

-17
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package builder
1717

1818
import (
19-
sk "github.com/arduino/arduino-cli/arduino/sketch"
2019
"github.com/arduino/arduino-cli/legacy/builder/types"
2120
"github.com/pkg/errors"
2221
)
@@ -48,22 +47,6 @@ func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(ctx *types.Context)
4847
ctx.PushProgress()
4948
}
5049

51-
if ctx.SketchLocation != nil {
52-
// get abs path to sketch
53-
sketchLocation, err := ctx.SketchLocation.Abs()
54-
if err != nil {
55-
return errors.WithStack(err)
56-
}
57-
58-
// load sketch
59-
sketch, err := sk.New(sketchLocation)
60-
if err != nil {
61-
return errors.WithStack(err)
62-
}
63-
sketch.BuildPath = ctx.BuildPath
64-
ctx.SketchLocation = sketch.MainFile
65-
ctx.Sketch = sketch
66-
}
6750
ctx.Progress.CompleteStep()
6851
ctx.PushProgress()
6952

legacy/builder/fail_if_buildpath_equals_sketchpath.go

+2-15
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,8 @@ import (
2323
type FailIfBuildPathEqualsSketchPath struct{}
2424

2525
func (s *FailIfBuildPathEqualsSketchPath) Run(ctx *types.Context) error {
26-
if ctx.BuildPath == nil || ctx.SketchLocation == nil {
27-
return nil
28-
}
29-
30-
buildPath, err := ctx.BuildPath.Abs()
31-
if err != nil {
32-
return errors.WithStack(err)
33-
}
34-
35-
sketchPath, err := ctx.SketchLocation.Abs()
36-
if err != nil {
37-
return errors.WithStack(err)
38-
}
39-
sketchPath = sketchPath.Parent()
40-
26+
buildPath := ctx.BuildPath.Canonical()
27+
sketchPath := ctx.Sketch.FullPath.Canonical()
4128
if buildPath.EqualsTo(sketchPath) {
4229
return errors.New(tr("Sketch cannot be located in build path. Please specify a different build path"))
4330
}

legacy/builder/setup_build_properties.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,7 @@ func (s *SetupBuildProperties) Run(ctx *types.Context) error {
109109
buildProperties.Set("software", DEFAULT_SOFTWARE)
110110
}
111111

112-
if ctx.SketchLocation != nil {
113-
sourcePath, err := ctx.SketchLocation.Abs()
114-
if err != nil {
115-
return err
116-
}
117-
sourcePath = sourcePath.Parent()
118-
buildProperties.SetPath("build.source.path", sourcePath)
119-
}
112+
buildProperties.SetPath("build.source.path", ctx.Sketch.FullPath)
120113

121114
now := time.Now()
122115
buildProperties.Set("extra.time.utc", strconv.FormatInt(now.Unix(), 10))

legacy/builder/sketch_loader.go

-55
This file was deleted.

legacy/builder/test/builder_test.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,21 @@ import (
2121
"testing"
2222
"time"
2323

24-
"github.com/arduino/go-paths-helper"
25-
"github.com/sirupsen/logrus"
26-
24+
"github.com/arduino/arduino-cli/arduino/sketch"
2725
"github.com/arduino/arduino-cli/legacy/builder"
2826
"github.com/arduino/arduino-cli/legacy/builder/constants"
2927
"github.com/arduino/arduino-cli/legacy/builder/phases"
3028
"github.com/arduino/arduino-cli/legacy/builder/types"
29+
"github.com/arduino/go-paths-helper"
30+
"github.com/sirupsen/logrus"
3131
"github.com/stretchr/testify/require"
3232
)
3333

3434
func prepareBuilderTestContext(t *testing.T, sketchPath *paths.Path, fqbn string) *types.Context {
35+
sk, err := sketch.New(sketchPath)
36+
require.NoError(t, err)
3537
return &types.Context{
36-
SketchLocation: sketchPath,
38+
Sketch: sk,
3739
FQBN: parseFQBN(t, fqbn),
3840
HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware"),
3941
BuiltInToolsDirs: paths.NewPathList("downloaded_tools"),
@@ -261,7 +263,7 @@ func TestBuilderBridgeRedBearLab(t *testing.T) {
261263
func TestBuilderSketchNoFunctions(t *testing.T) {
262264
DownloadCoresAndToolsAndLibraries(t)
263265

264-
ctx := prepareBuilderTestContext(t, paths.New("sketch_no_functions", "main.ino"), "RedBearLab:avr:blend")
266+
ctx := prepareBuilderTestContext(t, paths.New("sketch_no_functions", "sketch_no_functions.ino"), "RedBearLab:avr:blend")
265267
ctx.HardwareDirs = append(ctx.HardwareDirs, paths.New("downloaded_board_manager_stuff"))
266268
ctx.BuiltInToolsDirs = append(ctx.BuiltInToolsDirs, paths.New("downloaded_board_manager_stuff"))
267269

@@ -371,7 +373,7 @@ func TestBuilderCacheCoreAFile(t *testing.T) {
371373
SetupBuildPath(t, ctx)
372374
defer ctx.BuildPath.RemoveAll()
373375
SetupBuildCachePath(t, ctx)
374-
defer ctx.BuildCachePath.RemoveAll()
376+
defer ctx.CoreBuildCachePath.RemoveAll()
375377

376378
// Run build
377379
bldr := builder.Builder{}

legacy/builder/test/builder_utils_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package test
1717

1818
import (
19-
"io/ioutil"
19+
"os"
2020
"testing"
2121
"time"
2222

@@ -32,7 +32,7 @@ func sleep(t *testing.T) {
3232
}
3333

3434
func tempFile(t *testing.T, prefix string) *paths.Path {
35-
file, err := ioutil.TempFile("", prefix)
35+
file, err := os.CreateTemp("", prefix)
3636
file.Close()
3737
NoError(t, err)
3838
return paths.New(file.Name())

legacy/builder/test/create_build_options_map_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package test
1818
import (
1919
"testing"
2020

21+
"github.com/arduino/arduino-cli/arduino/sketch"
2122
"github.com/arduino/arduino-cli/legacy/builder"
2223
"github.com/arduino/arduino-cli/legacy/builder/types"
2324
"github.com/arduino/go-paths-helper"
@@ -29,7 +30,7 @@ func TestCreateBuildOptionsMap(t *testing.T) {
2930
HardwareDirs: paths.NewPathList("hardware", "hardware2"),
3031
BuiltInToolsDirs: paths.NewPathList("tools"),
3132
OtherLibrariesDirs: paths.NewPathList("libraries"),
32-
SketchLocation: paths.New("sketchLocation"),
33+
Sketch: &sketch.Sketch{FullPath: paths.New("sketchLocation")},
3334
FQBN: parseFQBN(t, "my:nice:fqbn"),
3435
ArduinoAPIVersion: "ideVersion",
3536
Verbose: true,

0 commit comments

Comments
 (0)